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.Locale;
|
12
|
import java.util.regex.Matcher;
|
13
|
import java.util.regex.Pattern;
|
14
|
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.joda.time.DateTimeFieldType;
|
17
|
import org.joda.time.Partial;
|
18
|
|
19
|
import com.vaadin.data.util.converter.Converter;
|
20
|
|
21
|
|
22
|
/**
|
23
|
* @author a.kohlbecker
|
24
|
* @since Apr 7, 2017
|
25
|
*
|
26
|
*/
|
27
|
public class JodaTimePartialConverter implements Converter<String, Partial> {
|
28
|
|
29
|
private static final long serialVersionUID = 976413549472527584L;
|
30
|
|
31
|
static final String GLUE = "-";
|
32
|
|
33
|
Pattern partialPattern = Pattern.compile("^(?<year>(?:[1,2][7,8,9,0,1])?[0-9]{2})(?:-(?<month>[0-1]?[0-9])(?:-(?<day>[0-3]?[0-9]))?)?$");
|
34
|
|
35
|
/**
|
36
|
*
|
37
|
*/
|
38
|
public JodaTimePartialConverter() {
|
39
|
super();
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* {@inheritDoc}
|
44
|
*/
|
45
|
@Override
|
46
|
public Partial convertToModel(String value, Class<? extends Partial> targetType, Locale locale)
|
47
|
throws com.vaadin.data.util.converter.Converter.ConversionException {
|
48
|
Partial partial = null;
|
49
|
if(value != null){
|
50
|
partial = new Partial();
|
51
|
Matcher m = partialPattern.matcher(value);
|
52
|
if(m.matches()){
|
53
|
try {
|
54
|
try {
|
55
|
partial.with(DateTimeFieldType.year(), Integer.parseInt(m.group("year")));
|
56
|
} catch (IllegalArgumentException e) {
|
57
|
// a valid year should be present here
|
58
|
throw new ConversionException(e);
|
59
|
}
|
60
|
try {
|
61
|
partial.with(DateTimeFieldType.monthOfYear(), Integer.parseInt(m.group("month")));
|
62
|
try {
|
63
|
partial.with(DateTimeFieldType.dayOfMonth(), Integer.parseInt(m.group("day")));
|
64
|
} catch (IllegalArgumentException e) {
|
65
|
/* IGNORE days are not required */
|
66
|
}
|
67
|
} catch (IllegalArgumentException e) {
|
68
|
/* IGNORE months are not required */
|
69
|
}
|
70
|
} catch (NumberFormatException ne) {
|
71
|
// all numbers should be parsable, this is guaranteed by the partialPattern
|
72
|
throw new ConversionException(ne);
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
return partial;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* {@inheritDoc}
|
81
|
*/
|
82
|
@Override
|
83
|
public String convertToPresentation(Partial value, Class<? extends String> targetType, Locale locale)
|
84
|
throws com.vaadin.data.util.converter.Converter.ConversionException {
|
85
|
StringBuffer sb = new StringBuffer();
|
86
|
if(value != null){
|
87
|
try {
|
88
|
sb.append(value.get(DateTimeFieldType.year()));
|
89
|
try {
|
90
|
sb.append(GLUE).append(StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.monthOfYear()))), 2, "0"));
|
91
|
try {
|
92
|
sb.append(GLUE).append(StringUtils.leftPad(Integer.toString((value.get(DateTimeFieldType.dayOfMonth()))), 2, "0"));
|
93
|
} catch (IllegalArgumentException e){
|
94
|
/* IGNORE */
|
95
|
}
|
96
|
} catch (IllegalArgumentException e){
|
97
|
/* IGNORE */
|
98
|
}
|
99
|
} catch (IllegalArgumentException e){
|
100
|
/* IGNORE */
|
101
|
}
|
102
|
}
|
103
|
return sb.toString();
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* {@inheritDoc}
|
108
|
*/
|
109
|
@Override
|
110
|
public Class<Partial> getModelType() {
|
111
|
return Partial.class;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* {@inheritDoc}
|
116
|
*/
|
117
|
@Override
|
118
|
public Class<String> getPresentationType() {
|
119
|
return String.class;
|
120
|
}
|
121
|
|
122
|
}
|