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