StylesJspBean.java

  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. import java.util.Collection;
  36. import java.util.Collections;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;

  40. import javax.servlet.http.HttpServletRequest;

  41. import org.apache.commons.collections.CollectionUtils;

  42. import fr.paris.lutece.portal.business.portlet.PortletHome;
  43. import fr.paris.lutece.portal.business.portlet.PortletImpl;
  44. import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
  45. import fr.paris.lutece.portal.business.style.Style;
  46. import fr.paris.lutece.portal.business.style.StyleHome;
  47. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  48. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  49. import fr.paris.lutece.portal.service.message.AdminMessage;
  50. import fr.paris.lutece.portal.service.message.AdminMessageService;
  51. import fr.paris.lutece.portal.service.security.SecurityTokenService;
  52. import fr.paris.lutece.portal.service.template.AppTemplateService;
  53. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  54. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  55. import fr.paris.lutece.portal.web.constants.Messages;
  56. import fr.paris.lutece.portal.web.constants.Parameters;
  57. import fr.paris.lutece.portal.web.util.LocalizedPaginator;
  58. import fr.paris.lutece.util.html.AbstractPaginator;
  59. import fr.paris.lutece.util.html.HtmlTemplate;
  60. import fr.paris.lutece.util.sort.AttributeComparator;

  61. /**
  62.  * This class provides the user interface to manage Styles features
  63.  */
  64. public class StylesJspBean extends AdminFeaturesPageJspBean
  65. {
  66.     // ////////////////////////////////////////////////////////////////////////////////
  67.     // Constants

  68.     // Right
  69.     /**
  70.      * Right to manage styles
  71.      */
  72.     public static final String RIGHT_MANAGE_STYLE = "CORE_STYLES_MANAGEMENT";

  73.     /**
  74.      * Serial version UID
  75.      */
  76.     private static final long serialVersionUID = 7138319350433775587L;

  77.     // Markers
  78.     private static final String MARK_STYLE_LIST = "style_list";
  79.     private static final String MARK_PORTLET_TYPE_LIST = "portlet_type_list";
  80.     private static final String MARK_PORTAL_COMPONENT_LIST = "portal_component_list";
  81.     private static final String MARK_STYLE = "style";
  82.     private static final String MARK_PAGINATOR = "paginator";
  83.     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";

  84.     // Properties
  85.     private static final String PROPERTY_STYLES_PER_PAGE = "paginator.style.itemsPerPage";

  86.     // Templates files path
  87.     private static final String TEMPLATE_MANAGE_STYLES = "admin/style/manage_styles.html";
  88.     private static final String TEMPLATE_CREATE_STYLE = "admin/style/create_style.html";
  89.     private static final String TEMPLATE_MODIFY_STYLE = "admin/style/modify_style.html";

  90.     // Portal Component definition
  91.     private static final int PORTAL_COMPONENT_ID_PORTLET = 0;

  92.     // Jsp Definition
  93.     private static final String JSP_DO_REMOVE_STYLE = "jsp/admin/style/DoRemoveStyle.jsp";
  94.     private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";

  95.     // Message keys
  96.     private static final String MESSAGE_CANT_DELETE_STYLE_PORTLETS = "portal.style.message.cannotDeleteStylePorlets";

  97.     private static final String MESSAGE_CONFIRM_DELETE_STYLE = "portal.style.message.confirmDeleteStyle";
  98.     private static final String MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID = "portal.style.message.createStyle.InvalidIdFormat";
  99.     private static final String MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS = "portal.style.message.createStyle.idAlreadyExists";
  100.     private static final String MESSAGE_CREATE_STYLE_COMPONENT_EXISTS = "portal.style.message.createStyle.componentHasAlreadyAStyle";
  101.     private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
  102.     private int _nItemsPerPage;
  103.     private String _strCurrentPageIndex;

  104.     /**
  105.      * Displays the styles list
  106.      *
  107.      * @param request
  108.      *            The HTTP request
  109.      * @return the html code for displaying the styles list
  110.      */
  111.     public String getStylesManagement( HttpServletRequest request )
  112.     {
  113.         List<Style> listStyles = (List<Style>) StyleHome.getStylesList( );

  114.         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
  115.         String strAscSort = null;

  116.         if ( strSortedAttributeName != null )
  117.         {
  118.             strAscSort = request.getParameter( Parameters.SORTED_ASC );

  119.             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );

  120.             Collections.sort( listStyles, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
  121.         }

  122.         int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLES_PER_PAGE, 10 );
  123.         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
  124.         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );

  125.         String strURL = getHomeUrl( request );

  126.         if ( strSortedAttributeName != null )
  127.         {
  128.             strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
  129.         }

  130.         if ( strAscSort != null )
  131.         {
  132.             strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
  133.         }

  134.         LocalizedPaginator<Style> paginator = new LocalizedPaginator<>( listStyles, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
  135.                 _strCurrentPageIndex, getLocale( ) );

  136.         Map<String, Object> model = new HashMap<>( );
  137.         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
  138.         model.put( MARK_PAGINATOR, paginator );
  139.         model.put( MARK_STYLE_LIST, paginator.getPageItems( ) );

  140.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLES, getLocale( ), model );

  141.         return getAdminPage( template.getHtml( ) );
  142.     }

  143.     /**
  144.      * Returns the create form of a new style
  145.      *
  146.      * @param request
  147.      *            The http request
  148.      * @return The html code for the create form of a new style
  149.      */
  150.     public String getCreateStyle( HttpServletRequest request )
  151.     {
  152.         Map<String, Object> model = new HashMap<>( );
  153.         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
  154.         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
  155.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_STYLE ) );

  156.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLE, getLocale( ), model );

  157.         return getAdminPage( template.getHtml( ) );
  158.     }

  159.     /**
  160.      * Processes the creation form of a new style by recovering the parameters in the http request
  161.      *
  162.      * @param request
  163.      *            the http request
  164.      * @return The Jsp URL of the process result
  165.      * @throws AccessDeniedException
  166.      *             If the security token is invalid
  167.      */
  168.     public String doCreateStyle( HttpServletRequest request ) throws AccessDeniedException
  169.     {
  170.         String strId = request.getParameter( Parameters.STYLE_ID );

  171.         // Mandatory fields
  172.         if ( request.getParameter( Parameters.STYLE_ID ).equals( "" ) || request.getParameter( Parameters.STYLE_NAME ).equals( "" ) )
  173.         {
  174.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  175.         }

  176.         int nId;

  177.         try
  178.         {
  179.             nId = Integer.parseInt( strId );
  180.         }
  181.         catch( NumberFormatException nb )
  182.         {
  183.             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID, AdminMessage.TYPE_STOP );
  184.         }

  185.         Style styleExisting = StyleHome.findByPrimaryKey( nId );

  186.         if ( styleExisting != null )
  187.         {
  188.             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
  189.         }

  190.         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );

  191.         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET ) )
  192.         {
  193.             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
  194.         }

  195.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_STYLE ) )
  196.         {
  197.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  198.         }

  199.         // The style doesn't exist in the database, we can create it
  200.         Style style = new Style( );
  201.         style.setId( nId );
  202.         style.setDescription( request.getParameter( Parameters.STYLE_NAME ) );
  203.         style.setPortalComponentId( nPortalComponentId );

  204.         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
  205.         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
  206.         style.setPortletTypeId( strPortletTypeId );
  207.         StyleHome.create( style );

  208.         return getHomeUrl( request );
  209.     }

  210.     /**
  211.      * Returns the form to update a style whose identifer is stored in the http request
  212.      *
  213.      * @param request
  214.      *            The http request
  215.      * @return The html code
  216.      */
  217.     public String getModifyStyle( HttpServletRequest request )
  218.     {
  219.         String strIdStyles = request.getParameter( Parameters.STYLE_ID );
  220.         int nStyleId = Integer.parseInt( strIdStyles );

  221.         Map<String, Object> model = new HashMap<>( );
  222.         model.put( MARK_STYLE, StyleHome.findByPrimaryKey( nStyleId ) );
  223.         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
  224.         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
  225.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_STYLE ) );

  226.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLE, getLocale( ), model );

  227.         return getAdminPage( template.getHtml( ) );
  228.     }

  229.     /**
  230.      * Processes the updating form of a style whose new parameters are stored in the http request
  231.      *
  232.      * @param request
  233.      *            The http request
  234.      * @return The Jsp URL of the process result
  235.      * @throws AccessDeniedException
  236.      *             if the security token is invalid
  237.      */
  238.     public String doModifyStyle( HttpServletRequest request ) throws AccessDeniedException
  239.     {
  240.         int nStyleId = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );

  241.         // the portlet type can be not present the request if the portal component is not a portlet
  242.         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
  243.         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";

  244.         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
  245.         String strStyleDescription = request.getParameter( Parameters.STYLE_NAME );

  246.         if ( strStyleDescription.trim( ).equals( "" ) )
  247.         {
  248.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  249.         }

  250.         Style style = StyleHome.findByPrimaryKey( nStyleId );
  251.         int nPortalComponentOld = style.getPortalComponentId( );

  252.         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET )
  253.                 && ( nPortalComponentId != nPortalComponentOld ) )
  254.         {
  255.             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
  256.         }
  257.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_STYLE ) )
  258.         {
  259.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  260.         }

  261.         style.setPortletTypeId( strPortletTypeId );
  262.         style.setPortalComponentId( nPortalComponentId );
  263.         style.setDescription( strStyleDescription );
  264.         StyleHome.update( style );

  265.         return getHomeUrl( request );
  266.     }

  267.     /**
  268.      * Returns the confirm of removing the style whose identifier is in the http request
  269.      *
  270.      * @param request
  271.      *            The Http request
  272.      * @return the html code for the remove confirmation page
  273.      */
  274.     public String getConfirmRemoveStyle( HttpServletRequest request )
  275.     {
  276.         String strId = request.getParameter( Parameters.STYLE_ID );
  277.         int nId = Integer.parseInt( strId );
  278.         Collection<PortletImpl> listPortlets = PortletHome.getPortletListByStyle( nId );
  279.         Collection<StyleSheet> listStyleSheets = StyleHome.getStyleSheetList( nId );

  280.         if ( CollectionUtils.isNotEmpty( listPortlets ) )
  281.         {
  282.             return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_DELETE_STYLE_PORTLETS, AdminMessage.TYPE_STOP );
  283.         }

  284.         if ( CollectionUtils.isNotEmpty( listStyleSheets ) )
  285.         {
  286.             for ( StyleSheet styleSheet : listStyleSheets )
  287.             {
  288.                 Object [ ] args = {
  289.                         styleSheet.getDescription( )
  290.                 };

  291.                 Map<String, Object> parameters = new HashMap<>( );
  292.                 parameters.put( Parameters.STYLESHEET_ID, Integer.toString( styleSheet.getId( ) ) );
  293.                 parameters.put( Parameters.STYLE_ID, Integer.toString( styleSheet.getStyleId( ) ) );
  294.                 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLESHEET ) );
  295.                 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args, null, JSP_DO_REMOVE_STYLESHEET, null,
  296.                         AdminMessage.TYPE_CONFIRMATION, parameters );
  297.             }
  298.         }

  299.         Map<String, String> parameters = new HashMap<>( );
  300.         parameters.put( Parameters.STYLE_ID, Integer.toString( nId ) );
  301.         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLE ) );

  302.         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLE, JSP_DO_REMOVE_STYLE, AdminMessage.TYPE_CONFIRMATION, parameters );
  303.     }

  304.     /**
  305.      * Processes the deletion of a style
  306.      *
  307.      * @param request
  308.      *            the http request
  309.      * @return The Jsp URL of the process result
  310.      * @throws AccessDeniedException
  311.      *             if the security token is invalid
  312.      */
  313.     public String doRemoveStyle( HttpServletRequest request ) throws AccessDeniedException
  314.     {
  315.         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_DO_REMOVE_STYLE ) )
  316.         {
  317.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  318.         }
  319.         String strId = request.getParameter( Parameters.STYLE_ID );
  320.         int nId = Integer.parseInt( strId );
  321.         StyleHome.remove( nId );

  322.         return getHomeUrl( request );
  323.     }
  324. }