View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.web.category;
35  
36  import fr.paris.lutece.plugins.document.business.category.Category;
37  import fr.paris.lutece.plugins.document.business.category.CategoryHome;
38  import fr.paris.lutece.plugins.document.service.category.CategoryService;
39  import fr.paris.lutece.plugins.document.service.category.CategoryService.CategoryDisplay;
40  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
41  import fr.paris.lutece.portal.business.user.AdminUser;
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.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
46  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
47  import fr.paris.lutece.portal.web.constants.Messages;
48  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49  import fr.paris.lutece.util.ReferenceList;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  import fr.paris.lutece.util.url.UrlItem;
52  
53  import org.apache.commons.fileupload.FileItem;
54  import org.apache.commons.lang3.StringUtils;
55  
56  import java.util.Collection;
57  import java.util.HashMap;
58  import java.util.Map;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  
63  /**
64   * This class provides the user interface to manage Lutece group features (
65   * manage, create, modify, remove )
66   */
67  public class CategoryJspBean extends AdminFeaturesPageJspBean
68  {
69      // Right
70      public static final String RIGHT_CATEGORY_MANAGEMENT = "DOCUMENT_CATEGORY_MANAGEMENT";
71  
72      /**
73       * Generated serial version UID
74       */
75      private static final long serialVersionUID = -3587422846030414228L;
76  
77      //Constants
78      private static final String REGEX_ID = "^[\\d]+$";
79      private static final int ERROR_ID_CATEGORY = -1;
80  
81      // JSP
82      private static final String JSP_URL_REMOVE_CATEGORY = "jsp/admin/plugins/document/DoRemoveCategory.jsp";
83  
84      //Markers
85      private static final String MARK_CATEGORY_LIST = "category_list";
86      private static final String MARK_CATEGORY_DISPLAY = "categoryDisplay";
87      private static final String MARK_USER_WORKGROUP_LIST = "user_workgroup_list";
88      private static final String MARK_WORKGROUP_SELECTED = "selected_workgroup";
89  
90      // Parameters
91      private static final String PARAMETER_CATEGORY_ID = "category_id";
92      private static final String PARAMETER_CATEGORY_NAME = "category_name";
93      private static final String PARAMETER_CATEGORY_DESCRIPTION = "category_description";
94      private static final String PARAMETER_CATEGORY_UPDATE_ICON = "update_icon";
95      private static final String PARAMETER_IMAGE_CONTENT = "category_icon";
96      private static final String PARAMETER_WORKGROUP_KEY = "workgroup_key";
97  
98      // Templates
99      private static final String TEMPLATE_MANAGE_CATEGORY = "admin/plugins/document/category/manage_category.html";
100     private static final String TEMPLATE_CREATE_CATEGORY = "admin/plugins/document/category/create_category.html";
101     private static final String TEMPLATE_MODIFY_CATEGORY = "admin/plugins/document/category/modify_category.html";
102 
103     // Properties
104     private static final String PROPERTY_PAGE_TITLE_CREATE_CATEGORY = "document.create_category.pageTitle";
105     private static final String PROPERTY_PAGE_TITLE_MODIFY_CATEGORY = "document.modify_category.pageTitle";
106 
107     // Message
108     private static final String MESSAGE_CATEGORY_EXIST = "document.message.categoryExist";
109     private static final String MESSAGE_CATEGORY_IS_LINKED = "document.message.categoryIsLinked";
110     private static final String MESSAGE_CATEGORY_ERROR = "document.message.categoryError";
111     private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "document.message.confirmRemoveCategory";
112 
113     /**
114      * Creates a new CategoryJspBean object.
115      */
116     public CategoryJspBean(  )
117     {
118     }
119 
120     /**
121      * Returns Category management form
122      * @param request The Http request
123      * @return Html form
124      */
125     public String getManageCategory( HttpServletRequest request )
126     {
127         setPageTitleProperty( null );
128 
129         AdminUser user = getUser(  );
130 
131         HashMap<String, Collection<CategoryDisplay>> model = new HashMap<String, Collection<CategoryDisplay>>(  );
132         model.put( MARK_CATEGORY_LIST, CategoryService.getAllCategoriesDisplay( user ) );
133 
134         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORY, getLocale(  ), model );
135 
136         return getAdminPage( template.getHtml(  ) );
137     }
138 
139     /**
140      * Insert a new Category
141      * @param request The HTTP request
142      * @return String The html code page
143      */
144     public String getCreateCategory( HttpServletRequest request )
145     {
146         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_CATEGORY );
147 
148         AdminUser user = getUser(  );
149         ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( user, getLocale(  ) );
150         Map<String, Object> model = new HashMap<String, Object>(  );
151         model.put( MARK_USER_WORKGROUP_LIST, refListWorkGroups );
152 
153         //LUTECE-890 : the first workgroup will be selected by default
154         if ( !refListWorkGroups.isEmpty(  ) )
155         {
156             model.put( MARK_WORKGROUP_SELECTED, refListWorkGroups.get( 0 ).getCode(  ) );
157         }
158 
159         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CATEGORY, getLocale(  ), model );
160 
161         return getAdminPage( template.getHtml(  ) );
162     }
163 
164     /**
165      * Create Category
166      * @param request The HTTP request
167      * @return String The url page
168      */
169     public String doCreateCategory( HttpServletRequest request )
170     {
171         Category/document/business/category/Category.html#Category">Category category = new Category(  );
172         String strCategoryName = request.getParameter( PARAMETER_CATEGORY_NAME ).trim(  );
173         String strCategoryDescription = request.getParameter( PARAMETER_CATEGORY_DESCRIPTION ).trim(  );
174         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP_KEY );
175 
176         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
177         FileItem item = mRequest.getFile( PARAMETER_IMAGE_CONTENT );
178 
179         // Mandatory field
180         if ( ( strCategoryName.length(  ) == 0 ) || ( strCategoryDescription.length(  ) == 0 ) )
181         {
182             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
183         }
184 
185         // check if category exist
186         if ( CategoryHome.findByName( strCategoryName ).size(  ) > 0 )
187         {
188             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_EXIST, AdminMessage.TYPE_STOP );
189         }
190 
191         category.setName( strCategoryName );
192         category.setDescription( strCategoryDescription );
193 
194         byte[] bytes = item.get(  );
195 
196         category.setIconContent( bytes );
197         category.setIconMimeType( item.getContentType(  ) );
198         category.setWorkgroup( strWorkgroup );
199         CategoryHome.create( category );
200 
201         return getHomeUrl( request );
202     }
203 
204     /**
205      * Returns Category modification form
206      * @param request The HTTP request
207      * @return String The html code page
208      */
209     public String getModifyCategory( HttpServletRequest request )
210     {
211         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_CATEGORY );
212 
213         AdminUser user = getUser(  );
214         ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( user, getLocale(  ) );
215         int nIdCategory = checkCategoryId( request );
216 
217         if ( nIdCategory == ERROR_ID_CATEGORY )
218         {
219             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
220         }
221 
222         CategoryDisplay categoryDisplay = CategoryService.getCategoryDisplay( nIdCategory );
223 
224         if ( categoryDisplay == null )
225         {
226             return getManageCategory( request );
227         }
228 
229         Map<String, Object> model = new HashMap<String, Object>(  );
230 
231         model.put( MARK_CATEGORY_DISPLAY, categoryDisplay );
232         model.put( MARK_USER_WORKGROUP_LIST, refListWorkGroups );
233 
234         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CATEGORY, getLocale(  ), model );
235 
236         return getAdminPage( template.getHtml(  ) );
237     }
238 
239     /**
240      * Modify Category
241      * @param request The HTTP request
242      * @return String The url page
243      */
244     public String doModifyCategory( HttpServletRequest request )
245     {
246         Category category = null;
247         String strCategoryName = request.getParameter( PARAMETER_CATEGORY_NAME ).trim(  );
248         String strCategoryDescription = request.getParameter( PARAMETER_CATEGORY_DESCRIPTION ).trim(  );
249         String strCategoryUpdateIcon = request.getParameter( PARAMETER_CATEGORY_UPDATE_ICON );
250         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP_KEY );
251 
252         int nIdCategory = checkCategoryId( request );
253 
254         if ( nIdCategory == ERROR_ID_CATEGORY )
255         {
256             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
257         }
258 
259         // Mandatory field
260         if ( ( strCategoryName.length(  ) == 0 ) || ( strCategoryDescription.length(  ) == 0 ) )
261         {
262             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
263         }
264 
265         // check if category exist
266         Collection<Category> categoriesList = CategoryHome.findByName( strCategoryName );
267 
268         if ( !categoriesList.isEmpty(  ) && ( categoriesList.iterator(  ).next(  ).getId(  ) != nIdCategory ) )
269         {
270             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_EXIST, AdminMessage.TYPE_STOP );
271         }
272 
273         category = CategoryHome.find( nIdCategory );
274         category.setName( strCategoryName );
275         category.setDescription( strCategoryDescription );
276 
277         if ( strCategoryUpdateIcon != null )
278         {
279             MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
280             FileItem item = mRequest.getFile( PARAMETER_IMAGE_CONTENT );
281 
282             byte[] bytes = item.get(  );
283             category.setIconContent( bytes );
284             category.setIconMimeType( item.getContentType(  ) );
285         }
286 
287         category.setWorkgroup( strWorkgroup );
288 
289         CategoryHome.update( category );
290 
291         return getHomeUrl( request );
292     }
293 
294     /**
295      * Returns the page of confirmation for deleting a workgroup
296      *
297      * @param request The Http Request
298      * @return the confirmation url
299      */
300     public String getConfirmRemoveCategory( HttpServletRequest request )
301     {
302         int nIdCategory = checkCategoryId( request );
303 
304         if ( nIdCategory == ERROR_ID_CATEGORY )
305         {
306             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
307         }
308 
309         // Test if the category is assigned
310         if ( CategoryHome.findCountIdDocuments( nIdCategory ) > 0 )
311         {
312             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_IS_LINKED, AdminMessage.TYPE_STOP );
313         }
314 
315         UrlItem url = new UrlItem( JSP_URL_REMOVE_CATEGORY );
316         url.addParameter( PARAMETER_CATEGORY_ID, Integer.toString( nIdCategory ) );
317 
318         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl(  ),
319             AdminMessage.TYPE_CONFIRMATION );
320     }
321 
322     /**
323      * Perform the deletion
324      * @param request The HTTP request
325      * @return The URL to go after performing the action
326      */
327     public String doRemoveCategory( HttpServletRequest request )
328     {
329         int nIdCategory = checkCategoryId( request );
330 
331         if ( nIdCategory == ERROR_ID_CATEGORY )
332         {
333             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
334         }
335 
336         // Test if the category is assigned
337         if ( CategoryHome.findCountIdDocuments( nIdCategory ) > 0 )
338         {
339             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_IS_LINKED, AdminMessage.TYPE_STOP );
340         }
341 
342         CategoryHome.remove( nIdCategory );
343 
344         return getHomeUrl( request );
345     }
346 
347     /**
348      *
349      * @param request The http request
350      * @return id of category, ERROR_ID_CATEGORY else
351      */
352     private int checkCategoryId( HttpServletRequest request )
353     {
354         String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID );
355 
356         if ( StringUtils.isBlank( strCategoryId ) || !strCategoryId.matches( REGEX_ID ) )
357         {
358             return ERROR_ID_CATEGORY;
359         }
360 
361         return IntegerUtils.convert( strCategoryId );
362     }
363 }