OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
94 ilm 5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.light;
15
 
132 ilm 16
import org.openconcerto.utils.io.JSONConverter;
17
 
18
import java.sql.Timestamp;
19
import java.text.ParseException;
20
import java.text.SimpleDateFormat;
156 ilm 21
import java.util.Date;
132 ilm 22
 
23
import net.minidev.json.JSONObject;
24
 
142 ilm 25
public class LightUIDate extends LightUserControl {
132 ilm 26
 
156 ilm 27
    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.S";
28
 
29
    public LightUIDate() {
30
        // Serialization
31
    }
32
 
132 ilm 33
    public LightUIDate(final JSONObject json) {
34
        super(json);
35
    }
36
 
37
    public LightUIDate(final String id) {
38
        super(id);
94 ilm 39
        this.setType(TYPE_DATE);
40
        this.setValueType(LightUIElement.VALUE_TYPE_DATE);
41
    }
132 ilm 42
 
43
    public LightUIDate(final LightUIDate date) {
44
        super(date);
45
    }
46
 
47
    public Timestamp getValueAsDate() {
144 ilm 48
        if (this.getValue() != null && !this.getValue().isEmpty()) {
156 ilm 49
            SimpleDateFormat df2 = new SimpleDateFormat(DATE_FORMAT);
132 ilm 50
            try {
51
                return new Timestamp(df2.parse(this.getValue()).getTime());
52
            } catch (final ParseException ex) {
53
                throw new IllegalArgumentException(ex.getMessage(), ex);
54
            }
55
        }
56
        return null;
57
    }
58
 
156 ilm 59
    public void setDate(Date date) {
60
        SimpleDateFormat df2 = new SimpleDateFormat(DATE_FORMAT);
61
        this.setValue(df2.format(date));
132 ilm 62
    }
63
 
64
    @Override
142 ilm 65
    public Object getValueForContext() {
132 ilm 66
        return this.getValueAsDate();
67
    }
68
 
69
    @Override
142 ilm 70
    public void _setValueFromContext(Object value) {
144 ilm 71
        final String strValue = JSONConverter.getObjectFromJSON(value, String.class);
132 ilm 72
        this.setValue(strValue);
73
    }
74
 
94 ilm 75
}