View Javadoc

1   /*
2    * Copyright (c) 2002-2014, 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.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   * This class is responsible of validating dates.
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       * Check the format according to the pattern given in parameters.
60       * The patterns allowed are the numeric ones, ie built on the following
61       * symbols.
62       * @param field The filed
63       * @param errors An error list
64       * @return True if validated, false otherwise
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              // we don't try to validate an empty date
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                         //dateMin = new Date(  );
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                     // if dateMin not parsed, just ignore it
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                         // dateMax = new Date(  );
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                     // if dateMax not parsed, just ignore it
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 }