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.resource.web;
35  
36  import fr.paris.lutece.plugins.resource.business.database.DatabaseResource;
37  import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceHome;
38  import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceSort;
39  import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceType;
40  import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceTypeHome;
41  import fr.paris.lutece.plugins.resource.service.action.IResourceAction;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
47  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
48  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
50  import fr.paris.lutece.portal.web.constants.Parameters;
51  import fr.paris.lutece.portal.web.util.LocalizedDelegatePaginator;
52  import fr.paris.lutece.util.ReferenceList;
53  import fr.paris.lutece.util.html.DelegatePaginator;
54  import fr.paris.lutece.util.html.Paginator;
55  import fr.paris.lutece.util.url.UrlItem;
56  
57  import org.apache.commons.lang3.StringUtils;
58  
59  import java.util.HashMap;
60  import java.util.List;
61  import java.util.Map;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  /**
66   * Jsp Bean to manage resources
67   */
68  @Controller( controllerJsp = "ManageResources.jsp", controllerPath = "jsp/admin/plugins/resource/", right = ResourceJspBean.RIGHT_MANAGE_RESOURCES )
69  public class ResourceJspBean extends MVCAdminJspBean
70  {
71      /**
72       * Right to manage resources
73       */
74      public static final String RIGHT_MANAGE_RESOURCES = "RESOURCE_MANAGE_RESOURCES";
75      private static final long serialVersionUID = -7527847344523994706L;
76  
77      // Views
78      private static final String VIEW_MANAGE_RESOURCES = "viewManageResources";
79      private static final String VIEW_CREATE_RESOURCE = "viewCreateResource";
80      private static final String VIEW_MODIFY_RESOURCE = "viewModifyResource";
81      private static final String VIEW_CONFIRM_REMOVE_RESOURCE = "viewConfirmRemoveResource";
82  
83      // Actions
84      private static final String ACTION_DO_CREATE_RESOURCE = "doCreateResource";
85      private static final String ACTION_DO_REMOVE_RESOURCE = "doRemoveResource";
86      private static final String ACTION_DO_MODIFY_RESOURCE = "doModifyResource";
87  
88      // Messages
89      private static final String MESSAGE_RESOURCE_MANAGEMENT_PAGE_TITLE = "resource.resourceManagement.pageTitle";
90      private static final String MESSAGE_CREATE_RESOURCE_PAGE_TITLE = "resource.createResource.pageTitle";
91      private static final String MESSAGE_MODIFY_RESOURCE_PAGE_TITLE = "resource.modifyResource.pageTitle";
92      private static final String MESSAGE_RESOURCE_CREATED = "resource.createResource.messageResourceCreated";
93      private static final String MESSAGE_CONFIRM_REMOVE_RESOURCE = "resource.removeResource.confirmationMessage";
94      private static final String MESSAGE_RESOURCE_REMOVED = "resource.removeResource.resourceRemoved";
95      private static final String MESSAGE_RESOURCE_MODIFIED = "resource.modifyResource.resourceModified";
96  
97      // Marks
98      private static final String MARK_LIST_RESOURCES = "listResources";
99      private static final String MARK_REFERENCE_LIST_RESOURCE_TYPES = "refListResourceTypes";
100     private static final String MARK_RESOURCE_TYPE = "resource_type";
101     private static final String MARK_RESOURCE = "resource";
102     private static final String MARK_PAGINATOR = "paginator";
103     private static final String MARK_ITEMS_PER_PAGE = "nb_items_per_page";
104     private static final String MARK_LIST_ACTIONS = "list_actions";
105     private static final String MARK_LOCALE = "locale";
106 
107     // Parameters
108     private static final String PARAMETER_ID_RESOURCE = "idResource";
109 
110     // Properties
111     private static final String PROPERTY_DEFAULT_ITEMS_PER_PAGE = "resource.resourceManagement.defaultItemsPerPage";
112     private static final String VALIDATION_ATTRIBUTES_PREFIX = "resource.model.entity.databaseResource.attribute.";
113 
114     // Templates
115     private static final String TEMPLATE_MANAGE_RESOURCES = "admin/plugins/resource/manage_resources.html";
116     private static final String TEMPLATE_CREATE_RESOURCE = "admin/plugins/resource/create_resource.html";
117     private static final String TEMPLATE_MODIFY_RESOURCE = "admin/plugins/resource/modify_resource.html";
118     private String _strSort;
119     private boolean _bSortAsc;
120     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_ITEMS_PER_PAGE, 10 );
121     private String _strCurrentPageIndex;
122     private int _nItemsPerPage;
123     private DatabaseResource _resource;
124 
125     /**
126      * Get the page to display the list of database resources
127      * 
128      * @param request
129      *            The request
130      * @return The HTML content to display
131      */
132     @View( value = VIEW_MANAGE_RESOURCES, defaultView = true )
133     public String getManageResource( HttpServletRequest request )
134     {
135         _resource = null;
136 
137         // We update the sort parameters
138         String strSort = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
139         String strSortAsc = request.getParameter( Parameters.SORTED_ASC );
140 
141         if ( StringUtils.isNotEmpty( strSort ) )
142         {
143             _strSort = strSort;
144         }
145 
146         if ( StringUtils.isNotEmpty( strSortAsc ) )
147         {
148             _bSortAsc = Boolean.parseBoolean( strSortAsc );
149         }
150 
151         // We update the pagination parameters
152         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
153         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
154 
155         DatabaseResourceSort resourceSort = DatabaseResourceSort.getDatabaseResourceSort( _strSort, _bSortAsc );
156 
157         // We get the items to display
158         List<Integer> listResourcesId = DatabaseResourceHome.findAllId( resourceSort );
159 
160         String strViewUrl = getViewFullUrl( VIEW_MANAGE_RESOURCES );
161 
162         Paginator<Integer> paginatorIds = new Paginator<>( listResourcesId, _nItemsPerPage, strViewUrl, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
163 
164         DelegatePaginator<DatabaseResource> paginatorItems = new LocalizedDelegatePaginator<>(
165                 DatabaseResourceHome.findByListId( paginatorIds.getPageItems( ), resourceSort ), _nItemsPerPage, strViewUrl, Paginator.PARAMETER_PAGE_INDEX,
166                 _strCurrentPageIndex, paginatorIds.getItemsCount( ), getLocale( ) );
167 
168         // We get the reference list of resource types
169         ReferenceList refListItems = new ReferenceList( );
170 
171         for ( DatabaseResourceType resourceType : DatabaseResourceTypeHome.findAll( ) )
172         {
173             refListItems.addItem( resourceType.getResourceTypeName( ), resourceType.getResourceTypeDescription( ) );
174         }
175 
176         Map<String, Object> model = new HashMap<>( );
177 
178         model.put( MARK_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
179         model.put( MARK_PAGINATOR, paginatorItems );
180         model.put( MARK_LIST_RESOURCES, paginatorItems.getPageItems( ) );
181         model.put( MARK_REFERENCE_LIST_RESOURCE_TYPES, refListItems );
182         model.put( MARK_LIST_ACTIONS, SpringContextService.getBeansOfType( IResourceAction.class ) );
183         model.put( MARK_LOCALE, getLocale( ) );
184         fillCommons( model );
185 
186         return getPage( MESSAGE_RESOURCE_MANAGEMENT_PAGE_TITLE, TEMPLATE_MANAGE_RESOURCES, model );
187     }
188 
189     /**
190      * Get the page to create a resource
191      * 
192      * @param request
193      *            The request
194      * @return The HTML content to display
195      */
196     @View( value = VIEW_CREATE_RESOURCE )
197     public String getCreateResource( HttpServletRequest request )
198     {
199         String strResourceType = ( _resource != null ) ? _resource.getResourceType( ) : request.getParameter( MARK_RESOURCE_TYPE );
200 
201         if ( StringUtils.isEmpty( strResourceType ) )
202         {
203             return redirectView( request, VIEW_MANAGE_RESOURCES );
204         }
205 
206         DatabaseResourceType databaseResourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
207 
208         Map<String, Object> model = new HashMap<>( );
209         model.put( MARK_RESOURCE_TYPE, databaseResourceType );
210         model.put( MARK_RESOURCE, _resource );
211         fillCommons( model );
212         _resource = null;
213 
214         return getPage( MESSAGE_CREATE_RESOURCE_PAGE_TITLE, TEMPLATE_CREATE_RESOURCE, model );
215     }
216 
217     /**
218      * Do create a resource
219      * 
220      * @param request
221      *            The request
222      * @return True
223      */
224     @Action( value = ACTION_DO_CREATE_RESOURCE )
225     public String doCreateResource( HttpServletRequest request )
226     {
227         DatabaseResourceusiness/database/DatabaseResource.html#DatabaseResource">DatabaseResource resource = new DatabaseResource( );
228 
229         populate( resource, request );
230 
231         if ( !validateBean( resource, VALIDATION_ATTRIBUTES_PREFIX ) )
232         {
233             _resource = resource;
234             redirectView( request, VIEW_CREATE_RESOURCE );
235         }
236 
237         DatabaseResourceHome.create( resource );
238         _resource = null;
239 
240         addInfo( MESSAGE_RESOURCE_CREATED, getLocale( ) );
241 
242         return redirectView( request, VIEW_MANAGE_RESOURCES );
243     }
244 
245     /**
246      * Get the page to create a resource
247      * 
248      * @param request
249      *            The request
250      * @return The HTML content to display
251      */
252     @View( value = VIEW_MODIFY_RESOURCE )
253     public String getModifyResource( HttpServletRequest request )
254     {
255         String strIdResource = ( _resource != null ) ? _resource.getIdResource( ) : request.getParameter( PARAMETER_ID_RESOURCE );
256 
257         if ( StringUtils.isEmpty( strIdResource ) || !StringUtils.isNumeric( strIdResource ) )
258         {
259             return redirectView( request, VIEW_MANAGE_RESOURCES );
260         }
261 
262         DatabaseResource databaseResource = ( _resource != null ) ? _resource : DatabaseResourceHome.findByPrimaryKey( Integer.parseInt( strIdResource ) );
263         _resource = null;
264 
265         Map<String, Object> model = new HashMap<>( );
266         model.put( MARK_RESOURCE, databaseResource );
267         model.put( MARK_RESOURCE_TYPE, DatabaseResourceTypeHome.findByPrimaryKey( databaseResource.getResourceType( ) ) );
268         fillCommons( model );
269 
270         return getPage( MESSAGE_MODIFY_RESOURCE_PAGE_TITLE, TEMPLATE_MODIFY_RESOURCE, model );
271     }
272 
273     /**
274      * Do modify a resource
275      * 
276      * @param request
277      *            The request
278      * @return The HTML content to display
279      */
280     @Action( value = ACTION_DO_MODIFY_RESOURCE )
281     public String doModifyResource( HttpServletRequest request )
282     {
283         String strIdResource = ( _resource != null ) ? _resource.getIdResource( ) : request.getParameter( PARAMETER_ID_RESOURCE );
284 
285         if ( StringUtils.isEmpty( strIdResource ) || !StringUtils.isNumeric( strIdResource ) )
286         {
287             return redirectView( request, VIEW_MANAGE_RESOURCES );
288         }
289 
290         DatabaseResource resource = DatabaseResourceHome.findByPrimaryKey( Integer.parseInt( strIdResource ) );
291 
292         populate( resource, request );
293 
294         if ( !validateBean( resource, VALIDATION_ATTRIBUTES_PREFIX ) )
295         {
296             _resource = resource;
297             redirectView( request, VIEW_MODIFY_RESOURCE );
298         }
299 
300         DatabaseResourceHome.update( resource );
301         _resource = null;
302 
303         addInfo( MESSAGE_RESOURCE_MODIFIED, getLocale( ) );
304 
305         return redirectView( request, VIEW_MANAGE_RESOURCES );
306     }
307 
308     /**
309      * Confirm the removal of a resource
310      * 
311      * @param request
312      *            the request
313      * @return The HTML content to display
314      */
315     @View( value = VIEW_CONFIRM_REMOVE_RESOURCE )
316     public String getConfirmRemoveResource( HttpServletRequest request )
317     {
318         String strIdResource = request.getParameter( PARAMETER_ID_RESOURCE );
319 
320         if ( StringUtils.isEmpty( strIdResource ) || !StringUtils.isNumeric( strIdResource ) )
321         {
322             redirectView( request, VIEW_MANAGE_RESOURCES );
323         }
324 
325         UrlItem urlItem = new UrlItem( getActionUrl( ACTION_DO_REMOVE_RESOURCE ) );
326         urlItem.addParameter( PARAMETER_ID_RESOURCE, strIdResource );
327 
328         return redirect( request,
329                 AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_RESOURCE, urlItem.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
330     }
331 
332     /**
333      * Do remove a resource
334      * 
335      * @param request
336      *            The request
337      * @return the next URL to redirect to
338      */
339     @Action( value = ACTION_DO_REMOVE_RESOURCE )
340     public String doRemoveResource( HttpServletRequest request )
341     {
342         String strIdResource = request.getParameter( PARAMETER_ID_RESOURCE );
343 
344         if ( StringUtils.isEmpty( strIdResource ) || !StringUtils.isNumeric( strIdResource ) )
345         {
346             redirectView( request, VIEW_MANAGE_RESOURCES );
347         }
348 
349         int nIdResource = Integer.parseInt( strIdResource );
350         DatabaseResourceHome.delete( nIdResource );
351 
352         addInfo( MESSAGE_RESOURCE_REMOVED, getLocale( ) );
353 
354         return redirectView( request, VIEW_MANAGE_RESOURCES );
355     }
356 }