1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
46
47 public final class ValidationErrorUtil
48 {
49
50
51
52 private ValidationErrorUtil( )
53 {
54
55 }
56
57
58
59
60
61
62
63
64
65
66 public static String getValue1( ConstraintViolation constraintViolation, ValidationErrorConfig config )
67 {
68 return getValue( constraintViolation, config.getValue1Attributes( ) );
69 }
70
71
72
73
74
75
76
77
78
79
80 public static String getValue2( ConstraintViolation constraintViolation, ValidationErrorConfig config )
81 {
82 return getValue( constraintViolation, config.getValue2Attributes( ) );
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 public static String getFieldname( ConstraintViolation constraintViolation, ValidationErrorConfig config, Locale locale )
97 {
98 String strField = constraintViolation.getPropertyPath( ).toString( );
99
100
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
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
116 if ( strFieldName.equals( "" ) )
117 {
118
119 strFieldName = "[" + strKey + "]";
120 }
121
122 strFieldName = config.getFieldWrapperBegin( ) + strFieldName + config.getFieldWrapperEnd( );
123
124 return strFieldName;
125 }
126
127
128
129
130
131
132
133
134
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
150
151
152
153
154
155
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
176
177
178
179
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
198
199
200
201
202
203
204
205
206
207 public static String getFieldId( ConstraintViolation constraintViolation, ValidationErrorConfig config, Locale locale )
208 {
209 String strField = constraintViolation.getPropertyPath( ).toString( );
210
211
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
220 strField = strField.substring( 0, 1 ).toLowerCase( ) + strField.substring( 1 );
221
222
223 return strField;
224 }
225 }