View Javadoc
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  
36  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
37  import fr.paris.lutece.portal.service.portlet.PortletEvent;
38  import fr.paris.lutece.portal.service.portlet.PortletEventListener;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.util.ReferenceList;
43  
44  import java.util.Collection;
45  import java.util.List;
46  
47  /**
48   * This class provides instances management methods (create, find, ...) for Portlet objects
49   */
50  public abstract class PortletHome implements PortletHomeInterface
51  {
52      private static final String PROPERTY_PORTLET_CREATION_STATUS = "lutece.portlet.creation.status";
53      private static final int CONSTANT_DEFAULT_STATUS = Portlet.STATUS_PUBLISHED;
54  
55      // Static variable pointed at the DAO instance
56      private static IPortletDAO _dao = SpringContextService.getBean( "portletDAO" );
57  
58      // /////////////////////////////////////////////////////////////////////////
59      // Finders
60  
61      /**
62       * Returns the Portlet whose primary key is specified in parameter
63       *
64       * @param nKey
65       *            the portlet identifier
66       * @return The portlet object
67       */
68      public static Portlet findByPrimaryKey( int nKey )
69      {
70          Portlet portlet = _dao.load( nKey );
71          String strHomeClass = portlet.getHomeClassName( );
72          Portlet p = null;
73  
74          try
75          {
76              PortletHomeInterface../../fr/paris/lutece/portal/business/portlet/PortletHomeInterface.html#PortletHomeInterface">PortletHomeInterface home = (PortletHomeInterface) Class.forName( strHomeClass ).newInstance( );
77              p = home.getDAO( ).load( nKey );
78              p.copy( portlet );
79          }
80          catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
81          {
82              AppLogService.error( e.getMessage( ), e );
83          }
84  
85          return p;
86      }
87  
88      /**
89       * Returns a collection of portlets according to the selected type
90       *
91       * @param strIdPortletType
92       *            the portlet type
93       * @return the portlets in form of Collection
94       */
95      public static List<Portlet> findByType( String strIdPortletType )
96      {
97          return _dao.selectPortletsByType( strIdPortletType );
98      }
99  
100     /**
101      * Returns the list of portlets for the search on publishing
102      *
103      * @param strPortletName
104      *            STh name of the portlet
105      * @return the list in form of Collection
106      */
107     public static Collection<PortletImpl> getPortletsListbyName( String strPortletName )
108     {
109         return _dao.selectPortletsListbyName( strPortletName );
110     }
111 
112     /**
113      * Returns the stylesheet of the portlet according to the mode
114      *
115      * @param nIdPortlet
116      *            the identifier of the portlet
117      * @param nIdMode
118      *            the selected mode
119      * @return the stylesheet
120      */
121     static StyleSheet getXsl( int nIdPortlet, int nIdMode )
122     {
123         return _dao.selectXslFile( nIdPortlet, nIdMode );
124     }
125 
126     /**
127      * Returns all the styles corresponding to a portlet typeun type de portlet
128      *
129      * @param strIdPortletType
130      *            the identifier of the portlet type
131      * @return the list of styles in form of ReferenceList
132      */
133     public static ReferenceList getStylesList( String strIdPortletType )
134     {
135         return _dao.selectStylesList( strIdPortletType );
136     }
137 
138     /**
139      * Gets a collection of portlets associated to a given role
140      * 
141      * @param strRole
142      *            The role
143      * @return The collection
144      */
145     public static Collection<Portlet> getPortletsByRoleKey( String strRole )
146     {
147         return _dao.selectPortletsByRole( strRole );
148     }
149 
150     /**
151      * Creates a new portlet in the database
152      *
153      * @param portlet
154      *            An instance of the portlet to create
155      * @return the Portlet instance created
156      */
157     public synchronized Portlet"../../../../../../fr/paris/lutece/portal/business/portlet/Portlet.html#Portlet">Portlet create( Portlet portlet )
158     {
159         portlet.setStatus( AppPropertiesService.getPropertyInt( PROPERTY_PORTLET_CREATION_STATUS, CONSTANT_DEFAULT_STATUS ) );
160 
161         // Creation of the portlet parent
162         _dao.insert( portlet );
163 
164         // Creation of the portlet child
165         getDAO( ).insert( portlet );
166 
167         // Invalidate the portlet
168         invalidate( portlet );
169 
170         return portlet;
171     }
172 
173     /**
174      * Deletes the portlet in the database
175      *
176      * @param portlet
177      *            the portlet to remove
178      */
179     public synchronized void remove( Portlet portlet )
180     {
181         // Deleting of the portlet child
182         getDAO( ).delete( portlet.getId( ) );
183 
184         // Deleting of the portlet parent and its alias if exist
185         _dao.delete( portlet.getId( ) );
186 
187         // Invalidate the portlet
188         invalidate( portlet );
189     }
190 
191     /**
192      * Updates a portlet with the values of the specified portlet instance
193      *
194      * @param portlet
195      *            portlet to update
196      */
197     public void update( Portlet portlet )
198     {
199         getDAO( ).store( portlet );
200         _dao.store( portlet );
201 
202         // Invalidate the portlet
203         invalidate( portlet );
204     }
205 
206     /**
207      * Invalidate the portlet which is specified in parameter Invalidates the alias portlets connected to this portlet too.
208      *
209      * @param portlet
210      *            the portlet instance
211      */
212     public static void invalidate( Portlet portlet )
213     {
214         PortletEventice/portlet/PortletEvent.html#PortletEvent">PortletEvent event = new PortletEvent( PortletEvent.INVALIDATE, portlet.getId( ), portlet.getPageId( ) );
215         notifyListeners( event );
216 
217         // invalidate aliases
218         Collection<Portlet> listAliases = getAliasList( portlet.getId( ) );
219 
220         for ( Portlet alias : listAliases )
221         {
222             PortletEventortlet/PortletEvent.html#PortletEvent">PortletEvent eventAlias = new PortletEvent( PortletEvent.INVALIDATE, alias.getId( ), alias.getPageId( ) );
223             notifyListeners( eventAlias );
224         }
225     }
226 
227     /**
228      * Invalidate a portlet whose identifier is specified in paramaeter
229      *
230      * @param nIdPortlet
231      *            the portlet identifier
232      */
233     public static void invalidate( int nIdPortlet )
234     {
235         Portlet portlet = findByPrimaryKey( nIdPortlet );
236         if ( portlet != null )
237         {
238             invalidate( portlet );
239         }
240     }
241 
242     /**
243      * Indicates if the portlet has alias
244      *
245      * @param nIdPortlet
246      *            the portlet identifier
247      * @return true if the portlet has alias, false if not.
248      */
249     public static boolean hasAlias( int nIdPortlet )
250     {
251         return _dao.hasAlias( nIdPortlet );
252     }
253 
254     /**
255      * Update the status of portlet
256      *
257      * @param portlet
258      *            the portlet to remove
259      * @param nStatus
260      *            The status to update
261      */
262     public static void updateStatus( Portlet portlet, int nStatus )
263     {
264         // Deleting of the portlet child
265         _dao.updateStatus( portlet, nStatus );
266 
267         // Invalidate the portlet
268         invalidate( portlet );
269     }
270     /**
271      * Update the status of portlet
272      *
273      * @param portlet
274      *            the portlet to remove
275      * @param nStatus
276      *            The status to update
277      */
278     public static void updatePosition( Portlet portlet, int nColumn, int nOrder )
279     {
280         // Deleting of the portlet child
281         _dao.updatePosition( portlet, nColumn, nOrder );
282 
283         // Invalidate the portlet
284         invalidate( portlet );
285     }
286 
287     /**
288      * Returns the instance of the PortletType whose identifier is specified in parameter
289      *
290      * @param strPortletTypeId
291      *            the identifier of the portlet type
292      * @return the instance of the portlet type
293      */
294     public static PortletType getPortletType( String strPortletTypeId )
295     {
296         return _dao.selectPortletType( strPortletTypeId );
297     }
298 
299     /**
300      * Returns the collection of the StyleSheet objects associated to the Style
301      *
302      * @param nStyleId
303      *            identifier of the style
304      * @return A collection of styles
305      */
306     public static Collection<PortletImpl> getPortletListByStyle( int nStyleId )
307     {
308         return _dao.selectPortletListByStyle( nStyleId );
309     }
310 
311     /**
312      * Returns the collection of the StyleSheet objects associated to the Style
313      *
314      * @param nPortletId
315      *            identifier of the portlet
316      * @return A collection of styles
317      */
318     public static Collection<Portlet> getAliasList( int nPortletId )
319     {
320         return _dao.selectAliasesForPortlet( nPortletId );
321     }
322 
323     /**
324      * Get the last modified portlet
325      * 
326      * @return the last modified portlet
327      */
328     public static Portlet getLastModifiedPortlet( )
329     {
330         return _dao.loadLastModifiedPortlet( );
331     }
332 
333     /**
334      * Notifies listeners
335      * 
336      * @param event
337      *            the event
338      */
339     public static void notifyListeners( PortletEvent event )
340     {
341         for ( PortletEventListener listener : SpringContextService.getBeansOfType( PortletEventListener.class ) )
342         {
343             listener.processPortletEvent( event );
344         }
345     }
346 
347     /**
348      * Get list of used orders for a column
349      * 
350      * @param pageId
351      *            the page id
352      * @param columnId
353      *            the column id
354      * @return list of orders used for this column
355      */
356     public static List<Integer> getUsedOrdersForColumns( int pageId, int columnId )
357     {
358         return _dao.getUsedOrdersForColumns( pageId, columnId );
359     }
360 }