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.document.service.attributes;
35
36 import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeParameter;
37 import fr.paris.lutece.portal.service.i18n.I18nService;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.util.date.DateUtil;
40
41 import java.text.ParseException;
42 import java.text.SimpleDateFormat;
43
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.Date;
47 import java.util.List;
48 import java.util.Locale;
49
50
51
52
53
54 public class DateManager extends DefaultManager
55 {
56 private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/document/attributes/create_date.html";
57 private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/document/attributes/modify_date.html";
58 private static final String TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE = "admin/plugins/document/attributes/create_parameters_date.html";
59 private static final String TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE = "admin/plugins/document/attributes/modify_parameters_date.html";
60 private static final String PROPERTY_MESSAGE_ERROR_FORMAT_DATE = "document.message.errorDateFormat";
61 private static final String[] DATE_FORMAT = {"yyyy-MM-dd HH:mm:ss", "yyyy", "MM", "MM/yyyy", "dd/MM/yyyy" };
62 private static final String CHECK_ON = "on";
63
64
65
66
67
68 protected String getCreateTemplate( )
69 {
70 return TEMPLATE_CREATE_ATTRIBUTE;
71 }
72
73
74
75
76
77 protected String getModifyTemplate( )
78 {
79 return TEMPLATE_MODIFY_ATTRIBUTE;
80 }
81
82
83
84
85
86 protected String getCreateParametersTemplate( )
87 {
88 return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
89 }
90
91
92
93
94
95 protected String getModifyParametersTemplate( )
96 {
97 return TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE;
98 }
99
100
101
102
103
104
105
106
107 public String validateValue( int nAttributeId, String strValue, Locale locale )
108 {
109 String strValidateValue = super.validateValue( nAttributeId, strValue, locale );
110
111 if ( strValidateValue != null )
112 {
113 return strValidateValue;
114 }
115
116 String strValidateDate = validateDate( strValue, locale );
117
118 if ( strValidateDate != null )
119 {
120 return I18nService.getLocalizedString( strValidateDate, locale );
121 }
122
123 return null;
124 }
125
126
127
128
129
130
131
132 public String validateValueParameters( List<AttributeTypeParameter> listParameters, Locale locale )
133 {
134 String strValue;
135 String strReturn = null;
136
137 for ( AttributeTypeParameter parameter : listParameters )
138 {
139 strValue = parameter.getValueList( ).iterator( ).hasNext( )
140 ? parameter.getValueList( ).iterator( ).next( ) : "";
141
142
143 if ( strValue.equals( CHECK_ON ) )
144 {
145 strReturn = null;
146 }
147 else
148 {
149
150 String strValidate = validateDate( strValue, locale );
151
152 if ( strValidate != null )
153 {
154 strReturn = strValidate;
155 }
156 }
157 }
158
159 return strReturn;
160 }
161
162
163
164
165
166
167
168 public Collection<AttributeTypeParameter> getAttributeParametersValues( int nAttributeId, Locale locale )
169 {
170 Collection<AttributeTypeParameter> listParameters = super.getAttributeParametersValues( nAttributeId, locale );
171
172 for ( AttributeTypeParameter parameter : listParameters )
173 {
174 if ( parameter.getValueList( ).iterator( ).hasNext( ) &&
175 parameter.getValueList( ).iterator( ).next( ).equals( CHECK_ON ) )
176 {
177
178 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( DATE_FORMAT[3] );
179 Date date = new Date( );
180 ArrayList<String> listValues = new ArrayList<String>( );
181 listValues.add( simpleDateFormat.format( date ) );
182 parameter.setValueList( listValues );
183 }
184 }
185
186 return listParameters;
187 }
188
189
190
191
192
193
194
195 private String validateDate( String strDate, Locale locale )
196 {
197 Date date = null;
198 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( );
199 int i = 0;
200
201 if ( ( strDate == null ) || strDate.equals( "" ) )
202 {
203 return null;
204 }
205
206 while ( ( date == null ) && ( i < DATE_FORMAT.length ) )
207 {
208 simpleDateFormat.applyPattern( DATE_FORMAT[i] );
209
210 if ( strDate.length( ) == DATE_FORMAT[i].length( ) )
211 {
212 try
213 {
214 date = simpleDateFormat.parse( strDate );
215 }
216 catch ( ParseException e )
217 {
218 AppLogService.debug( "Bad date format" );
219 }
220 }
221
222 i++;
223 }
224
225 if ( date == null )
226 {
227 date = DateUtil.formatDate( strDate, locale );
228 }
229 else if ( !simpleDateFormat.format( date ).equals( strDate ) )
230 {
231 return PROPERTY_MESSAGE_ERROR_FORMAT_DATE;
232 }
233
234 if ( date == null )
235 {
236 return PROPERTY_MESSAGE_ERROR_FORMAT_DATE;
237 }
238
239 return null;
240 }
241 }