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 fr.paris.lutece.portal.service.i18n.I18nService;
37
38 import java.util.Locale;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42 import javax.validation.ConstraintViolation;
43
44
45 /**
46 * ValidationError Utils
47 */
48 public final class ValidationErrorUtil
49 {
50 /**
51 * Private constructor
52 */
53 private ValidationErrorUtil( )
54 {
55 // Nothing to do
56 }
57
58 /**
59 * Return the attribute's value to set as the value#1 of the message
60 * @param constraintViolation The Constraint violation
61 * @param config The config
62 * @return The value
63 */
64 public static String getValue1( ConstraintViolation constraintViolation, ValidationErrorConfig config )
65 {
66 return getValue( constraintViolation, config, config.getValue1Attributes( ) );
67 }
68
69 /**
70 * Return the attribute's value to set as the value#2 of the message
71 * @param constraintViolation The Constraint violation
72 * @param config The config
73 * @return The value
74 */
75 public static String getValue2( ConstraintViolation constraintViolation, ValidationErrorConfig config )
76 {
77 return getValue( constraintViolation, config, config.getValue2Attributes( ) );
78 }
79
80 /**
81 * Return the field name as it will appear in the message
82 * @param constraintViolation The Constraint violation
83 * @param config The config
84 * @param locale The locale
85 * @return The field name
86 */
87 public static String getFieldname( ConstraintViolation constraintViolation, ValidationErrorConfig config,
88 Locale locale )
89 {
90 String strField = constraintViolation.getPropertyPath( ).toString( );
91
92 // remove the variable prefix
93 String[] prefix = config.getVariablesPrefix( );
94
95 for ( int i = 0; i < prefix.length; i++ )
96 {
97 strField = removePrefix( strField, prefix[i] );
98 }
99
100 // set first letter in lower case
101 strField = strField.substring( 0, 1 ).toLowerCase( ) + strField.substring( 1 );
102
103 String strKey = config.getFieldKeysPrefix( ) + strField;
104
105 String strFieldName = I18nService.getLocalizedString( strKey, locale );
106
107 // if the key isn't found
108 if ( strFieldName.equals( "" ) )
109 {
110 // display the missing key as the field name
111 strFieldName = "[" + strKey + "]";
112 }
113
114 strFieldName = config.getFieldWrapperBegin( ) + strFieldName + config.getFieldWrapperEnd( );
115
116 return strFieldName;
117 }
118
119 /**
120 * Remove the variable prefix
121 * @param strSource The source
122 * @param strPrefix The prefix
123 * @return The string with the prefix removed
124 */
125 private static String removePrefix( String strSource, String strPrefix )
126 {
127 String strReturn = strSource;
128
129 if ( strSource.startsWith( strPrefix ) )
130 {
131 strReturn = strSource.substring( strPrefix.length( ) );
132 }
133
134 return strReturn;
135 }
136
137 /**
138 * Return the attribute's value to set as value the of the message
139 * @param constraintViolation The Constraint violation
140 * @param config The config
141 * @param strAttributes The attributes names list
142 * @return The value
143 */
144 private static String getValue( ConstraintViolation constraintViolation, ValidationErrorConfig config,
145 String strAttributes )
146 {
147 String strValue = "";
148
149 Map<String, Object> mapAttributes = constraintViolation.getConstraintDescriptor( ).getAttributes( );
150
151 for ( Entry<String, Object> entry : mapAttributes.entrySet( ) )
152 {
153 if ( strAttributes.contains( entry.getKey( ) ) )
154 {
155 strValue = getValue( entry.getValue( ) );
156 }
157 }
158
159 return strValue;
160 }
161
162 /**
163 * Convert an unkown type value to a String value
164 * @param value The valus
165 * @return The value as a String
166 */
167 private static String getValue( Object value )
168 {
169 if ( value instanceof Integer )
170 {
171 return Integer.toString( (Integer) value );
172 }
173
174 if ( value instanceof Long )
175 {
176 return Long.toString( (Long) value );
177 }
178
179 return (String) value;
180 }
181 }