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.plugins.unittree.web.action;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
47  import fr.paris.lutece.portal.business.right.Level;
48  import fr.paris.lutece.portal.business.right.LevelHome;
49  import fr.paris.lutece.portal.business.user.AdminUser;
50  import fr.paris.lutece.portal.business.user.AdminUserFilter;
51  import fr.paris.lutece.portal.business.user.AdminUserHome;
52  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53  import fr.paris.lutece.portal.service.admin.AdminUserService;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.web.constants.Parameters;
56  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
57  import fr.paris.lutece.util.html.Paginator;
58  import fr.paris.lutece.util.sort.AttributeComparator;
59  import fr.paris.lutece.util.url.UrlItem;
60  
61  /**
62   *
63   * UnitUserSearchFields
64   *
65   */
66  public class UnitUserSearchFields extends DefaultUnitSearchFields
67  {
68      private static final long serialVersionUID = -4692457181851661527L;
69  
70      // PARAMETERS
71      private static final String PARAMETER_ID_UNIT = "idUnit";
72      private static final String PARAMETER_SESSION = "session";
73  
74      // MARKS
75      private static final String MARK_USER_LEVELS_LIST = "user_levels";
76      private static final String MARK_LIST_USERS = "listUsers";
77      private static final String MARK_PAGINATOR = "paginator";
78      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
79      private static final String MARK_SEARCH_ADMIN_USER_FILTER = "search_admin_user_filter";
80      private static final String MARK_IS_IN_DEPTH_SEARCH = "isInDepthSearch";
81  
82      // MESSAGES
83      private static final String MESSAGE_ERROR_USER_NOT_LOGGED = "unittree.message.error.userNotLogged";
84  
85      // SERVICES
86      private AdminUser _user;
87      private AdminUserFilter _auFilter = new AdminUserFilter( );
88  
89      /**
90       * Constructor
91       */
92      public UnitUserSearchFields( )
93      {
94          super( );
95      }
96  
97      /**
98       * Constructor
99       * 
100      * @param request
101      *            the HTTP request
102      */
103     public UnitUserSearchFields( HttpServletRequest request )
104     {
105         super( );
106         _user = AdminUserService.getAdminUser( request );
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public void fillModelForUserSearchForm( List<AdminUser> listUsers, String strBaseUrl, HttpServletRequest request, Map<String, Object> model, Unit unit )
114             throws AccessDeniedException
115     {
116         if ( _user == null )
117         {
118             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_USER_NOT_LOGGED, request.getLocale( ) );
119             throw new AccessDeniedException( strErrorMessage );
120         }
121 
122         if ( StringUtils.isBlank( request.getParameter( PARAMETER_SESSION ) ) )
123         {
124             _auFilter = new AdminUserFilter( );
125             _auFilter.setAdminUserFilter( request );
126         }
127 
128         UrlItem url = new UrlItem( strBaseUrl );
129 
130         List<AdminUser> listFilteredUsers = new ArrayList<AdminUser>( );
131 
132         for ( AdminUser filteredUser : AdminUserHome.findUserByFilter( _auFilter ) )
133         {
134             boolean bIsFiltered = false;
135 
136             for ( AdminUser user : listUsers )
137             {
138                 if ( user.getUserId( ) == filteredUser.getUserId( ) )
139                 {
140                     bIsFiltered = true;
141 
142                     break;
143                 }
144             }
145 
146             if ( bIsFiltered && ( _user.isParent( filteredUser ) || ( _user.isAdmin( ) ) ) )
147             {
148                 listFilteredUsers.add( filteredUser );
149             }
150         }
151 
152         // SORT
153         this.setSortedAttributeName( request );
154 
155         if ( getSortedAttributeName( ) != null )
156         {
157             this.setAscSort( request );
158 
159             Collections.sort( listFilteredUsers, new AttributeComparator( getSortedAttributeName( ), this.isAscSort( ) ) );
160         }
161 
162         this.setCurrentPageIndex( Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, this.getCurrentPageIndex( ) ) );
163         this.setItemsPerPage(
164                 Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, this.getItemsPerPage( ), this.getDefaultItemsPerPage( ) ) );
165 
166         if ( getSortedAttributeName( ) != null )
167         {
168             url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, getSortedAttributeName( ) );
169             url.addParameter( Parameters.SORTED_ASC, Boolean.toString( this.isAscSort( ) ) );
170         }
171 
172         url.addParameter( PARAMETER_ID_UNIT, unit.getIdUnit( ) );
173         url.addParameter( PARAMETER_SESSION, PARAMETER_SESSION );
174 
175         LocalizedPaginator<AdminUser> paginator = new LocalizedPaginator<AdminUser>( listFilteredUsers, getItemsPerPage( ), url.getUrl( ),
176                 Paginator.PARAMETER_PAGE_INDEX, getCurrentPageIndex( ), request.getLocale( ) );
177 
178         // USER LEVEL
179         Collection<Level> filteredLevels = new ArrayList<Level>( );
180 
181         for ( Level level : LevelHome.getLevelsList( ) )
182         {
183             if ( _user.isAdmin( ) || _user.hasRights( level.getId( ) ) )
184             {
185                 filteredLevels.add( level );
186             }
187         }
188 
189         model.put( MARK_USER_LEVELS_LIST, filteredLevels );
190         model.put( MARK_LIST_USERS, paginator.getPageItems( ) );
191         model.put( MARK_SEARCH_ADMIN_USER_FILTER, _auFilter );
192         model.put( MARK_PAGINATOR, paginator );
193         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( paginator.getItemsPerPage( ) ) );
194         model.put( MARK_IS_IN_DEPTH_SEARCH, this.isInDepthSearch( ) );
195     }
196 }