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.portal.service.user.attribute;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.apache.commons.collections.CollectionUtils;
45 import org.apache.commons.fileupload.FileItem;
46 import org.apache.commons.lang3.StringUtils;
47
48 import fr.paris.lutece.portal.business.user.AdminUser;
49 import fr.paris.lutece.portal.business.user.attribute.AdminUserField;
50 import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldFilter;
51 import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldHome;
52 import fr.paris.lutece.portal.business.user.attribute.IAttribute;
53 import fr.paris.lutece.portal.service.message.AdminMessage;
54 import fr.paris.lutece.portal.service.message.AdminMessageService;
55 import fr.paris.lutece.portal.service.spring.SpringContextService;
56 import fr.paris.lutece.portal.web.constants.Messages;
57 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
58
59
60
61
62
63
64 public final class AdminUserFieldService
65 {
66
67 private static final String CONSTANT_EMPTY_STRING = "";
68 private static final String CONSTANT_UNDERSCORE = "_";
69
70
71 private static final String PARAMETER_ATTRIBUTE = "attribute";
72 private static final String PARAMETER_UPDATE_ATTRIBUTE = "update_attribute";
73 private static final AttributeService _attributeService = AttributeService.getInstance( );
74
75
76
77
78 private AdminUserFieldService( )
79 {
80 }
81
82
83
84
85
86
87
88
89
90
91 public static String checkUserFields( HttpServletRequest request, Locale locale )
92 {
93
94 List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( locale );
95
96 for ( IAttribute attribute : listAttributes )
97 {
98 if ( attribute.isAttributeImage( ) )
99 {
100 MultipartHttpServletRequestutece/portal/web/upload/MultipartHttpServletRequest.html#MultipartHttpServletRequest">MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
101 FileItem fileItem = multipartRequest.getFile( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
102 String strUpdateAttribute = request.getParameter( PARAMETER_UPDATE_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
103
104 if ( attribute.isMandatory( ) && ( strUpdateAttribute != null ) && ( ( fileItem == null ) || ( fileItem.getSize( ) == 0 ) ) )
105 {
106 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
107 }
108 }
109 else
110 {
111 String value = request.getParameter( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
112
113 if ( attribute.isMandatory( ) && ( ( value == null ) || value.equals( CONSTANT_EMPTY_STRING ) ) )
114 {
115 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
116 }
117 }
118 }
119
120 return null;
121 }
122
123
124
125
126
127
128
129
130
131
132
133 public static void doCreateUserFields( AdminUser user, HttpServletRequest request, Locale locale )
134 {
135
136 List<IAttribute> listAttributes = _attributeService.getCoreAttributesWithoutFields( locale );
137
138 for ( IAttribute attribute : listAttributes )
139 {
140 List<AdminUserField> listUserFields = attribute.getUserFieldsData( request, user );
141
142 for ( AdminUserField userField : listUserFields )
143 {
144 if ( userField != null )
145 {
146 AdminUserFieldHome.create( userField );
147 }
148 }
149 }
150
151
152 for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
153 {
154 adminUserFieldListenerService.doCreateUserFields( user, request, locale );
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public static void doModifyUserFields( AdminUserl/business/user/AdminUser.html#AdminUser">AdminUser user, HttpServletRequest request, Locale locale, AdminUser currentUser )
171 {
172
173 List<IAttribute> listAttributes = _attributeService.getCoreAttributesWithoutFields( locale );
174 Map<Integer, List<AdminUserField>> map = new HashMap<>( );
175
176 for ( IAttribute attribute : listAttributes )
177 {
178 List<AdminUserField> listUserFields = attribute.getUserFieldsData( request, user );
179
180 map.put( attribute.getIdAttribute( ), listUserFields );
181 }
182
183
184 AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
185 auFieldFilter.setIdUser( user.getUserId( ) );
186 AdminUserFieldHome.removeByFilter( auFieldFilter );
187
188 for ( Entry<Integer, List<AdminUserField>> entry : map.entrySet( ) )
189 {
190 for ( AdminUserField userField : entry.getValue( ) )
191 {
192 if ( userField != null )
193 {
194 AdminUserFieldHome.create( userField );
195 }
196 }
197 }
198
199
200 for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
201 {
202 adminUserFieldListenerService.doModifyUserFields( user, request, locale, currentUser );
203 }
204 }
205
206
207
208
209
210
211
212
213
214
215
216 public static void doRemoveUserFields( AdminUser user, HttpServletRequest request, Locale locale )
217 {
218 AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
219 auFieldFilter.setIdUser( user.getUserId( ) );
220 AdminUserFieldHome.removeByFilter( auFieldFilter );
221
222
223 for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
224 {
225 adminUserFieldListenerService.doRemoveUserFields( user, request, locale );
226 }
227 }
228
229
230
231
232
233
234
235 public static void doRemoveUserFieldsByIdAttribute( int nIdAttribute )
236 {
237 AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
238 auFieldFilter.setIdAttribute( nIdAttribute );
239 AdminUserFieldHome.removeByFilter( auFieldFilter );
240 }
241
242
243
244
245
246
247
248 public static void doRemoveUserFieldsByIdField( int nIdAttributeField )
249 {
250 AdminUserFieldHome.removeUserFieldsFromIdField( nIdAttributeField );
251 }
252
253
254
255
256
257
258
259
260
261
262 public static Map<String, Object> getAdminUserFields( int nUserId, Locale locale )
263 {
264 List<IAttribute> listAttributes = _attributeService.getAllAttributesWithFields( locale );
265
266 return getAdminUserFields( listAttributes, nUserId, locale );
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280 public static Map<String, Object> getAdminUserFields( List<IAttribute> listAttributes, int nUserId, Locale locale )
281 {
282 Map<String, Object> map = new HashMap<>( );
283
284 for ( IAttribute attribute : listAttributes )
285 {
286 List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( nUserId, attribute.getIdAttribute( ) );
287
288 if ( attribute.isAttributeImage( ) )
289 {
290 if ( CollectionUtils.isNotEmpty( listUserFields ) )
291 {
292 AdminUserField userField = listUserFields.get( 0 );
293
294 if ( userField.getFile( ) != null )
295 {
296 map.put( String.valueOf( attribute.getIdAttribute( ) ), userField.getFile( ) );
297 }
298 }
299 }
300 else
301 {
302 if ( CollectionUtils.isEmpty( listUserFields ) )
303 {
304 AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
305 userField.setValue( StringUtils.EMPTY );
306 listUserFields.add( userField );
307 }
308
309 map.put( String.valueOf( attribute.getIdAttribute( ) ), listUserFields );
310 }
311 }
312
313 return map;
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327 public Object getAdminUserField( IAttribute attribute, int nUserId, Locale locale )
328 {
329 List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( nUserId, attribute.getIdAttribute( ) );
330
331 if ( attribute.isAttributeImage( ) )
332 {
333 if ( CollectionUtils.isNotEmpty( listUserFields ) )
334 {
335 AdminUserField userField = listUserFields.get( 0 );
336
337 if ( userField.getFile( ) != null )
338 {
339 return userField.getFile( );
340 }
341 }
342 }
343 else
344 {
345 if ( CollectionUtils.isEmpty( listUserFields ) )
346 {
347 AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
348 userField.setValue( StringUtils.EMPTY );
349 listUserFields.add( userField );
350 }
351
352 return listUserFields;
353 }
354
355 return null;
356 }
357 }