Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ParameterConstraintDateInPastImpl |
|
| 4.333333333333333;4,333 |
1 | 0 | /** |
2 | * 2010, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos | |
3 | * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt | |
4 | */ | |
5 | ||
6 | package pt.digitalis.dif.dem.objects.parameters.constraints.impl.date; | |
7 | ||
8 | import java.text.ParseException; | |
9 | import java.util.Calendar; | |
10 | import java.util.Date; | |
11 | ||
12 | import pt.digitalis.dif.dem.interfaces.IStageInstance; | |
13 | import pt.digitalis.utils.common.DateUtils; | |
14 | ||
15 | /** | |
16 | * Date in Past parameter constraint implementation.<br> | |
17 | * Will validate if the given date is in the past as it is mandatory. | |
18 | * | |
19 | * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/> | |
20 | * @created 2010/09/14 | |
21 | */ | |
22 | 0 | public class ParameterConstraintDateInPastImpl extends ParameterConstraintDateImpl { |
23 | ||
24 | /** | |
25 | * @param value | |
26 | * the date to see if it's in the past | |
27 | * @return T if the given date is not after the current one | |
28 | */ | |
29 | private boolean isDateInPast(Date value) | |
30 | { | |
31 | 0 | Calendar calendarValue = Calendar.getInstance(); |
32 | 0 | calendarValue.setTime(value); |
33 | ||
34 | 0 | return !Calendar.getInstance().before(calendarValue); |
35 | } | |
36 | ||
37 | /** | |
38 | * @see pt.digitalis.dif.dem.objects.parameters.constraints.impl.date.ParameterConstraintDateImpl#validateConstraint(java.lang.Object, | |
39 | * pt.digitalis.dif.dem.interfaces.IStageInstance) | |
40 | */ | |
41 | @Override | |
42 | public boolean validateConstraint(Object value, IStageInstance stageInstance) | |
43 | { | |
44 | 0 | if (value == null) |
45 | 0 | return true; |
46 | 0 | else if (value instanceof Date) |
47 | { | |
48 | 0 | lastTestedValue = DateUtils.simpleDateToString((Date) value); |
49 | ||
50 | 0 | return isDateInPast((Date) value); |
51 | } | |
52 | 0 | else if (value instanceof String) |
53 | 0 | return this.validateConstraint(value.toString(), stageInstance); |
54 | else | |
55 | 0 | return false; |
56 | } | |
57 | ||
58 | /** | |
59 | * @see pt.digitalis.dif.dem.objects.parameters.constraints.impl.date.ParameterConstraintDateImpl#validateConstraint(java.lang.String, | |
60 | * pt.digitalis.dif.dem.interfaces.IStageInstance) | |
61 | */ | |
62 | @Override | |
63 | public boolean validateConstraint(String value, IStageInstance stageInstance) | |
64 | { | |
65 | 0 | if (super.validateConstraint(value, stageInstance)) |
66 | { | |
67 | try | |
68 | { | |
69 | 0 | return isDateInPast(DateUtils.stringToSimpleDate(value)); |
70 | } | |
71 | 0 | catch (ParseException e) |
72 | { | |
73 | 0 | return false; |
74 | } | |
75 | } | |
76 | else | |
77 | 0 | return false; |
78 | } | |
79 | } |