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.web.workgroup;
35
36 import fr.paris.lutece.portal.business.right.Level;
37 import fr.paris.lutece.portal.business.right.LevelHome;
38 import fr.paris.lutece.portal.business.user.AdminUser;
39 import fr.paris.lutece.portal.business.user.AdminUserHome;
40 import fr.paris.lutece.portal.business.workgroup.AdminWorkgroup;
41 import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupFilter;
42 import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
43 import fr.paris.lutece.portal.service.admin.AdminUserService;
44 import fr.paris.lutece.portal.service.message.AdminMessage;
45 import fr.paris.lutece.portal.service.message.AdminMessageService;
46 import fr.paris.lutece.portal.service.template.AppTemplateService;
47 import fr.paris.lutece.portal.service.util.AppPathService;
48 import fr.paris.lutece.portal.service.util.AppPropertiesService;
49 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
50 import fr.paris.lutece.portal.service.workgroup.WorkgroupRemovalListenerService;
51 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
52 import fr.paris.lutece.portal.web.constants.Messages;
53 import fr.paris.lutece.portal.web.constants.Parameters;
54 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
55 import fr.paris.lutece.util.ReferenceItem;
56 import fr.paris.lutece.util.ReferenceList;
57 import fr.paris.lutece.util.html.HtmlTemplate;
58 import fr.paris.lutece.util.html.ItemNavigator;
59 import fr.paris.lutece.util.html.Paginator;
60 import fr.paris.lutece.util.sort.AttributeComparator;
61 import fr.paris.lutece.util.string.StringUtil;
62 import fr.paris.lutece.util.url.UrlItem;
63
64 import org.apache.commons.lang.StringUtils;
65
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.List;
71 import java.util.Map;
72
73 import javax.servlet.http.HttpServletRequest;
74
75
76
77
78
79 public class AdminWorkgroupJspBean extends AdminFeaturesPageJspBean
80 {
81
82
83
84
85 public static final String RIGHT_MANAGE_WORKGROUPS = "CORE_WORKGROUPS_MANAGEMENT";
86
87
88
89
90 private static final long serialVersionUID = 5945178935890410656L;
91
92
93 private static final String TEMPLATE_MANAGE_WORGROUPS = "admin/workgroup/manage_workgroups.html";
94 private static final String TEMPLATE_CREATE_WORKGROUP = "admin/workgroup/create_workgroup.html";
95 private static final String TEMPLATE_MODIFY_WORKGROUP = "admin/workgroup/modify_workgroup.html";
96 private static final String TEMPLATE_ASSIGN_USERS = "admin/workgroup/assign_users_workgroup.html";
97
98
99 private static final String MARK_WORKGROUPS_LIST = "workgroups_list";
100 private static final String MARK_WORKGROUP = "workgroup";
101 private static final String MARK_USERS_LIST = "users_list";
102 private static final String MARK_ASSIGNED_USERS_LIST = "assigned_users_list";
103 private static final String MARK_ASSIGNED_USERS_NUMBER = "assigned_users_number";
104 private static final String MARK_SEARCH_IS_SEARCH = "search_is_search";
105 private static final String MARK_SEARCH_ADMIN_WORKGROUP_FILTER = "search_admin_workgroup_filter";
106 private static final String MARK_USER_LEVELS_LIST = "user_levels";
107 private static final String MARK_ITEM_NAVIGATOR = "item_navigator";
108 private static final String MARK_PAGINATOR = "paginator";
109 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
110 private static final String MARK_SORT_SEARCH_ATTRIBUTE = "sort_search_attribute";
111
112
113 private static final String PROPERTY_CREATE_WORKGROUP_PAGETITLE = "portal.workgroup.create_workgroup.pageTitle";
114 private static final String PROPERTY_MODIFY_WORKGROUP_PAGETITLE = "portal.workgroup.modify_workgroup.pageTitle";
115 private static final String PROPERTY_ASSIGN_USERS_PAGETITLE = "portal.workgroup.assign_users.pageTitle";
116 private static final String PROPERTY_MANAGE_WORKGROUPS_PAGETITLE = "portal.workgroup.manage_workgroups.pageTitle";
117 private static final String PROPERTY_USERS_PER_PAGE = "paginator.user.itemsPerPage";
118
119
120 private static final String PARAMETER_WORKGROUP_KEY = "workgroup_key";
121 private static final String PARAMETER_WORKGROUP_DESCRIPTION = "workgroup_description";
122 private static final String PARAMETER_USERS_LIST = "list_users";
123 private static final String PARAMETER_CANCEL = "cancel";
124 private static final String PARAMETER_ID_USER = "id_user";
125 private static final String PARAMETER_ANCHOR = "anchor";
126
127
128 private static final String JSP_MANAGE_WORKGROUPS = "ManageWorkgroups.jsp";
129 private static final String JSP_ASSIGN_USERS_TO_WORKGROUPS = "AssignUsersWorkgroup.jsp";
130 private static final String JSP_URL_REMOVE_WORKGROUP = "jsp/admin/workgroup/DoRemoveWorkgroup.jsp";
131 private static final String JSP_URL_ASSIGN_USERS_TO_WORKGROUPS = "jsp/admin/workgroup/AssignUsersWorkgroup.jsp";
132
133
134 private static final String MESSAGE_WORKGROUP_ALREADY_EXIST = "portal.workgroup.message.workgroupAlreadyExist";
135 private static final String MESSAGE_CONFIRM_REMOVE = "portal.workgroup.message.confirmRemove";
136 private static final String MESSAGE_WORKGROUP_ALREADY_USED = "portal.workgroup.message.workgroupAlreadyUsed";
137 private static final String MESSAGE_CANNOT_REMOVE_WORKGROUP = "portal.workgroup.message.cannotRemoveWorkgroup";
138 private static final String MESSAGE_WORKGROUP_ACCENTUATED_CHARACTER = "portal.workgroup.message.accentuatedCharacter";
139 private int _nItemsPerPage;
140 private int _nDefaultItemsPerPage;
141 private String _strCurrentPageIndex;
142 private ItemNavigator _itemNavigator;
143
144
145
146
147
148
149
150 public String getManageWorkgroups( HttpServletRequest request )
151 {
152 setPageTitleProperty( PROPERTY_MANAGE_WORKGROUPS_PAGETITLE );
153
154
155 reinitItemNavigator( );
156
157
158 AdminWorkgroupFilter awFilter = new AdminWorkgroupFilter( );
159 boolean bIsSearch = awFilter.setAdminWorkgroupFilter( request );
160
161 List<AdminWorkgroup> listFilteredWorkgroups = (List<AdminWorkgroup>) AdminWorkgroupHome.findByFilter( awFilter );
162
163 HashMap<String, Object> model = new HashMap<String, Object>( );
164
165 if ( !getUser( ).isAdmin( ) )
166 {
167 listFilteredWorkgroups = (List<AdminWorkgroup>) AdminWorkgroupService.getAuthorizedCollection( listFilteredWorkgroups,
168 getUser( ) );
169 }
170
171
172 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
173 String strAscSort = null;
174
175 if ( strSortedAttributeName != null )
176 {
177 strAscSort = request.getParameter( Parameters.SORTED_ASC );
178
179 boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
180
181 Collections.sort( listFilteredWorkgroups, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
182 }
183
184 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
185 _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_USERS_PER_PAGE, 50 );
186 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
187 _nDefaultItemsPerPage );
188
189 String strURL = getHomeUrl( request );
190 UrlItem url = new UrlItem( strURL );
191
192 if ( strSortedAttributeName != null )
193 {
194 url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
195 }
196
197 if ( strAscSort != null )
198 {
199 url.addParameter( Parameters.SORTED_ASC, strAscSort );
200 }
201
202 String strSortSearchAttribute = "";
203
204 if ( bIsSearch )
205 {
206 awFilter.setUrlAttributes( url );
207
208 if ( !awFilter.getUrlAttributes( ).equals( "" ) )
209 {
210 strSortSearchAttribute = "&" + awFilter.getUrlAttributes( );
211 }
212 }
213
214
215 LocalizedPaginator<AdminWorkgroup> paginator = new LocalizedPaginator<AdminWorkgroup>( listFilteredWorkgroups,
216 _nItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
217
218 model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
219 model.put( MARK_PAGINATOR, paginator );
220 model.put( MARK_WORKGROUPS_LIST, paginator.getPageItems( ) );
221 model.put( MARK_SEARCH_IS_SEARCH, bIsSearch );
222 model.put( MARK_SEARCH_ADMIN_WORKGROUP_FILTER, awFilter );
223 model.put( MARK_SORT_SEARCH_ATTRIBUTE, strSortSearchAttribute );
224
225 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_WORGROUPS, getLocale( ), model );
226
227 return getAdminPage( template.getHtml( ) );
228 }
229
230
231
232
233
234
235
236 public String getCreateWorkgroup( HttpServletRequest request )
237 {
238 setPageTitleProperty( PROPERTY_CREATE_WORKGROUP_PAGETITLE );
239
240 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_WORKGROUP, getLocale( ) );
241
242 return getAdminPage( template.getHtml( ) );
243 }
244
245
246
247
248
249
250
251 public String doCreateWorkgroup( HttpServletRequest request )
252 {
253 String strKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
254 String strDescription = request.getParameter( PARAMETER_WORKGROUP_DESCRIPTION );
255
256 if ( ( strKey == null ) || ( strKey.equals( "" ) ) )
257 {
258 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
259 }
260
261 if ( ( strDescription == null ) || ( strDescription.equals( "" ) ) )
262 {
263 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
264 }
265
266
267 if ( AdminWorkgroupHome.checkExistWorkgroup( strKey ) == Boolean.TRUE )
268 {
269 return AdminMessageService.getMessageUrl( request, MESSAGE_WORKGROUP_ALREADY_EXIST, AdminMessage.TYPE_STOP );
270 }
271
272
273 if ( !StringUtil.checkCodeKey( strKey ) )
274 {
275 return AdminMessageService.getMessageUrl( request, MESSAGE_WORKGROUP_ACCENTUATED_CHARACTER,
276 AdminMessage.TYPE_STOP );
277 }
278
279 AdminWorkgroup adminWorkgroup = new AdminWorkgroup( );
280 adminWorkgroup.setKey( strKey.trim( ) );
281 adminWorkgroup.setDescription( strDescription );
282 AdminWorkgroupHome.create( adminWorkgroup );
283 AdminWorkgroupHome.addUserForWorkgroup( getUser( ), strKey );
284
285 return JSP_MANAGE_WORKGROUPS;
286 }
287
288
289
290
291
292
293
294 public String getConfirmRemoveWorkgroup( HttpServletRequest request )
295 {
296 String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
297 String strUrlRemove = JSP_URL_REMOVE_WORKGROUP + "?" + PARAMETER_WORKGROUP_KEY + "=" + strWorkgroupKey;
298
299 String strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE, strUrlRemove,
300 AdminMessage.TYPE_CONFIRMATION );
301
302 return strUrl;
303 }
304
305
306
307
308
309
310
311 public String doRemoveWorkgroup( HttpServletRequest request )
312 {
313 String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
314 ArrayList<String> listErrors = new ArrayList<String>( );
315
316 if ( AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroupKey ).size( ) > 0 )
317 {
318 return AdminMessageService.getMessageUrl( request, MESSAGE_WORKGROUP_ALREADY_USED, AdminMessage.TYPE_STOP );
319 }
320
321 if ( !WorkgroupRemovalListenerService.getService( ).checkForRemoval( strWorkgroupKey, listErrors, getLocale( ) ) )
322 {
323 String strCause = AdminMessageService.getFormattedList( listErrors, getLocale( ) );
324 Object[] args = { strCause };
325
326 return AdminMessageService.getMessageUrl( request, MESSAGE_CANNOT_REMOVE_WORKGROUP, args,
327 AdminMessage.TYPE_STOP );
328 }
329
330 AdminWorkgroupHome.remove( strWorkgroupKey );
331
332 return JSP_MANAGE_WORKGROUPS;
333 }
334
335
336
337
338
339
340
341 public String getModifyWorkgroup( HttpServletRequest request )
342 {
343 setPageTitleProperty( PROPERTY_MODIFY_WORKGROUP_PAGETITLE );
344
345 String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
346
347 HashMap<String, AdminWorkgroup> model = new HashMap<String, AdminWorkgroup>( );
348 HtmlTemplate template;
349
350 AdminWorkgroup workgroup = AdminWorkgroupHome.findByPrimaryKey( strWorkgroupKey );
351
352 if ( workgroup == null )
353 {
354 return getManageWorkgroups( request );
355 }
356
357 model.put( MARK_WORKGROUP, workgroup );
358 template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_WORKGROUP, getLocale( ), model );
359
360 return getAdminPage( template.getHtml( ) );
361 }
362
363
364
365
366
367
368
369 public String doModifyWorkgroup( HttpServletRequest request )
370 {
371 String strWorgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
372 String strDescription = request.getParameter( PARAMETER_WORKGROUP_DESCRIPTION );
373
374 if ( StringUtils.isEmpty( strDescription ) )
375 {
376 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
377 }
378
379 AdminWorkgroup adminWorkgroup = new AdminWorkgroup( );
380 adminWorkgroup.setKey( strWorgroupKey );
381 adminWorkgroup.setDescription( strDescription );
382 AdminWorkgroupHome.update( adminWorkgroup );
383
384 return JSP_MANAGE_WORKGROUPS;
385 }
386
387
388
389
390
391
392
393 public String getAssignUsers( HttpServletRequest request )
394 {
395 Map<String, Object> model = new HashMap<String, Object>( );
396 setPageTitleProperty( PROPERTY_ASSIGN_USERS_PAGETITLE );
397
398 String strBaseUrl = AppPathService.getBaseUrl( request ) + JSP_URL_ASSIGN_USERS_TO_WORKGROUPS;
399 UrlItem url = new UrlItem( strBaseUrl );
400
401
402 String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
403 AdminWorkgroup adminWorkgroup = AdminWorkgroupHome.findByPrimaryKey( strWorkgroupKey );
404
405 if ( adminWorkgroup == null )
406 {
407 return getManageWorkgroups( request );
408 }
409
410
411 List<AdminUser> listAssignedUsers = new ArrayList<AdminUser>( );
412
413 for ( AdminUser user : AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroupKey ) )
414 {
415
416 if ( ( user.getUserLevel( ) > getUser( ).getUserLevel( ) ) || ( getUser( ).isAdmin( ) ) )
417 {
418 listAssignedUsers.add( user );
419 }
420 }
421
422 List<AdminUser> listFilteredUsers = AdminUserService.getFilteredUsersInterface( listAssignedUsers, request,
423 model, url );
424
425
426 ReferenceList listUsers = new ReferenceList( );
427 ReferenceItem itemUser = null;
428 boolean bAssigned;
429
430 for ( AdminUser user : AdminUserHome.findUserList( ) )
431 {
432 itemUser = new ReferenceItem( );
433 itemUser.setCode( Integer.toString( user.getUserId( ) ) );
434 itemUser.setName( user.getLastName( ) + " " + user.getFirstName( ) + " (" + user.getAccessCode( ) + ")" );
435 bAssigned = Boolean.FALSE;
436
437 for ( AdminUser assignedUser : listAssignedUsers )
438 {
439 if ( Integer.toString( assignedUser.getUserId( ) ).equals( itemUser.getCode( ) ) )
440 {
441 bAssigned = Boolean.TRUE;
442 }
443 }
444
445
446 if ( !bAssigned &&
447 ( ( user.getUserLevel( ) > getUser( ).getUserLevel( ) ) || ( getUser( ).isAdmin( ) ) ) )
448 {
449 listUsers.add( itemUser );
450 }
451 }
452
453
454 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
455 String strAscSort = null;
456
457 if ( strSortedAttributeName != null )
458 {
459 strAscSort = request.getParameter( Parameters.SORTED_ASC );
460
461 boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
462
463 Collections.sort( listFilteredUsers, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
464 }
465
466 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
467 _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_USERS_PER_PAGE, 50 );
468 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
469 _nDefaultItemsPerPage );
470
471 if ( strSortedAttributeName != null )
472 {
473 url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
474 }
475
476 if ( strAscSort != null )
477 {
478 url.addParameter( Parameters.SORTED_ASC, strAscSort );
479 }
480
481
482 setItemNavigator( strWorkgroupKey, url.getUrl( ) );
483
484
485 url.addParameter( PARAMETER_WORKGROUP_KEY, adminWorkgroup.getKey( ) );
486
487 LocalizedPaginator<AdminUser> paginator = new LocalizedPaginator<AdminUser>( listFilteredUsers, _nItemsPerPage,
488 url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
489
490
491 Collection<Level> filteredLevels = new ArrayList<Level>( );
492
493 for ( Level level : LevelHome.getLevelsList( ) )
494 {
495 if ( getUser( ).isAdmin( ) || getUser( ).hasRights( level.getId( ) ) )
496 {
497 filteredLevels.add( level );
498 }
499 }
500
501 model.put( MARK_WORKGROUP, adminWorkgroup );
502 model.put( MARK_USERS_LIST, listUsers );
503 model.put( MARK_ASSIGNED_USERS_LIST, paginator.getPageItems( ) );
504 model.put( MARK_ASSIGNED_USERS_NUMBER, listAssignedUsers.size( ) );
505 model.put( MARK_USER_LEVELS_LIST, filteredLevels );
506 model.put( MARK_ITEM_NAVIGATOR, _itemNavigator );
507 model.put( MARK_PAGINATOR, paginator );
508 model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
509
510 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ASSIGN_USERS, getLocale( ), model );
511
512 return getAdminPage( template.getHtml( ) );
513 }
514
515
516
517
518
519
520
521 public String doAssignUsers( HttpServletRequest request )
522 {
523 String strReturn;
524
525 String strActionCancel = request.getParameter( PARAMETER_CANCEL );
526
527 if ( strActionCancel != null )
528 {
529 strReturn = JSP_MANAGE_WORKGROUPS;
530 }
531 else
532 {
533 String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
534
535
536 String[] arrayUsersIds = request.getParameterValues( PARAMETER_USERS_LIST );
537
538 if ( ( arrayUsersIds != null ) )
539 {
540 for ( int i = 0; i < arrayUsersIds.length; i++ )
541 {
542 int nUserId = Integer.parseInt( arrayUsersIds[i] );
543 AdminUser user = AdminUserHome.findByPrimaryKey( nUserId );
544
545 if ( !AdminWorkgroupHome.isUserInWorkgroup( user, strWorkgroupKey ) )
546 {
547 AdminWorkgroupHome.addUserForWorkgroup( user, strWorkgroupKey );
548 }
549 }
550 }
551
552 strReturn = JSP_ASSIGN_USERS_TO_WORKGROUPS + "?" + PARAMETER_WORKGROUP_KEY + "=" + strWorkgroupKey;
553 }
554
555 return strReturn;
556 }
557
558
559
560
561
562
563 public String doUnAssignUser( HttpServletRequest request )
564 {
565 String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
566 int nIdUser = Integer.parseInt( request.getParameter( PARAMETER_ID_USER ) );
567 String strAnchor = request.getParameter( PARAMETER_ANCHOR );
568
569 AdminUser adminUser = AdminUserHome.findByPrimaryKey( nIdUser );
570
571 if ( adminUser != null )
572 {
573 AdminWorkgroupHome.removeUserFromWorkgroup( adminUser, strWorkgroupKey );
574 }
575
576 return JSP_ASSIGN_USERS_TO_WORKGROUPS + "?" + PARAMETER_WORKGROUP_KEY + "=" + strWorkgroupKey + "#" +
577 strAnchor;
578 }
579
580
581
582
583
584
585 private void setItemNavigator( String strWorkgroupKey, String strUrl )
586 {
587 if ( _itemNavigator == null )
588 {
589 List<String> listWorkgroupKeys = new ArrayList<String>( );
590 int nCurrentItemId = 0;
591 int nIndex = 0;
592
593 for ( AdminWorkgroup workgroup : AdminWorkgroupHome.findAll( ) )
594 {
595 if ( ( workgroup != null ) && StringUtils.isNotBlank( workgroup.getKey( ) ) )
596 {
597 listWorkgroupKeys.add( workgroup.getKey( ) );
598
599 if ( workgroup.getKey( ).equals( strWorkgroupKey ) )
600 {
601 nCurrentItemId = nIndex;
602 }
603
604 nIndex++;
605 }
606 }
607
608 _itemNavigator = new ItemNavigator( listWorkgroupKeys, nCurrentItemId, strUrl, PARAMETER_WORKGROUP_KEY );
609 }
610 else
611 {
612 _itemNavigator.setCurrentItemId( strWorkgroupKey );
613 }
614 }
615
616
617
618
619 private void reinitItemNavigator( )
620 {
621 _itemNavigator = null;
622 }
623 }