OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 156
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ui.light;
14
 package org.openconcerto.ui.light;
15
 
15
 
16
import org.openconcerto.utils.io.JSONConverter;
16
import org.openconcerto.utils.io.JSONConverter;
17
 
17
 
18
import java.sql.Timestamp;
18
import java.sql.Timestamp;
19
import java.text.ParseException;
19
import java.text.ParseException;
20
import java.text.SimpleDateFormat;
20
import java.text.SimpleDateFormat;
-
 
21
import java.util.Date;
21
 
22
 
22
import net.minidev.json.JSONObject;
23
import net.minidev.json.JSONObject;
23
 
24
 
24
public class LightUIDate extends LightUserControl {
25
public class LightUIDate extends LightUserControl {
25
 
26
 
-
 
27
    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.S";
-
 
28
 
-
 
29
    public LightUIDate() {
-
 
30
        // Serialization
-
 
31
    }
-
 
32
 
26
    public LightUIDate(final JSONObject json) {
33
    public LightUIDate(final JSONObject json) {
27
        super(json);
34
        super(json);
28
    }
35
    }
29
 
36
 
30
    public LightUIDate(final String id) {
37
    public LightUIDate(final String id) {
31
        super(id);
38
        super(id);
32
        this.setType(TYPE_DATE);
39
        this.setType(TYPE_DATE);
33
        this.setValueType(LightUIElement.VALUE_TYPE_DATE);
40
        this.setValueType(LightUIElement.VALUE_TYPE_DATE);
34
    }
41
    }
35
 
42
 
36
    public LightUIDate(final LightUIDate date) {
43
    public LightUIDate(final LightUIDate date) {
37
        super(date);
44
        super(date);
38
    }
45
    }
39
 
46
 
40
    public Timestamp getValueAsDate() {
47
    public Timestamp getValueAsDate() {
41
        if (this.getValue() != null && !this.getValue().isEmpty()) {
48
        if (this.getValue() != null && !this.getValue().isEmpty()) {
42
            SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
49
            SimpleDateFormat df2 = new SimpleDateFormat(DATE_FORMAT);
43
            try {
50
            try {
44
                return new Timestamp(df2.parse(this.getValue()).getTime());
51
                return new Timestamp(df2.parse(this.getValue()).getTime());
45
            } catch (final ParseException ex) {
52
            } catch (final ParseException ex) {
46
                throw new IllegalArgumentException(ex.getMessage(), ex);
53
                throw new IllegalArgumentException(ex.getMessage(), ex);
47
            }
54
            }
48
        }
55
        }
49
        return null;
56
        return null;
50
    }
57
    }
51
 
58
 
52
    @Override
-
 
53
    public JSONToLightUIConvertor getConvertor() {
59
    public void setDate(Date date) {
54
        return new JSONToLightUIConvertor() {
-
 
55
            @Override
-
 
56
            public LightUIElement convert(final JSONObject json) {
60
        SimpleDateFormat df2 = new SimpleDateFormat(DATE_FORMAT);
57
                return new LightUIDate(json);
61
        this.setValue(df2.format(date));
58
            }
-
 
59
        };
-
 
60
    }
62
    }
61
 
63
 
62
    @Override
64
    @Override
63
    public Object getValueForContext() {
65
    public Object getValueForContext() {
64
        return this.getValueAsDate();
66
        return this.getValueAsDate();
65
    }
67
    }
66
 
68
 
67
    @Override
69
    @Override
68
    public void _setValueFromContext(Object value) {
70
    public void _setValueFromContext(Object value) {
69
        final String strValue = JSONConverter.getObjectFromJSON(value, String.class);
71
        final String strValue = JSONConverter.getObjectFromJSON(value, String.class);
70
        this.setValue(strValue);
72
        this.setValue(strValue);
71
    }
73
    }
72
 
74
 
73
}
75
}