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.util.beanvalidation;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Set;
40  
41  import javax.validation.ConstraintViolation;
42  import javax.validation.Validation;
43  import javax.validation.Validator;
44  import javax.validation.ValidatorFactory;
45  
46  
47  /**
48   * BeanValidationUtils validates beans using JSR303 annotations.
49   *
50   * @see #validate(Object)
51   */
52  public final class BeanValidationUtil
53  {
54      /**
55       * Validator (JSR 303) is thread safe.
56       */
57      private static final Validator VALIDATOR;
58  
59      static
60      {
61          // initialize the validator
62          ValidatorFactory factory = Validation.buildDefaultValidatorFactory(  );
63          VALIDATOR = factory.getValidator(  );
64      }
65  
66      /**
67       * Utility class
68       */
69      private BeanValidationUtil(  )
70      {
71          // nothing
72      }
73  
74      /**
75       * Validates a bean.
76       *
77       * @param <T> the bean type
78       * @param bean the bean to validate
79       * @return the sets the
80       */
81      public static <T> Set<ConstraintViolation<T>> validate( T bean )
82      {
83          return VALIDATOR.validate( bean );
84      }
85  
86      /**
87       * Use this in case you need more than a global validation
88       *
89       * @return the validator
90       */
91      public static Validator getValidator(  )
92      {
93          return VALIDATOR;
94      }
95  
96      /**
97       * Validate a bean
98       * @param <T> The bean class
99       * @param bean The bean
100      * @param locale The locale
101      * @param strFieldKeysPrefix The fields keys prefix in the resource file
102      * @return A list of errors
103      */
104     public static <T> List<ValidationError> validate( T bean, Locale locale, String strFieldKeysPrefix )
105     {
106         DefaultValidationErrorConfig config = new DefaultValidationErrorConfig(  );
107         config.setFieldKeysPrefix( strFieldKeysPrefix );
108 
109         return validate( bean, locale, config );
110     }
111 
112     /**
113      * Validate a bean
114      * @param <T> The bean class
115      * @param bean The bean
116      * @param locale The locale
117      * @param config  The config for validation errors rendering
118      * @return A list of errors
119      */
120     public static <T> List<ValidationError> validate( T bean, Locale locale, ValidationErrorConfig config )
121     {
122         List<ValidationError> list = new ArrayList<ValidationError>(  );
123         Set<ConstraintViolation<T>> setViolation = validate( bean );
124 
125         for ( ConstraintViolation<T> cv : setViolation )
126         {
127             list.add( new ValidationError( cv, locale, config ) );
128         }
129 
130         return list;
131     }
132 }