1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.formengine.service.validator;
35
36 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Field;
37 import fr.paris.lutece.plugins.formengine.web.FormErrorsList;
38 import fr.paris.lutece.portal.service.util.AppPropertiesService;
39
40 import java.text.ParseException;
41 import java.text.SimpleDateFormat;
42
43 import java.util.Date;
44
45
46
47
48
49 public class ValidatorDateFormat extends FieldValidator
50 {
51 private static final String PROPERTY_VALIDATION_MESSAGE_FORMAT = "formengine.validator.message.date.format";
52 private static final String PROPERTY_VALIDATION_MESSAGE_MIN = "formengine.validator.message.date.min";
53 private static final String PROPERTY_VALIDATION_MESSAGE_MAX = "formengine.validator.message.date.max";
54 private static final String PROPERTY_VALIDATION_MESSAGE_MIN_NOW = "formengine.validator.message.date.min.now";
55 private static final String PROPERTY_VALIDATION_MESSAGE_MAX_NOW = "formengine.validator.message.date.max.now";
56 private static final String PROPERTY_VALIDATION_KEYWORD_NOW = "formengine.validator.keyword.date.now";
57
58
59
60
61
62
63
64
65
66 public boolean validate( Field field, FormErrorsList errors )
67 {
68 String strDateValue = field.getValue( );
69 String strDateFormat = this.getRuleParameter( );
70
71 String[] messageParams = new String[3];
72 messageParams[0] = field.getName( );
73 messageParams[1] = field.getLabel( );
74
75 String strErrorPropertyMessage;
76
77 if ( ( strDateValue == null ) || ( strDateValue.trim( ).equals( "" ) ) )
78 {
79
80 return true;
81 }
82
83 if ( strDateFormat == null )
84 {
85 return false;
86 }
87
88 try
89 {
90 SimpleDateFormat dateFormat = new SimpleDateFormat( strDateFormat );
91 dateFormat.setLenient( false );
92
93 Date date = dateFormat.parse( strDateValue );
94
95 String strDateMinValue = this.getMin( );
96 String strDateMaxValue = this.getMax( );
97 String strKeywordDateNow = AppPropertiesService.getProperty( PROPERTY_VALIDATION_KEYWORD_NOW );
98
99 if ( strDateMinValue != null )
100 {
101 try
102 {
103 Date dateMin;
104
105 if ( strDateMinValue.equals( strKeywordDateNow ) )
106 {
107
108 dateMin = dateFormat.parse( dateFormat.format( new Date( ) ) );
109 strErrorPropertyMessage = PROPERTY_VALIDATION_MESSAGE_MIN_NOW;
110 }
111 else
112 {
113 dateMin = dateFormat.parse( strDateMinValue );
114 messageParams[2] = dateFormat.format( dateMin );
115 strErrorPropertyMessage = PROPERTY_VALIDATION_MESSAGE_MIN;
116 }
117
118 if ( date.before( dateMin ) )
119 {
120 if ( this.getErrorMessage( ) != null )
121 {
122 errors.addErrorMessage( this.getErrorMessage( ) );
123 }
124 else
125 {
126 errors.addError( strErrorPropertyMessage, messageParams );
127 }
128
129 return false;
130 }
131 }
132 catch ( ParseException e )
133 {
134
135 }
136 }
137
138 if ( strDateMaxValue != null )
139 {
140 try
141 {
142 Date dateMax;
143
144 if ( strDateMaxValue.equals( strKeywordDateNow ) )
145 {
146 dateMax = dateFormat.parse( dateFormat.format( new Date( ) ) );
147
148
149 strErrorPropertyMessage = PROPERTY_VALIDATION_MESSAGE_MAX_NOW;
150 }
151 else
152 {
153 dateMax = dateFormat.parse( strDateMaxValue );
154 messageParams[2] = dateFormat.format( dateMax );
155 strErrorPropertyMessage = PROPERTY_VALIDATION_MESSAGE_MAX;
156 }
157
158 if ( date.after( dateMax ) )
159 {
160 if ( this.getErrorMessage( ) != null )
161 {
162 errors.addErrorMessage( this.getErrorMessage( ) );
163 }
164 else
165 {
166 errors.addError( strErrorPropertyMessage, messageParams );
167 }
168
169 return false;
170 }
171 }
172 catch ( ParseException e )
173 {
174
175 }
176 }
177
178 return true;
179 }
180 catch ( Exception e )
181 {
182 messageParams[2] = strDateFormat;
183 strErrorPropertyMessage = PROPERTY_VALIDATION_MESSAGE_FORMAT;
184
185 if ( this.getErrorMessage( ) != null )
186 {
187 errors.addErrorMessage( this.getErrorMessage( ) );
188 }
189 else
190 {
191 errors.addError( strErrorPropertyMessage, messageParams );
192 }
193
194 return false;
195 }
196 }
197 }