1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
package pt.digitalis.dif.dem.objects.parameters.constraints.impl.date; |
6 | |
|
7 | |
import java.text.ParseException; |
8 | |
import java.util.ArrayList; |
9 | |
import java.util.Date; |
10 | |
import java.util.HashMap; |
11 | |
import java.util.List; |
12 | |
import java.util.Map; |
13 | |
|
14 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
15 | |
import pt.digitalis.dif.dem.objects.parameters.constraints.AbstractParameterConstraint; |
16 | |
import pt.digitalis.utils.common.DateUtils; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 0 | public class ParameterConstraintDateImpl extends AbstractParameterConstraint { |
25 | |
|
26 | |
|
27 | |
@SuppressWarnings("serial") |
28 | 0 | final static private List<String> suportedClasses = new ArrayList<String>() { |
29 | |
|
30 | |
{ |
31 | 0 | add(Date.class.getCanonicalName()); |
32 | |
} |
33 | |
}; |
34 | |
|
35 | |
|
36 | |
String lastTestedValue; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public void configureConstraint(String configurationString) |
42 | |
{ |
43 | |
|
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Override |
50 | |
protected Map<String, String> getErrorMessageValues(boolean parseValues) |
51 | |
{ |
52 | 0 | Map<String, String> substitutions = new HashMap<String, String>(); |
53 | |
|
54 | 0 | if (parseValues) |
55 | |
{ |
56 | 0 | substitutions.put("value", lastTestedValue); |
57 | |
} |
58 | |
|
59 | 0 | return substitutions; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public String getJavaScriptValidationCondition() |
66 | |
{ |
67 | 0 | return "Ext.Date.parse(value,'d/m/Y', true) != undefined"; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public boolean isSupportedClass(Class<?> clazz) |
74 | |
{ |
75 | 0 | return suportedClasses.contains(clazz.getCanonicalName()); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
public boolean validateConstraint(Object value, IStageInstance stageInstance) |
84 | |
{ |
85 | |
|
86 | 0 | if (value == null) |
87 | 0 | return this.validateConstraint((String) null, stageInstance); |
88 | 0 | else if (value instanceof Date) |
89 | 0 | return true; |
90 | 0 | else if (value instanceof String) |
91 | 0 | return this.validateConstraint(value.toString(), stageInstance); |
92 | |
else |
93 | 0 | return false; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public boolean validateConstraint(String value, IStageInstance stageInstance) |
101 | |
{ |
102 | 0 | lastTestedValue = value; |
103 | |
|
104 | 0 | if (value == null) |
105 | 0 | return true; |
106 | |
else |
107 | |
try |
108 | |
{ |
109 | 0 | DateUtils.stringToSimpleDate(value); |
110 | 0 | return true; |
111 | |
|
112 | |
} |
113 | 0 | catch (ParseException e) |
114 | |
{ |
115 | 0 | return false; |
116 | |
} |
117 | 0 | catch (NumberFormatException e) |
118 | |
{ |
119 | 0 | return false; |
120 | |
} |
121 | |
} |
122 | |
} |