View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.form.modules.datevalidators.web;
35  
36  import fr.paris.lutece.plugins.form.modules.datevalidators.business.ComparatorHome;
37  import fr.paris.lutece.plugins.form.modules.datevalidators.business.DateCalculated;
38  import fr.paris.lutece.plugins.form.modules.datevalidators.business.DateCalculatedHome;
39  import fr.paris.lutece.plugins.form.modules.datevalidators.business.Operator;
40  import fr.paris.lutece.plugins.form.modules.datevalidators.business.OperatorHome;
41  import fr.paris.lutece.plugins.form.modules.datevalidators.business.Rule;
42  import fr.paris.lutece.plugins.form.modules.datevalidators.business.RuleHome;
43  import fr.paris.lutece.plugins.form.modules.datevalidators.business.Unit;
44  import fr.paris.lutece.plugins.form.modules.datevalidators.business.UnitHome;
45  import fr.paris.lutece.plugins.form.modules.datevalidators.service.DateValidatorsPlugin;
46  import fr.paris.lutece.plugins.form.modules.datevalidators.service.DateValidatorsResourceIdService;
47  import fr.paris.lutece.plugins.form.modules.datevalidators.service.DateValidatorsService;
48  import fr.paris.lutece.plugins.form.modules.datevalidators.util.DateValidatorsConstants;
49  import fr.paris.lutece.plugins.form.modules.datevalidators.util.DateValidatorsUtils;
50  import fr.paris.lutece.plugins.form.web.FormJspBean;
51  import fr.paris.lutece.portal.business.rbac.RBAC;
52  import fr.paris.lutece.portal.service.admin.AdminUserService;
53  import fr.paris.lutece.portal.service.i18n.I18nService;
54  import fr.paris.lutece.portal.service.message.AdminMessage;
55  import fr.paris.lutece.portal.service.message.AdminMessageService;
56  import fr.paris.lutece.portal.service.plugin.Plugin;
57  import fr.paris.lutece.portal.service.plugin.PluginService;
58  import fr.paris.lutece.portal.service.rbac.RBACService;
59  import fr.paris.lutece.portal.service.template.AppTemplateService;
60  import fr.paris.lutece.portal.service.util.AppPathService;
61  import fr.paris.lutece.portal.service.util.AppPropertiesService;
62  import fr.paris.lutece.portal.web.constants.Parameters;
63  import fr.paris.lutece.util.ReferenceItem;
64  import fr.paris.lutece.util.ReferenceList;
65  import fr.paris.lutece.util.date.DateUtil;
66  import fr.paris.lutece.util.html.HtmlTemplate;
67  import fr.paris.lutece.util.html.Paginator;
68  import fr.paris.lutece.util.sort.AttributeComparator;
69  import fr.paris.lutece.util.url.UrlItem;
70  
71  import org.apache.commons.lang.StringUtils;
72  
73  import java.sql.Timestamp;
74  
75  import java.util.Collections;
76  import java.util.HashMap;
77  import java.util.List;
78  import java.util.Map;
79  
80  import javax.servlet.http.HttpServletRequest;
81  
82  
83  /**
84   * DateValidatorsJspBean
85   */
86  public class DateValidatorsJspBean extends FormJspBean
87  {
88      // Markers
89      private static final String MARK_TYPE_ENTRY = "type_entry";
90      private static final String MARK_TYPE_DATE_REFERENCE = "type_date_reference";
91      private static final String MARK_TYPE_DATE_CALCULATED = "type_date_calculated";
92      private static final String MARK_TYPE = "type";
93      private static final String MARK_LOCALE = "locale";
94      private static final String MARK_RULE = "rule";
95      private static final String MARK_DATE_CALCULATED_LIST = "date_calculated_list";
96      private static final String MARK_OPERATOR_LIST = "operator_list";
97      private static final String MARK_UNIT_LIST = "unit_list";
98      private static final String MARK_DATE_CALCULATED = "date_calculated";
99  
100     // Parameters
101     private static final String PARAMETER_ID_RULE = "id_rule";
102     private static final String PARAMETER_ID_ENTRY1 = "id_entry1";
103     private static final String PARAMETER_ID_ENTRY2 = "id_entry2";
104     private static final String PARAMETER_ID_COMPARATOR = "id_comparator";
105     private static final String PARAMETER_TYPE = "type";
106     private static final String PARAMETER_CANCEL = "cancel";
107     private static final String PARAMETER_BACK = "back";
108     private static final String PARAMETER_DATE_REFERENCE = "date_reference";
109     private static final String PARAMETER_ID_DATE_CALCULATED = "id_date_calculated";
110     private static final String PARAMETER_ID_OPERATOR = "id_operator";
111     private static final String PARAMETER_NUMBER = "number";
112     private static final String PARAMETER_ID_UNIT = "id_unit";
113 
114     // Titles
115     private static final String PROPERTY_SELECT_TYPE_TITLE = "module.form.datevalidators.selectType.title";
116     private static final String PROPERTY_CREATE_RULE_TITLE = "module.form.datevalidators.createRule.title";
117     private static final String PROPERTY_MODIFY_RULE_TITLE = "module.form.datevalidators.modifyRule.title";
118     private static final String PROPERTY_MANAGE_DATE_CALCULATED_TITLE = "module.form.datevalidators.manageDateCalculated.title";
119     private static final String PROPERTY_CREATE_DATE_CALCULATED_TITLE = "module.form.datevalidators.createDateCalculated.title";
120     private static final String PROPERTY_MODIFY_DATE_CALCULATED_TITLE = "module.form.datevalidators.modifyDateCalculated.title";
121 
122     // Messages
123     private static final String MESSAGE_MANDATORY_FIELD = "module.form.datevalidators.message.mandatory.field";
124     private static final String MESSAGE_ERROR_IDENTICAL_ENTRIES = "module.form.datevalidators.message.error.identicalEntries";
125     private static final String MESSAGE_ERROR_RULE_ALREADY_EXISTS_ENTRY = "module.form.datevalidators.message.error.ruleAlreadyExists.entry";
126     private static final String MESSAGE_ERROR_RULE_ALREADY_EXISTS_DATE_REFERENCE = "module.form.datevalidators.message.error.ruleAlreadyExists.dateReference";
127     private static final String MESSAGE_ERROR_RULE_ALREADY_EXISTS_DATE_CALCULATED = "module.form.datevalidators.message.error.ruleAlreadyExists.dateCalculated";
128     private static final String MESSAGE_ERROR_DATE_CALCULATED_ALREADY_EXISTS = "module.form.datevalidators.message.error.dateCalculatedAlreadyExists";
129     private static final String MESSAGE_CONFIRM_REMOVE_RULE = "module.form.datevalidators.message.confirmRemoveRule";
130     private static final String MESSAGE_NUMERIC_FIELD = "module.form.datevalidators.message.numeric.field";
131     private static final String MESSAGE_ERROR_TYPE_NOT_EXISTS = "module.form.datevalidators.message.error.typeNotExists";
132     private static final String MESSAGE_CONFIRM_REMOVE_DATE_CALCULATED = "module.form.datevalidators.message.confirmRemoveDateCalculated";
133 
134     // Fields
135     private static final String FIELD_SELECT_TYPE_TYPE = "module.form.datevalidators.selectType.labelType";
136     private static final String FIELD_RULE_ENTRY_ENTRY = "module.form.datevalidators.createRule.entry.labelEntry1";
137     private static final String FIELD_RULE_DATE_REFERENCE_ENTRY = "module.form.datevalidators.createRule.dateReference.labelEntry";
138     private static final String FIELD_RULE_DATE_CALCULATED_ENTRY = "module.form.datevalidators.createRule.dateCalculated.labelEntry";
139     private static final String FIELD_RULE_ENTRY2 = "module.form.datevalidators.createRule.entry.labelEntry2";
140     private static final String FIELD_RULE_COMPARATOR = "module.form.datevalidators.createRule.labelComparator";
141     private static final String FIELD_RULE_DATE_REFERENCE = "module.form.datevalidators.createRule.dateReference.labelDateReference";
142     private static final String FIELD_RULE_DATE_CALCULATED = "module.form.datevalidators.createRule.dateCalculated.labelDateCalculated";
143     private static final String FIELD_DATE_CALCULATED_DATE_REFERENCE = "module.form.datevalidators.createDateCalculated.labelDateReference";
144     private static final String FIELD_DATE_CALCULATED_OPERATOR = "module.form.datevalidators.createDateCalculated.labelOperator";
145     private static final String FIELD_DATE_CALCULATED_NUMBER = "module.form.datevalidators.createDateCalculated.labelNumber";
146     private static final String FIELD_DATE_CALCULATED_UNIT = "module.form.datevalidators.createDateCalculated.labelUnit";
147 
148     // Templates
149     private static final String TEMPLATE_SELECT_TYPE = "admin/plugins/form/modules/datevalidators/select_type.html";
150     private static final String TEMPLATE_CREATE_RULE_ENTRY = "admin/plugins/form/modules/datevalidators/create_rule_entry.html";
151     private static final String TEMPLATE_CREATE_RULE_DATE_REFERENCE = "admin/plugins/form/modules/datevalidators/create_rule_date_reference.html";
152     private static final String TEMPLATE_CREATE_RULE_DATE_CALCULATED = "admin/plugins/form/modules/datevalidators/create_rule_date_calculated.html";
153     private static final String TEMPLATE_MODIFY_RULE_ENTRY = "admin/plugins/form/modules/datevalidators/modify_rule_entry.html";
154     private static final String TEMPLATE_MODIFY_RULE_DATE_REFERENCE = "admin/plugins/form/modules/datevalidators/modify_rule_date_reference.html";
155     private static final String TEMPLATE_MODIFY_RULE_DATE_CALCULATED = "admin/plugins/form/modules/datevalidators/modify_rule_date_calculated.html";
156     private static final String TEMPLATE_MANAGE_DATE_CALCULATED = "admin/plugins/form/modules/datevalidators/manage_date_calculated.html";
157     private static final String TEMPLATE_CREATE_DATE_CALCULATED = "admin/plugins/form/modules/datevalidators/create_date_calculated.html";
158     private static final String TEMPLATE_MODIFY_DATE_CALCULATED = "admin/plugins/form/modules/datevalidators/modify_date_calculated.html";
159 
160     // JSPs
161     private static final String JSP_SELECT_TYPE = "jsp/admin/plugins/form/modules/datevalidators/SelectType.jsp";
162     private static final String JSP_CREATE_RULE_ENTRY = "/jsp/admin/plugins/form/modules/datevalidators/CreateRuleEntry.jsp";
163     private static final String JSP_CREATE_RULE_DATE_REFERENCE = "jsp/admin/plugins/form/modules/datevalidators/CreateRuleDateReference.jsp";
164     private static final String JSP_CREATE_RULE_DATE_CALCULATED = "jsp/admin/plugins/form/modules/datevalidators/CreateRuleDateCalculated.jsp";
165     private static final String JSP_DO_REMOVE_RULE = "jsp/admin/plugins/form/modules/datevalidators/DoRemoveRule.jsp";
166     private static final String JSP_MANAGE_DATE_CALCULATED = "jsp/admin/plugins/form/modules/datevalidators/ManageDateCalculated.jsp";
167     private static final String JSP_DO_REMOVE_DATE_CALCULATED = "jsp/admin/plugins/form/modules/datevalidators/DoRemoveDateCalculated.jsp";
168 
169     // Plugin
170     private static final Plugin PLUGIN = PluginService.getPlugin( DateValidatorsPlugin.PLUGIN_NAME );
171 
172     // Paginator
173     private int _nDefaultItemsPerPage;
174     private String _strCurrentPageIndex;
175     private int _nItemsPerPage;
176 
177     /**
178      * Gets the type selection page
179      *
180      * @param request The HTTP request
181      * @return The type selection page
182      */
183     public String getSelectType( HttpServletRequest request )
184     {
185         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
186                     DateValidatorsResourceIdService.PERMISSION_RULE_CREATE, getUser(  ) ) )
187         {
188             return getManageValidator( request );
189         }
190 
191         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
192                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
193 
194         if ( nIdForm == null )
195         {
196             return getManageValidator( request );
197         }
198 
199         setPageTitleProperty( PROPERTY_SELECT_TYPE_TITLE );
200 
201         Map<String, Object> model = new HashMap<String, Object>(  );
202         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
203         model.put( MARK_TYPE_ENTRY, String.valueOf( DateValidatorsConstants.TYPE_ENTRY ) );
204         model.put( MARK_TYPE_DATE_REFERENCE, String.valueOf( DateValidatorsConstants.TYPE_DATE_REFERENCE ) );
205         model.put( MARK_TYPE_DATE_CALCULATED, String.valueOf( DateValidatorsConstants.TYPE_DATE_CALCULATED ) );
206 
207         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECT_TYPE, getLocale(  ), model );
208 
209         return getAdminPage( template.getHtml(  ) );
210     }
211 
212     /**
213      * Performs the type selection
214      *
215      * @param request The HTTP request
216      * @return The URL to go after performing the action
217      */
218     public String doSelectType( HttpServletRequest request )
219     {
220         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
221                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
222 
223         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
224                     DateValidatorsResourceIdService.PERMISSION_RULE_CREATE, getUser(  ) ) )
225         {
226             return getJspManageValidator( request, nIdForm );
227         }
228 
229         // Cancel
230         if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_CANCEL ) ) )
231         {
232             return getJspManageValidator( request, nIdForm );
233         }
234 
235         Integer nType = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_TYPE ) );
236 
237         if ( nType == null )
238         {
239             Object[] tabRequiredFields = { I18nService.getLocalizedString( FIELD_SELECT_TYPE_TYPE, getLocale(  ) ) };
240 
241             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
242                 AdminMessage.TYPE_STOP );
243         }
244 
245         String strJsp;
246 
247         switch ( nType )
248         {
249             case DateValidatorsConstants.TYPE_ENTRY:
250                 strJsp = JSP_CREATE_RULE_ENTRY;
251 
252                 break;
253 
254             case DateValidatorsConstants.TYPE_DATE_REFERENCE:
255                 strJsp = JSP_CREATE_RULE_DATE_REFERENCE;
256 
257                 break;
258 
259             case DateValidatorsConstants.TYPE_DATE_CALCULATED:
260                 strJsp = JSP_CREATE_RULE_DATE_CALCULATED;
261 
262                 break;
263 
264             default:
265                 strJsp = DateValidatorsConstants.JSP_MANAGE_VALIDATOR;
266 
267                 break;
268         }
269 
270         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + strJsp );
271         url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
272 
273         return url.getUrl(  );
274     }
275 
276     /**
277      * Gets the rule creation page for the type entry
278      *
279      * @param request The HTTP request
280      * @return The rule creation page for the type entry
281      */
282     public String getCreateRuleEntry( HttpServletRequest request )
283     {
284         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
285                     DateValidatorsResourceIdService.PERMISSION_RULE_CREATE, getUser(  ) ) )
286         {
287             return getManageValidator( request );
288         }
289 
290         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
291                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
292 
293         if ( nIdForm == null )
294         {
295             return getManageValidator( request );
296         }
297 
298         setPageTitleProperty( PROPERTY_CREATE_RULE_TITLE );
299 
300         ReferenceList refListComparator = ComparatorHome.findAll( PLUGIN );
301         ReferenceItem refItem = new ReferenceItem(  );
302         refItem.setCode( String.valueOf( -1 ) );
303         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
304         refListComparator.add( 0, refItem );
305 
306         Map<String, Object> model = new HashMap<String, Object>(  );
307         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
308         model.put( DateValidatorsConstants.MARK_ENTRY_LIST, DateValidatorsService.getAuthorizedEntries( nIdForm ) );
309         model.put( DateValidatorsConstants.MARK_COMPARATOR_LIST, refListComparator );
310         model.put( MARK_TYPE, DateValidatorsConstants.TYPE_ENTRY );
311 
312         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_RULE_ENTRY, getLocale(  ), model );
313 
314         return getAdminPage( template.getHtml(  ) );
315     }
316 
317     /**
318      * Gets the rule creation page for the type reference date
319      *
320      * @param request The HTTP request
321      * @return The rule creation page for the type reference date
322      */
323     public String getCreateRuleDateReference( HttpServletRequest request )
324     {
325         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
326                     DateValidatorsResourceIdService.PERMISSION_RULE_CREATE, getUser(  ) ) )
327         {
328             return getManageValidator( request );
329         }
330 
331         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
332                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
333 
334         if ( nIdForm == null )
335         {
336             return getManageValidator( request );
337         }
338 
339         setPageTitleProperty( PROPERTY_CREATE_RULE_TITLE );
340 
341         ReferenceList refListComparator = ComparatorHome.findAll( PLUGIN );
342         ReferenceItem refItem = new ReferenceItem(  );
343         refItem.setCode( String.valueOf( -1 ) );
344         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
345         refListComparator.add( 0, refItem );
346 
347         Map<String, Object> model = new HashMap<String, Object>(  );
348         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
349         model.put( DateValidatorsConstants.MARK_ENTRY_LIST, DateValidatorsService.getAuthorizedEntries( nIdForm ) );
350         model.put( DateValidatorsConstants.MARK_COMPARATOR_LIST, refListComparator );
351         model.put( MARK_TYPE, DateValidatorsConstants.TYPE_DATE_REFERENCE );
352         model.put( MARK_LOCALE, getLocale(  ) );
353 
354         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_RULE_DATE_REFERENCE, getLocale(  ),
355                 model );
356 
357         return getAdminPage( template.getHtml(  ) );
358     }
359 
360     /**
361      * Gets the rule creation page for the type calculated date
362      *
363      * @param request The HTTP request
364      * @return The rule creation page for the type calculated date
365      */
366     public String getCreateRuleDateCalculated( HttpServletRequest request )
367     {
368         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
369                     DateValidatorsResourceIdService.PERMISSION_RULE_CREATE, getUser(  ) ) )
370         {
371             return getManageValidator( request );
372         }
373 
374         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
375                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
376 
377         if ( nIdForm == null )
378         {
379             return getManageValidator( request );
380         }
381 
382         setPageTitleProperty( PROPERTY_CREATE_RULE_TITLE );
383 
384         ReferenceList refListComparator = ComparatorHome.findAll( PLUGIN );
385         ReferenceItem refItem = new ReferenceItem(  );
386         refItem.setCode( String.valueOf( -1 ) );
387         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
388         refListComparator.add( 0, refItem );
389 
390         Map<String, Object> model = new HashMap<String, Object>(  );
391         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
392         model.put( DateValidatorsConstants.MARK_ENTRY_LIST, DateValidatorsService.getAuthorizedEntries( nIdForm ) );
393         model.put( DateValidatorsConstants.MARK_COMPARATOR_LIST, refListComparator );
394         model.put( MARK_DATE_CALCULATED_LIST, DateCalculatedHome.findAll( PLUGIN ) );
395         model.put( MARK_TYPE, DateValidatorsConstants.TYPE_DATE_CALCULATED );
396 
397         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_RULE_DATE_CALCULATED, getLocale(  ),
398                 model );
399 
400         return getAdminPage( template.getHtml(  ) );
401     }
402 
403     /**
404      * Performs the rule creation
405      *
406      * @param request The HTTP request
407      * @return The URL to go after performing the action
408      */
409     public String doCreateRule( HttpServletRequest request )
410     {
411         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
412                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
413 
414         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
415                     DateValidatorsResourceIdService.PERMISSION_RULE_CREATE, getUser(  ) ) )
416         {
417             return getJspManageValidator( request, nIdForm );
418         }
419 
420         // Back
421         if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_BACK ) ) )
422         {
423             UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_SELECT_TYPE );
424             url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
425 
426             return url.getUrl(  );
427         }
428 
429         Integer nType = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_TYPE ) );
430 
431         if ( nType == null )
432         {
433             return getJspManageValidator( request, nIdForm );
434         }
435 
436         Rule rule = new Rule(  );
437         rule.setIdForm( nIdForm );
438 
439         // Get fields
440         String strError = getRuleFields( request, rule, nType );
441 
442         if ( strError != null )
443         {
444             return strError;
445         }
446 
447         RuleHome.create( rule, PLUGIN );
448 
449         return getJspManageValidator( request, nIdForm );
450     }
451 
452     /**
453      * Gets the rule modification page for the type entry
454      *
455      * @param request The HTTP request
456      * @return The rule modification page for the type entry
457      */
458     public String getModifyRuleEntry( HttpServletRequest request )
459     {
460         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
461                     DateValidatorsResourceIdService.PERMISSION_RULE_MODIFY, getUser(  ) ) )
462         {
463             return getManageValidator( request );
464         }
465 
466         Integer nIdRule = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_RULE ) );
467 
468         if ( nIdRule == null )
469         {
470             return getManageValidator( request );
471         }
472 
473         Rule rule = RuleHome.findByPrimaryKey( nIdRule, PLUGIN );
474 
475         if ( rule == null )
476         {
477             return getManageValidator( request );
478         }
479 
480         setPageTitleProperty( PROPERTY_MODIFY_RULE_TITLE );
481 
482         ReferenceList refListComparator = ComparatorHome.findAll( PLUGIN );
483         ReferenceItem refItem = new ReferenceItem(  );
484         refItem.setCode( String.valueOf( -1 ) );
485         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
486         refListComparator.add( 0, refItem );
487 
488         Map<String, Object> model = new HashMap<String, Object>(  );
489         model.put( MARK_RULE, rule );
490         model.put( DateValidatorsConstants.MARK_ENTRY_LIST,
491             DateValidatorsService.getAuthorizedEntries( rule.getIdForm(  ) ) );
492         model.put( DateValidatorsConstants.MARK_COMPARATOR_LIST, refListComparator );
493         model.put( MARK_TYPE, DateValidatorsConstants.TYPE_ENTRY );
494 
495         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_RULE_ENTRY, getLocale(  ), model );
496 
497         return getAdminPage( template.getHtml(  ) );
498     }
499 
500     /**
501      * Gets the rule modification page for the type reference date
502      *
503      * @param request The HTTP request
504      * @return The rule modification page for the type reference date
505      */
506     public String getModifyRuleDateReference( HttpServletRequest request )
507     {
508         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
509                     DateValidatorsResourceIdService.PERMISSION_RULE_MODIFY, getUser(  ) ) )
510         {
511             return getManageValidator( request );
512         }
513 
514         Integer nIdRule = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_RULE ) );
515 
516         if ( nIdRule == null )
517         {
518             return getManageValidator( request );
519         }
520 
521         Rule rule = RuleHome.findByPrimaryKey( nIdRule, PLUGIN );
522 
523         if ( rule == null )
524         {
525             return getManageValidator( request );
526         }
527 
528         setPageTitleProperty( PROPERTY_MODIFY_RULE_TITLE );
529 
530         ReferenceList refListComparator = ComparatorHome.findAll( PLUGIN );
531         ReferenceItem refItem = new ReferenceItem(  );
532         refItem.setCode( String.valueOf( -1 ) );
533         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
534         refListComparator.add( 0, refItem );
535 
536         Map<String, Object> model = new HashMap<String, Object>(  );
537         model.put( MARK_RULE, rule );
538         model.put( DateValidatorsConstants.MARK_ENTRY_LIST,
539             DateValidatorsService.getAuthorizedEntries( rule.getIdForm(  ) ) );
540         model.put( DateValidatorsConstants.MARK_COMPARATOR_LIST, refListComparator );
541         model.put( MARK_TYPE, DateValidatorsConstants.TYPE_DATE_REFERENCE );
542         model.put( MARK_LOCALE, request.getLocale(  ) );
543 
544         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_RULE_DATE_REFERENCE, getLocale(  ),
545                 model );
546 
547         return getAdminPage( template.getHtml(  ) );
548     }
549 
550     /**
551      * Gets the rule modification page for the type calculated date
552      *
553      * @param request The HTTP request
554      * @return The rule modification page for the type calculated date
555      */
556     public String getModifyRuleDateCalculated( HttpServletRequest request )
557     {
558         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
559                     DateValidatorsResourceIdService.PERMISSION_RULE_MODIFY, getUser(  ) ) )
560         {
561             return getManageValidator( request );
562         }
563 
564         Integer nIdRule = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_RULE ) );
565 
566         if ( nIdRule == null )
567         {
568             return getManageValidator( request );
569         }
570 
571         Rule rule = RuleHome.findByPrimaryKey( nIdRule, PLUGIN );
572 
573         if ( rule == null )
574         {
575             return getManageValidator( request );
576         }
577 
578         setPageTitleProperty( PROPERTY_MODIFY_RULE_TITLE );
579 
580         ReferenceList refListComparator = ComparatorHome.findAll( PLUGIN );
581         ReferenceItem refItem = new ReferenceItem(  );
582         refItem.setCode( String.valueOf( -1 ) );
583         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
584         refListComparator.add( 0, refItem );
585 
586         Map<String, Object> model = new HashMap<String, Object>(  );
587         model.put( MARK_RULE, rule );
588         model.put( DateValidatorsConstants.MARK_ENTRY_LIST,
589             DateValidatorsService.getAuthorizedEntries( rule.getIdForm(  ) ) );
590         model.put( DateValidatorsConstants.MARK_COMPARATOR_LIST, refListComparator );
591         model.put( MARK_DATE_CALCULATED_LIST, DateCalculatedHome.findAll( PLUGIN ) );
592         model.put( MARK_TYPE, DateValidatorsConstants.TYPE_DATE_CALCULATED );
593 
594         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_RULE_DATE_CALCULATED, getLocale(  ),
595                 model );
596 
597         return getAdminPage( template.getHtml(  ) );
598     }
599 
600     /**
601      * Performs the rule modification
602      *
603      * @param request The HTTP request
604      * @return The URL to go after performing the action
605      */
606     public String doModifyRule( HttpServletRequest request )
607     {
608         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
609                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
610 
611         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
612                     DateValidatorsResourceIdService.PERMISSION_RULE_MODIFY, getUser(  ) ) )
613         {
614             return getJspManageValidator( request, nIdForm );
615         }
616 
617         // Cancel
618         if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_CANCEL ) ) )
619         {
620             return getJspManageValidator( request, nIdForm );
621         }
622 
623         Integer nIdRule = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_RULE ) );
624 
625         if ( nIdRule == null )
626         {
627             return getJspManageValidator( request, nIdForm );
628         }
629 
630         Rule rule = RuleHome.findByPrimaryKey( nIdRule, PLUGIN );
631 
632         if ( rule == null )
633         {
634             return getJspManageValidator( request, nIdForm );
635         }
636 
637         Integer nType = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_TYPE ) );
638 
639         if ( nType == null )
640         {
641             return getJspManageValidator( request, nIdForm );
642         }
643 
644         // Get fields
645         String strError = getRuleFields( request, rule, nType );
646 
647         if ( strError != null )
648         {
649             return strError;
650         }
651 
652         RuleHome.update( rule, PLUGIN );
653 
654         return getJspManageValidator( request, nIdForm );
655     }
656 
657     /**
658      * Gets the rule form fields
659      *
660      * @param request The HTTP request
661      * @param rule The rule
662      * @param nType The rule type
663      * @return Null if no error, else the AdminMessage URL
664      */
665     private String getRuleFields( HttpServletRequest request, Rule rule, int nType )
666     {
667         Integer nIdEntry1 = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_ENTRY1 ) );
668         Integer nIdComparator = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_COMPARATOR ) );
669 
670         String strFieldError = DateValidatorsConstants.EMPTY_STRING;
671 
672         if ( nIdEntry1 == null )
673         {
674             switch ( nType )
675             {
676                 case DateValidatorsConstants.TYPE_ENTRY:
677                     strFieldError = FIELD_RULE_ENTRY_ENTRY;
678 
679                     break;
680 
681                 case DateValidatorsConstants.TYPE_DATE_REFERENCE:
682                     strFieldError = FIELD_RULE_DATE_REFERENCE_ENTRY;
683 
684                     break;
685 
686                 case DateValidatorsConstants.TYPE_DATE_CALCULATED:
687                     strFieldError = FIELD_RULE_DATE_CALCULATED_ENTRY;
688 
689                     break;
690 
691                 default:
692                     return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_TYPE_NOT_EXISTS,
693                         AdminMessage.TYPE_STOP );
694             }
695         }
696         else if ( nIdComparator == null )
697         {
698             strFieldError = FIELD_RULE_COMPARATOR;
699         }
700 
701         if ( StringUtils.isNotBlank( strFieldError ) )
702         {
703             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, getLocale(  ) ) };
704 
705             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
706                 AdminMessage.TYPE_STOP );
707         }
708 
709         rule.setIdEntry1( nIdEntry1 );
710         rule.setIdComparator( nIdComparator );
711 
712         switch ( nType )
713         {
714             case DateValidatorsConstants.TYPE_ENTRY:
715 
716                 Integer nIdEntry2 = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_ENTRY2 ) );
717 
718                 if ( nIdEntry2 == null )
719                 {
720                     Object[] tabRequiredFields = { I18nService.getLocalizedString( FIELD_RULE_ENTRY2, getLocale(  ) ) };
721 
722                     return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
723                         AdminMessage.TYPE_STOP );
724                 }
725 
726                 // Entries may not be identical
727                 if ( nIdEntry1 == nIdEntry2 )
728                 {
729                     return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_IDENTICAL_ENTRIES,
730                         AdminMessage.TYPE_STOP );
731                 }
732 
733                 // Checks if a rule already exists for these entries
734                 for ( Rule currentRule : RuleHome.findRulesByForm( rule.getIdForm(  ), PLUGIN ) )
735                 {
736                     if ( ( currentRule.getIdRule(  ) != rule.getIdRule(  ) ) &&
737                             ( ( ( currentRule.getIdEntry1(  ) == nIdEntry1 ) && ( currentRule.getIdEntry2(  ) != null ) &&
738                             ( currentRule.getIdEntry2(  ) == nIdEntry2 ) ) ||
739                             ( ( currentRule.getIdEntry1(  ) == nIdEntry2 ) && ( currentRule.getIdEntry2(  ) != null ) &&
740                             ( currentRule.getIdEntry2(  ) == nIdEntry1 ) ) ) )
741                     {
742                         return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_RULE_ALREADY_EXISTS_ENTRY,
743                             AdminMessage.TYPE_STOP );
744                     }
745                 }
746 
747                 rule.setIdEntry2( nIdEntry2 );
748 
749                 break;
750 
751             case DateValidatorsConstants.TYPE_DATE_REFERENCE:
752 
753                 Timestamp dateReference = DateUtil.formatTimestamp( request.getParameter( PARAMETER_DATE_REFERENCE ),
754                         request.getLocale(  ) );
755 
756                 if ( dateReference == null )
757                 {
758                     Object[] tabRequiredFields = 
759                         {
760                             I18nService.getLocalizedString( FIELD_RULE_DATE_REFERENCE, getLocale(  ) )
761                         };
762 
763                     return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
764                         AdminMessage.TYPE_STOP );
765                 }
766 
767                 // Checks if a rule already exists for this entry and this reference date
768                 for ( Rule currentRule : RuleHome.findRulesByForm( rule.getIdForm(  ), PLUGIN ) )
769                 {
770                     if ( ( currentRule.getIdRule(  ) != rule.getIdRule(  ) ) &&
771                             ( currentRule.getIdEntry1(  ) == nIdEntry1 ) && ( currentRule.getDateReference(  ) != null ) &&
772                             currentRule.getDateReference(  ).equals( dateReference ) )
773                     {
774                         return AdminMessageService.getMessageUrl( request,
775                             MESSAGE_ERROR_RULE_ALREADY_EXISTS_DATE_REFERENCE, AdminMessage.TYPE_STOP );
776                     }
777                 }
778 
779                 rule.setDateReference( dateReference );
780 
781                 break;
782 
783             case DateValidatorsConstants.TYPE_DATE_CALCULATED:
784 
785                 Integer nIdDateCalculated = DateValidatorsUtils.stringToInt( request.getParameter( 
786                             PARAMETER_ID_DATE_CALCULATED ) );
787 
788                 if ( nIdDateCalculated == null )
789                 {
790                     Object[] tabRequiredFields = 
791                         {
792                             I18nService.getLocalizedString( FIELD_RULE_DATE_CALCULATED, getLocale(  ) )
793                         };
794 
795                     return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
796                         AdminMessage.TYPE_STOP );
797                 }
798 
799                 // Checks if a rule already exists for this entry and this calculated date
800                 for ( Rule currentRule : RuleHome.findRulesByForm( rule.getIdForm(  ), PLUGIN ) )
801                 {
802                     if ( ( currentRule.getIdRule(  ) != rule.getIdRule(  ) ) &&
803                             ( currentRule.getIdEntry1(  ) == nIdEntry1 ) &&
804                             ( currentRule.getDateCalculated(  ) != null ) &&
805                             ( currentRule.getDateCalculated(  ).getIdDateCalculated(  ) == nIdDateCalculated ) )
806                     {
807                         return AdminMessageService.getMessageUrl( request,
808                             MESSAGE_ERROR_RULE_ALREADY_EXISTS_DATE_CALCULATED, AdminMessage.TYPE_STOP );
809                     }
810                 }
811 
812                 DateCalculated dateCalculated = new DateCalculated(  );
813                 dateCalculated.setIdDateCalculated( nIdDateCalculated );
814                 rule.setDateCalculated( dateCalculated );
815 
816                 break;
817 
818             default:
819                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_TYPE_NOT_EXISTS, AdminMessage.TYPE_STOP );
820         }
821 
822         // No error
823         return null;
824     }
825 
826     /**
827      * Gets the confirmation page for removing rule
828      * @param request The HTTP request
829      * @return The confirmation page for removing rule
830      */
831     public String getConfirmRemoveRule( HttpServletRequest request )
832     {
833         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
834                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
835 
836         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
837                     DateValidatorsResourceIdService.PERMISSION_RULE_DELETE, getUser(  ) ) )
838         {
839             return getJspManageValidator( request, nIdForm );
840         }
841 
842         Integer nIdRule = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_RULE ) );
843 
844         if ( nIdRule == null )
845         {
846             return getJspManageValidator( request, nIdForm );
847         }
848 
849         UrlItem url = new UrlItem( JSP_DO_REMOVE_RULE );
850         url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
851         url.addParameter( PARAMETER_ID_RULE, nIdRule );
852 
853         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_RULE, url.getUrl(  ),
854             AdminMessage.TYPE_CONFIRMATION );
855     }
856 
857     /**
858      * Performs the rule removal
859      *
860      * @param request The HTTP request
861      * @return The URL to go after performing the action
862      */
863     public String doRemoveRule( HttpServletRequest request )
864     {
865         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
866                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
867 
868         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
869                     DateValidatorsResourceIdService.PERMISSION_RULE_DELETE, getUser(  ) ) )
870         {
871             return getJspManageValidator( request, nIdForm );
872         }
873 
874         Integer nIdRule = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_RULE ) );
875 
876         if ( nIdRule == null )
877         {
878             return getJspManageValidator( request, nIdForm );
879         }
880 
881         RuleHome.remove( nIdRule, PLUGIN );
882 
883         return getJspManageValidator( request, nIdForm );
884     }
885 
886     /**
887      * Gets the calculated dates management page
888      *
889      * @param request The HTTP request
890      * @return The calculated dates management page
891      */
892     public String getManageDateCalculated( HttpServletRequest request )
893     {
894         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
895                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_MANAGE, getUser(  ) ) )
896         {
897             return getManageValidator( request );
898         }
899 
900         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
901                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
902 
903         if ( nIdForm == null )
904         {
905             return getManageValidator( request );
906         }
907 
908         setPageTitleProperty( PROPERTY_MANAGE_DATE_CALCULATED_TITLE );
909 
910         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( DateValidatorsConstants.PROPERTY_ITEMS_PER_PAGE, 10 );
911         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
912         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
913                 _nDefaultItemsPerPage );
914 
915         List<DateCalculated> listDatesCalculated = DateCalculatedHome.findAll( PLUGIN );
916 
917         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
918         String strAscSort = null;
919 
920         if ( strSortedAttributeName != null )
921         {
922             strAscSort = request.getParameter( Parameters.SORTED_ASC );
923 
924             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
925 
926             Collections.sort( listDatesCalculated, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
927         }
928 
929         UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_DATE_CALCULATED );
930         urlItem.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
931 
932         if ( strSortedAttributeName != null )
933         {
934             urlItem.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
935         }
936 
937         if ( strAscSort != null )
938         {
939             urlItem.addParameter( Parameters.SORTED_ASC, strAscSort );
940         }
941 
942         Paginator<DateCalculated> paginator = new Paginator<DateCalculated>( listDatesCalculated, _nItemsPerPage,
943                 urlItem.getUrl(  ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
944 
945         Map<String, Object> model = new HashMap<String, Object>(  );
946         model.put( DateValidatorsConstants.MARK_PAGINATOR, paginator );
947         model.put( DateValidatorsConstants.MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
948         model.put( MARK_DATE_CALCULATED_LIST, paginator.getPageItems(  ) );
949         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
950         model.put( DateValidatorsConstants.MARK_SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
951         model.put( DateValidatorsConstants.MARK_SORTED_ASC, strAscSort );
952         addPermissions( request, model );
953 
954         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DATE_CALCULATED, getLocale(  ), model );
955 
956         return getAdminPage( template.getHtml(  ) );
957     }
958 
959     /**
960      * Add permissions in the model
961      * @param request {@link HttpServletRequest}
962      * @param model The model
963      */
964     private void addPermissions( HttpServletRequest request, Map<String, Object> model )
965     {
966         // Create
967         boolean bPermissionCreate = false;
968 
969         if ( RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
970                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_CREATE,
971                     AdminUserService.getAdminUser( request ) ) )
972         {
973             bPermissionCreate = true;
974         }
975 
976         model.put( DateValidatorsConstants.MARK_PERMISSION_CREATE, bPermissionCreate );
977 
978         // Modify
979         boolean bPermissionModify = false;
980 
981         if ( RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
982                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_MODIFY,
983                     AdminUserService.getAdminUser( request ) ) )
984         {
985             bPermissionModify = true;
986         }
987 
988         model.put( DateValidatorsConstants.MARK_PERMISSION_MODIFY, bPermissionModify );
989 
990         // Delete
991         boolean bPermissionDelete = false;
992 
993         if ( RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
994                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_DELETE,
995                     AdminUserService.getAdminUser( request ) ) )
996         {
997             bPermissionDelete = true;
998         }
999 
1000         model.put( DateValidatorsConstants.MARK_PERMISSION_DELETE, bPermissionDelete );
1001     }
1002 
1003     /**
1004      * Gets the calculated date creation page
1005      *
1006      * @param request The HTTP request
1007      * @return The calculated date creation page
1008      */
1009     public String getCreateDateCalculated( HttpServletRequest request )
1010     {
1011         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
1012                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_CREATE, getUser(  ) ) )
1013         {
1014             return getManageValidator( request );
1015         }
1016 
1017         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
1018                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
1019 
1020         if ( nIdForm == null )
1021         {
1022             return getManageValidator( request );
1023         }
1024 
1025         setPageTitleProperty( PROPERTY_CREATE_DATE_CALCULATED_TITLE );
1026 
1027         ReferenceList refListOperator = OperatorHome.findAll( PLUGIN );
1028         ReferenceItem refItem = new ReferenceItem(  );
1029         refItem.setCode( String.valueOf( -1 ) );
1030         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
1031         refListOperator.add( 0, refItem );
1032 
1033         Map<String, Object> model = new HashMap<String, Object>(  );
1034         model.put( MARK_OPERATOR_LIST, refListOperator );
1035         model.put( MARK_UNIT_LIST, UnitHome.findAll( PLUGIN ) );
1036         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
1037         model.put( MARK_LOCALE, request.getLocale(  ) );
1038 
1039         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_DATE_CALCULATED, getLocale(  ), model );
1040 
1041         return getAdminPage( template.getHtml(  ) );
1042     }
1043 
1044     /**
1045      * Performs the calculated date creation
1046      *
1047      * @param request The HTTP request
1048      * @return The URL to go after performing the action
1049      */
1050     public String doCreateDateCalculated( HttpServletRequest request )
1051     {
1052         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
1053                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
1054 
1055         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
1056                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_CREATE, getUser(  ) ) )
1057         {
1058             return getJspManageValidator( request, nIdForm );
1059         }
1060 
1061         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_DATE_CALCULATED );
1062         url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
1063 
1064         // Cancel
1065         if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_CANCEL ) ) )
1066         {
1067             return url.getUrl(  );
1068         }
1069 
1070         DateCalculated dateCalculated = new DateCalculated(  );
1071 
1072         // Get fields
1073         String strError = getDateCalculatedFields( request, dateCalculated );
1074 
1075         if ( strError != null )
1076         {
1077             return strError;
1078         }
1079 
1080         DateCalculatedHome.create( dateCalculated, PLUGIN );
1081 
1082         return url.getUrl(  );
1083     }
1084 
1085     /**
1086      * Gets the calculated date modification page
1087      *
1088      * @param request The HTTP request
1089      * @return The calculated date modification page
1090      */
1091     public String getModifyDateCalculated( HttpServletRequest request )
1092     {
1093         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
1094                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_MODIFY, getUser(  ) ) )
1095         {
1096             return getManageValidator( request );
1097         }
1098 
1099         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
1100                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
1101 
1102         if ( nIdForm == null )
1103         {
1104             return getManageValidator( request );
1105         }
1106 
1107         Integer nIdDateCalculated = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_DATE_CALCULATED ) );
1108 
1109         if ( nIdDateCalculated == null )
1110         {
1111             return getManageValidator( request );
1112         }
1113 
1114         DateCalculated dateCalculated = DateCalculatedHome.findByPrimaryKey( nIdDateCalculated, PLUGIN );
1115 
1116         if ( dateCalculated == null )
1117         {
1118             return getManageValidator( request );
1119         }
1120 
1121         setPageTitleProperty( PROPERTY_MODIFY_DATE_CALCULATED_TITLE );
1122 
1123         ReferenceList refListOperator = OperatorHome.findAll( PLUGIN );
1124         ReferenceItem refItem = new ReferenceItem(  );
1125         refItem.setCode( String.valueOf( -1 ) );
1126         refItem.setName( DateValidatorsConstants.EMPTY_STRING );
1127         refListOperator.add( 0, refItem );
1128 
1129         Map<String, Object> model = new HashMap<String, Object>(  );
1130         model.put( MARK_DATE_CALCULATED, dateCalculated );
1131         model.put( MARK_OPERATOR_LIST, refListOperator );
1132         model.put( MARK_UNIT_LIST, UnitHome.findAll( PLUGIN ) );
1133         model.put( DateValidatorsConstants.MARK_ID_FORM, nIdForm );
1134         model.put( MARK_LOCALE, request.getLocale(  ) );
1135 
1136         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_DATE_CALCULATED, getLocale(  ), model );
1137 
1138         return getAdminPage( template.getHtml(  ) );
1139     }
1140 
1141     /**
1142      * Performs the calculated date modification
1143      *
1144      * @param request The HTTP request
1145      * @return The URL to go after performing the action
1146      */
1147     public String doModifyDateCalculated( HttpServletRequest request )
1148     {
1149         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
1150                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
1151 
1152         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
1153                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_MODIFY, getUser(  ) ) )
1154         {
1155             return getJspManageValidator( request, nIdForm );
1156         }
1157 
1158         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_DATE_CALCULATED );
1159         url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
1160 
1161         // Cancel
1162         if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_CANCEL ) ) )
1163         {
1164             return url.getUrl(  );
1165         }
1166 
1167         Integer nIdDateCalculated = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_DATE_CALCULATED ) );
1168 
1169         if ( nIdDateCalculated == null )
1170         {
1171             return getJspManageValidator( request, nIdForm );
1172         }
1173 
1174         DateCalculated dateCalculated = DateCalculatedHome.findByPrimaryKey( nIdDateCalculated, PLUGIN );
1175 
1176         if ( dateCalculated == null )
1177         {
1178             return getJspManageValidator( request, nIdForm );
1179         }
1180 
1181         // Get fields
1182         String strError = getDateCalculatedFields( request, dateCalculated );
1183 
1184         if ( strError != null )
1185         {
1186             return strError;
1187         }
1188 
1189         DateCalculatedHome.update( dateCalculated, PLUGIN );
1190 
1191         return url.getUrl(  );
1192     }
1193 
1194     /**
1195      * Gets the calculated date form fields
1196      *
1197      * @param request The HTTP request
1198      * @param dateCalculated The calculated date
1199      * @return Null if no error, else the AdminMessage URL
1200      */
1201     private String getDateCalculatedFields( HttpServletRequest request, DateCalculated dateCalculated )
1202     {
1203         Timestamp dateReference = DateUtil.formatTimestamp( request.getParameter( PARAMETER_DATE_REFERENCE ),
1204                 request.getLocale(  ) );
1205         Integer nIdOperator = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_OPERATOR ) );
1206         Integer nNumber = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_NUMBER ) );
1207         Integer nIdUnit = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_UNIT ) );
1208 
1209         String strFieldError = DateValidatorsConstants.EMPTY_STRING;
1210 
1211         if ( dateReference == null )
1212         {
1213             strFieldError = FIELD_DATE_CALCULATED_DATE_REFERENCE;
1214         }
1215         else if ( nIdOperator == null )
1216         {
1217             strFieldError = FIELD_DATE_CALCULATED_OPERATOR;
1218         }
1219         else if ( nIdUnit == null )
1220         {
1221             strFieldError = FIELD_DATE_CALCULATED_UNIT;
1222         }
1223 
1224         if ( StringUtils.isNotBlank( strFieldError ) )
1225         {
1226             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, getLocale(  ) ) };
1227 
1228             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
1229                 AdminMessage.TYPE_STOP );
1230         }
1231 
1232         // Number may be positive
1233         if ( ( nNumber == null ) || ( nNumber <= 0 ) )
1234         {
1235             Object[] tabRequiredFields = { I18nService.getLocalizedString( FIELD_DATE_CALCULATED_NUMBER, getLocale(  ) ) };
1236 
1237             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields,
1238                 AdminMessage.TYPE_STOP );
1239         }
1240 
1241         // Checks if this calculated date already exists
1242         for ( DateCalculated currentDC : DateCalculatedHome.findAll( PLUGIN ) )
1243         {
1244             if ( ( currentDC.getIdDateCalculated(  ) != dateCalculated.getIdDateCalculated(  ) ) &&
1245                     currentDC.getDateReference(  ).equals( dateReference ) &&
1246                     ( currentDC.getOperator(  ).getIdOperator(  ) == nIdOperator ) &&
1247                     ( currentDC.getNumber(  ) == nNumber ) && ( currentDC.getUnit(  ).getIdUnit(  ) == nIdUnit ) )
1248             {
1249                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DATE_CALCULATED_ALREADY_EXISTS,
1250                     AdminMessage.TYPE_STOP );
1251             }
1252         }
1253 
1254         dateCalculated.setDateReference( dateReference );
1255 
1256         Operator operator = new Operator(  );
1257         operator.setIdOperator( nIdOperator );
1258         dateCalculated.setOperator( operator );
1259 
1260         dateCalculated.setNumber( nNumber );
1261 
1262         Unit unit = new Unit(  );
1263         unit.setIdUnit( nIdUnit );
1264         dateCalculated.setUnit( unit );
1265 
1266         // No error
1267         return null;
1268     }
1269 
1270     /**
1271      * Gets the confirmation page for removing calculated date
1272      * @param request The HTTP request
1273      * @return The confirmation page for removing calculated date
1274      */
1275     public String getConfirmRemoveDateCalculated( HttpServletRequest request )
1276     {
1277         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
1278                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
1279 
1280         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
1281                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_DELETE, getUser(  ) ) )
1282         {
1283             return getJspManageValidator( request, nIdForm );
1284         }
1285 
1286         Integer nIdDateCalculated = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_DATE_CALCULATED ) );
1287 
1288         if ( nIdDateCalculated == null )
1289         {
1290             return getJspManageValidator( request, nIdForm );
1291         }
1292 
1293         UrlItem url = new UrlItem( JSP_DO_REMOVE_DATE_CALCULATED );
1294         url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
1295         url.addParameter( PARAMETER_ID_DATE_CALCULATED, nIdDateCalculated );
1296 
1297         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_DATE_CALCULATED, url.getUrl(  ),
1298             AdminMessage.TYPE_CONFIRMATION );
1299     }
1300 
1301     /**
1302      * Performs the calculated date removal
1303      *
1304      * @param request The HTTP request
1305      * @return The URL to go after performing the action
1306      */
1307     public String doRemoveDateCalculated( HttpServletRequest request )
1308     {
1309         Integer nIdForm = DateValidatorsUtils.stringToInt( request.getParameter( 
1310                     DateValidatorsConstants.PARAMETER_ID_FORM ) );
1311 
1312         if ( !RBACService.isAuthorized( Rule.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
1313                     DateValidatorsResourceIdService.PERMISSION_DATE_CALCULATED_DELETE, getUser(  ) ) )
1314         {
1315             return getJspManageValidator( request, nIdForm );
1316         }
1317 
1318         Integer nIdDateCalculated = DateValidatorsUtils.stringToInt( request.getParameter( PARAMETER_ID_DATE_CALCULATED ) );
1319 
1320         if ( nIdDateCalculated == null )
1321         {
1322             return getJspManageValidator( request, nIdForm );
1323         }
1324 
1325         DateCalculatedHome.remove( nIdDateCalculated, PLUGIN );
1326 
1327         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_DATE_CALCULATED );
1328         url.addParameter( DateValidatorsConstants.PARAMETER_ID_FORM, nIdForm );
1329 
1330         return url.getUrl(  );
1331     }
1332 }