Portlet.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.business.portlet;

  35. import fr.paris.lutece.portal.business.XmlContent;
  36. import fr.paris.lutece.portal.business.page.Page;
  37. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  38. import fr.paris.lutece.portal.web.l10n.LocaleService;
  39. import fr.paris.lutece.util.xml.XmlUtil;

  40. import java.sql.Timestamp;
  41. import java.util.Locale;
  42. import java.util.Map;

  43. import javax.servlet.http.HttpServletRequest;

  44. /**
  45.  * This class represents business objects Portlet. It is the base class of all portlets. It is abstract and the implementation of the interface XmlContent is
  46.  * compulsory.
  47.  */
  48. public abstract class Portlet implements XmlContent
  49. {
  50.     // //////////////////////////////////////////////////////////////////////////
  51.     // Publics variables common to all the portlets
  52.     public static final int STATUS_PUBLISHED = 0;
  53.     public static final int STATUS_UNPUBLISHED = 1;
  54.     public static final int FLAG_DISPLAY_ON_SMALL_DEVICE = 0x00000001;
  55.     public static final int FLAG_DISPLAY_ON_NORMAL_DEVICE = 0x00000010;
  56.     public static final int FLAG_DISPLAY_ON_LARGE_DEVICE = 0x00000100;
  57.     public static final int FLAG_DISPLAY_ON_XLARGE_DEVICE = 0x00001000;
  58.     private static final String VALUE_TRUE = "1";
  59.     private static final String VALUE_FALSE = "0";

  60.     // //////////////////////////////////////////////////////////////////////////
  61.     // Privates variables common to all the portlets
  62.     private static final int MODE_NORMAL = 0;
  63.     private static final int MODE_ADMIN = 1;
  64.     private int _nId;
  65.     private int _nPageId;
  66.     private int _nStyleId;
  67.     private int _nColumn;
  68.     private int _nOrder;
  69.     private int _nStatus;
  70.     private int _nAcceptAlias;
  71.     private int _nDisplayPortletTitle;
  72.     private String _strName;
  73.     private String _strPortletTypeId;
  74.     private String _strPortletTypeName;
  75.     private String _strUrlCreation;
  76.     private String _strUrlUpdate;
  77.     private String _strPluginName;
  78.     private String _strHomeClassName;
  79.     private String _strRole;
  80.     private Timestamp _dateUpdate;
  81.     private int _nDeviceFlags;

  82.     // //////////////////////////////////////////////////////////////////////////
  83.     // Accessors

  84.     /**
  85.      * Returns the identifier of this portlet.
  86.      *
  87.      * @return the portlet identifier
  88.      */
  89.     public int getId( )
  90.     {
  91.         return _nId;
  92.     }

  93.     /**
  94.      * Sets the identifier of the portlet to the specified int.
  95.      *
  96.      * @param nId
  97.      *            the new identifier
  98.      */
  99.     public void setId( int nId )
  100.     {
  101.         _nId = nId;
  102.     }

  103.     /**
  104.      * Returns the style identifier of this portlet
  105.      *
  106.      * @return the style identifier
  107.      */
  108.     public int getStyleId( )
  109.     {
  110.         return _nStyleId;
  111.     }

  112.     /**
  113.      * Sets the identifier of the portlet style with the specified int.
  114.      *
  115.      * @param nStyleId
  116.      *            the new style identifier
  117.      */
  118.     public void setStyleId( int nStyleId )
  119.     {
  120.         _nStyleId = nStyleId;
  121.     }

  122.     /**
  123.      * Returns the page identifier associated to this portlet
  124.      *
  125.      * @return the page identifier
  126.      */
  127.     public int getPageId( )
  128.     {
  129.         return _nPageId;
  130.     }

  131.     /**
  132.      * Sets the identifier of the portlet style with the specified int.
  133.      *
  134.      * @param nPageId
  135.      *            The identifier of the page
  136.      */
  137.     public void setPageId( int nPageId )
  138.     {
  139.         _nPageId = nPageId;
  140.     }

  141.     /**
  142.      * Returns the identifier of this portlet.
  143.      *
  144.      * @return the portlet identifier
  145.      */
  146.     public int getStatus( )
  147.     {
  148.         return _nStatus;
  149.     }

  150.     /**
  151.      * Sets the identifier of the portlet to the specified int.
  152.      *
  153.      * @param nStatus
  154.      *            the new status
  155.      */
  156.     public void setStatus( int nStatus )
  157.     {
  158.         _nStatus = nStatus;
  159.     }

  160.     /**
  161.      * Returns the name of this portlet
  162.      *
  163.      * @return the portlet name
  164.      */
  165.     public String getName( )
  166.     {
  167.         return _strName;
  168.     }

  169.     /**
  170.      * Sets the name of this portlet to the specified string.
  171.      *
  172.      * @param strName
  173.      *            the new name
  174.      */
  175.     public void setName( String strName )
  176.     {
  177.         _strName = strName;
  178.     }

  179.     /**
  180.      * Returns the identifier of the portlet type of this portlet which caracterizes the portlet.
  181.      *
  182.      * @return the portlet type identifier
  183.      */
  184.     public String getPortletTypeId( )
  185.     {
  186.         return _strPortletTypeId;
  187.     }

  188.     /**
  189.      * Sets the identifier of the portlet type to the specified int.
  190.      *
  191.      * @param strPortletTypeId
  192.      *            the portlet type identifier
  193.      */
  194.     public void setPortletTypeId( String strPortletTypeId )
  195.     {
  196.         _strPortletTypeId = strPortletTypeId;
  197.     }

  198.     /**
  199.      * Returns the portlet type name of this portlet
  200.      *
  201.      * @return the portlet type name
  202.      */
  203.     public String getPortletTypeName( )
  204.     {
  205.         return _strPortletTypeName;
  206.     }

  207.     /**
  208.      * Sets the name of this portlet type with the specified string.
  209.      *
  210.      * @param strPortletTypeName
  211.      *            the new portlet type name
  212.      */
  213.     public void setPortletTypeName( String strPortletTypeName )
  214.     {
  215.         _strPortletTypeName = strPortletTypeName;
  216.     }

  217.     /**
  218.      * Returns the url of the program which manages the creation of a portlet
  219.      *
  220.      * @return the url of the creation
  221.      */
  222.     public String getUrlCreation( )
  223.     {
  224.         return _strUrlCreation;
  225.     }

  226.     /**
  227.      * Sets the url of the program which creates this portlet
  228.      *
  229.      * @param strUrlCreation
  230.      *            The url of creation
  231.      */
  232.     public void setUrlCreation( String strUrlCreation )
  233.     {
  234.         _strUrlCreation = strUrlCreation;
  235.     }

  236.     /**
  237.      * Returns the url of the program which manages the update of a portlet
  238.      *
  239.      * @return the url of the program as a String
  240.      */
  241.     public String getUrlUpdate( )
  242.     {
  243.         return _strUrlUpdate;
  244.     }

  245.     /**
  246.      * Sets the url of the program which updates this portlet
  247.      *
  248.      * @param strUrlUpdate
  249.      *            The url of update
  250.      */
  251.     public void setUrlUpdate( String strUrlUpdate )
  252.     {
  253.         _strUrlUpdate = strUrlUpdate;
  254.     }

  255.     /**
  256.      * Returns the date of update of this portlet
  257.      *
  258.      * @return the update date
  259.      */
  260.     public Timestamp getDateUpdate( )
  261.     {
  262.         return _dateUpdate;
  263.     }

  264.     /**
  265.      * Sets the date of update of this portlet with the specified date.
  266.      *
  267.      * @param dateUpdate
  268.      *            the new date
  269.      */
  270.     public void setDateUpdate( Timestamp dateUpdate )
  271.     {
  272.         _dateUpdate = dateUpdate;
  273.     }

  274.     /**
  275.      * Return the number of the column of this portlet in the page
  276.      *
  277.      * @return the number of the column
  278.      */
  279.     public int getColumn( )
  280.     {
  281.         return _nColumn;
  282.     }

  283.     /**
  284.      * Sets the number of the column of this portlet in its page with the specified int.
  285.      *
  286.      * @param nColumn
  287.      *            the new number of column
  288.      */
  289.     public void setColumn( int nColumn )
  290.     {
  291.         _nColumn = nColumn;
  292.     }

  293.     /**
  294.      * Returns the order of this portlet in the page which contains it.
  295.      *
  296.      * @return the order
  297.      */
  298.     public int getOrder( )
  299.     {
  300.         return _nOrder;
  301.     }

  302.     /**
  303.      * Sets the order of this portlet in its page with the specified int.
  304.      *
  305.      * @param nType
  306.      *            the new order
  307.      */
  308.     public void setOrder( int nType )
  309.     {
  310.         _nOrder = nType;
  311.     }

  312.     /**
  313.      * Gets device display flags
  314.      *
  315.      * @return Flags
  316.      */
  317.     public int getDeviceDisplayFlags( )
  318.     {
  319.         return _nDeviceFlags;
  320.     }

  321.     /**
  322.      * Check if a flag is setted
  323.      *
  324.      * @param nFlag
  325.      *            The flag to check
  326.      * @return true if the flag is set, otherwise false
  327.      */
  328.     public boolean hasDeviceDisplayFlag( int nFlag )
  329.     {
  330.         return ( _nDeviceFlags & nFlag ) != 0;
  331.     }

  332.     /**
  333.      * Set device display flags
  334.      *
  335.      * @param nFlags
  336.      *            Flags
  337.      */
  338.     public void setDeviceDisplayFlags( int nFlags )
  339.     {
  340.         _nDeviceFlags = nFlags;
  341.     }

  342.     /**
  343.      * Returns the name of the java class which manages this type of portlet.
  344.      *
  345.      * @return the java class name
  346.      */
  347.     public String getHomeClassName( )
  348.     {
  349.         return _strHomeClassName;
  350.     }

  351.     /**
  352.      * Sets the name of the java class which manages this type of portlet with the specified string.
  353.      *
  354.      * @param strHomeClassName
  355.      *            The Home Class name
  356.      */
  357.     public void setHomeClassName( String strHomeClassName )
  358.     {
  359.         _strHomeClassName = strHomeClassName;
  360.     }

  361.     /**
  362.      * Indicates if this portlet can be modified after its creation or not.
  363.      *
  364.      * @return 1 if the portlet can be updated, 0 if not
  365.      */
  366.     public int getAcceptAlias( )
  367.     {
  368.         return _nAcceptAlias;
  369.     }

  370.     /**
  371.      * Sets the flag which indicates that this portlet can be have a title or not.
  372.      *
  373.      * @param nDisplayPortletTitle
  374.      *            The flag
  375.      */
  376.     public void setDisplayPortletTitle( int nDisplayPortletTitle )
  377.     {
  378.         _nDisplayPortletTitle = nDisplayPortletTitle;
  379.     }

  380.     /**
  381.      * Indicates if this portlet can be modified have a title or not.
  382.      *
  383.      * @return 1 if the portlet can be have a title, 0 if not
  384.      */
  385.     public int getDisplayPortletTitle( )
  386.     {
  387.         return _nDisplayPortletTitle;
  388.     }

  389.     /**
  390.      * Sets the flag which indicates that this portlet can be updated or not.
  391.      *
  392.      * @param nAcceptAlias
  393.      *            The flag
  394.      */
  395.     public void setAcceptAlias( int nAcceptAlias )
  396.     {
  397.         _nAcceptAlias = nAcceptAlias;
  398.     }

  399.     /**
  400.      * Get the plugin Name
  401.      *
  402.      * @return The pluginName
  403.      */
  404.     public String getPluginName( )
  405.     {
  406.         return _strPluginName;
  407.     }

  408.     /**
  409.      * Sets the flag which indicates that this portlet can be updated or not.
  410.      *
  411.      * @param strPluginName
  412.      *            The flag
  413.      */
  414.     public void setPluginName( String strPluginName )
  415.     {
  416.         _strPluginName = strPluginName;
  417.     }

  418.     /**
  419.      * Gets the portlet's role
  420.      *
  421.      * @return page's role as a String
  422.      * @since v2.5
  423.      */
  424.     public String getRole( )
  425.     {
  426.         _strRole = ( _strRole == null ) ? Page.ROLE_NONE : _strRole;

  427.         return _strRole;
  428.     }

  429.     /**
  430.      * Sets the portlet's role
  431.      *
  432.      * @param strRole
  433.      *            The role
  434.      * @since v2.5
  435.      */
  436.     public void setRole( String strRole )
  437.     {
  438.         _strRole = ( ( strRole == null ) || ( strRole.equals( "" ) ) ) ? Page.ROLE_NONE : strRole;
  439.     }

  440.     // //////////////////////////////////////////////////////////////////////////
  441.     // Operations

  442.     /**
  443.      * This method copies the fields of the portlet specified in this portlet.
  444.      *
  445.      * @param portlet
  446.      *            the portlet to copy
  447.      */
  448.     public void copy( Portlet portlet )
  449.     {
  450.         setId( portlet.getId( ) );
  451.         setPortletTypeId( portlet.getPortletTypeId( ) );
  452.         setPageId( portlet.getPageId( ) );
  453.         setStyleId( portlet.getStyleId( ) );
  454.         setName( portlet.getName( ) );
  455.         setPortletTypeName( portlet.getPortletTypeName( ) );
  456.         setUrlCreation( portlet.getUrlCreation( ) );
  457.         setUrlUpdate( portlet.getUrlUpdate( ) );
  458.         setDateUpdate( portlet.getDateUpdate( ) );
  459.         setColumn( portlet.getColumn( ) );
  460.         setOrder( portlet.getOrder( ) );
  461.         setAcceptAlias( portlet.getAcceptAlias( ) );
  462.         setPluginName( portlet.getPluginName( ) );
  463.         setDisplayPortletTitle( portlet.getDisplayPortletTitle( ) );
  464.         setDeviceDisplayFlags( portlet.getDeviceDisplayFlags( ) );
  465.         setStatus( portlet.getStatus( ) );
  466.         setRole( portlet.getRole( ) );
  467.     }

  468.     /**
  469.      * Add the common tags to all the portlets to the XML document
  470.      *
  471.      * @param strPortlet
  472.      *            The string buffer which contains the XML content of this portlet
  473.      * @return The XML content of this portlet encapsulated by the common tags
  474.      */
  475.     protected String addPortletTags( StringBuffer strPortlet )
  476.     {
  477.         StringBuffer strXml = new StringBuffer( );
  478.         XmlUtil.beginElement( strXml, TAG_PORTLET );
  479.         XmlUtil.addElementHtml( strXml, TAG_PORTLET_NAME, getName( ) );
  480.         XmlUtil.addElement( strXml, TAG_PORTLET_ID, getId( ) );
  481.         XmlUtil.addElement( strXml, TAG_PAGE_ID, getPageId( ) );
  482.         XmlUtil.addElement( strXml, TAG_PLUGIN_NAME, getPluginName( ) );
  483.         XmlUtil.addElement( strXml, TAG_DISPLAY_PORTLET_TITLE, getDisplayPortletTitle( ) );

  484.         String strDisplayOnSmallDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_SMALL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
  485.         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_SMALL_DEVICE, strDisplayOnSmallDevice );

  486.         String strDisplayOnNormalDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_NORMAL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
  487.         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_NORMAL_DEVICE, strDisplayOnNormalDevice );

  488.         String strDisplayOnLargeDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_LARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
  489.         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_LARGE_DEVICE, strDisplayOnLargeDevice );

  490.         String strDisplayOnXLargeDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_XLARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
  491.         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_XLARGE_DEVICE, strDisplayOnXLargeDevice );

  492.         strXml.append( strPortlet.toString( ) );
  493.         XmlUtil.endElement( strXml, TAG_PORTLET );

  494.         return strXml.toString( );
  495.     }

  496.     /**
  497.      * Recovers the stylesheet of the portlet according to the mode
  498.      *
  499.      * @param nMode
  500.      *            the selected mode.
  501.      * @return the name of the stylesheet file
  502.      */
  503.     public String getXslFile( int nMode )
  504.     {
  505.         StyleSheet xsl;

  506.         // Added in v1.3
  507.         // Use the same stylesheet for normal or admin mode
  508.         switch( nMode )
  509.         {
  510.             case MODE_NORMAL:
  511.             case MODE_ADMIN:
  512.                 xsl = PortletHome.getXsl( getId( ), MODE_NORMAL );

  513.                 break;

  514.             default:
  515.                 xsl = PortletHome.getXsl( getId( ), nMode );

  516.                 break;
  517.         }

  518.         return xsl.getFile( );
  519.     }

  520.     /**
  521.      * Recovers the stylesheet of the portlet according to the mode
  522.      *
  523.      * @param nMode
  524.      *            the selected mode.
  525.      * @return the content of the stylesheet file
  526.      */
  527.     public byte [ ] getXslSource( int nMode )
  528.     {
  529.         StyleSheet xsl;

  530.         // Added in v1.3
  531.         // Use the same stylesheet for normal or admin mode
  532.         switch( nMode )
  533.         {
  534.             case MODE_NORMAL:
  535.             case MODE_ADMIN:
  536.                 xsl = PortletHome.getXsl( getId( ), MODE_NORMAL );

  537.                 break;

  538.             default:
  539.                 xsl = PortletHome.getXsl( getId( ), nMode );

  540.                 break;
  541.         }

  542.         return xsl.getSource( );
  543.     }

  544.     /**
  545.      * Recovers the parameters to use with the stylesheet at the time of the transformation.<br>
  546.      * By default, portlets do not return any parameter
  547.      *
  548.      * @return a collection of the type Dictionary (Use the Hashtable implementation)
  549.      */
  550.     public Map<String, String> getXslParams( )
  551.     {
  552.         return null;
  553.     }

  554.     /**
  555.      * Remove the portlet. This method MUST be overloaded on the level of the implementation of each portlet
  556.      */
  557.     public abstract void remove( );

  558.     /**
  559.      * Check if the content of this portlet is generated by xml and xsl, or if it manage its own content generation
  560.      *
  561.      * @return True if the content must be generated from XML and XSL, false if it must be generated by the {@link #getHtmlContent(HttpServletRequest request)}
  562.      *         method
  563.      */
  564.     public boolean isContentGeneratedByXmlAndXsl( )
  565.     {
  566.         return true;
  567.     }

  568.     /**
  569.      * Get the HTML content of the portlet. If the content must be generated from XML and XSL, then this method should return null. This method should only be
  570.      * overrided if the method {@link #isContentGeneratedByXmlAndXsl()} returns true
  571.      *
  572.      * @param request
  573.      *            The request
  574.      * @return The HTML content of the portlet, or null if the content is generated from XML and XSL
  575.      */
  576.     public String getHtmlContent( HttpServletRequest request )
  577.     {
  578.         return null;
  579.     }

  580.     /**
  581.      * Default getLocale() implementation. Could be overriden
  582.      *
  583.      * @param request
  584.      *            The HTTP request
  585.      * @return The Locale
  586.      */
  587.     protected Locale getLocale( HttpServletRequest request )
  588.     {
  589.         return LocaleService.getContextUserLocale( request );
  590.     }
  591.    
  592.     /**
  593.      * Check if the content of the portlet can be put in cache if the current user is not authenticated. If a cache is disabled for a portlet, then every page
  594.      * that contains a portlet of this type will NOT use the page cache, and portlet contents of this portlet type will not be saved into portlet cache.<br>
  595.      * WARNING : Overrides this method with extreme care : disabling page cache can cause severe performance issues !
  596.      *
  597.      * @return True if the content of the portlet can be put in cache if the user is not authenticated, false otherwise.<br>
  598.      *         The default value is true.
  599.      * @see #canBeCachedForConnectedUsers() {@link #canBeCachedForConnectedUsers()} for cache for authenticated users
  600.      */
  601.     public boolean canBeCachedForAnonymousUsers( )
  602.     {
  603.         return true;
  604.     }

  605.     /**
  606.      * Check if the content of the portlet can be put in cache if the current user is authenticated. If a cache is disabled for a portlet, then every page that
  607.      * contains a portlet of this type will NOT use the page cache, and portlet contents of this portlet type will not be saved into portlet cache.<br>
  608.      * WARNING : Overrides this method with extreme care : disabling page cache can cause severe performance issues !
  609.      *
  610.      * @return True if the content of the portlet can be put in cache if the user is authenticated, false otherwise.<br>
  611.      *         The default value is true.
  612.      * @see #canBeCachedForAnonymousUsers() {@link #canBeCachedForAnonymousUsers()} for cache for anonymous users
  613.      */
  614.     public boolean canBeCachedForConnectedUsers( )
  615.     {
  616.         return true;
  617.     }
  618. }