View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.extend.web.action;
35  
36  import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTO;
37  import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTOFilter;
38  import fr.paris.lutece.plugins.extend.service.extender.IResourceExtenderService;
39  import fr.paris.lutece.plugins.extend.service.extender.ResourceExtenderService;
40  import fr.paris.lutece.plugins.extend.service.type.ExtendableResourceTypeService;
41  import fr.paris.lutece.plugins.extend.service.type.IExtendableResourceTypeService;
42  import fr.paris.lutece.portal.business.user.AdminUser;
43  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44  import fr.paris.lutece.portal.service.admin.AdminUserService;
45  import fr.paris.lutece.portal.service.i18n.I18nService;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.portal.web.constants.Parameters;
49  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
50  import fr.paris.lutece.util.ReferenceList;
51  import fr.paris.lutece.util.html.Paginator;
52  import fr.paris.lutece.util.url.UrlItem;
53  
54  import org.apache.commons.lang3.StringUtils;
55  
56  import java.io.Serializable;
57  
58  import java.util.List;
59  import java.util.Map;
60  
61  import javax.servlet.http.HttpServletRequest;
62  
63  /**
64   *
65   * ResourceExtenderSearchFields
66   *
67   */
68  public class ResourceExtenderSearchFields implements IResourceExtenderSearchFields, Serializable
69  {
70      private static final long serialVersionUID = 5171135962785175642L;
71  
72      // PROPERTIES
73      private static final String PROPERTY_DEFAULT_LIST_RESOURCES_EXTENDERS_PER_PAGE = "extend.listResourceExtenders.itemsPerPage";
74      private static final String PROPERTY_LABEL_ALL = "extend.labelAll";
75  
76      // PARAMETERS
77      private static final String PARAMETER_SESSION = "session";
78      private static final String PARAMETER_RESET = "reset";
79  
80      // MARKS
81      private static final String MARK_LIST_RESOURCE_EXTENDERS = "listResourceExtenders";
82      private static final String MARK_LIST_EXTENDERS = "listExtenders";
83      private static final String MARK_FILTER = "filter";
84      private static final String MARK_PAGINATOR = "paginator";
85      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
86      private static final String MARK_RESOURCE_TYPES_FOR_FILTER = "resourceTypesForFilter";
87      private static final String MARK_EXTENDER_TYPES_FOR_FILTER = "extenderTypesForFilter";
88      private static final String MARK_MAP_ACTION_PERMISSIONS = "mapActionPermissions";
89  
90      // VARIABLES
91      private int _nItemsPerPage;
92      private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_RESOURCES_EXTENDERS_PER_PAGE, 50 );
93      private String _strCurrentPageIndex;
94      private String _strSortedAttributeName;
95      private boolean _bIsAscSort;
96      private ResourceExtenderDTOFilter _filter;
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public String getCurrentPageIndex( )
103     {
104         return _strCurrentPageIndex;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public int getDefaultItemsPerPage( )
112     {
113         return _nDefaultItemsPerPage;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public void setCurrentPageIndex( String strCurrentPageIndex )
121     {
122         _strCurrentPageIndex = strCurrentPageIndex;
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public void setDefaultItemsPerPage( int nDefaultItemsPerPage )
130     {
131         _nDefaultItemsPerPage = nDefaultItemsPerPage;
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public int getItemsPerPage( )
139     {
140         return _nItemsPerPage;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public void setItemsPerPage( int nItemsPerPage )
148     {
149         _nItemsPerPage = nItemsPerPage;
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public String getSortedAttributeName( )
157     {
158         return _strSortedAttributeName;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public void setSortedAttributeName( HttpServletRequest request )
166     {
167         if ( StringUtils.isNotBlank( request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME ) ) )
168         {
169             _strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
170         }
171         else
172         {
173             _strSortedAttributeName = StringUtils.EMPTY;
174         }
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public boolean isAscSort( )
182     {
183         return _bIsAscSort;
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public void setAscSort( HttpServletRequest request )
191     {
192         if ( StringUtils.isNotBlank( request.getParameter( Parameters.SORTED_ASC ) ) )
193         {
194             _bIsAscSort = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
195         }
196     }
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public void fillModel( String strBaseUrl, HttpServletRequest request, Map<String, Object> model, AdminUser user ) throws AccessDeniedException
203     {
204         fillModel( strBaseUrl, request, model, null, user );
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public void fillModel( String strBaseUrl, HttpServletRequest request, Map<String, Object> model, String strIdExtendableResource, AdminUser user )
212             throws AccessDeniedException
213     {
214         initFilter( request );
215 
216         // SORT
217         setSortedAttributeName( request );
218         setAscSort( request );
219         _filter.setSortedAttributeName( _strSortedAttributeName );
220         _filter.setAscSort( _bIsAscSort );
221 
222         if ( StringUtils.isNotBlank( strIdExtendableResource ) )
223         {
224             _filter.setFilterIdExtendableResource( strIdExtendableResource );
225         }
226 
227         UrlItem url = new UrlItem( strBaseUrl );
228 
229         if ( getSortedAttributeName( ) != null )
230         {
231             url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, getSortedAttributeName( ) );
232             url.addParameter( Parameters.SORTED_ASC, Boolean.toString( isAscSort( ) ) );
233         }
234 
235         url.addParameter( PARAMETER_SESSION, PARAMETER_SESSION );
236 
237         // PAGINATOR
238         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
239         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_RESOURCES_EXTENDERS_PER_PAGE, 50 );
240         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
241 
242         IResourceExtenderService resourceExtenderService = SpringContextService.getBean( ResourceExtenderService.BEAN_SERVICE );
243         IExtendableResourceTypeService resourceTypeService = SpringContextService.getBean( ExtendableResourceTypeService.BEAN_SERVICE );
244         List<Integer> listIdsExtenders = resourceExtenderService.findIdsByFilter( _filter );
245 
246         LocalizedPaginator<Integer> paginator = new LocalizedPaginator<Integer>( listIdsExtenders, getItemsPerPage( ), url.getUrl( ),
247                 Paginator.PARAMETER_PAGE_INDEX, getCurrentPageIndex( ), request.getLocale( ) );
248 
249         List<ResourceExtenderDTO> listResourceExtenders = resourceExtenderService.findByListIds( paginator.getPageItems( ) );
250 
251         Map<String, Map<String, Boolean>> mapActionPermissions = resourceExtenderService.getActionPermissions( paginator.getPageItems( ), user );
252 
253         // RESOURCE TYPES
254         ReferenceList listResourceTypes = resourceTypeService.findAllAsRef( AdminUserService.getLocale( request ) );
255         listResourceTypes.addItem( StringUtils.EMPTY, I18nService.getLocalizedString( PROPERTY_LABEL_ALL, request.getLocale( ) ) );
256 
257         // EXTENDER TYPES
258         ReferenceList listExtenderTypes = resourceExtenderService.getExtenderTypes( request.getLocale( ) );
259         listExtenderTypes.addItem( StringUtils.EMPTY, I18nService.getLocalizedString( PROPERTY_LABEL_ALL, request.getLocale( ) ) );
260 
261         model.put( MARK_LIST_RESOURCE_EXTENDERS, listResourceExtenders );
262         model.put( MARK_LIST_EXTENDERS, resourceExtenderService.getResourceExtenders( ) );
263         model.put( MARK_FILTER, _filter );
264         model.put( MARK_PAGINATOR, paginator );
265         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( paginator.getItemsPerPage( ) ) );
266         model.put( MARK_RESOURCE_TYPES_FOR_FILTER, listResourceTypes );
267         model.put( MARK_EXTENDER_TYPES_FOR_FILTER, listExtenderTypes );
268         model.put( MARK_MAP_ACTION_PERMISSIONS, mapActionPermissions );
269     }
270 
271     /**
272      * {@inheritDoc}
273      */
274     @Override
275     public void initFilter( HttpServletRequest request )
276     {
277         if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_RESET ) ) )
278         {
279             _filter = new ResourceExtenderDTOFilter( );
280         }
281         else
282             if ( StringUtils.isBlank( request.getParameter( PARAMETER_SESSION ) ) || ( _filter == null ) )
283             {
284                 _filter = new ResourceExtenderDTOFilter( );
285                 _filter.init( request );
286             }
287     }
288 }