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