ref #7023 switched to vaadin-grid
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / util / converter / JodaTimePartialConverter.java
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 String text = null;
126 if(value != null){
127 switch(format) {
128 case ISO8601:
129 text = formatIso8601(value);
130 break;
131 case DAY_MONTH_YEAR_DOT:
132 text = formatYyyymmddDot(value);
133 break;
134 default:
135 text = "JodaTimePartialConverter Error: unsupported format";
136 }
137 }
138 return text;
139 }
140
141 /**
142 * @param value
143 * @param sb
144 */
145 private String formatIso8601(Partial value) {
146 StringBuffer sb = new StringBuffer();
147 String glue = "-";
148 try {
149 sb.append(value.get(DateTimeFieldType.year()));
150 try {
151 String month = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0");
152 sb.append(glue).append(month);
153 try {
154 String day = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.dayOfMonth()))), 2, "0");
155 sb.append(glue).append(day);
156 } catch (IllegalArgumentException e){
157 /* IGNORE */
158 }
159 } catch (IllegalArgumentException e){
160 /* IGNORE */
161 }
162 } catch (IllegalArgumentException e){
163 /* IGNORE */
164 }
165 return sb.toString();
166 }
167
168 /**
169 * @param value
170 * @param sb
171 */
172 private String formatYyyymmddDot(Partial value) {
173 StringBuffer sb = new StringBuffer();
174 String glue = ".";
175 try {
176 sb.append(StringUtils.reverse(Integer.toString(value.get(DateTimeFieldType.year()))));
177 try {
178 String month = StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0");
179 sb.append(glue).append(StringUtils.reverse(month));
180 try {
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){
184 /* IGNORE */
185 }
186 } catch (IllegalArgumentException e){
187 /* IGNORE */
188 }
189 } catch (IllegalArgumentException e){
190 /* IGNORE */
191 }
192 return StringUtils.reverse(sb.toString());
193 }
194
195 /**
196 * {@inheritDoc}
197 */
198 @Override
199 public Class<Partial> getModelType() {
200 return Partial.class;
201 }
202
203 /**
204 * {@inheritDoc}
205 */
206 @Override
207 public Class<String> getPresentationType() {
208 return String.class;
209 }
210
211 }