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