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.portal.web.style;
35  
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  
46  import fr.paris.lutece.portal.business.portlet.PortletHome;
47  import fr.paris.lutece.portal.business.portlet.PortletImpl;
48  import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
49  import fr.paris.lutece.portal.business.style.Style;
50  import fr.paris.lutece.portal.business.style.StyleHome;
51  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
52  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53  import fr.paris.lutece.portal.service.message.AdminMessage;
54  import fr.paris.lutece.portal.service.message.AdminMessageService;
55  import fr.paris.lutece.portal.service.security.SecurityTokenService;
56  import fr.paris.lutece.portal.service.template.AppTemplateService;
57  import fr.paris.lutece.portal.service.util.AppPropertiesService;
58  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
59  import fr.paris.lutece.portal.web.constants.Messages;
60  import fr.paris.lutece.portal.web.constants.Parameters;
61  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
62  import fr.paris.lutece.util.html.AbstractPaginator;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  import fr.paris.lutece.util.sort.AttributeComparator;
65  
66  /**
67   * This class provides the user interface to manage Styles features
68   */
69  public class StylesJspBean extends AdminFeaturesPageJspBean
70  {
71      // ////////////////////////////////////////////////////////////////////////////////
72      // Constants
73  
74      // Right
75      /**
76       * Right to manage styles
77       */
78      public static final String RIGHT_MANAGE_STYLE = "CORE_STYLES_MANAGEMENT";
79  
80      /**
81       * Serial version UID
82       */
83      private static final long serialVersionUID = 7138319350433775587L;
84  
85      // Markers
86      private static final String MARK_STYLE_LIST = "style_list";
87      private static final String MARK_PORTLET_TYPE_LIST = "portlet_type_list";
88      private static final String MARK_PORTAL_COMPONENT_LIST = "portal_component_list";
89      private static final String MARK_STYLE = "style";
90      private static final String MARK_PAGINATOR = "paginator";
91      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
92  
93      // Properties
94      private static final String PROPERTY_STYLES_PER_PAGE = "paginator.style.itemsPerPage";
95  
96      // Templates files path
97      private static final String TEMPLATE_MANAGE_STYLES = "admin/style/manage_styles.html";
98      private static final String TEMPLATE_CREATE_STYLE = "admin/style/create_style.html";
99      private static final String TEMPLATE_MODIFY_STYLE = "admin/style/modify_style.html";
100 
101     // Portal Component definition
102     private static final int PORTAL_COMPONENT_ID_PORTLET = 0;
103 
104     // Jsp Definition
105     private static final String JSP_DO_REMOVE_STYLE = "jsp/admin/style/DoRemoveStyle.jsp";
106     private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";
107 
108     // Message keys
109     private static final String MESSAGE_CANT_DELETE_STYLE_PORTLETS = "portal.style.message.cannotDeleteStylePorlets";
110 
111     private static final String MESSAGE_CONFIRM_DELETE_STYLE = "portal.style.message.confirmDeleteStyle";
112     private static final String MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID = "portal.style.message.createStyle.InvalidIdFormat";
113     private static final String MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS = "portal.style.message.createStyle.idAlreadyExists";
114     private static final String MESSAGE_CREATE_STYLE_COMPONENT_EXISTS = "portal.style.message.createStyle.componentHasAlreadyAStyle";
115     private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
116     private int _nItemsPerPage;
117     private String _strCurrentPageIndex;
118 
119     /**
120      * Displays the styles list
121      * 
122      * @param request
123      *            The HTTP request
124      * @return the html code for displaying the styles list
125      */
126     public String getStylesManagement( HttpServletRequest request )
127     {
128         List<Style> listStyles = (List<Style>) StyleHome.getStylesList( );
129 
130         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
131         String strAscSort = null;
132 
133         if ( strSortedAttributeName != null )
134         {
135             strAscSort = request.getParameter( Parameters.SORTED_ASC );
136 
137             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
138 
139             Collections.sort( listStyles, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
140         }
141 
142         int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLES_PER_PAGE, 10 );
143         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
144         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
145 
146         String strURL = getHomeUrl( request );
147 
148         if ( strSortedAttributeName != null )
149         {
150             strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
151         }
152 
153         if ( strAscSort != null )
154         {
155             strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
156         }
157 
158         LocalizedPaginator<Style> paginator = new LocalizedPaginator<>( listStyles, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
159                 _strCurrentPageIndex, getLocale( ) );
160 
161         Map<String, Object> model = new HashMap<>( );
162         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
163         model.put( MARK_PAGINATOR, paginator );
164         model.put( MARK_STYLE_LIST, paginator.getPageItems( ) );
165 
166         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLES, getLocale( ), model );
167 
168         return getAdminPage( template.getHtml( ) );
169     }
170 
171     /**
172      * Returns the create form of a new style
173      * 
174      * @param request
175      *            The http request
176      * @return The html code for the create form of a new style
177      */
178     public String getCreateStyle( HttpServletRequest request )
179     {
180         Map<String, Object> model = new HashMap<>( );
181         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
182         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
183         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_STYLE ) );
184 
185         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLE, getLocale( ), model );
186 
187         return getAdminPage( template.getHtml( ) );
188     }
189 
190     /**
191      * Processes the creation form of a new style by recovering the parameters in the http request
192      * 
193      * @param request
194      *            the http request
195      * @return The Jsp URL of the process result
196      * @throws AccessDeniedException
197      *             If the security token is invalid
198      */
199     public String doCreateStyle( HttpServletRequest request ) throws AccessDeniedException
200     {
201         String strId = request.getParameter( Parameters.STYLE_ID );
202 
203         // Mandatory fields
204         if ( request.getParameter( Parameters.STYLE_ID ).equals( "" ) || request.getParameter( Parameters.STYLE_NAME ).equals( "" ) )
205         {
206             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
207         }
208 
209         int nId;
210 
211         try
212         {
213             nId = Integer.parseInt( strId );
214         }
215         catch( NumberFormatException nb )
216         {
217             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID, AdminMessage.TYPE_STOP );
218         }
219 
220         Style styleExisting = StyleHome.findByPrimaryKey( nId );
221 
222         if ( styleExisting != null )
223         {
224             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
225         }
226 
227         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
228 
229         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET ) )
230         {
231             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
232         }
233 
234         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_STYLE ) )
235         {
236             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
237         }
238 
239         // The style doesn't exist in the database, we can create it
240         Styleal/business/style/Style.html#Style">Style style = new Style( );
241         style.setId( nId );
242         style.setDescription( request.getParameter( Parameters.STYLE_NAME ) );
243         style.setPortalComponentId( nPortalComponentId );
244 
245         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
246         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
247         style.setPortletTypeId( strPortletTypeId );
248         StyleHome.create( style );
249 
250         return getHomeUrl( request );
251     }
252 
253     /**
254      * Returns the form to update a style whose identifer is stored in the http request
255      * 
256      * @param request
257      *            The http request
258      * @return The html code
259      */
260     public String getModifyStyle( HttpServletRequest request )
261     {
262         String strIdStyles = request.getParameter( Parameters.STYLE_ID );
263         int nStyleId = Integer.parseInt( strIdStyles );
264 
265         Map<String, Object> model = new HashMap<>( );
266         model.put( MARK_STYLE, StyleHome.findByPrimaryKey( nStyleId ) );
267         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
268         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
269         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_STYLE ) );
270 
271         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLE, getLocale( ), model );
272 
273         return getAdminPage( template.getHtml( ) );
274     }
275 
276     /**
277      * Processes the updating form of a style whose new parameters are stored in the http request
278      * 
279      * @param request
280      *            The http request
281      * @return The Jsp URL of the process result
282      * @throws AccessDeniedException
283      *             if the security token is invalid
284      */
285     public String doModifyStyle( HttpServletRequest request ) throws AccessDeniedException
286     {
287         int nStyleId = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );
288 
289         // the portlet type can be not present the request if the portal component is not a portlet
290         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
291         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
292 
293         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
294         String strStyleDescription = request.getParameter( Parameters.STYLE_NAME );
295 
296         if ( strStyleDescription.trim( ).equals( "" ) )
297         {
298             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
299         }
300 
301         Style style = StyleHome.findByPrimaryKey( nStyleId );
302         int nPortalComponentOld = style.getPortalComponentId( );
303 
304         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET )
305                 && ( nPortalComponentId != nPortalComponentOld ) )
306         {
307             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
308         }
309         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_STYLE ) )
310         {
311             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
312         }
313 
314         style.setPortletTypeId( strPortletTypeId );
315         style.setPortalComponentId( nPortalComponentId );
316         style.setDescription( strStyleDescription );
317         StyleHome.update( style );
318 
319         return getHomeUrl( request );
320     }
321 
322     /**
323      * Returns the confirm of removing the style whose identifier is in the http request
324      *
325      * @param request
326      *            The Http request
327      * @return the html code for the remove confirmation page
328      */
329     public String getConfirmRemoveStyle( HttpServletRequest request )
330     {
331         String strId = request.getParameter( Parameters.STYLE_ID );
332         int nId = Integer.parseInt( strId );
333         Collection<PortletImpl> listPortlets = PortletHome.getPortletListByStyle( nId );
334         Collection<StyleSheet> listStyleSheets = StyleHome.getStyleSheetList( nId );
335 
336         if ( CollectionUtils.isNotEmpty( listPortlets ) )
337         {
338             return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_DELETE_STYLE_PORTLETS, AdminMessage.TYPE_STOP );
339         }
340 
341         if ( CollectionUtils.isNotEmpty( listStyleSheets ) )
342         {
343             for ( StyleSheet styleSheet : listStyleSheets )
344             {
345                 Object [ ] args = {
346                         styleSheet.getDescription( )
347                 };
348 
349                 Map<String, Object> parameters = new HashMap<>( );
350                 parameters.put( Parameters.STYLESHEET_ID, Integer.toString( styleSheet.getId( ) ) );
351                 parameters.put( Parameters.STYLE_ID, Integer.toString( styleSheet.getStyleId( ) ) );
352                 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLESHEET ) );
353                 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args, null, JSP_DO_REMOVE_STYLESHEET, null,
354                         AdminMessage.TYPE_CONFIRMATION, parameters );
355             }
356         }
357 
358         Map<String, String> parameters = new HashMap<>( );
359         parameters.put( Parameters.STYLE_ID, Integer.toString( nId ) );
360         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLE ) );
361 
362         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLE, JSP_DO_REMOVE_STYLE, AdminMessage.TYPE_CONFIRMATION, parameters );
363     }
364 
365     /**
366      * Processes the deletion of a style
367      * 
368      * @param request
369      *            the http request
370      * @return The Jsp URL of the process result
371      * @throws AccessDeniedException
372      *             if the security token is invalid
373      */
374     public String doRemoveStyle( HttpServletRequest request ) throws AccessDeniedException
375     {
376         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_DO_REMOVE_STYLE ) )
377         {
378             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
379         }
380         String strId = request.getParameter( Parameters.STYLE_ID );
381         int nId = Integer.parseInt( strId );
382         StyleHome.remove( nId );
383 
384         return getHomeUrl( request );
385     }
386 }