PortletHome.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.stylesheet.StyleSheet;
  36. import fr.paris.lutece.portal.service.portlet.PortletEvent;
  37. import fr.paris.lutece.portal.service.portlet.PortletEventListener;
  38. import fr.paris.lutece.portal.service.spring.SpringContextService;
  39. import fr.paris.lutece.portal.service.util.AppLogService;
  40. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  41. import fr.paris.lutece.util.ReferenceList;

  42. import java.util.Collection;
  43. import java.util.List;

  44. /**
  45.  * This class provides instances management methods (create, find, ...) for Portlet objects
  46.  */
  47. public abstract class PortletHome implements PortletHomeInterface
  48. {
  49.     private static final String PROPERTY_PORTLET_CREATION_STATUS = "lutece.portlet.creation.status";
  50.     private static final int CONSTANT_DEFAULT_STATUS = Portlet.STATUS_PUBLISHED;

  51.     // Static variable pointed at the DAO instance
  52.     private static IPortletDAO _dao = SpringContextService.getBean( "portletDAO" );

  53.     // /////////////////////////////////////////////////////////////////////////
  54.     // Finders

  55.     /**
  56.      * Returns the Portlet whose primary key is specified in parameter
  57.      *
  58.      * @param nKey
  59.      *            the portlet identifier
  60.      * @return The portlet object
  61.      */
  62.     public static Portlet findByPrimaryKey( int nKey )
  63.     {
  64.         Portlet portlet = _dao.load( nKey );
  65.         String strHomeClass = portlet.getHomeClassName( );
  66.         Portlet p = null;

  67.         try
  68.         {
  69.             PortletHomeInterface home = (PortletHomeInterface) Class.forName( strHomeClass ).newInstance( );
  70.             p = home.getDAO( ).load( nKey );
  71.             p.copy( portlet );
  72.         }
  73.         catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
  74.         {
  75.             AppLogService.error( e.getMessage( ), e );
  76.         }

  77.         return p;
  78.     }

  79.     /**
  80.      * Returns a collection of portlets according to the selected type
  81.      *
  82.      * @param strIdPortletType
  83.      *            the portlet type
  84.      * @return the portlets in form of Collection
  85.      */
  86.     public static List<Portlet> findByType( String strIdPortletType )
  87.     {
  88.         return _dao.selectPortletsByType( strIdPortletType );
  89.     }

  90.     /**
  91.      * Returns the list of portlets for the search on publishing
  92.      *
  93.      * @param strPortletName
  94.      *            STh name of the portlet
  95.      * @return the list in form of Collection
  96.      */
  97.     public static Collection<PortletImpl> getPortletsListbyName( String strPortletName )
  98.     {
  99.         return _dao.selectPortletsListbyName( strPortletName );
  100.     }

  101.     /**
  102.      * Returns the stylesheet of the portlet according to the mode
  103.      *
  104.      * @param nIdPortlet
  105.      *            the identifier of the portlet
  106.      * @param nIdMode
  107.      *            the selected mode
  108.      * @return the stylesheet
  109.      */
  110.     static StyleSheet getXsl( int nIdPortlet, int nIdMode )
  111.     {
  112.         return _dao.selectXslFile( nIdPortlet, nIdMode );
  113.     }

  114.     /**
  115.      * Returns all the styles corresponding to a portlet typeun type de portlet
  116.      *
  117.      * @param strIdPortletType
  118.      *            the identifier of the portlet type
  119.      * @return the list of styles in form of ReferenceList
  120.      */
  121.     public static ReferenceList getStylesList( String strIdPortletType )
  122.     {
  123.         return _dao.selectStylesList( strIdPortletType );
  124.     }

  125.     /**
  126.      * Gets a collection of portlets associated to a given role
  127.      *
  128.      * @param strRole
  129.      *            The role
  130.      * @return The collection
  131.      */
  132.     public static Collection<Portlet> getPortletsByRoleKey( String strRole )
  133.     {
  134.         return _dao.selectPortletsByRole( strRole );
  135.     }

  136.     /**
  137.      * Creates a new portlet in the database
  138.      *
  139.      * @param portlet
  140.      *            An instance of the portlet to create
  141.      * @return the Portlet instance created
  142.      */
  143.     public synchronized Portlet create( Portlet portlet )
  144.     {
  145.         portlet.setStatus( AppPropertiesService.getPropertyInt( PROPERTY_PORTLET_CREATION_STATUS, CONSTANT_DEFAULT_STATUS ) );

  146.         // Creation of the portlet parent
  147.         _dao.insert( portlet );

  148.         // Creation of the portlet child
  149.         getDAO( ).insert( portlet );

  150.         // Invalidate the portlet
  151.         invalidate( portlet );

  152.         return portlet;
  153.     }

  154.     /**
  155.      * Deletes the portlet in the database
  156.      *
  157.      * @param portlet
  158.      *            the portlet to remove
  159.      */
  160.     public synchronized void remove( Portlet portlet )
  161.     {
  162.         // Deleting of the portlet child
  163.         getDAO( ).delete( portlet.getId( ) );

  164.         // Deleting of the portlet parent and its alias if exist
  165.         _dao.delete( portlet.getId( ) );

  166.         // Invalidate the portlet
  167.         invalidate( portlet );
  168.     }

  169.     /**
  170.      * Updates a portlet with the values of the specified portlet instance
  171.      *
  172.      * @param portlet
  173.      *            portlet to update
  174.      */
  175.     public void update( Portlet portlet )
  176.     {
  177.         getDAO( ).store( portlet );
  178.         _dao.store( portlet );

  179.         // Invalidate the portlet
  180.         invalidate( portlet );
  181.     }

  182.     /**
  183.      * Invalidate the portlet which is specified in parameter Invalidates the alias portlets connected to this portlet too.
  184.      *
  185.      * @param portlet
  186.      *            the portlet instance
  187.      */
  188.     public static void invalidate( Portlet portlet )
  189.     {
  190.         PortletEvent event = new PortletEvent( PortletEvent.INVALIDATE, portlet.getId( ), portlet.getPageId( ) );
  191.         notifyListeners( event );

  192.         // invalidate aliases
  193.         Collection<Portlet> listAliases = getAliasList( portlet.getId( ) );

  194.         for ( Portlet alias : listAliases )
  195.         {
  196.             PortletEvent eventAlias = new PortletEvent( PortletEvent.INVALIDATE, alias.getId( ), alias.getPageId( ) );
  197.             notifyListeners( eventAlias );
  198.         }
  199.     }

  200.     /**
  201.      * Invalidate a portlet whose identifier is specified in paramaeter
  202.      *
  203.      * @param nIdPortlet
  204.      *            the portlet identifier
  205.      */
  206.     public static void invalidate( int nIdPortlet )
  207.     {
  208.         Portlet portlet = findByPrimaryKey( nIdPortlet );
  209.         if ( portlet != null )
  210.         {
  211.             invalidate( portlet );
  212.         }
  213.     }

  214.     /**
  215.      * Indicates if the portlet has alias
  216.      *
  217.      * @param nIdPortlet
  218.      *            the portlet identifier
  219.      * @return true if the portlet has alias, false if not.
  220.      */
  221.     public static boolean hasAlias( int nIdPortlet )
  222.     {
  223.         return _dao.hasAlias( nIdPortlet );
  224.     }

  225.     /**
  226.      * Update the status of portlet
  227.      *
  228.      * @param portlet
  229.      *            the portlet to remove
  230.      * @param nStatus
  231.      *            The status to update
  232.      */
  233.     public static void updateStatus( Portlet portlet, int nStatus )
  234.     {
  235.         // Deleting of the portlet child
  236.         _dao.updateStatus( portlet, nStatus );

  237.         // Invalidate the portlet
  238.         invalidate( portlet );
  239.     }
  240.     /**
  241.      * Update the status of portlet
  242.      *
  243.      * @param portlet
  244.      *            the portlet to remove
  245.      * @param nStatus
  246.      *            The status to update
  247.      */
  248.     public static void updatePosition( Portlet portlet, int nColumn, int nOrder )
  249.     {
  250.         // Deleting of the portlet child
  251.         _dao.updatePosition( portlet, nColumn, nOrder );

  252.         // Invalidate the portlet
  253.         invalidate( portlet );
  254.     }

  255.     /**
  256.      * Returns the instance of the PortletType whose identifier is specified in parameter
  257.      *
  258.      * @param strPortletTypeId
  259.      *            the identifier of the portlet type
  260.      * @return the instance of the portlet type
  261.      */
  262.     public static PortletType getPortletType( String strPortletTypeId )
  263.     {
  264.         return _dao.selectPortletType( strPortletTypeId );
  265.     }

  266.     /**
  267.      * Returns the collection of the StyleSheet objects associated to the Style
  268.      *
  269.      * @param nStyleId
  270.      *            identifier of the style
  271.      * @return A collection of styles
  272.      */
  273.     public static Collection<PortletImpl> getPortletListByStyle( int nStyleId )
  274.     {
  275.         return _dao.selectPortletListByStyle( nStyleId );
  276.     }

  277.     /**
  278.      * Returns the collection of the StyleSheet objects associated to the Style
  279.      *
  280.      * @param nPortletId
  281.      *            identifier of the portlet
  282.      * @return A collection of styles
  283.      */
  284.     public static Collection<Portlet> getAliasList( int nPortletId )
  285.     {
  286.         return _dao.selectAliasesForPortlet( nPortletId );
  287.     }

  288.     /**
  289.      * Get the last modified portlet
  290.      *
  291.      * @return the last modified portlet
  292.      */
  293.     public static Portlet getLastModifiedPortlet( )
  294.     {
  295.         return _dao.loadLastModifiedPortlet( );
  296.     }

  297.     /**
  298.      * Notifies listeners
  299.      *
  300.      * @param event
  301.      *            the event
  302.      */
  303.     public static void notifyListeners( PortletEvent event )
  304.     {
  305.         for ( PortletEventListener listener : SpringContextService.getBeansOfType( PortletEventListener.class ) )
  306.         {
  307.             listener.processPortletEvent( event );
  308.         }
  309.     }

  310.     /**
  311.      * Get list of used orders for a column
  312.      *
  313.      * @param pageId
  314.      *            the page id
  315.      * @param columnId
  316.      *            the column id
  317.      * @return list of orders used for this column
  318.      */
  319.     public static List<Integer> getUsedOrdersForColumns( int pageId, int columnId )
  320.     {
  321.         return _dao.getUsedOrdersForColumns( pageId, columnId );
  322.     }
  323. }