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  
39  
40  /**
41   * This class is responsible of validating numeric fields.
42   */
43  public class ValidatorFieldNumeric extends ValidatorPattern
44  {
45      private static final String PROPERTY_VALIDATION_MESSAGE = "formengine.validator.message.numeric";
46      private static final String PROPERTY_VALIDATION_MESSAGE_MIN = "formengine.validator.message.numeric.min";
47      private static final String PROPERTY_VALIDATION_MESSAGE_MAX = "formengine.validator.message.numeric.max";
48      private static final String NUMERIC_PATTERN = "^\\d*$";
49  
50      /**
51      * The validate implementation.
52      * Check that the value of a field is only made of digits.
53      * @param field The field to validate
54      * @param errors A FormErrorsList to add errors into
55      * @return True if valid, false otherwise
56       */
57      public boolean validate( Field field, FormErrorsList errors )
58      {
59          String strFieldValue = field.getValue(  );
60  
61          if ( ( strFieldValue == null ) || ( strFieldValue.trim(  ).equals( "" ) ) )
62          {
63              return true;
64          }
65  
66          String[] messageParams = new String[3];
67          messageParams[0] = field.getName(  );
68          messageParams[1] = field.getLabel(  );
69  
70          boolean bMatches = checkValueOnPattern( NUMERIC_PATTERN, strFieldValue );
71  
72          if ( bMatches )
73          {
74              int nFieldValue;
75  
76              try
77              {
78                  nFieldValue = Integer.parseInt( strFieldValue );
79              }
80              catch ( NumberFormatException nfe )
81              {
82                  if ( this.getErrorMessage(  ) != null )
83                  {
84                      errors.addErrorMessage( this.getErrorMessage(  ) );
85                  }
86                  else
87                  {
88                      errors.addError( PROPERTY_VALIDATION_MESSAGE, messageParams );
89                  }
90  
91                  return false;
92              }
93  
94              try
95              {
96                  int nMinValue = Integer.parseInt( this.getMin(  ) );
97  
98                  if ( nFieldValue < nMinValue )
99                  {
100                     messageParams[2] = this.getMin(  );
101 
102                     if ( this.getErrorMessage(  ) != null )
103                     {
104                         errors.addErrorMessage( this.getErrorMessage(  ) );
105                     }
106                     else
107                     {
108                         errors.addError( PROPERTY_VALIDATION_MESSAGE_MIN, messageParams );
109                     }
110 
111                     return false;
112                 }
113             }
114             catch ( NumberFormatException e )
115             {
116                 // do nothing if min not of correct format
117             }
118 
119             try
120             {
121                 int nMaxValue = Integer.parseInt( this.getMax(  ) );
122 
123                 if ( nFieldValue > nMaxValue )
124                 {
125                     messageParams[2] = this.getMax(  );
126 
127                     if ( this.getErrorMessage(  ) != null )
128                     {
129                         errors.addErrorMessage( this.getErrorMessage(  ) );
130                     }
131                     else
132                     {
133                         errors.addError( PROPERTY_VALIDATION_MESSAGE_MAX, messageParams );
134                     }
135 
136                     return false;
137                 }
138             }
139             catch ( NumberFormatException e )
140             {
141                 // do nothing if min not of correct format
142             }
143 
144             return true;
145         }
146         else
147         {
148             if ( this.getErrorMessage(  ) != null )
149             {
150                 errors.addErrorMessage( this.getErrorMessage(  ) );
151             }
152             else
153             {
154                 errors.addError( PROPERTY_VALIDATION_MESSAGE, messageParams );
155             }
156 
157             return false;
158         }
159     }
160 }