2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.util
.converter
;
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
;
17 import org
.apache
.commons
.lang
.StringUtils
;
18 import org
.joda
.time
.DateTimeFieldType
;
19 import org
.joda
.time
.Partial
;
21 import com
.vaadin
.data
.util
.converter
.Converter
;
26 * @author a.kohlbecker
30 public class JodaTimePartialConverter
implements Converter
<String
, Partial
> {
34 * yyyymmddDot: dd.mm.yyyy
36 * @author a.kohlbecker
40 public enum DateFormat
{
45 private static final long serialVersionUID
= 976413549472527584L;
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]))?)?$");
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})$");
53 List
<Pattern
> patterns
= Arrays
.asList(new Pattern
[]{partialPatternIso8601
, partialPatternDayMonthYearDot
});
58 * @param format The format of the string representation
60 public JodaTimePartialConverter(DateFormat format
) {
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;
73 partial
= new Partial();
74 for(Pattern pattern
: patterns
){
75 Matcher m
= pattern
.matcher(value
);
77 partial
= makePartial(m
);
89 private Partial
makePartial(Matcher m
) {
91 Partial partial
= new Partial();
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
);
101 String month
= m
.group("month");
102 partial
= partial
.with(DateTimeFieldType
.monthOfYear(), Integer
.parseInt(month
));
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 */
109 } catch (IllegalArgumentException e
) {
110 /* IGNORE months are not required */
112 } catch (NumberFormatException ne
) {
113 // all numbers should be parsable, this is guaranteed by the partialPattern
114 throw new ConversionException(ne
);
123 public String
convertToPresentation(Partial value
, Class
<?
extends String
> targetType
, Locale locale
)
124 throws com
.vaadin
.data
.util
.converter
.Converter
.ConversionException
{
129 text
= formatIso8601(value
);
131 case DAY_MONTH_YEAR_DOT
:
132 text
= formatYyyymmddDot(value
);
135 text
= "JodaTimePartialConverter Error: unsupported format";
145 private String
formatIso8601(Partial value
) {
146 StringBuffer sb
= new StringBuffer();
149 sb
.append(value
.get(DateTimeFieldType
.year()));
151 String month
= StringUtils
.leftPad(Integer
.toString((value
.get(DateTimeFieldType
.monthOfYear()))), 2, "0");
152 sb
.append(glue
).append(month
);
154 String day
= StringUtils
.leftPad(Integer
.toString((value
.get(DateTimeFieldType
.dayOfMonth()))), 2, "0");
155 sb
.append(glue
).append(day
);
156 } catch (IllegalArgumentException e
){
159 } catch (IllegalArgumentException e
){
162 } catch (IllegalArgumentException e
){
165 return sb
.toString();
172 private String
formatYyyymmddDot(Partial value
) {
173 StringBuffer sb
= new StringBuffer();
176 sb
.append(StringUtils
.reverse(Integer
.toString(value
.get(DateTimeFieldType
.year()))));
178 String month
= StringUtils
.leftPad(Integer
.toString((value
.get(DateTimeFieldType
.monthOfYear()))), 2, "0");
179 sb
.append(glue
).append(StringUtils
.reverse(month
));
181 String day
= StringUtils
.leftPad(Integer
.toString((value
.get(DateTimeFieldType
.dayOfMonth()))), 2, "0");
182 sb
.append(glue
).append(StringUtils
.reverse(day
));
183 } catch (IllegalArgumentException e
){
186 } catch (IllegalArgumentException e
){
189 } catch (IllegalArgumentException e
){
192 return StringUtils
.reverse(sb
.toString());
199 public Class
<Partial
> getModelType() {
200 return Partial
.class;
207 public Class
<String
> getPresentationType() {