View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.web.workgroup;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.stream.Collectors;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.collections.CollectionUtils;
47  import org.apache.commons.lang3.StringUtils;
48  
49  import fr.paris.lutece.portal.business.right.Level;
50  import fr.paris.lutece.portal.business.right.LevelHome;
51  import fr.paris.lutece.portal.business.user.AdminUser;
52  import fr.paris.lutece.portal.business.user.AdminUserHome;
53  import fr.paris.lutece.portal.business.workgroup.AdminWorkgroup;
54  import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupFilter;
55  import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
56  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
57  import fr.paris.lutece.portal.service.admin.AdminUserService;
58  import fr.paris.lutece.portal.service.message.AdminMessage;
59  import fr.paris.lutece.portal.service.message.AdminMessageService;
60  import fr.paris.lutece.portal.service.security.SecurityTokenService;
61  import fr.paris.lutece.portal.service.template.AppTemplateService;
62  import fr.paris.lutece.portal.service.util.AppPathService;
63  import fr.paris.lutece.portal.service.util.AppPropertiesService;
64  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
65  import fr.paris.lutece.portal.service.workgroup.WorkgroupRemovalListenerService;
66  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
67  import fr.paris.lutece.portal.web.constants.Messages;
68  import fr.paris.lutece.portal.web.constants.Parameters;
69  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
70  import fr.paris.lutece.util.ReferenceItem;
71  import fr.paris.lutece.util.ReferenceList;
72  import fr.paris.lutece.util.html.AbstractPaginator;
73  import fr.paris.lutece.util.html.HtmlTemplate;
74  import fr.paris.lutece.util.html.ItemNavigator;
75  import fr.paris.lutece.util.sort.AttributeComparator;
76  import fr.paris.lutece.util.string.StringUtil;
77  import fr.paris.lutece.util.url.UrlItem;
78  
79  /**
80   * AdminWorkgroup Jsp Bean
81   */
82  public class AdminWorkgroupJspBean extends AdminFeaturesPageJspBean
83  {
84      // Rights
85      /**
86       * Right to manage workgroups
87       */
88      public static final String RIGHT_MANAGE_WORKGROUPS = "CORE_WORKGROUPS_MANAGEMENT";
89  
90      /**
91       * Serial version UID
92       */
93      private static final long serialVersionUID = 5945178935890410656L;
94  
95      // Templates
96      private static final String TEMPLATE_MANAGE_WORGROUPS = "admin/workgroup/manage_workgroups.html";
97      private static final String TEMPLATE_CREATE_WORKGROUP = "admin/workgroup/create_workgroup.html";
98      private static final String TEMPLATE_MODIFY_WORKGROUP = "admin/workgroup/modify_workgroup.html";
99      private static final String TEMPLATE_ASSIGN_USERS = "admin/workgroup/assign_users_workgroup.html";
100 
101     // Markers Freemarker
102     private static final String MARK_WORKGROUPS_LIST = "workgroups_list";
103     private static final String MARK_WORKGROUP = "workgroup";
104     private static final String MARK_USERS_LIST = "users_list";
105     private static final String MARK_ASSIGNED_USERS_LIST = "assigned_users_list";
106     private static final String MARK_ASSIGNED_USERS_NUMBER = "assigned_users_number";
107     private static final String MARK_SEARCH_IS_SEARCH = "search_is_search";
108     private static final String MARK_SEARCH_ADMIN_WORKGROUP_FILTER = "search_admin_workgroup_filter";
109     private static final String MARK_USER_LEVELS_LIST = "user_levels";
110     private static final String MARK_ITEM_NAVIGATOR = "item_navigator";
111     private static final String MARK_PAGINATOR = "paginator";
112     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
113     private static final String MARK_SORT_SEARCH_ATTRIBUTE = "sort_search_attribute";
114 
115     // Properties
116     private static final String PROPERTY_CREATE_WORKGROUP_PAGETITLE = "portal.workgroup.create_workgroup.pageTitle";
117     private static final String PROPERTY_MODIFY_WORKGROUP_PAGETITLE = "portal.workgroup.modify_workgroup.pageTitle";
118     private static final String PROPERTY_ASSIGN_USERS_PAGETITLE = "portal.workgroup.assign_users.pageTitle";
119     private static final String PROPERTY_MANAGE_WORKGROUPS_PAGETITLE = "portal.workgroup.manage_workgroups.pageTitle";
120     private static final String PROPERTY_USERS_PER_PAGE = "paginator.user.itemsPerPage";
121 
122     // Parameters
123     private static final String PARAMETER_WORKGROUP_KEY = "workgroup_key";
124     private static final String PARAMETER_WORKGROUP_DESCRIPTION = "workgroup_description";
125     private static final String PARAMETER_USERS_LIST = "list_users";
126     private static final String PARAMETER_ID_USER = "id_user";
127     private static final String PARAMETER_ANCHOR = "anchor";
128 
129     // JSP
130     private static final String JSP_MANAGE_WORKGROUPS = "ManageWorkgroups.jsp";
131     private static final String JSP_ASSIGN_USERS_TO_WORKGROUPS = "AssignUsersWorkgroup.jsp";
132     private static final String JSP_URL_REMOVE_WORKGROUP = "jsp/admin/workgroup/DoRemoveWorkgroup.jsp";
133     private static final String JSP_URL_ASSIGN_USERS_TO_WORKGROUPS = "jsp/admin/workgroup/AssignUsersWorkgroup.jsp";
134 
135     // Messages
136     private static final String MESSAGE_WORKGROUP_ALREADY_EXIST = "portal.workgroup.message.workgroupAlreadyExist";
137     private static final String MESSAGE_CONFIRM_REMOVE = "portal.workgroup.message.confirmRemove";
138     private static final String MESSAGE_WORKGROUP_ALREADY_USED = "portal.workgroup.message.workgroupAlreadyUsed";
139     private static final String MESSAGE_CANNOT_REMOVE_WORKGROUP = "portal.workgroup.message.cannotRemoveWorkgroup";
140     private static final String MESSAGE_WORKGROUP_ACCENTUATED_CHARACTER = "portal.workgroup.message.accentuatedCharacter";
141     private int _nItemsPerPage;
142     private int _nDefaultItemsPerPage;
143     private String _strCurrentPageIndex;
144     private ItemNavigator _itemNavigator;
145 
146     /**
147      * Get the workgroups management page. This page provides the list of all existing workgroups.
148      * 
149      * @param request
150      *            the http request
151      * @return the html code for the workgroup management page
152      */
153     public String getManageWorkgroups( HttpServletRequest request )
154     {
155         setPageTitleProperty( PROPERTY_MANAGE_WORKGROUPS_PAGETITLE );
156 
157         // Reinit session
158         reinitItemNavigator( );
159 
160         // FILTER
161         AdminWorkgroupFilteroup/AdminWorkgroupFilter.html#AdminWorkgroupFilter">AdminWorkgroupFilter awFilter = new AdminWorkgroupFilter( );
162         boolean bIsSearch = awFilter.setAdminWorkgroupFilter( request );
163 
164         List<AdminWorkgroup> listFilteredWorkgroups = (List<AdminWorkgroup>) AdminWorkgroupHome.findByFilter( awFilter );
165 
166         HashMap<String, Object> model = new HashMap<>( );
167 
168         if ( !getUser( ).isAdmin( ) )
169         {
170             listFilteredWorkgroups = (List<AdminWorkgroup>) AdminWorkgroupService.getAuthorizedCollection( listFilteredWorkgroups, getUser( ) );
171         }
172 
173         // SORT
174         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
175         String strAscSort = null;
176 
177         if ( strSortedAttributeName != null )
178         {
179             strAscSort = request.getParameter( Parameters.SORTED_ASC );
180 
181             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
182 
183             Collections.sort( listFilteredWorkgroups, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
184         }
185 
186         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
187         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_USERS_PER_PAGE, 50 );
188         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
189 
190         String strURL = getHomeUrl( request );
191         UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strURL );
192 
193         if ( strSortedAttributeName != null )
194         {
195             url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
196         }
197 
198         if ( strAscSort != null )
199         {
200             url.addParameter( Parameters.SORTED_ASC, strAscSort );
201         }
202 
203         String strSortSearchAttribute = "";
204 
205         if ( bIsSearch )
206         {
207             awFilter.setUrlAttributes( url );
208 
209             if ( !awFilter.getUrlAttributes( ).equals( "" ) )
210             {
211                 strSortSearchAttribute = "&" + awFilter.getUrlAttributes( );
212             }
213         }
214 
215         // PAGINATOR
216         LocalizedPaginator<AdminWorkgroup> paginator = new LocalizedPaginator<>( listFilteredWorkgroups, _nItemsPerPage, url.getUrl( ),
217                 AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
218 
219         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
220         model.put( MARK_PAGINATOR, paginator );
221         model.put( MARK_WORKGROUPS_LIST, paginator.getPageItems( ) );
222         model.put( MARK_SEARCH_IS_SEARCH, bIsSearch );
223         model.put( MARK_SEARCH_ADMIN_WORKGROUP_FILTER, awFilter );
224         model.put( MARK_SORT_SEARCH_ATTRIBUTE, strSortSearchAttribute );
225 
226         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_WORGROUPS, getLocale( ), model );
227 
228         return getAdminPage( template.getHtml( ) );
229     }
230 
231     /**
232      * Returns the data capture form of a new Workgroup
233      *
234      * @param request
235      *            The HTTP Request
236      * @return The HTML form
237      */
238     public String getCreateWorkgroup( HttpServletRequest request )
239     {
240         setPageTitleProperty( PROPERTY_CREATE_WORKGROUP_PAGETITLE );
241 
242         Map<String, Object> model = new HashMap<>( 1 );
243         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_WORKGROUP ) );
244 
245         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_WORKGROUP, getLocale( ), model );
246 
247         return getAdminPage( template.getHtml( ) );
248     }
249 
250     /**
251      * Process the data capture form of a new workgroup
252      *
253      * @param request
254      *            The HTTP Request
255      * @return The Jsp URL of the process result
256      * @throws AccessDeniedException
257      *             if the security token is invalid
258      */
259     public String doCreateWorkgroup( HttpServletRequest request ) throws AccessDeniedException
260     {
261         String strKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
262         String strDescription = request.getParameter( PARAMETER_WORKGROUP_DESCRIPTION );
263 
264         if ( ( strKey == null ) || ( strKey.equals( "" ) ) )
265         {
266             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
267         }
268 
269         if ( ( strDescription == null ) || ( strDescription.equals( "" ) ) )
270         {
271             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
272         }
273 
274         // Check if workgroup already exist
275         if ( Boolean.TRUE.equals( AdminWorkgroupHome.checkExistWorkgroup( strKey ) ) )
276         {
277             return AdminMessageService.getMessageUrl( request, MESSAGE_WORKGROUP_ALREADY_EXIST, AdminMessage.TYPE_STOP );
278         }
279 
280         // Check if strKey contains accentuated caracters
281         if ( !StringUtil.checkCodeKey( strKey ) )
282         {
283             return AdminMessageService.getMessageUrl( request, MESSAGE_WORKGROUP_ACCENTUATED_CHARACTER, AdminMessage.TYPE_STOP );
284         }
285         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_WORKGROUP ) )
286         {
287             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
288         }
289 
290         AdminWorkgroupoup/AdminWorkgroup.html#AdminWorkgroup">AdminWorkgroup adminWorkgroup = new AdminWorkgroup( );
291         adminWorkgroup.setKey( strKey.trim( ) );
292         adminWorkgroup.setDescription( strDescription );
293         AdminWorkgroupHome.create( adminWorkgroup );
294         AdminWorkgroupHome.addUserForWorkgroup( getUser( ), strKey );
295 
296         return JSP_MANAGE_WORKGROUPS;
297     }
298 
299     /**
300      * Returns the page of confirmation for deleting a workgroup
301      *
302      * @param request
303      *            The Http Request
304      * @return the confirmation url
305      */
306     public String getConfirmRemoveWorkgroup( HttpServletRequest request )
307     {
308         String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
309         String strUrlRemove = JSP_URL_REMOVE_WORKGROUP;
310         Map<String, String> parameters = new HashMap<>( );
311         parameters.put( PARAMETER_WORKGROUP_KEY, strWorkgroupKey );
312         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_URL_REMOVE_WORKGROUP ) );
313 
314         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE, strUrlRemove, AdminMessage.TYPE_CONFIRMATION, parameters );
315     }
316 
317     /**
318      * Process to the confirmation of deleting a workgroup
319      *
320      * @param request
321      *            The Http Request
322      * @return the HTML page
323      * @throws AccessDeniedException
324      *             if the security token is invalid
325      */
326     public String doRemoveWorkgroup( HttpServletRequest request ) throws AccessDeniedException
327     {
328         String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
329         ArrayList<String> listErrors = new ArrayList<>( );
330 
331         if ( CollectionUtils.isNotEmpty( AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroupKey ) ) )
332         {
333             return AdminMessageService.getMessageUrl( request, MESSAGE_WORKGROUP_ALREADY_USED, AdminMessage.TYPE_STOP );
334         }
335 
336         if ( !WorkgroupRemovalListenerService.getService( ).checkForRemoval( strWorkgroupKey, listErrors, getLocale( ) ) )
337         {
338             String strCause = AdminMessageService.getFormattedList( listErrors, getLocale( ) );
339             Object [ ] args = {
340                     strCause
341             };
342 
343             return AdminMessageService.getMessageUrl( request, MESSAGE_CANNOT_REMOVE_WORKGROUP, args, AdminMessage.TYPE_STOP );
344         }
345         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_URL_REMOVE_WORKGROUP ) )
346         {
347             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
348         }
349 
350         AdminWorkgroupHome.remove( strWorkgroupKey );
351 
352         return JSP_MANAGE_WORKGROUPS;
353     }
354 
355     /**
356      * Returns the form to update info about a Workgroup
357      *
358      * @param request
359      *            The Http request
360      * @return The HTML form to update info
361      */
362     public String getModifyWorkgroup( HttpServletRequest request )
363     {
364         setPageTitleProperty( PROPERTY_MODIFY_WORKGROUP_PAGETITLE );
365 
366         String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
367 
368         AdminWorkgroup workgroup = AdminWorkgroupHome.findByPrimaryKey( strWorkgroupKey );
369 
370         if ( workgroup == null )
371         {
372             return getManageWorkgroups( request );
373         }
374 
375         HashMap<String, Object> model = new HashMap<>( );
376         model.put( MARK_WORKGROUP, workgroup );
377         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_WORKGROUP ) );
378         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_WORKGROUP, getLocale( ), model );
379 
380         return getAdminPage( template.getHtml( ) );
381     }
382 
383     /**
384      * Process the data capture form for modify a workgroup
385      *
386      * @param request
387      *            The HTTP Request
388      * @return The Jsp URL of the process result
389      * @throws AccessDeniedException
390      *             if the security token is invalid
391      */
392     public String doModifyWorkgroup( HttpServletRequest request ) throws AccessDeniedException
393     {
394         String strWorgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
395         String strDescription = request.getParameter( PARAMETER_WORKGROUP_DESCRIPTION );
396 
397         if ( StringUtils.isEmpty( strDescription ) )
398         {
399             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
400         }
401         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_WORKGROUP ) )
402         {
403             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
404         }
405 
406         AdminWorkgroupoup/AdminWorkgroup.html#AdminWorkgroup">AdminWorkgroup adminWorkgroup = new AdminWorkgroup( );
407         adminWorkgroup.setKey( strWorgroupKey );
408         adminWorkgroup.setDescription( strDescription );
409         AdminWorkgroupHome.update( adminWorkgroup );
410 
411         return JSP_MANAGE_WORKGROUPS;
412     }
413 
414     /**
415      * Returns the users assignation form
416      *
417      * @param request
418      *            The Http request
419      * @return the html code for display the modes list
420      */
421     public String getAssignUsers( HttpServletRequest request )
422     {
423         Map<String, Object> model = new HashMap<>( );
424         setPageTitleProperty( PROPERTY_ASSIGN_USERS_PAGETITLE );
425 
426         String strBaseUrl = AppPathService.getBaseUrl( request ) + JSP_URL_ASSIGN_USERS_TO_WORKGROUPS;
427         UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strBaseUrl );
428 
429         // WORKGROUP
430         String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
431         AdminWorkgroup adminWorkgroup = AdminWorkgroupHome.findByPrimaryKey( strWorkgroupKey );
432 
433         if ( adminWorkgroup == null )
434         {
435             return getManageWorkgroups( request );
436         }
437 
438         // ASSIGNED USERS
439         // Add users with higher level then connected user or add all users if connected
440         // user is administrator
441         List<AdminUser> listAssignedUsers = AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroupKey ).stream( )
442                 .filter( this::isUserHigherThanConnectedUser ).collect( Collectors.toList( ) );
443 
444         List<AdminUser> listFilteredUsers = AdminUserService.getFilteredUsersInterface( listAssignedUsers, request, model, url );
445 
446         // AVAILABLE USERS
447         ReferenceListst.html#ReferenceList">ReferenceList listUsers = new ReferenceList( );
448 
449         for ( AdminUser user : AdminUserHome.findUserList( ) )
450         {
451             final ReferenceItemtem.html#ReferenceItem">ReferenceItem itemUser = new ReferenceItem( );
452             itemUser.setCode( Integer.toString( user.getUserId( ) ) );
453             itemUser.setName( user.getLastName( ) + " " + user.getFirstName( ) + " (" + user.getAccessCode( ) + ")" );
454 
455             boolean bAssigned = listAssignedUsers.stream( )
456                     .anyMatch( assignedUser -> Integer.toString( assignedUser.getUserId( ) ).equals( itemUser.getCode( ) ) );
457 
458             // Add users with higher level then connected user or add all users if connected
459             // user is administrator
460             if ( !bAssigned && isUserHigherThanConnectedUser( user ) )
461             {
462                 listUsers.add( itemUser );
463             }
464         }
465 
466         // SORT
467         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
468         String strAscSort = null;
469 
470         if ( strSortedAttributeName != null )
471         {
472             url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
473 
474             strAscSort = request.getParameter( Parameters.SORTED_ASC );
475 
476             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
477 
478             Collections.sort( listFilteredUsers, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
479         }
480 
481         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
482         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_USERS_PER_PAGE, 50 );
483         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
484 
485         if ( strAscSort != null )
486         {
487             url.addParameter( Parameters.SORTED_ASC, strAscSort );
488         }
489 
490         // ITEM NAVIGATION
491         setItemNavigator( strWorkgroupKey, url.getUrl( ) );
492 
493         // PAGINATOR
494         url.addParameter( PARAMETER_WORKGROUP_KEY, adminWorkgroup.getKey( ) );
495 
496         LocalizedPaginator<AdminUser> paginator = new LocalizedPaginator<>( listFilteredUsers, _nItemsPerPage, url.getUrl( ),
497                 AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
498 
499         // USER LEVEL
500         Collection<Level> filteredLevels = new ArrayList<>( );
501 
502         for ( Level level : LevelHome.getLevelsList( ) )
503         {
504             if ( getUser( ).isAdmin( ) || getUser( ).hasRights( level.getId( ) ) )
505             {
506                 filteredLevels.add( level );
507             }
508         }
509 
510         model.put( MARK_WORKGROUP, adminWorkgroup );
511         model.put( MARK_USERS_LIST, listUsers );
512         model.put( MARK_ASSIGNED_USERS_LIST, paginator.getPageItems( ) );
513         model.put( MARK_ASSIGNED_USERS_NUMBER, listAssignedUsers.size( ) );
514         model.put( MARK_USER_LEVELS_LIST, filteredLevels );
515         model.put( MARK_ITEM_NAVIGATOR, _itemNavigator );
516         model.put( MARK_PAGINATOR, paginator );
517         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
518         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_ASSIGN_USERS ) );
519 
520         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ASSIGN_USERS, getLocale( ), model );
521 
522         return getAdminPage( template.getHtml( ) );
523     }
524 
525     /**
526      * Process the data capture form for assign users to a workgroup
527      *
528      * @param request
529      *            The HTTP Request
530      * @return The Jsp URL of the process result
531      * @throws AccessDeniedException
532      *             if the security token is invalid
533      */
534     public String doAssignUsers( HttpServletRequest request ) throws AccessDeniedException
535     {
536         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_ASSIGN_USERS ) )
537         {
538             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
539         }
540         String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
541 
542         // retrieve the selected portlets ids
543         String [ ] arrayUsersIds = request.getParameterValues( PARAMETER_USERS_LIST );
544 
545         if ( ( arrayUsersIds != null ) )
546         {
547             for ( int i = 0; i < arrayUsersIds.length; i++ )
548             {
549                 int nUserId = Integer.parseInt( arrayUsersIds [i] );
550                 AdminUser user = AdminUserHome.findByPrimaryKey( nUserId );
551 
552                 if ( !AdminWorkgroupHome.isUserInWorkgroup( user, strWorkgroupKey ) )
553                 {
554                     AdminWorkgroupHome.addUserForWorkgroup( user, strWorkgroupKey );
555                 }
556             }
557         }
558 
559         return JSP_ASSIGN_USERS_TO_WORKGROUPS + "?" + PARAMETER_WORKGROUP_KEY + "=" + strWorkgroupKey;
560     }
561 
562     /**
563      * unassigns user from workgroup
564      * 
565      * @param request
566      *            The HttpRequest
567      * @return the HTML code of list assignations
568      * @throws AccessDeniedException
569      *             if the security token is invalid
570      */
571     public String doUnAssignUser( HttpServletRequest request ) throws AccessDeniedException
572     {
573         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_ASSIGN_USERS ) )
574         {
575             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
576         }
577         String strWorkgroupKey = request.getParameter( PARAMETER_WORKGROUP_KEY );
578         int nIdUser = Integer.parseInt( request.getParameter( PARAMETER_ID_USER ) );
579         String strAnchor = request.getParameter( PARAMETER_ANCHOR );
580 
581         AdminUser adminUser = AdminUserHome.findByPrimaryKey( nIdUser );
582 
583         if ( adminUser != null )
584         {
585             AdminWorkgroupHome.removeUserFromWorkgroup( adminUser, strWorkgroupKey );
586         }
587 
588         return JSP_ASSIGN_USERS_TO_WORKGROUPS + "?" + PARAMETER_WORKGROUP_KEY + "=" + strWorkgroupKey + "#" + strAnchor;
589     }
590 
591     /**
592      * Get the item navigator
593      * 
594      * @param strWorkgroupKey
595      *            the workgroup key
596      * @param strUrl
597      *            the url
598      */
599     private void setItemNavigator( String strWorkgroupKey, String strUrl )
600     {
601         if ( _itemNavigator == null )
602         {
603             List<String> listWorkgroupKeys = new ArrayList<>( );
604             int nCurrentItemId = 0;
605             int nIndex = 0;
606 
607             for ( AdminWorkgroup workgroup : AdminWorkgroupHome.findAll( ) )
608             {
609                 if ( ( workgroup != null ) && StringUtils.isNotBlank( workgroup.getKey( ) ) )
610                 {
611                     listWorkgroupKeys.add( workgroup.getKey( ) );
612 
613                     if ( workgroup.getKey( ).equals( strWorkgroupKey ) )
614                     {
615                         nCurrentItemId = nIndex;
616                     }
617 
618                     nIndex++;
619                 }
620             }
621 
622             _itemNavigator = new ItemNavigator( listWorkgroupKeys, nCurrentItemId, strUrl, PARAMETER_WORKGROUP_KEY );
623         }
624         else
625         {
626             _itemNavigator.setCurrentItemId( strWorkgroupKey );
627         }
628     }
629 
630     /**
631      * Reinit the item navigator
632      */
633     private void reinitItemNavigator( )
634     {
635         _itemNavigator = null;
636     }
637 }