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 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
47
48 public final class ValidationErrorUtil
49 {
50
51
52
53 private ValidationErrorUtil( )
54 {
55
56 }
57
58
59
60
61
62
63
64 public static String getValue1( ConstraintViolation constraintViolation, ValidationErrorConfig config )
65 {
66 return getValue( constraintViolation, config, config.getValue1Attributes( ) );
67 }
68
69
70
71
72
73
74
75 public static String getValue2( ConstraintViolation constraintViolation, ValidationErrorConfig config )
76 {
77 return getValue( constraintViolation, config, config.getValue2Attributes( ) );
78 }
79
80
81
82
83
84
85
86
87 public static String getFieldname( ConstraintViolation constraintViolation, ValidationErrorConfig config,
88 Locale locale )
89 {
90 String strField = constraintViolation.getPropertyPath( ).toString( );
91
92
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
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
108 if ( strFieldName.equals( "" ) )
109 {
110
111 strFieldName = "[" + strKey + "]";
112 }
113
114 strFieldName = config.getFieldWrapperBegin( ) + strFieldName + config.getFieldWrapperEnd( );
115
116 return strFieldName;
117 }
118
119
120
121
122
123
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
139
140
141
142
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
164
165
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 }