PortletJspBean.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.portlet;

  35. import java.util.HashMap;
  36. import java.util.Locale;
  37. import java.util.Map;

  38. import javax.servlet.http.HttpServletRequest;

  39. import fr.paris.lutece.portal.business.page.PageHome;
  40. import fr.paris.lutece.portal.business.portlet.Portlet;
  41. import fr.paris.lutece.portal.business.portlet.PortletHome;
  42. import fr.paris.lutece.portal.business.portlet.PortletType;
  43. import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
  44. import fr.paris.lutece.portal.business.role.RoleHome;
  45. import fr.paris.lutece.portal.service.message.AdminMessage;
  46. import fr.paris.lutece.portal.service.message.AdminMessageService;
  47. import fr.paris.lutece.portal.service.template.AppTemplateService;
  48. import fr.paris.lutece.portal.service.util.AppLogService;
  49. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  50. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  51. import fr.paris.lutece.portal.web.constants.Messages;
  52. import fr.paris.lutece.portal.web.constants.Parameters;
  53. import fr.paris.lutece.util.ReferenceList;
  54. import fr.paris.lutece.util.html.HtmlTemplate;
  55. import fr.paris.lutece.util.string.StringUtil;

  56. /**
  57.  * This class represents user interface Portlet. It is the base class of all user interface portlets. It is abstract and the implementation of the interface
  58.  * PortletJspBeanInterface is compulsary.
  59.  */
  60. public abstract class PortletJspBean extends AdminFeaturesPageJspBean
  61. {
  62.     // //////////////////////////////////////////////////////////////////////////
  63.     // Constants
  64.     public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";

  65.     // Parameters
  66.     protected static final String PARAMETER_PAGE_ID = "page_id";
  67.     protected static final String PARAMETER_PORTLET_ID = "portlet_id";
  68.     protected static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
  69.     private static final long serialVersionUID = -3546292252642160812L;

  70.     // Markers
  71.     private static final String MARK_PORTLET = "portlet";
  72.     private static final String MARK_PORTLET_TYPE = "portletType";
  73.     private static final String MARK_PORTLET_PAGE_ID = "portlet_page_id";
  74.     private static final String MARK_PORTLET_ORDER_COMBO = "portlet_order_combo";
  75.     private static final String MARK_PORTLET_COLUMNS_COMBO = "portlet_columns_combo";
  76.     private static final String MARK_PORTLET_STYLES_COMBO = "portlet_style_combo";
  77.     private static final String MARK_PORTLET_ROLES_COMBO = "portlet_role_combo";
  78.     private static final String MARK_SMALL_CHECKED = "small_checked";
  79.     private static final String MARK_NORMAL_CHECKED = "normal_checked";
  80.     private static final String MARK_LARGE_CHECKED = "large_checked";
  81.     private static final String MARK_XLARGE_CHECKED = "xlarge_checked";
  82.     private static final String VALUE_CHECKED = "checked=\"checked\"";
  83.     private static final String VALUE_UNCHECKED = "";

  84.     // Templates
  85.     private static final String TEMPLATE_CREATE_PORTLET = "admin/portlet/create_portlet.html";
  86.     private static final String TEMPLATE_MODIFY_PORTLET = "admin/portlet/modify_portlet.html";

  87.     // Properties
  88.     private static final String PROPERTY_LIST_ORDER_MAX = "list.order.max";
  89.     private static final String PROPERTY_COLUMN_NUM_MAX = "nb.columns";

  90.     // Messages
  91.     private static final String MESSAGE_INVALID_PAGE_ID = "portal.site.message.pageIdInvalid";

  92.     // Jsp
  93.     private static final String JSP_ADMIN_SITE = "../../site/AdminSite.jsp";

  94.     /**
  95.      * Displays the portlet's creation form
  96.      *
  97.      * @param request
  98.      *            The http request
  99.      * @return The html code for displaying the creation form
  100.      */
  101.     public abstract String getCreate( HttpServletRequest request );

  102.     /**
  103.      * Processes portlet's creation
  104.      *
  105.      * @param request
  106.      *            The http request
  107.      * @return The Management jsp url
  108.      */
  109.     public abstract String doCreate( HttpServletRequest request );

  110.     /**
  111.      * Displays the portlet's modification form
  112.      *
  113.      * @param request
  114.      *            The http request
  115.      * @return The html code for displaying the modification form
  116.      */
  117.     public abstract String getModify( HttpServletRequest request );

  118.     /**
  119.      * Processes portlet's modification
  120.      *
  121.      * @param request
  122.      *            The http request
  123.      * @return The Management jsp url
  124.      */
  125.     public abstract String doModify( HttpServletRequest request );

  126.     // //////////////////////////////////////////////////////////////////////////
  127.     // Management of the combos common to all portlets
  128.     // Order combo

  129.     /**
  130.      * Returns a list of orders
  131.      *
  132.      * @return the list of orders in form of ReferenceList
  133.      */
  134.     private ReferenceList getOrdersList( )
  135.     {
  136.         ReferenceList list = new ReferenceList( );
  137.         int nOrderMax = AppPropertiesService.getPropertyInt( PROPERTY_LIST_ORDER_MAX, 15 );

  138.         for ( int i = 1; i <= nOrderMax; i++ )
  139.         {
  140.             list.addItem( i, String.valueOf( i ) );
  141.         }

  142.         return list;
  143.     }

  144.     /**
  145.      * Returns the list of the columns of the page where the portlet can be positioned
  146.      *
  147.      * @return the list of the page columns in form of ReferenceList
  148.      */
  149.     private ReferenceList getColumnsList( )
  150.     {
  151.         ReferenceList list = new ReferenceList( );
  152.         int nColumnNumMax = AppPropertiesService.getPropertyInt( PROPERTY_COLUMN_NUM_MAX, 1 );

  153.         for ( int i = 1; i <= nColumnNumMax; i++ )
  154.         {
  155.             list.addItem( i, String.valueOf( i ) );
  156.         }

  157.         return list;
  158.     }

  159.     /**
  160.      * Recovers the common attributes of the portlet
  161.      *
  162.      * @param request
  163.      *            the http request
  164.      * @param portlet
  165.      *            The instance of the portlet
  166.      * @return An error Key if error, otherwise null.
  167.      */
  168.     protected String setPortletCommonData( HttpServletRequest request, Portlet portlet )
  169.     {
  170.         String strErrorKey = null;

  171.         // get portlet attributes
  172.         String strName = request.getParameter( Parameters.PORTLET_NAME );
  173.         String strStyleId = request.getParameter( Parameters.STYLE );
  174.         String strColumn = request.getParameter( Parameters.COLUMN );
  175.         String strOrder = request.getParameter( Parameters.ORDER );
  176.         String strAcceptAlias = request.getParameter( Parameters.ACCEPT_ALIAS );
  177.         String strAcceptPortletTitle = request.getParameter( Parameters.DISPLAY_PORTLET_TITLE );
  178.         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE_ID );
  179.         String strRole = request.getParameter( Parameters.ROLE );
  180.         String strDisplaySmall = request.getParameter( Parameters.DISPLAY_ON_SMALL_DEVICE );
  181.         String strDisplayNormal = request.getParameter( Parameters.DISPLAY_ON_NORMAL_DEVICE );
  182.         String strDisplayLarge = request.getParameter( Parameters.DISPLAY_ON_LARGE_DEVICE );
  183.         String strDisplayXLarge = request.getParameter( Parameters.DISPLAY_ON_XLARGE_DEVICE );

  184.         strName = strName.replaceAll( "\"", "" );

  185.         // Check Mandatory fields
  186.         if ( StringUtil.isAnyEmpty( strName, strOrder, strColumn, strAcceptAlias, strAcceptPortletTitle ) )
  187.         {
  188.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  189.         }

  190.         // style id is not mandatory if the content is not generated from XML and XSL
  191.         if ( portlet.isContentGeneratedByXmlAndXsl( ) && ( strStyleId == null || strStyleId.trim( ).equals( "" ) ) )
  192.         {
  193.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  194.         }

  195.         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
  196.         int nPageId;

  197.         // Test format of the id and the existence of the page
  198.         try
  199.         {
  200.             nPageId = Integer.parseInt( strPageId );

  201.             if ( !PageHome.checkPageExist( nPageId ) )
  202.             {
  203.                 return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PAGE_ID, AdminMessage.TYPE_STOP );
  204.             }
  205.         }
  206.         catch( NumberFormatException e )
  207.         {
  208.             AppLogService.error( e.getMessage( ), e );

  209.             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PAGE_ID, AdminMessage.TYPE_STOP );
  210.         }

  211.         int nOrder = Integer.parseInt( strOrder );
  212.         int nColumn = Integer.parseInt( strColumn );
  213.         int nAcceptAlias = Integer.parseInt( strAcceptAlias );
  214.         int nAcceptPortletTitle = Integer.parseInt( strAcceptPortletTitle );

  215.         int nStyleId = portlet.isContentGeneratedByXmlAndXsl( ) ? Integer.parseInt( strStyleId ) : 0;

  216.         int nDeviceDisplayFlags = 0;

  217.         if ( strDisplaySmall != null )
  218.         {
  219.             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE;
  220.         }

  221.         if ( strDisplayNormal != null )
  222.         {
  223.             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE;
  224.         }

  225.         if ( strDisplayLarge != null )
  226.         {
  227.             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE;
  228.         }

  229.         if ( strDisplayXLarge != null )
  230.         {
  231.             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE;
  232.         }

  233.         portlet.setName( strName );
  234.         portlet.setOrder( nOrder );
  235.         portlet.setColumn( nColumn );
  236.         portlet.setStyleId( nStyleId );
  237.         portlet.setPageId( nPageId );
  238.         portlet.setAcceptAlias( nAcceptAlias );
  239.         portlet.setDisplayPortletTitle( nAcceptPortletTitle );
  240.         portlet.setPortletTypeId( strPortletTypeId );
  241.         portlet.setRole( strRole );
  242.         portlet.setDeviceDisplayFlags( nDeviceDisplayFlags );

  243.         return strErrorKey;
  244.     }

  245.     /**
  246.      * Fills templates with common values shared by portlet creation form
  247.      *
  248.      * @param strPageId
  249.      *            the page identifier
  250.      * @param strPortletTypeId
  251.      *            the Portlet type identifier
  252.      * @return the template filled
  253.      */
  254.     protected HtmlTemplate getCreateTemplate( String strPageId, String strPortletTypeId )
  255.     {
  256.         return getCreateTemplate( strPageId, strPortletTypeId, new HashMap<>( ) );
  257.     }

  258.     /**
  259.      * Fills templates with common values shared by portlet creation form
  260.      *
  261.      * @param strPageId
  262.      *            the page identifier
  263.      * @param strPortletTypeId
  264.      *            the Portlet type identifier
  265.      * @param model
  266.      *            Specific data stored in a hashtable
  267.      * @return the template filled
  268.      */
  269.     protected HtmlTemplate getCreateTemplate( String strPageId, String strPortletTypeId, Map<String, Object> model )
  270.     {
  271.         PortletType portletType = PortletTypeHome.findByPrimaryKey( strPortletTypeId );
  272.         Locale locale = getLocale( );
  273.         portletType.setLocale( locale );

  274.         model.put( MARK_PORTLET_TYPE, portletType );
  275.         model.put( MARK_PORTLET_PAGE_ID, strPageId );
  276.         model.put( MARK_PORTLET_ORDER_COMBO, getOrdersList( ) );
  277.         model.put( MARK_PORTLET_COLUMNS_COMBO, getColumnsList( ) );
  278.         model.put( MARK_PORTLET_STYLES_COMBO, PortletHome.getStylesList( strPortletTypeId ) );
  279.         model.put( MARK_PORTLET_ROLES_COMBO, RoleHome.getRolesList( getUser( ) ) );

  280.         return AppTemplateService.getTemplate( TEMPLATE_CREATE_PORTLET, locale, model );
  281.     }

  282.     /**
  283.      * Fills update template with portlet values
  284.      *
  285.      * @param portlet
  286.      *            the object to update
  287.      * @return the update template filled
  288.      */
  289.     protected HtmlTemplate getModifyTemplate( Portlet portlet )
  290.     {
  291.         return getModifyTemplate( portlet, new HashMap<>( ) );
  292.     }

  293.     /**
  294.      * Fills update template with portlet values
  295.      *
  296.      * @param portlet
  297.      *            the object to update
  298.      * @param model
  299.      *            The Data model
  300.      * @return the update template filled
  301.      */
  302.     protected HtmlTemplate getModifyTemplate( Portlet portlet, Map<String, Object> model )
  303.     {
  304.         PortletType portletType = PortletTypeHome.findByPrimaryKey( portlet.getPortletTypeId( ) );
  305.         portletType.setLocale( getLocale( ) );
  306.         model.put( MARK_PORTLET_TYPE, portletType );
  307.         model.put( MARK_PORTLET, portlet );
  308.         model.put( MARK_PORTLET_ORDER_COMBO, getOrdersList( ) );
  309.         model.put( MARK_PORTLET_COLUMNS_COMBO, getColumnsList( ) );
  310.         model.put( MARK_PORTLET_STYLES_COMBO, PortletHome.getStylesList( portlet.getPortletTypeId( ) ) );
  311.         model.put( MARK_PORTLET_ROLES_COMBO, RoleHome.getRolesList( getUser( ) ) );
  312.         putCheckBox( model, MARK_SMALL_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE ) );
  313.         putCheckBox( model, MARK_NORMAL_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE ) );
  314.         putCheckBox( model, MARK_LARGE_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE ) );
  315.         putCheckBox( model, MARK_XLARGE_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE ) );

  316.         return AppTemplateService.getTemplate( TEMPLATE_MODIFY_PORTLET, getLocale( ), model );
  317.     }

  318.     /**
  319.      * Put check box.
  320.      *
  321.      * @param model
  322.      *            the model
  323.      * @param strMarkerChecked
  324.      *            the str marker checked
  325.      * @param bChecked
  326.      *            the b checked
  327.      */
  328.     protected void putCheckBox( Map<String, Object> model, String strMarkerChecked, boolean bChecked )
  329.     {
  330.         String strChecked = ( bChecked ) ? VALUE_CHECKED : VALUE_UNCHECKED;
  331.         model.put( strMarkerChecked, strChecked );
  332.     }

  333.     /**
  334.      * Gets the page URL
  335.      *
  336.      * @param nIdPage
  337.      *            Page ID
  338.      * @return The page URL
  339.      */
  340.     protected String getPageUrl( int nIdPage )
  341.     {
  342.         return JSP_ADMIN_SITE + "?" + PARAMETER_PAGE_ID + "=" + nIdPage;
  343.     }
  344. }