View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.announce.web;
35  
36  import java.util.Collections;
37  import java.util.HashMap;
38  import java.util.List;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.announce.utils.AnnounceUtils;
45  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  import fr.paris.lutece.portal.service.security.LuteceUser;
48  import fr.paris.lutece.portal.service.security.SecurityService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
52  import fr.paris.lutece.portal.web.constants.Parameters;
53  import fr.paris.lutece.util.html.AbstractPaginator;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  import fr.paris.lutece.util.html.Paginator;
56  import fr.paris.lutece.util.sort.AttributeComparator;
57  import fr.paris.lutece.util.url.UrlItem;
58  
59  /**
60   * Announce User Jsp Bean
61   */
62  public class AnnounceUserJspBean extends PluginAdminPageJspBean
63  {
64      /**
65       * Right to manage announces
66       */
67      public static final String RIGHT_MANAGE_ANNOUNCE = "ANNOUNCE_MANAGEMENT";
68      private static final long serialVersionUID = 302676061356843811L;
69  
70      /* Templates */
71      private static final String TEMPLATE_ANNOUNCE_LIST_USERS = "admin/plugins/announce/list_users.html";
72  
73      /* Properties */
74      private static final String PROPERTY_PAGE_TITLE_LIST_USERS = "announce.list_users.pageTitle";
75      private static final String PROPERTY_ITEM_PER_PAGE = "module.mylutece.directory.items_per_page";
76  
77      // Parameters
78      private static final String PARAMETER_PAGE_INDEX = "page_index";
79  
80      // Markers
81      private static final String MARK_LOCALE = "locale";
82      private static final String MARK_PAGINATOR = "paginator";
83      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
84      private static final String MARK_USERS_LIST = "users_list";
85      private static final String MARK_NAME_FOUND = "name_found";
86      private static final String MARK_NO_USERS = "no_users";
87  
88      // Jsp
89      private static final String JSP_URL_MANAGE_USERS = "jsp/admin/plugins/announce/ShowAnnounceUsers.jsp";
90  
91      // Session sectors
92      private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 50 );
93      private String _strCurrentPageIndex;
94      private int _nItemsPerPage;
95      private AttributeComparator _attributeComparator;
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public Plugin getPlugin( )
102     {
103         Plugin plugin = super.getPlugin( );
104 
105         if ( plugin == null )
106         {
107             plugin = AnnounceUtils.getPlugin( );
108         }
109 
110         return plugin;
111     }
112 
113     /**
114      * Get the list of the Announce users
115      * 
116      * @return a list containing all the Announce users
117      * @param request
118      *            The http request
119      * @throws AccessDeniedException
120      *             Message displayed if an exception occures
121      */
122     public String getList( HttpServletRequest request ) throws AccessDeniedException
123     {
124         Boolean nameFound = false;
125         Boolean noUsers = false;
126 
127         HashMap<String, Object> model = new HashMap<>( );
128 
129         setPageTitleProperty( PROPERTY_PAGE_TITLE_LIST_USERS );
130         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
131         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
132 
133         List<LuteceUser> listUsers = (List<LuteceUser>) SecurityService.getInstance( ).getUsers( );
134 
135         if ( listUsers != null )
136         {
137             for ( LuteceUser user : listUsers )
138             {
139                 if ( StringUtils.isNotEmpty( user.getUserInfo( LuteceUser.NAME_GIVEN ) )
140                         || StringUtils.isNotEmpty( user.getUserInfo( LuteceUser.NAME_FAMILY ) ) )
141                 {
142                     nameFound = true;
143 
144                     break;
145                 }
146             }
147         }
148         else
149         {
150             noUsers = true;
151         }
152 
153         String strSort = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
154 
155         if ( StringUtils.isNotEmpty( strSort ) )
156         {
157             _attributeComparator = new AttributeComparator( strSort, Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) ) );
158         }
159 
160         if ( _attributeComparator != null )
161         {
162             Collections.sort( listUsers, _attributeComparator );
163         }
164 
165         Paginator<LuteceUser> paginator = new Paginator<>( listUsers, _nItemsPerPage, getUrlPage( ), PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
166 
167         model.put( MARK_NAME_FOUND, nameFound );
168         model.put( MARK_NO_USERS, noUsers );
169         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
170         model.put( MARK_PAGINATOR, paginator );
171         model.put( MARK_USERS_LIST, paginator.getPageItems( ) );
172         model.put( MARK_LOCALE, getLocale( ) );
173 
174         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_ANNOUNCE_LIST_USERS, getLocale( ), model );
175 
176         return getAdminPage( templateList.getHtml( ) );
177     }
178 
179     /**
180      * Return UrlPage Url
181      * 
182      * @return url
183      */
184     private String getUrlPage( )
185     {
186         UrlItem url = new UrlItem( JSP_URL_MANAGE_USERS );
187 
188         return url.getUrl( );
189     }
190 }