View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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     private static final String JSP_MANAGE_STYLESHEETS = "jsp/admin/style/ManageStyles.jsp";
108 
109     // Message keys
110     private static final String MESSAGE_CANT_DELETE_STYLE_PORTLETS = "portal.style.message.cannotDeleteStylePorlets";
111 
112     private static final String MESSAGE_CONFIRM_DELETE_STYLE = "portal.style.message.confirmDeleteStyle";
113     private static final String MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID = "portal.style.message.createStyle.InvalidIdFormat";
114     private static final String MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS = "portal.style.message.createStyle.idAlreadyExists";
115     private static final String MESSAGE_CREATE_STYLE_COMPONENT_EXISTS = "portal.style.message.createStyle.componentHasAlreadyAStyle";
116     private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
117     private int _nItemsPerPage;
118     private String _strCurrentPageIndex;
119 
120     /**
121      * Displays the styles list
122      * 
123      * @param request
124      *            The HTTP request
125      * @return the html code for displaying the styles list
126      */
127     public String getStylesManagement( HttpServletRequest request )
128     {
129         List<Style> listStyles = (List<Style>) StyleHome.getStylesList( );
130 
131         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
132         String strAscSort = null;
133 
134         if ( strSortedAttributeName != null )
135         {
136             strAscSort = request.getParameter( Parameters.SORTED_ASC );
137 
138             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
139 
140             Collections.sort( listStyles, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
141         }
142 
143         int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLES_PER_PAGE, 10 );
144         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
145         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
146 
147         String strURL = getHomeUrl( request );
148 
149         if ( strSortedAttributeName != null )
150         {
151             strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
152         }
153 
154         if ( strAscSort != null )
155         {
156             strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
157         }
158 
159         LocalizedPaginator<Style> paginator = new LocalizedPaginator<>( listStyles, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
160                 _strCurrentPageIndex, getLocale( ) );
161 
162         Map<String, Object> model = new HashMap<>( );
163         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
164         model.put( MARK_PAGINATOR, paginator );
165         model.put( MARK_STYLE_LIST, paginator.getPageItems( ) );
166 
167         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLES, getLocale( ), model );
168 
169         return getAdminPage( template.getHtml( ) );
170     }
171 
172     /**
173      * Returns the create form of a new style
174      * 
175      * @param request
176      *            The http request
177      * @return The html code for the create form of a new style
178      */
179     public String getCreateStyle( HttpServletRequest request )
180     {
181         Map<String, Object> model = new HashMap<>( );
182         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
183         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
184         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_STYLE ) );
185 
186         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLE, getLocale( ), model );
187 
188         return getAdminPage( template.getHtml( ) );
189     }
190 
191     /**
192      * Processes the creation form of a new style by recovering the parameters in the http request
193      * 
194      * @param request
195      *            the http request
196      * @return The Jsp URL of the process result
197      * @throws AccessDeniedException
198      *             If the security token is invalid
199      */
200     public String doCreateStyle( HttpServletRequest request ) throws AccessDeniedException
201     {
202         String strId = request.getParameter( Parameters.STYLE_ID );
203 
204         // Mandatory fields
205         if ( request.getParameter( Parameters.STYLE_ID ).equals( "" ) || request.getParameter( Parameters.STYLE_NAME ).equals( "" ) )
206         {
207             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
208         }
209 
210         int nId;
211 
212         try
213         {
214             nId = Integer.parseInt( strId );
215         }
216         catch( NumberFormatException nb )
217         {
218             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID, AdminMessage.TYPE_STOP );
219         }
220 
221         Style styleExisting = StyleHome.findByPrimaryKey( nId );
222 
223         if ( styleExisting != null )
224         {
225             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
226         }
227 
228         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
229 
230         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET ) )
231         {
232             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
233         }
234 
235         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_STYLE ) )
236         {
237             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
238         }
239 
240         // The style doesn't exist in the database, we can create it
241         Styleal/business/style/Style.html#Style">Style style = new Style( );
242         style.setId( nId );
243         style.setDescription( request.getParameter( Parameters.STYLE_NAME ) );
244         style.setPortalComponentId( nPortalComponentId );
245 
246         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
247         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
248         style.setPortletTypeId( strPortletTypeId );
249         StyleHome.create( style );
250 
251         return getHomeUrl( request );
252     }
253 
254     /**
255      * Returns the form to update a style whose identifer is stored in the http request
256      * 
257      * @param request
258      *            The http request
259      * @return The html code
260      */
261     public String getModifyStyle( HttpServletRequest request )
262     {
263         String strIdStyles = request.getParameter( Parameters.STYLE_ID );
264         int nStyleId = Integer.parseInt( strIdStyles );
265 
266         Map<String, Object> model = new HashMap<>( );
267         model.put( MARK_STYLE, StyleHome.findByPrimaryKey( nStyleId ) );
268         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
269         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
270         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_STYLE ) );
271 
272         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLE, getLocale( ), model );
273 
274         return getAdminPage( template.getHtml( ) );
275     }
276 
277     /**
278      * Processes the updating form of a style whose new parameters are stored in the http request
279      * 
280      * @param request
281      *            The http request
282      * @return The Jsp URL of the process result
283      * @throws AccessDeniedException
284      *             if the security token is invalid
285      */
286     public String doModifyStyle( HttpServletRequest request ) throws AccessDeniedException
287     {
288         int nStyleId = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );
289 
290         // the portlet type can be not present the request if the portal component is not a portlet
291         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
292         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
293 
294         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
295         String strStyleDescription = request.getParameter( Parameters.STYLE_NAME );
296 
297         if ( strStyleDescription.trim( ).equals( "" ) )
298         {
299             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
300         }
301 
302         Style style = StyleHome.findByPrimaryKey( nStyleId );
303         int nPortalComponentOld = style.getPortalComponentId( );
304 
305         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET )
306                 && ( nPortalComponentId != nPortalComponentOld ) )
307         {
308             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
309         }
310         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_STYLE ) )
311         {
312             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
313         }
314 
315         style.setPortletTypeId( strPortletTypeId );
316         style.setPortalComponentId( nPortalComponentId );
317         style.setDescription( strStyleDescription );
318         StyleHome.update( style );
319 
320         return getHomeUrl( request );
321     }
322 
323     /**
324      * Returns the confirm of removing the style whose identifier is in the http request
325      *
326      * @param request
327      *            The Http request
328      * @return the html code for the remove confirmation page
329      */
330     public String getConfirmRemoveStyle( HttpServletRequest request )
331     {
332         String strId = request.getParameter( Parameters.STYLE_ID );
333         int nId = Integer.parseInt( strId );
334         Collection<PortletImpl> listPortlets = PortletHome.getPortletListByStyle( nId );
335         Collection<StyleSheet> listStyleSheets = StyleHome.getStyleSheetList( nId );
336 
337         if ( CollectionUtils.isNotEmpty( listPortlets ) )
338         {
339             return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_DELETE_STYLE_PORTLETS, JSP_MANAGE_STYLESHEETS, AdminMessage.TYPE_STOP );
340         }
341 
342         if ( CollectionUtils.isNotEmpty( listStyleSheets ) )
343         {
344             for ( StyleSheet styleSheet : listStyleSheets )
345             {
346                 Object [ ] args = {
347                         styleSheet.getDescription( )
348                 };
349 
350                 Map<String, Object> parameters = new HashMap<>( );
351                 parameters.put( Parameters.STYLESHEET_ID, Integer.toString( styleSheet.getId( ) ) );
352                 parameters.put( Parameters.STYLE_ID, Integer.toString( styleSheet.getStyleId( ) ) );
353                 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLESHEET ) );
354                 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args, null, JSP_DO_REMOVE_STYLESHEET, null,
355                         AdminMessage.TYPE_CONFIRMATION, parameters, JSP_MANAGE_STYLESHEETS );
356             }
357         }
358 
359         Map<String, Object> parameters = new HashMap<>( );
360         parameters.put( Parameters.STYLE_ID, Integer.toString( nId ) );
361         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLE ) );
362 
363         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLE, null, null, JSP_DO_REMOVE_STYLE, null, AdminMessage.TYPE_CONFIRMATION, parameters, JSP_MANAGE_STYLESHEETS );
364     }
365 
366     /**
367      * Processes the deletion of a style
368      * 
369      * @param request
370      *            the http request
371      * @return The Jsp URL of the process result
372      * @throws AccessDeniedException
373      *             if the security token is invalid
374      */
375     public String doRemoveStyle( HttpServletRequest request ) throws AccessDeniedException
376     {
377         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_DO_REMOVE_STYLE ) )
378         {
379             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
380         }
381         String strId = request.getParameter( Parameters.STYLE_ID );
382         int nId = Integer.parseInt( strId );
383         StyleHome.remove( nId );
384 
385         return getHomeUrl( request );
386     }
387 }