Project

General

Profile

Download (6.29 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.util.converter;
10

    
11
import java.util.Arrays;
12
import java.util.List;
13
import java.util.Locale;
14
import java.util.regex.Matcher;
15
import java.util.regex.Pattern;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.joda.time.DateTimeFieldType;
19
import org.joda.time.Partial;
20

    
21
import com.vaadin.data.util.converter.Converter;
22

    
23

    
24
/**
25
 *
26
 * @author a.kohlbecker
27
 * @since Apr 7, 2017
28
 *
29
 */
30
public class JodaTimePartialConverter implements Converter<String, Partial> {
31

    
32
    /**
33
     * iso8601: YYY-MM-DD
34
     * yyyymmddDot: dd.mm.yyyy
35
     *
36
     * @author a.kohlbecker
37
     * @since Apr 10, 2017
38
     *
39
     */
40
    public enum DateFormat {
41
        ISO8601,
42
        DAY_MONTH_YEAR_DOT
43
    }
44

    
45
    private static final long serialVersionUID = 976413549472527584L;
46

    
47
    DateFormat format;
48

    
49
    Pattern partialPatternIso8601 = Pattern.compile("^(?<year>(?:[1,2][7,8,9,0,1])?[0-9]{2})(?:-(?<month>[0-1]?[0-9])(?:-(?<day>[0-3]?[0-9]))?)?$");
50

    
51
    Pattern partialPatternDayMonthYearDot = Pattern.compile("^(?:(?:(?<day>[0-3]?[0-9])\\.)?(?<month>[0-1]?[0-9])\\.)?(?<year>(?:[1,2][7,8,9,0,1])?[0-9]{2})$");
52

    
53
    List<Pattern> patterns = Arrays.asList(new Pattern[]{partialPatternIso8601, partialPatternDayMonthYearDot});
54

    
55

    
56

    
57
    /**
58
     * @param format The format of the string representation
59
     */
60
    public JodaTimePartialConverter(DateFormat format) {
61
        super();
62
        this.format = format;
63
    }
64

    
65
    /**
66
     * {@inheritDoc}
67
     */
68
    @Override
69
    public Partial convertToModel(String value, Class<? extends Partial> targetType, Locale locale)
70
            throws com.vaadin.data.util.converter.Converter.ConversionException {
71
        Partial partial = null;
72
        if(value != null){
73
            partial = new Partial();
74
            for(Pattern pattern : patterns){
75
            Matcher m = pattern.matcher(value);
76
                if(m.matches()){
77
                    partial = makePartial(m);
78
                    break;
79
                }
80
            }
81
        }
82
        return partial;
83
    }
84

    
85
    /**
86
     * @param partial
87
     * @param m
88
     */
89
    private Partial makePartial(Matcher m) {
90

    
91
        Partial partial = new Partial();
92
        try {
93
            try {
94
                String year = m.group("year");
95
                partial = partial.with(DateTimeFieldType.year(), Integer.parseInt(year));
96
            } catch (IllegalArgumentException e) {
97
                // a valid year should be present here
98
                throw new ConversionException(e);
99
            }
100
            try {
101
                String month = m.group("month");
102
                partial = partial.with(DateTimeFieldType.monthOfYear(), Integer.parseInt(month));
103
                try {
104
                    String day = m.group("day");
105
                    partial = partial.with(DateTimeFieldType.dayOfMonth(), Integer.parseInt(day));
106
                } catch (IllegalArgumentException e) {
107
                    /* IGNORE days are not required */
108
                }
109
            } catch (IllegalArgumentException e) {
110
                /* IGNORE months are not required */
111
            }
112
        } catch (NumberFormatException ne) {
113
            // all numbers should be parsable, this is guaranteed by the partialPattern
114
            throw new ConversionException(ne);
115
        }
116
        return partial;
117
    }
118

    
119
    /**
120
     * {@inheritDoc}
121
     */
122
    @Override
123
    public String convertToPresentation(Partial value, Class<? extends String> targetType, Locale locale)
124
            throws com.vaadin.data.util.converter.Converter.ConversionException {
125
        if(value != null){
126
            switch(format) {
127
            case ISO8601:
128
                return formatIso8601(value);
129
            case DAY_MONTH_YEAR_DOT:
130
                return formatYyyymmddDot(value);
131
            default:
132
                return "JodaTimePartialConverter Error: unsupported format";
133
           }
134
        }
135
        return "";
136
    }
137

    
138
    /**
139
     * @param value
140
     * @param sb
141
     */
142
    private String formatIso8601(Partial value) {
143
        StringBuffer sb = new StringBuffer();
144
        String glue = "-";
145
        try {
146
            sb.append(value.get(DateTimeFieldType.year()));
147
            try {
148
                String month = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0");
149
                sb.append(glue).append(month);
150
                try {
151
                    String day = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.dayOfMonth()))), 2, "0");
152
                    sb.append(glue).append(day);
153
                } catch (IllegalArgumentException e){
154
                    /* IGNORE */
155
                }
156
            } catch (IllegalArgumentException e){
157
                /* IGNORE */
158
            }
159
        } catch (IllegalArgumentException e){
160
            /* IGNORE */
161
        }
162
        return sb.toString();
163
    }
164

    
165
    /**
166
     * @param value
167
     * @param sb
168
     */
169
    private String formatYyyymmddDot(Partial value) {
170
        StringBuffer sb = new StringBuffer();
171
        String glue = ".";
172
        try {
173
            sb.append(StringUtils.reverse(Integer.toString(value.get(DateTimeFieldType.year()))));
174
            try {
175
                String month = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0");
176
                sb.append(glue).append(StringUtils.reverse(month));
177
                try {
178
                    String day = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.dayOfMonth()))), 2, "0");
179
                    sb.append(glue).append(StringUtils.reverse(day));
180
                } catch (IllegalArgumentException e){
181
                    /* IGNORE */
182
                }
183
            } catch (IllegalArgumentException e){
184
                /* IGNORE */
185
            }
186
        } catch (IllegalArgumentException e){
187
            /* IGNORE */
188
        }
189
        return StringUtils.reverse(sb.toString());
190
    }
191

    
192
    /**
193
     * {@inheritDoc}
194
     */
195
    @Override
196
    public Class<Partial> getModelType() {
197
        return Partial.class;
198
    }
199

    
200
    /**
201
     * {@inheritDoc}
202
     */
203
    @Override
204
    public Class<String> getPresentationType() {
205
        return String.class;
206
    }
207

    
208
}
(2-2/3)