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.plugins.identitystore.service.attribute;
35
36 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeChangeStatus;
37 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeStatus;
38 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.IdentityDto;
39 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.crud.IdentityChangeRequest;
40 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.merge.IdentityMergeRequest;
41 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
42 import org.apache.commons.lang3.StringUtils;
43
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collections;
47 import java.util.List;
48 import java.util.stream.Collectors;
49
50
51
52
53 public class IdentityAttributeFormatterService
54 {
55
56 private static IdentityAttributeFormatterService _instance;
57 private static final List<String> PHONE_ATTR_KEYS = Arrays.asList( "mobile_phone", "fixed_phone" );
58 private static final List<String> DATE_ATTR_KEYS = Collections.singletonList( "birthdate" );
59 private static final List<String> FIRSTNAME_ATTR_KEYS = Collections.singletonList( "first_name" );
60 private static final List<String> UPPERCASE_ATTR_KEYS = Arrays.asList( "birthcountry", "family_name", "preferred_username" );
61 private static final List<String> LOWERCASE_ATTR_KEYS = Arrays.asList( "login", "email" );
62
63 public static IdentityAttributeFormatterService instance( )
64 {
65 if ( _instance == null )
66 {
67 _instance = new IdentityAttributeFormatterService( );
68 }
69 return _instance;
70 }
71
72
73
74
75
76
77
78
79 public List<AttributeStatus> formatIdentityChangeRequestAttributeValues( final IdentityChangeRequest request )
80 {
81 final IdentityDto identity = request.getIdentity( );
82 final List<AttributeStatus> statuses = this.formatIdentityAttributeValues( identity );
83 request.setIdentity( identity );
84 return statuses;
85 }
86
87
88
89
90
91
92
93
94 public List<AttributeStatus> formatIdentityMergeRequestAttributeValues( final IdentityMergeRequest request )
95 {
96 final List<AttributeStatus> statuses = new ArrayList<>( );
97 final IdentityDto identity = request.getIdentity( );
98 if ( identity != null )
99 {
100 statuses.addAll( this.formatIdentityAttributeValues( identity ) );
101 request.setIdentity( identity );
102 }
103 return statuses;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 private List<AttributeStatus> formatIdentityAttributeValues( final IdentityDto identity )
146 {
147 final List<AttributeStatus> statuses = new ArrayList<>( );
148 identity.getAttributes( ).stream( ).filter( attributeDto -> StringUtils.isNotBlank( attributeDto.getValue( ) ) ).forEach( attribute -> {
149
150
151 String formattedValue = attribute.getValue( ).trim( ).replaceAll( "\\s+", " " );
152
153 if ( PHONE_ATTR_KEYS.contains( attribute.getKey( ) ) )
154 {
155 formattedValue = formatPhoneValue( formattedValue );
156 }
157 if ( DATE_ATTR_KEYS.contains( attribute.getKey( ) ) )
158 {
159 formattedValue = formatDateValue( formattedValue );
160 }
161 if ( FIRSTNAME_ATTR_KEYS.contains( attribute.getKey( ) ) )
162 {
163 formattedValue = formatFirstnameValue( formattedValue );
164 }
165 if ( UPPERCASE_ATTR_KEYS.contains( attribute.getKey( ) ) )
166 {
167 formattedValue = StringUtils.upperCase( formattedValue );
168 }
169 if ( LOWERCASE_ATTR_KEYS.contains( attribute.getKey( ) ) )
170 {
171 formattedValue = StringUtils.lowerCase( formattedValue );
172 }
173
174
175 if ( !formattedValue.equals( attribute.getValue( ) ) )
176 {
177 statuses.add( buildAttributeValueFormattedStatus( attribute.getKey( ), attribute.getValue( ), formattedValue ) );
178 }
179 attribute.setValue( formattedValue );
180 } );
181 return statuses;
182 }
183
184
185
186
187
188
189
190
191
192
193
194 private String formatPhoneValue( final String value )
195 {
196
197 String formattedValue = value.replaceAll( "\\s", "" ).replace( ".", "" ).replace( "-", "" ).replace( "(", "" ).replace( ")", "" );
198
199 formattedValue = formattedValue.replaceAll( "^(0{2}|\\+)3{2}", "0" );
200
201 return formattedValue;
202 }
203
204
205
206
207
208
209
210
211 public String formatDateValue( final String value )
212 {
213 final StringBuilder sb = new StringBuilder( );
214 final String [ ] splittedDate = value.split( "/" );
215 if ( splittedDate.length == 3 )
216 {
217 final String day = splittedDate [0];
218 if ( day.length( ) == 1 )
219 {
220 sb.append( "0" );
221 }
222 sb.append( day ).append( "/" );
223
224 final String month = splittedDate [1];
225 if ( month.length( ) == 1 )
226 {
227 sb.append( "0" );
228 }
229 sb.append( month ).append( "/" ).append( splittedDate [2] );
230
231 return sb.toString( );
232 }
233 else
234 {
235 return value;
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248
249 private String formatFirstnameValue( final String value )
250 {
251 if ( StringUtils.isBlank( value ) )
252 {
253 return value;
254 }
255 return Arrays.stream( value.replace( ",", " " ).trim( ).split( " " ) ).filter( StringUtils::isNotBlank ).map( String::trim )
256 .map( firstname -> firstname.substring( 0, 1 ).toUpperCase( ) + firstname.substring( 1 ).toLowerCase( ) ).collect( Collectors.joining( " " ) );
257 }
258
259
260
261
262
263
264
265
266 public AttributeStatus buildAttributeValueFormattedStatus( final String attrStrKey, final String oldValue, final String newValue )
267 {
268 final AttributeStatus status = new AttributeStatus( );
269 status.setKey( attrStrKey );
270 status.setStatus( AttributeChangeStatus.FORMATTED_VALUE );
271 status.setMessage( "[" + oldValue + "] -> [" + newValue + "]" );
272 status.setMessageKey( Constants.PROPERTY_ATTRIBUTE_STATUS_FORMATTED_VALUE );
273 return status;
274 }
275
276 }