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.directory.business;
35
36 import fr.paris.lutece.plugins.directory.service.directorysearch.DirectorySearchItem;
37 import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
38 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39 import fr.paris.lutece.portal.service.i18n.I18nService;
40 import fr.paris.lutece.portal.service.message.AdminMessage;
41 import fr.paris.lutece.portal.service.message.AdminMessageService;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.util.date.DateUtil;
44
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Locale;
50
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56
57
58 public class EntryTypeDate extends Entry
59 {
60 private static final String _message_illogical_date = "directory.message.illogical_date";
61 private static final String _message_illogical_date_begin = "directory.message.illogical_date_begin";
62 private static final String _message_illogical_date_end = "directory.message.illogical_date_end";
63 private static final String _message_illogical_date_end_before_date_begin = "directory.message.illogical_date_end_before_date_begin";
64 private static final String _message_mandatory_date_begin_date_end = "directory.message.mandatory_date_begin_date_end";
65 private static final int _nIdFieldDateBegin = -2;
66 private static final int _nIdFieldDateEnd = -3;
67 private final String _template_create = "admin/plugins/directory/entrytypedate/create_entry_type_date.html";
68 private final String _template_modify = "admin/plugins/directory/entrytypedate/modify_entry_type_date.html";
69 private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypedate/html_code_form_entry_type_date.html";
70 private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypedate/html_code_form_search_entry_type_date.html";
71 private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypedate/html_code_entry_value_type_date.html";
72 private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypedate/html_code_form_entry_type_date.html";
73 private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypedate/html_code_form_search_entry_type_date.html";
74 private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypedate/html_code_entry_value_type_date.html";
75
76
77
78
79 @Override
80 public String getTemplateHtmlFormEntry( boolean isDisplayFront )
81 {
82 if ( isDisplayFront )
83 {
84 return _template_html_front_code_form_entry;
85 }
86
87 return _template_html_code_form_entry;
88 }
89
90
91
92
93 @Override
94 public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
95 {
96 if ( isDisplayFront )
97 {
98 return _template_html_front_code_entry_value;
99 }
100
101 return _template_html_code_entry_value;
102 }
103
104
105
106
107 @Override
108 public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
109 {
110 if ( isDisplayFront )
111 {
112 return _template_html_front_code_form_search_entry;
113 }
114
115 return _template_html_code_form_search_entry;
116 }
117
118
119
120
121 @Override
122 public String getEntryData( HttpServletRequest request, Locale locale )
123 {
124 String strTitle = request.getParameter( PARAMETER_TITLE );
125 String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
126 String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
127 .trim( ) : null;
128 String strComment = request.getParameter( PARAMETER_COMMENT );
129 String strValue = request.getParameter( PARAMETER_VALUE );
130 String strMandatory = request.getParameter( PARAMETER_MANDATORY );
131 String strIndexed = request.getParameter( PARAMETER_INDEXED );
132 String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
133 String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
134 String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
135 String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
136 String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
137 String strMultipleSearchFields = request.getParameter( PARAMETER_MULTIPLE_SEARCH_FIELDS );
138 String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
139 String strFieldError = DirectoryUtils.EMPTY_STRING;
140 String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
141 String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
142
143 if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
144 {
145 strFieldError = FIELD_TITLE;
146 }
147
148 if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
149 {
150 Object [ ] tabRequiredFields = {
151 I18nService.getLocalizedString( strFieldError, locale )
152 };
153
154 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
155 }
156
157 Date dDateValue = null;
158
159 if ( ( strValue != null ) && !strValue.equals( DirectoryUtils.EMPTY_STRING ) )
160 {
161 dDateValue = DateUtil.formatDate( strValue, locale );
162
163 if ( dDateValue == null )
164 {
165 return AdminMessageService.getMessageUrl( request, _message_illogical_date, AdminMessage.TYPE_STOP );
166 }
167 }
168
169 this.setTitle( strTitle );
170 this.setHelpMessage( strHelpMessage );
171 this.setHelpMessageSearch( strHelpMessageSearch );
172 this.setComment( strComment );
173
174 if ( this.getFields( ) == null )
175 {
176 ArrayList<Field> listFields = new ArrayList<Field>( );
177 Field field = new Field( );
178 listFields.add( field );
179 this.setFields( listFields );
180 }
181
182 this.getFields( ).get( 0 ).setValueTypeDate( dDateValue );
183 this.setMandatory( strMandatory != null );
184 this.setIndexed( strIndexed != null );
185 this.setIndexedAsTitle( strIndexedAsTitle != null );
186 this.setIndexedAsSummary( strIndexedAsSummary != null );
187 this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
188 this.setShownInResultList( strShowInResultList != null );
189 this.setShownInResultRecord( strShowInResultRecord != null );
190 this.setMultipleSearchFields( strMultipleSearchFields != null );
191 this.setShownInHistory( strShowInHistory != null );
192 this.setShownInExport( strShowInExport != null );
193 this.setShownInCompleteness( strShowInCompleteness != null );
194
195 return null;
196 }
197
198
199
200
201 @Override
202 public String getTemplateCreate( )
203 {
204 return _template_create;
205 }
206
207
208
209
210 @Override
211 public String getTemplateModify( )
212 {
213 return _template_modify;
214 }
215
216
217
218
219 @Override
220 public void getRecordFieldData( Record record, HttpServletRequest request, boolean bTestDirectoryError, boolean bAddNewValue,
221 List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
222 {
223 if ( !this.isMultipleSearchFields( ) || ( request.getParameterValues( PARAMETER_SEARCH_ACTION ) == null ) )
224 {
225 super.getRecordFieldData( record, request, bTestDirectoryError, bAddNewValue, listRecordField, locale );
226 }
227 else
228 {
229 List<String> lstValue = new ArrayList<String>( );
230 String strDateBegin = request.getParameter( PARAMETER_DATE_BEGIN + "_" + this.getIdEntry( ) );
231 String strDateEnd = request.getParameter( PARAMETER_DATE_END + "_" + this.getIdEntry( ) );
232 lstValue.add( strDateBegin );
233 lstValue.add( strDateEnd );
234 getRecordFieldData( record, lstValue, bTestDirectoryError, bAddNewValue, listRecordField, locale );
235 }
236 }
237
238
239
240
241 @Override
242 public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
243 Locale locale ) throws DirectoryErrorException
244 {
245 RecordField recordField;
246
247 if ( this.isMultipleSearchFields( ) && ( ( lstValue != null ) && ( lstValue.size( ) > 1 ) ) )
248 {
249 String strDateBegin = lstValue.get( 0 );
250 String strDateEnd = lstValue.get( 1 );
251 Date tDateBegin = null;
252 Date tDateEnd = null;
253 String strError = null;
254
255 if ( ( !strDateBegin.equals( DirectoryUtils.EMPTY_STRING ) && strDateEnd.equals( DirectoryUtils.EMPTY_STRING ) )
256 || ( !strDateEnd.equals( DirectoryUtils.EMPTY_STRING ) && strDateBegin.equals( DirectoryUtils.EMPTY_STRING ) ) )
257 {
258 strError = _message_mandatory_date_begin_date_end;
259 }
260
261 if ( ( strError == null ) && !strDateBegin.equals( DirectoryUtils.EMPTY_STRING ) )
262 {
263 tDateBegin = DateUtil.formatDate( strDateBegin, locale );
264
265 if ( tDateBegin == null )
266 {
267 strError = _message_illogical_date_begin;
268 }
269 }
270
271 if ( ( strError == null ) && !strDateEnd.equals( DirectoryUtils.EMPTY_STRING ) )
272 {
273 tDateEnd = DateUtil.formatDate( strDateEnd, locale );
274
275 if ( tDateEnd == null )
276 {
277 strError = _message_illogical_date_end;
278 }
279 }
280
281 if ( ( strError == null ) && ( tDateBegin != null ) && ( tDateEnd != null ) && tDateEnd.before( tDateBegin ) )
282 {
283 strError = _message_illogical_date_end_before_date_begin;
284 }
285
286 if ( strError != null )
287 {
288 strError = I18nService.getLocalizedString( strError, locale );
289
290 throw new DirectoryErrorException( this.getTitle( ), strError );
291 }
292
293 if ( tDateBegin != null )
294 {
295 recordField = new RecordField( );
296
297 Field mockFieldDateBegin = new Field( );
298 mockFieldDateBegin.setIdField( _nIdFieldDateBegin );
299 recordField.setValue( ( DirectoryUtils.EMPTY_STRING + tDateBegin.getTime( ) ) );
300 recordField.setField( mockFieldDateBegin );
301 recordField.setEntry( this );
302 listRecordField.add( recordField );
303 }
304
305 if ( tDateEnd != null )
306 {
307 recordField = new RecordField( );
308
309 Field mockFieldDateEnd = new Field( );
310 mockFieldDateEnd.setIdField( _nIdFieldDateEnd );
311 recordField.setValue( ( DirectoryUtils.EMPTY_STRING + tDateEnd.getTime( ) ) );
312 recordField.setField( mockFieldDateEnd );
313 recordField.setEntry( this );
314 listRecordField.add( recordField );
315 }
316 }
317 else
318 {
319 recordField = new RecordField( );
320 recordField.setEntry( this );
321
322 String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
323
324 if ( strValueEntry != null )
325 {
326 if ( bTestDirectoryError && this.isMandatory( ) && strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
327 {
328 throw new DirectoryErrorException( this.getTitle( ) );
329 }
330
331 if ( !strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
332 {
333 Date tDateValue = DateUtil.formatDate( strValueEntry, locale );
334
335 if ( tDateValue == null )
336 {
337 String strError = I18nService.getLocalizedString( _message_illogical_date, locale );
338
339 throw new DirectoryErrorException( this.getTitle( ), strError );
340 }
341
342 recordField.setValue( ( DirectoryUtils.EMPTY_STRING + tDateValue.getTime( ) ) );
343 }
344 }
345
346 listRecordField.add( recordField );
347 }
348 }
349
350
351
352
353 @Override
354 public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bDisplayExport )
355 {
356 if ( recordField.getValue( ) != null )
357 {
358 try
359 {
360 Long lTime = Long.parseLong( recordField.getValue( ) );
361 Date date = new Date( lTime );
362
363 return DateUtil.getDateString( date, locale );
364 }
365 catch( Exception e )
366 {
367 AppLogService.error( e );
368 }
369 }
370
371 return DirectoryUtils.EMPTY_STRING;
372 }
373
374
375
376
377 @Override
378 public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
379 {
380 return convertRecordFieldValueToString( recordField, locale, bDisplayFront, false );
381 }
382
383
384
385
386 @Override
387 public void addSearchCriteria( HashMap<String, Object> mapSearchItem, RecordField recordField )
388 {
389 if ( recordField.getValue( ) != null )
390 {
391 try
392 {
393 Long lTime = Long.parseLong( new String( recordField.getValue( ) ) );
394 Date date = new Date( lTime );
395
396 if ( recordField.getField( ) != null )
397 {
398 if ( recordField.getField( ).getIdField( ) == _nIdFieldDateBegin )
399 {
400 mapSearchItem.put( DirectorySearchItem.FIELD_DATE_BEGIN, date );
401 }
402 else
403 if ( recordField.getField( ).getIdField( ) == _nIdFieldDateEnd )
404 {
405 mapSearchItem.put( DirectorySearchItem.FIELD_DATE_END, date );
406 }
407 }
408 else
409 {
410 mapSearchItem.put( DirectorySearchItem.FIELD_DATE, date );
411 }
412 }
413 catch( Exception e )
414 {
415 AppLogService.error( e );
416 }
417 }
418 }
419
420
421
422
423 @Override
424 public boolean isSortable( )
425 {
426 return true;
427 }
428 }