1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
package pt.digitalis.dif.dem.objects.parameters.types; |
6 | |
|
7 | |
import java.math.BigDecimal; |
8 | |
import java.util.ArrayList; |
9 | |
import java.util.Calendar; |
10 | |
import java.util.Date; |
11 | |
import java.util.List; |
12 | |
|
13 | |
import pt.digitalis.dif.controller.interfaces.IDIFContext; |
14 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
15 | |
import pt.digitalis.dif.dem.objects.parameters.constraints.IParameterConstraint; |
16 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterError; |
17 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorList; |
18 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorType; |
19 | |
import pt.digitalis.dif.exception.objects.ParameterException; |
20 | |
import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters; |
21 | |
import pt.digitalis.utils.common.DateUtils; |
22 | |
import pt.digitalis.utils.common.StringUtils; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class DateParameter extends AbstractParameter<Date> { |
34 | |
|
35 | |
|
36 | |
@SuppressWarnings("serial") |
37 | 0 | final static private List<String> supportedClasses = new ArrayList<String>() { |
38 | |
|
39 | |
{ |
40 | 0 | add(Date.class.getCanonicalName()); |
41 | |
} |
42 | |
}; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
@Override |
48 | |
protected String automaticConstraints() |
49 | |
{ |
50 | 0 | return "date"; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
@Override |
57 | |
protected String convertObjectToString(Object obj) |
58 | |
{ |
59 | 0 | if (obj instanceof Date) |
60 | 0 | return DateUtils.simpleDateToString((Date) obj); |
61 | |
else |
62 | 0 | return super.convertObjectToString(obj); |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public List<String> getSupportedClasses() |
69 | |
{ |
70 | 0 | return supportedClasses; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public BigDecimal getValueAsBigDecimal(IDIFContext context) throws ParameterException |
77 | |
{ |
78 | 0 | return BigDecimal.valueOf(getValue(context).getTime()); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public boolean getValueAsBoolean(IDIFContext context) |
85 | |
{ |
86 | 0 | return false; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public Date getValueAsDate(IDIFContext context) throws ParameterException |
93 | |
{ |
94 | 0 | return getValue(context); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Double getValueAsDouble(IDIFContext context) throws ParameterException |
101 | |
{ |
102 | 0 | return Double.valueOf(getValue(context).getTime()); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public Long getValueAsLong(IDIFContext context) throws ParameterException |
109 | |
{ |
110 | 0 | return getValue(context).getTime(); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@Override |
117 | |
public String getValueAsString(IDIFContext context) throws ParameterException |
118 | |
{ |
119 | |
|
120 | 0 | Date currentDate = getValue(context); |
121 | |
|
122 | 0 | if (currentDate == null) |
123 | 0 | return null; |
124 | |
else |
125 | 0 | return DateUtils.simpleDateToString(currentDate); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public boolean isNumeric() |
132 | |
{ |
133 | 0 | return false; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public ParameterErrorList setValueFromString(String value, IStageInstance stageInstance, |
141 | |
boolean initializationInProgress) |
142 | |
{ |
143 | |
|
144 | 0 | ParameterErrorList list = new ParameterErrorList(this, value); |
145 | |
ParameterError error; |
146 | 0 | IParameterConstraint constraint = getConstraints().get("date"); |
147 | |
IDIFContext context; |
148 | |
|
149 | 0 | if (stageInstance == null) |
150 | 0 | context = null; |
151 | |
else |
152 | 0 | context = stageInstance.getContext(); |
153 | |
|
154 | |
|
155 | 0 | if (!constraint.validateConstraint(value, stageInstance)) |
156 | |
{ |
157 | |
String language; |
158 | |
|
159 | 0 | if (context == null) |
160 | 0 | language = DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage(); |
161 | |
else |
162 | 0 | language = context.getLanguage(); |
163 | |
|
164 | 0 | error = new ParameterError(constraint.validationErrorMessage(language), ParameterErrorType.CONSTRAINT); |
165 | 0 | error.setConstraint("date", constraint); |
166 | |
|
167 | 0 | list.addError(error); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | |
|
172 | 0 | if (getConstraints().containsKey("futuredate") || getConstraints().containsKey("pastdate") |
173 | 0 | || getConstraints().containsKey("absolutedate") || getConstraints().containsKey("relativedate")) |
174 | |
{ |
175 | 0 | String[] parts = value.split("/"); |
176 | |
|
177 | 0 | if (parts[2].length() < 3) |
178 | |
{ |
179 | |
|
180 | 0 | parts[2] = StringUtils.fillStringLeft(parts[2], 2, "0"); |
181 | 0 | Calendar currentDate = Calendar.getInstance(); |
182 | 0 | Integer currentCenturyPrefix = currentDate.get(Calendar.YEAR) / 100; |
183 | 0 | Integer valueDateYear2Digits = Integer.parseInt(parts[2]); |
184 | 0 | Integer currentDateYear2Digits = currentDate.get(Calendar.YEAR) % 100; |
185 | 0 | Calendar currentCenturyTempDate = Calendar.getInstance(); |
186 | 0 | currentCenturyTempDate.set(currentCenturyPrefix * 100 + valueDateYear2Digits, |
187 | 0 | Integer.parseInt(parts[1]), Integer.parseInt(parts[0])); |
188 | |
|
189 | 0 | if (getConstraints().containsKey("pastdate")) |
190 | |
{ |
191 | 0 | if (currentCenturyTempDate.before(currentDate)) |
192 | 0 | parts[2] = currentCenturyPrefix + parts[2]; |
193 | |
else |
194 | 0 | parts[2] = (currentCenturyPrefix - 1) + parts[2]; |
195 | |
} |
196 | 0 | else if (getConstraints().containsKey("futuredate")) |
197 | |
{ |
198 | 0 | if (currentCenturyTempDate.after(currentDate)) |
199 | 0 | parts[2] = currentCenturyPrefix + parts[2]; |
200 | |
else |
201 | 0 | parts[2] = (currentCenturyPrefix + 1) + parts[2]; |
202 | |
} |
203 | 0 | else if (getConstraints().containsKey("relativedate")) |
204 | |
{ |
205 | |
|
206 | 0 | if (Math.abs(valueDateYear2Digits - currentDateYear2Digits) <= Math.abs(currentDateYear2Digits |
207 | 0 | - valueDateYear2Digits + 100)) |
208 | |
|
209 | 0 | parts[2] = currentCenturyPrefix + parts[2]; |
210 | |
else |
211 | |
|
212 | 0 | parts[2] = (currentCenturyPrefix - 1) + parts[2]; |
213 | |
} |
214 | 0 | else if (getConstraints().containsKey("absolutedate")) |
215 | |
|
216 | 0 | parts[2] = currentCenturyPrefix + parts[2]; |
217 | |
} |
218 | |
|
219 | |
|
220 | 0 | value = parts[0] + "/" + parts[1] + "/" + parts[2]; |
221 | |
} |
222 | |
|
223 | |
try |
224 | |
{ |
225 | 0 | if (value == null || "".equals(value)) |
226 | 0 | return setValue(null, stageInstance, initializationInProgress); |
227 | |
else |
228 | 0 | return setValue(DateUtils.stringToSimpleDate(value), stageInstance, initializationInProgress); |
229 | |
|
230 | |
} |
231 | 0 | catch (Exception e) |
232 | |
{ |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | 0 | error = new ParameterError("Error parsing value", ParameterErrorType.OTHER); |
241 | 0 | error.setException(e); |
242 | 0 | list.addError(error); |
243 | |
} |
244 | |
} |
245 | |
|
246 | 0 | return list; |
247 | |
} |
248 | |
} |