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.workflow.web;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.collections.CollectionUtils;
43  import org.apache.commons.fileupload.FileItem;
44  
45  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
46  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
47  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
48  import fr.paris.lutece.plugins.workflowcore.business.icon.Icon;
49  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
50  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
51  import fr.paris.lutece.plugins.workflowcore.service.icon.IIconService;
52  import fr.paris.lutece.plugins.workflowcore.service.icon.IconService;
53  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.service.message.AdminMessage;
56  import fr.paris.lutece.portal.service.message.AdminMessageService;
57  import fr.paris.lutece.portal.service.spring.SpringContextService;
58  import fr.paris.lutece.portal.service.template.AppTemplateService;
59  import fr.paris.lutece.portal.service.util.AppPathService;
60  import fr.paris.lutece.portal.service.util.AppPropertiesService;
61  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
62  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
63  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
64  import fr.paris.lutece.util.html.AbstractPaginator;
65  import fr.paris.lutece.util.html.HtmlTemplate;
66  import fr.paris.lutece.util.url.UrlItem;
67  
68  /**
69   * JspBean to manage workflow icons
70   */
71  public class IconJspBean extends PluginAdminPageJspBean
72  {
73      /**
74       * Serial version UID
75       */
76      private static final long serialVersionUID = 1953322601057873584L;
77  
78      // templates
79      private static final String TEMPLATE_MANAGE_ICON = "admin/plugins/workflow/manage_icon.html";
80      private static final String TEMPLATE_CREATE_ICON = "admin/plugins/workflow/create_icon.html";
81      private static final String TEMPLATE_MODIFY_ICON = "admin/plugins/workflow/modify_icon.html";
82  
83      // Markers
84      private static final String MARK_ICON_LIST = "icon_list";
85      private static final String MARK_MODIFY_ICON = "modify_icon";
86      private static final String MARK_PAGINATOR = "paginator";
87      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
88  
89      // parameters form
90      private static final String PARAMETER_ID_ICON = "id_icon";
91      private static final String PARAMETER_NAME = "name";
92      private static final String PARAMETER_ID_FILE = "id_file";
93      private static final String PARAMETER_PAGE_INDEX = "page_index";
94      private static final String PARAMETER_WIDTH = "width";
95      private static final String PARAMETER_HEIGHT = "height";
96      private static final String PARAMETER_CANCEL = "cancel";
97  
98      // other constants
99      private static final String EMPTY_STRING = "";
100 
101     // message
102     private static final String MESSAGE_CONFIRM_REMOVE_ICON = "workflow.message.confirm_remove_icon";
103     private static final String MESSAGE_MANDATORY_FIELD = "workflow.message.mandatory.field";
104     private static final String MESSAGE_CAN_NOT_REMOVE_ICON_ACTIONS_ARE_ASSOCIATE = "workflow.message.can_not_remove_icon_actions_are_associate";
105     private static final String MESSAGE_NUMERIC_FIELD = "workflow.message.numeric_field";
106 
107     // properties
108     private static final String PROPERTY_ITEM_PER_PAGE = "workflow.itemsPerPage";
109     private static final String PROPERTY_MANAGE_ICON = "workflow.manage_icon.page_title";
110     private static final String PROPERTY_MODIFY_ICON = "workflow.modify_icon.page_title";
111     private static final String PROPERTY_CREATE_ICON = "workflow.create_icon.page_title";
112     private static final String FIELD_NAME = "workflow.create_icon.label_name";
113     private static final String FIELD_WIDTH = "workflow.create_icon.label_width";
114     private static final String FIELD_HEIGHT = "workflow.create_icon.label_height";
115     private static final String FIELD_FILE = "workflow.create_icon.label_file";
116 
117     // Jsp Definition
118     private static final String JSP_MANAGE_ICON = "jsp/admin/plugins/workflow/ManageIcon.jsp";
119     private static final String JSP_DO_REMOVE_ICON = "jsp/admin/plugins/workflow/DoRemoveIcon.jsp";
120 
121     // session fields
122     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
123     private String _strCurrentPageIndex;
124     private int _nItemsPerPage;
125     private IIconService _iconService = SpringContextService.getBean( IconService.BEAN_SERVICE );
126     private IActionService _actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
127 
128     /**
129      * Return management icon ( list of icon )
130      * 
131      * @param request
132      *            The Http request
133      * @return Html management icon
134      */
135     public String getManageIcon( HttpServletRequest request )
136     {
137         Map<String, Object> model = new HashMap<>( );
138         List<Icon> listDirectoryXsl = _iconService.getListIcons( );
139         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
140         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
141 
142         LocalizedPaginator<Icon> paginator = new LocalizedPaginator<>( listDirectoryXsl, _nItemsPerPage, getJspManageIcon( request ), PARAMETER_PAGE_INDEX,
143                 _strCurrentPageIndex, getLocale( ) );
144 
145         model.put( MARK_PAGINATOR, paginator );
146         model.put( MARK_NB_ITEMS_PER_PAGE, EMPTY_STRING + _nItemsPerPage );
147         model.put( MARK_ICON_LIST, paginator.getPageItems( ) );
148         setPageTitleProperty( PROPERTY_MANAGE_ICON );
149 
150         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ICON, getLocale( ), model );
151 
152         return getAdminPage( templateList.getHtml( ) );
153     }
154 
155     /**
156      * Gets the icon creation page
157      * 
158      * @param request
159      *            The HTTP request
160      * @return The directory xsl creation page
161      */
162     public String getCreateIcon( HttpServletRequest request )
163     {
164         setPageTitleProperty( PROPERTY_CREATE_ICON );
165 
166         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_ICON, getLocale( ) );
167 
168         return getAdminPage( template.getHtml( ) );
169     }
170 
171     /**
172      * Perform the icon creation
173      * 
174      * @param request
175      *            The HTTP request
176      * @return The URL to go after performing the action
177      */
178     public String doCreateIcon( HttpServletRequest request )
179     {
180         if ( request.getParameter( PARAMETER_CANCEL ) == null )
181         {
182             Icon icon = new Icon( );
183             String strError = getIconData( request, icon );
184 
185             if ( strError != null )
186             {
187                 return strError;
188             }
189 
190             _iconService.create( icon );
191         }
192 
193         return getJspManageIcon( request );
194     }
195 
196     /**
197      * Gets the icon modification page
198      * 
199      * @param request
200      *            The HTTP request
201      * @throws AccessDeniedException
202      *             the {@link AccessDeniedException}
203      * @return The icon creation page
204      */
205     public String getModifyIcon( HttpServletRequest request ) throws AccessDeniedException
206     {
207         String strIdIcon = request.getParameter( PARAMETER_ID_ICON );
208         int nIdIcon = WorkflowUtils.convertStringToInt( strIdIcon );
209         Icon icon = _iconService.findByPrimaryKey( nIdIcon );
210 
211         if ( icon == null )
212         {
213             throw new AccessDeniedException( "The icon is not found for ID " + nIdIcon );
214         }
215 
216         Map<String, Object> model = new HashMap<>( );
217 
218         model.put( MARK_MODIFY_ICON, icon );
219         setPageTitleProperty( PROPERTY_MODIFY_ICON );
220 
221         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_ICON, getLocale( ), model );
222 
223         return getAdminPage( template.getHtml( ) );
224     }
225 
226     /**
227      * Perform the icon modification
228      * 
229      * @param request
230      *            The HTTP request
231      * @throws AccessDeniedException
232      *             the {@link AccessDeniedException}
233      * @return The URL to go after performing the action
234      */
235     public String doModifyIcon( HttpServletRequest request ) throws AccessDeniedException
236     {
237         if ( request.getParameter( PARAMETER_CANCEL ) == null )
238         {
239             String strIdIcon = request.getParameter( PARAMETER_ID_ICON );
240             int nIdIcon = WorkflowUtils.convertStringToInt( strIdIcon );
241             Icon icon = _iconService.findByPrimaryKey( nIdIcon );
242 
243             if ( icon == null )
244             {
245                 throw new AccessDeniedException( "The icon is not found for ID " + nIdIcon );
246             }
247 
248             String strError = getIconData( request, icon );
249 
250             if ( strError != null )
251             {
252                 return strError;
253             }
254 
255             if ( icon.getValue( ) != null )
256             {
257                 _iconService.update( icon );
258             }
259             else
260             {
261                 _iconService.updateMetadata( icon );
262             }
263         }
264 
265         return getJspManageIcon( request );
266     }
267 
268     /**
269      * Gets the confirmation page of delete icon
270      * 
271      * @param request
272      *            The HTTP request
273      * @throws AccessDeniedException
274      *             the {@link AccessDeniedException}
275      * @return the confirmation page of delete directory xsl
276      */
277     public String getConfirmRemoveIcon( HttpServletRequest request ) throws AccessDeniedException
278     {
279         String strIdIcon = request.getParameter( PARAMETER_ID_ICON );
280         int nIdIcon = WorkflowUtils.convertStringToInt( strIdIcon );
281         ActionFilter filter = new ActionFilter( );
282         filter.setAutomaticReflexiveAction( false );
283         filter.setIdIcon( nIdIcon );
284 
285         List<Action> listAction = _actionService.getListActionByFilter( filter );
286 
287         if ( CollectionUtils.isNotEmpty( listAction ) )
288         {
289             return AdminMessageService.getMessageUrl( request, MESSAGE_CAN_NOT_REMOVE_ICON_ACTIONS_ARE_ASSOCIATE, AdminMessage.TYPE_STOP );
290         }
291 
292         UrlItem url = new UrlItem( JSP_DO_REMOVE_ICON );
293         url.addParameter( PARAMETER_ID_ICON, strIdIcon );
294 
295         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ICON, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
296     }
297 
298     /**
299      * Perform the icon supression
300      * 
301      * @param request
302      *            The HTTP request
303      * @throws AccessDeniedException
304      *             the {@link AccessDeniedException}
305      * @return The URL to go after performing the action
306      */
307     public String doRemoveIcon( HttpServletRequest request ) throws AccessDeniedException
308     {
309         String strIdIcon = request.getParameter( PARAMETER_ID_ICON );
310         int nIdIcon = WorkflowUtils.convertStringToInt( strIdIcon );
311         ActionFilter filter = new ActionFilter( );
312         filter.setAutomaticReflexiveAction( false );
313         filter.setIdIcon( nIdIcon );
314 
315         List<Action> listAction = _actionService.getListActionByFilter( filter );
316 
317         if ( !listAction.isEmpty( ) )
318         {
319             return AdminMessageService.getMessageUrl( request, MESSAGE_CAN_NOT_REMOVE_ICON_ACTIONS_ARE_ASSOCIATE, AdminMessage.TYPE_STOP );
320         }
321 
322         _iconService.remove( nIdIcon );
323 
324         return getJspManageIcon( request );
325     }
326 
327     /**
328      * Get the request data and if there is no error insert the data in the icon object specified in parameter. return null if there is no error or else return
329      * the error page url
330      * 
331      * @param request
332      *            the request
333      * @param icon
334      *            the Icon Object
335      * @return null if there is no error or else return the error page url
336      */
337     private String getIconData( HttpServletRequest request, Icon icon )
338     {
339         String strError = WorkflowUtils.EMPTY_STRING;
340         String strName = request.getParameter( PARAMETER_NAME );
341         String strWidth = request.getParameter( PARAMETER_WIDTH );
342         String strHeight = request.getParameter( PARAMETER_HEIGHT );
343 
344         int nWidth = WorkflowUtils.convertStringToInt( strWidth );
345         int nHeight = WorkflowUtils.convertStringToInt( strHeight );
346 
347         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
348         FileItem fileItem = multipartRequest.getFile( PARAMETER_ID_FILE );
349 
350         if ( ( strName == null ) || strName.trim( ).equals( EMPTY_STRING ) )
351         {
352             strError = FIELD_NAME;
353         }
354         else
355             if ( ( icon.getValue( ) == null )
356                     && ( ( fileItem == null ) || ( ( fileItem.getName( ) == null ) && WorkflowUtils.EMPTY_STRING.equals( fileItem.getName( ) ) ) ) )
357             {
358                 strError = FIELD_FILE;
359             }
360 
361         // Mandatory fields
362         if ( !strError.equals( EMPTY_STRING ) )
363         {
364             Object [ ] tabRequiredFields = {
365                     I18nService.getLocalizedString( strError, getLocale( ) )
366             };
367 
368             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
369         }
370 
371         if ( ( strWidth != null ) && ( !strWidth.trim( ).equals( WorkflowUtils.EMPTY_STRING ) ) && ( nWidth == -1 ) )
372         {
373             strError = FIELD_WIDTH;
374         }
375         else
376             if ( ( strHeight != null ) && ( !strHeight.trim( ).equals( WorkflowUtils.EMPTY_STRING ) ) && ( nHeight == -1 ) )
377             {
378                 strError = FIELD_HEIGHT;
379             }
380 
381         if ( !strError.equals( WorkflowUtils.EMPTY_STRING ) )
382         {
383             Object [ ] tabRequiredFields = {
384                     I18nService.getLocalizedString( strError, getLocale( ) )
385             };
386 
387             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
388         }
389 
390         icon.setName( strName );
391 
392         if ( ( fileItem != null ) && ( fileItem.getName( ) != null ) && !WorkflowUtils.EMPTY_STRING.equals( fileItem.getName( ) ) )
393         {
394             icon.setValue( fileItem.get( ) );
395             icon.setMimeType( fileItem.getContentType( ) );
396         }
397         else
398         {
399             icon.setValue( null );
400         }
401 
402         icon.setWidth( nWidth );
403         icon.setHeight( nHeight );
404 
405         return null;
406     }
407 
408     /**
409      * return the url of manage export format
410      * 
411      * @param request
412      *            the request
413      * @return the url of manage export format
414      */
415     private String getJspManageIcon( HttpServletRequest request )
416     {
417         return AppPathService.getBaseUrl( request ) + JSP_MANAGE_ICON;
418     }
419 }