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.DatabaseResourceType;
37  import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceTypeHome;
38  import fr.paris.lutece.plugins.resource.service.listeners.DatabaseResourceTypeRemovalManager;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
43  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47  import fr.paris.lutece.util.html.Paginator;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   * Jsp Bean to manage resource types
58   */
59  @Controller( controllerJsp = "ManageResourceTypes.jsp", controllerPath = "jsp/admin/plugins/resource/", right = ResourceJspBean.RIGHT_MANAGE_RESOURCES )
60  public class ResourceTypeJspBean extends MVCAdminJspBean
61  {
62      private static final long serialVersionUID = -5823475836167733365L;
63  
64      // Views
65      private static final String VIEW_MANAGE_RESOURCE_TYPE = "viewManageResourceType";
66      private static final String VIEW_CREATE_RESOURCE_TYPE = "viewCreateResourceType";
67      private static final String VIEW_MODIFY_RESOURCE_TYPE = "viewModifyResourceType";
68      private static final String VIEW_REMOVE_RESOURCE_TYPE = "viewConfirmRemoveResourceType";
69  
70      // Actions
71      private static final String ACTION_DO_CREATE_RESOURCE_TYPE = "doCreateResourceType";
72      private static final String ACTION_DO_MODIFY_RESOURCE_TYPE = "doModifyResourceType";
73      private static final String ACTION_DO_REMOVE_RESOURCE_TYPE = "doRemoveResourceType";
74  
75      // Messages
76      private static final String MESSAGE_RESOURCE_TYPE_MANAGEMENT_PAGE_TITLE = "resource.resourceTypeManagement.pageTitle";
77      private static final String MESSAGE_CREATE_RESOURCE_TYPE_PAGE_TITLE = "resource.createResourceType.pageTitle";
78      private static final String MESSAGE_MODIFY_RESOURCE_TYPE_PAGE_TITLE = "resource.modifyResourceType.pageTitle";
79      private static final String MESSAGE_RESOURCE_TYPE_CREATE = "resource.createResourceType.resourceTypeCreated";
80      private static final String MESSAGE_RESOURCE_TYPE_UPDATED = "resource.modifyResourceType.resourceTypeUpdated";
81      private static final String MESSAGE_CONFIRM_REMOVE_RESOURCE_TYPE = "resource.removeResourceType.confirmRemoveResourceType";
82      private static final String MESSAGE_UNKNOWN_RESOURCE_TYPE = "resource.removeResourceType.unknownResourceType";
83      private static final String MESSAGE_RESOURCE_TYPE_REMOVED = "resource.removeResourceType.resourceTypeRemoved";
84      private static final String VALIDATION_ATTRIBUTES_PREFIX = "resource.model.entity.databaseResourceType.attribute.";
85  
86      // Marks
87      private static final String MARK_LIST_RESOURCE_TYPES = "list_resource_types";
88      private static final String MARK_RESOURCE_TYPE = "resource_type";
89      private static final String MARK_PAGINATOR = "paginator";
90      private static final String MARK_ITEMS_PER_PAGE = "nb_items_per_page";
91      private static final String PARAMETER_RESOURCE_TYPE_NAME = "resourceTypeName";
92  
93      // Properties
94      private static final String PROPERTY_DEFAULT_ITEMS_PER_PAGE = "resource.resourceManagement.defaultItemsPerPage";
95  
96      // Templates
97      private static final String TEMPLATE_MANAGE_RESOURCE_TYPE = "admin/plugins/resource/manage_resource_types.html";
98      private static final String TEMPLATE_CREATE_RESOURCE_TYPE = "admin/plugins/resource/create_resource_type.html";
99      private static final String TEMPLATE_MODIFY_RESOURCE_TYPE = "admin/plugins/resource/modify_resource_type.html";
100     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_ITEMS_PER_PAGE, 10 );
101     private String _strCurrentPageIndex;
102     private int _nItemsPerPage;
103     private DatabaseResourceType _resourceType;
104 
105     /**
106      * Get the manage resource type page
107      * 
108      * @param request
109      *            The request
110      * @return The HTML content to display
111      */
112     @View( value = VIEW_MANAGE_RESOURCE_TYPE, defaultView = true )
113     public String getManageResourceType( HttpServletRequest request )
114     {
115         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
116         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
117 
118         Paginator<DatabaseResourceType> paginator = new LocalizedPaginator<>( DatabaseResourceTypeHome.findAll( ), _nItemsPerPage,
119                 getViewFullUrl( VIEW_MANAGE_RESOURCE_TYPE ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
120 
121         _resourceType = null;
122 
123         Map<String, Object> model = getModel( );
124 
125         model.put( MARK_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
126         model.put( MARK_PAGINATOR, paginator );
127         model.put( MARK_LIST_RESOURCE_TYPES, paginator.getPageItems( ) );
128 
129         return getPage( MESSAGE_RESOURCE_TYPE_MANAGEMENT_PAGE_TITLE, TEMPLATE_MANAGE_RESOURCE_TYPE, model );
130     }
131 
132     /**
133      * Get the page to create a resource type
134      * 
135      * @param request
136      *            The request
137      * @return The HTML content to display
138      */
139     @View( value = VIEW_CREATE_RESOURCE_TYPE )
140     public String viewCreateResourceType( HttpServletRequest request )
141     {
142         Map<String, Object> model = getModel( );
143 
144         if ( _resourceType != null )
145         {
146             model.put( MARK_RESOURCE_TYPE, _resourceType );
147             _resourceType = null;
148         }
149 
150         return getPage( MESSAGE_CREATE_RESOURCE_TYPE_PAGE_TITLE, TEMPLATE_CREATE_RESOURCE_TYPE, model );
151     }
152 
153     /**
154      * Do create a resource type
155      * 
156      * @param request
157      *            The request
158      * @return the next URL to redirect to
159      */
160     @Action( ACTION_DO_CREATE_RESOURCE_TYPE )
161     public String doCreateResourceType( HttpServletRequest request )
162     {
163         DatabaseResourceTypedatabase/DatabaseResourceType.html#DatabaseResourceType">DatabaseResourceType resourceType = new DatabaseResourceType( );
164         populate( resourceType, request );
165 
166         if ( !validateBean( resourceType, VALIDATION_ATTRIBUTES_PREFIX ) )
167         {
168             _resourceType = resourceType;
169 
170             return redirectView( request, VIEW_CREATE_RESOURCE_TYPE );
171         }
172 
173         DatabaseResourceTypeHome.insert( resourceType );
174         addInfo( MESSAGE_RESOURCE_TYPE_CREATE, getLocale( ) );
175 
176         return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
177     }
178 
179     /**
180      * Get the page to create a resource type
181      * 
182      * @param request
183      *            The request
184      * @return The HTML content to display
185      */
186     @View( value = VIEW_MODIFY_RESOURCE_TYPE )
187     public String viewModifyResourceType( HttpServletRequest request )
188     {
189         Map<String, Object> model = getModel( );
190 
191         DatabaseResourceType resourceType;
192 
193         if ( _resourceType != null )
194         {
195             resourceType = _resourceType;
196             _resourceType = null;
197         }
198         else
199         {
200             String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE_NAME );
201 
202             if ( StringUtils.isEmpty( strResourceType ) )
203             {
204                 redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
205             }
206 
207             resourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
208         }
209 
210         model.put( MARK_RESOURCE_TYPE, resourceType );
211 
212         return getPage( MESSAGE_MODIFY_RESOURCE_TYPE_PAGE_TITLE, TEMPLATE_MODIFY_RESOURCE_TYPE, model );
213     }
214 
215     /**
216      * Do create a resource type
217      * 
218      * @param request
219      *            The request
220      * @return the next URL to redirect to
221      */
222     @Action( ACTION_DO_MODIFY_RESOURCE_TYPE )
223     public String doModifyResourceType( HttpServletRequest request )
224     {
225         DatabaseResourceTypedatabase/DatabaseResourceType.html#DatabaseResourceType">DatabaseResourceType resourceType = new DatabaseResourceType( );
226         populate( resourceType, request );
227 
228         if ( !validateBean( resourceType, VALIDATION_ATTRIBUTES_PREFIX ) )
229         {
230             _resourceType = resourceType;
231 
232             return redirectView( request, VIEW_MODIFY_RESOURCE_TYPE );
233         }
234 
235         DatabaseResourceTypeHome.update( resourceType );
236         addInfo( MESSAGE_RESOURCE_TYPE_UPDATED, getLocale( ) );
237 
238         return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
239     }
240 
241     /**
242      * Get the confirmation message before removing a resource type
243      * 
244      * @param request
245      *            the request
246      * @return The next URL to redirect to
247      */
248     @View( value = VIEW_REMOVE_RESOURCE_TYPE )
249     public String getConfirmRemoveResourceType( HttpServletRequest request )
250     {
251         String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE_NAME );
252 
253         if ( StringUtils.isEmpty( strResourceType ) )
254         {
255             addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
256 
257             return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
258         }
259 
260         DatabaseResourceType resourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
261 
262         if ( resourceType == null )
263         {
264             addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
265 
266             return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
267         }
268 
269         UrlItem urlItem = new UrlItem( getActionUrl( ACTION_DO_REMOVE_RESOURCE_TYPE ) );
270         urlItem.addParameter( PARAMETER_RESOURCE_TYPE_NAME, strResourceType );
271 
272         return redirect( request,
273                 AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_RESOURCE_TYPE, urlItem.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
274     }
275 
276     /**
277      * Do remove a resource type
278      * 
279      * @param request
280      *            The request
281      * @return The next URL to redirect to
282      */
283     @Action( value = ACTION_DO_REMOVE_RESOURCE_TYPE )
284     public String doRemoveResourceType( HttpServletRequest request )
285     {
286         String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE_NAME );
287 
288         if ( StringUtils.isEmpty( strResourceType ) )
289         {
290             addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
291         }
292         else
293         {
294             DatabaseResourceType resourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
295 
296             if ( resourceType == null )
297             {
298                 addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
299             }
300             else
301             {
302                 String strError = DatabaseResourceTypeRemovalManager.canResourceTypeBeRemoved( strResourceType, getLocale( ) );
303 
304                 if ( strError != null )
305                 {
306                     addError( strError );
307                 }
308                 else
309                 {
310                     DatabaseResourceTypeHome.delete( strResourceType );
311                     addInfo( MESSAGE_RESOURCE_TYPE_REMOVED, getLocale( ) );
312                 }
313             }
314         }
315 
316         return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
317     }
318 }