View Javadoc
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.plugins.myportal.service;
35  
36  import fr.paris.lutece.plugins.myportal.business.UserPageConfig;
37  import fr.paris.lutece.plugins.myportal.business.UserPageConfigHome;
38  import fr.paris.lutece.plugins.myportal.business.Widget;
39  import fr.paris.lutece.plugins.myportal.business.WidgetFilter;
40  import fr.paris.lutece.plugins.myportal.business.WidgetHome;
41  import fr.paris.lutece.plugins.myportal.business.WidgetStatusEnum;
42  import fr.paris.lutece.plugins.myportal.business.page.PageConfig;
43  import fr.paris.lutece.plugins.myportal.business.page.TabConfig;
44  import fr.paris.lutece.plugins.myportal.business.page.WidgetConfig;
45  import fr.paris.lutece.plugins.myportal.service.cache.WidgetCacheService;
46  import fr.paris.lutece.portal.service.cache.CacheService;
47  import fr.paris.lutece.portal.service.cache.ICacheKeyService;
48  import fr.paris.lutece.portal.service.security.LuteceUser;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.portal.web.PortalJspBean;
51  
52  import java.util.ArrayList;
53  import java.util.Collection;
54  import java.util.HashMap;
55  import java.util.List;
56  import java.util.Map;
57  
58  /**
59   * Widget Service class : retrieve Widget description from a cache
60   */
61  public class WidgetService
62  {
63      // CACHE KEYS
64      private static final String KEY_ESSENTIAL_WIDGETS = "essential_widgets";
65      private static final String KEY_NEW_WIDGETS = "new_widgets";
66      private static final String KEY_WIDGET = "widget";
67      private static final String KEY_ICON = "icon";
68      private static final String KEY_CATEGORY_WIDGET_LIST = "category_widgets";
69  
70      // CONSTANTS
71      private static final String COMMA = ",";
72      private static final String TRUE = "true";
73      private static final String NONE = "-";
74  
75      // PROPERTIES
76      private static final String PROPERTY_ACCEPTED_ICON_FORMATS = "myportal.acceptedIconFormats";
77      private static final String PROPERTY_CACHE_WIDGETSERVICE_ENABLE = "myportal.cache.widgetService.enable";
78  
79      // VARIABLES
80      private ICacheKeyService _cksWidget;
81      private ICacheKeyService _cksIcon;
82      private ICacheKeyService _cksEssentialWidgets;
83      private ICacheKeyService _cksNewWidgets;
84      private ICacheKeyService _cksCategoryWidgets;
85      private WidgetContentService _widgetContentService;
86      private WidgetCacheService _cacheWidget = WidgetCacheService.getInstance( );
87  
88      /**
89       * constructor
90       */
91      public WidgetService( )
92      {
93          init( );
94      }
95  
96      /**
97       * Init Service
98       */
99      private void init( )
100     {
101         String strCacheEnable = AppPropertiesService.getProperty( PROPERTY_CACHE_WIDGETSERVICE_ENABLE, TRUE );
102         boolean bCacheEnable = TRUE.equalsIgnoreCase( strCacheEnable );
103 
104         if ( bCacheEnable )
105         {
106             _cacheWidget.initCache( );
107         }
108         else
109         {
110             CacheService.registerCacheableService( _cacheWidget );
111         }
112     }
113 
114     /**
115      * Returns widgets description from the cache
116      * 
117      * @param nWidgetId
118      *            The Widget ID
119      * @return The widget object
120      */
121     public Widget getWidget( int nWidgetId )
122     {
123         String strKey = getKey( nWidgetId );
124         Widget../../../../../../fr/paris/lutece/plugins/myportal/business/Widget.html#Widget">Widget widget = (Widget) _cacheWidget.getFromCache( strKey );
125 
126         if ( widget == null )
127         {
128             widget = WidgetHome.findByPrimaryKey( nWidgetId );
129             _cacheWidget.putInCache( strKey, widget );
130         }
131 
132         return widget;
133     }
134 
135     /**
136      * Load the data of all the widget objects and returns them in form of a collection
137      * 
138      * @return the collection which contains the data of all the widget objects
139      */
140     public Collection<Widget> getWidgetsList( )
141     {
142         return WidgetHome.getWidgetsList( );
143     }
144 
145     /**
146      * Get the list of widgets given an id category
147      * 
148      * @param nCategoryId
149      *            the id category
150      * @return a list of {@link Widget}
151      */
152     public List<Widget> getWidgetsByCategoryId( int nCategoryId )
153     {
154         String strKey = getCategoryListKey( nCategoryId );
155         List<Widget> listWidgets = (List<Widget>) _cacheWidget.getFromCache( strKey );
156 
157         if ( listWidgets == null )
158         {
159             WidgetFiltertal/business/WidgetFilter.html#WidgetFilter">WidgetFilter wFilter = new WidgetFilter( );
160             wFilter.setIdCategory( nCategoryId );
161             wFilter.setStatus( WidgetStatusEnum.PUBLIC.getId( ) );
162             wFilter.setIsWideSearch( false );
163             listWidgets = WidgetHome.getWidgetsByFilter( wFilter );
164             _cacheWidget.putInCache( strKey, listWidgets );
165         }
166 
167         return listWidgets;
168     }
169 
170     /**
171      * Get the list of essential widgets
172      * 
173      * @return a list of {@link Widget}
174      */
175     public List<Widget> getEssentialWidgets( )
176     {
177         String strKey = getEssentialWidgetsKey( );
178         List<Widget> listWidgets = (List<Widget>) _cacheWidget.getFromCache( strKey );
179 
180         if ( listWidgets == null )
181         {
182             WidgetFiltertal/business/WidgetFilter.html#WidgetFilter">WidgetFilter wFilter = new WidgetFilter( );
183             wFilter.setIsEssential( WidgetFilter.FILTER_TRUE );
184             wFilter.setStatus( WidgetStatusEnum.PUBLIC.getId( ) );
185             wFilter.setIsWideSearch( false );
186             listWidgets = WidgetHome.getWidgetsByFilter( wFilter );
187             _cacheWidget.putInCache( strKey, listWidgets );
188         }
189 
190         return listWidgets;
191     }
192 
193     /**
194      * Get the list of new widgets
195      * 
196      * @return a list of {@link Widget}
197      */
198     public List<Widget> getNewWidgets( )
199     {
200         String strKey = getNewWidgetsKey( );
201         List<Widget> listWidgets = (List<Widget>) _cacheWidget.getFromCache( strKey );
202 
203         if ( listWidgets == null )
204         {
205             WidgetFiltertal/business/WidgetFilter.html#WidgetFilter">WidgetFilter wFilter = new WidgetFilter( );
206             wFilter.setIsNew( WidgetFilter.FILTER_TRUE );
207             wFilter.setStatus( WidgetStatusEnum.PUBLIC.getId( ) );
208             wFilter.setIsWideSearch( false );
209             listWidgets = WidgetHome.getWidgetsByFilter( wFilter );
210             _cacheWidget.putInCache( strKey, listWidgets );
211         }
212 
213         return listWidgets;
214     }
215 
216     /**
217      * Get the list of widgets given a name
218      * 
219      * @param strName
220      *            the name
221      * @return a list of {@link Widget}
222      */
223     public List<Widget> getWidgetsByName( String strName )
224     {
225         WidgetFiltertal/business/WidgetFilter.html#WidgetFilter">WidgetFilter wFilter = new WidgetFilter( );
226         wFilter.setName( strName );
227         wFilter.setStatus( WidgetStatusEnum.PUBLIC.getId( ) );
228         wFilter.setIsWideSearch( false );
229 
230         return WidgetHome.getWidgetsByFilter( wFilter );
231     }
232 
233     /**
234      * Create a new widget. If the cache is enable, it will reset the cache.
235      * 
236      * @param widget
237      *            The {@link Widget}
238      */
239     public void createWidget( Widget widget )
240     {
241         _cacheWidget.resetCache( );
242         WidgetHome.create( widget );
243     }
244 
245     /**
246      * Remove a widget. If the cache is enable, it will reset the cache.
247      * 
248      * @param nWidgetId
249      *            the widget ID
250      */
251     public void removeWidget( int nWidgetId )
252     {
253         _cacheWidget.resetCache( );
254         _widgetContentService.removeCache( nWidgetId );
255         WidgetHome.remove( nWidgetId );
256     }
257 
258     /**
259      * Update a widget. If the cache is enable, it will reset the cache.
260      * 
261      * @param widget
262      *            The {@link Widget}
263      */
264     public void updateWidget( Widget widget )
265     {
266         _cacheWidget.resetCache( );
267         _widgetContentService.removeCache( widget.getIdWidget( ) );
268         WidgetHome.update( widget );
269     }
270 
271     /**
272      * Get the widget IDs from a given LuteceUser
273      * 
274      * @param user
275      *            the {@link LuteceUser}
276      * @return a list of widget IDs
277      */
278     public List<Integer> getUserWidgetIds( LuteceUser user )
279     {
280         List<Integer> listWidgetIds = new ArrayList<Integer>( );
281         UserPageConfig userConf = UserPageConfigHome.findByPrimaryKey( user.getName( ) );
282 
283         if ( userConf != null )
284         {
285             PageConfig pageConfig = PageConfigJsonUtil.parseJson( userConf.getUserPageConfig( ) );
286 
287             for ( TabConfig tabConfig : pageConfig.getTabList( ) )
288             {
289                 for ( WidgetConfig widgetConfig : tabConfig.getWidgetList( ) )
290                 {
291                     listWidgetIds.add( widgetConfig.getWidgetId( ) );
292                 }
293             }
294         }
295 
296         return listWidgetIds;
297     }
298 
299     /**
300      * Get the list of public or mandatory widgets
301      * 
302      * @return the list widgets
303      */
304     public List<Widget> getPublicMandatoryWidgets( )
305     {
306         return WidgetHome.getPublicMandatoryWidgets( );
307     }
308 
309     // SETTERS
310     /**
311      * Set the cache key service
312      * 
313      * @param cacheKeyService
314      *            the _cacheKeyService to set
315      */
316     public void setWidgetCacheKeyService( ICacheKeyService cacheKeyService )
317     {
318         _cksWidget = cacheKeyService;
319     }
320 
321     /**
322      * Set the cache key service
323      * 
324      * @param cacheKeyService
325      *            the _cacheKeyService to set
326      */
327     public void setIconCacheKeyService( ICacheKeyService cacheKeyService )
328     {
329         _cksIcon = cacheKeyService;
330     }
331 
332     /**
333      * Set the cache key service
334      * 
335      * @param cacheKeyService
336      *            the _cacheKeyService to set
337      */
338     public void setEssentialWidgetsCacheKeyService( ICacheKeyService cacheKeyService )
339     {
340         _cksEssentialWidgets = cacheKeyService;
341     }
342 
343     /**
344      * Set the cache key service
345      * 
346      * @param cacheKeyService
347      *            the _cacheKeyService to set
348      */
349     public void setNewWidgetsCacheKeyService( ICacheKeyService cacheKeyService )
350     {
351         _cksNewWidgets = cacheKeyService;
352     }
353 
354     /**
355      * Set the cache key service
356      * 
357      * @param cacheKeyService
358      *            the _cacheKeyService to set
359      */
360     public void setCategoryWidgetsCacheKeyService( ICacheKeyService cacheKeyService )
361     {
362         _cksCategoryWidgets = cacheKeyService;
363     }
364 
365     /**
366      * Set the widget content service
367      * 
368      * @param widgetContentService
369      *            the widget content service
370      */
371     public void setWidgetContentService( WidgetContentService widgetContentService )
372     {
373         _widgetContentService = widgetContentService;
374     }
375 
376     // BUILD CACHE KEYS
377 
378     /**
379      * Get the cache key for the widget
380      * 
381      * @param nId
382      *            the id
383      * @return the key
384      */
385     private String getKey( int nId )
386     {
387         Map<String, String> mapParams = new HashMap<String, String>( );
388         mapParams.put( KEY_WIDGET, Integer.toString( nId ) );
389 
390         return _cksWidget.getKey( mapParams, PortalJspBean.MODE_HTML, null );
391     }
392 
393     /**
394      * Get the cache key for the icon
395      * 
396      * @param nId
397      *            the id
398      * @return the key
399      */
400     private String getIconKey( int nId )
401     {
402         Map<String, String> mapParams = new HashMap<String, String>( );
403         mapParams.put( KEY_ICON, Integer.toString( nId ) );
404 
405         return _cksIcon.getKey( mapParams, PortalJspBean.MODE_HTML, null );
406     }
407 
408     /**
409      * Get the cache key for the category list
410      * 
411      * @param nId
412      *            the id
413      * @return the key
414      */
415     private String getCategoryListKey( int nId )
416     {
417         Map<String, String> mapParams = new HashMap<String, String>( );
418         mapParams.put( KEY_CATEGORY_WIDGET_LIST, Integer.toString( nId ) );
419 
420         return _cksCategoryWidgets.getKey( mapParams, PortalJspBean.MODE_HTML, null );
421     }
422 
423     /**
424      * Get the cache key for the list of essential widgets
425      * 
426      * @return the key
427      */
428     private String getEssentialWidgetsKey( )
429     {
430         Map<String, String> mapParams = new HashMap<String, String>( );
431         mapParams.put( KEY_ESSENTIAL_WIDGETS, NONE );
432 
433         return _cksEssentialWidgets.getKey( mapParams, PortalJspBean.MODE_HTML, null );
434     }
435 
436     /**
437      * Get the cache key for the list of new widgets
438      * 
439      * @return the key
440      */
441     private String getNewWidgetsKey( )
442     {
443         Map<String, String> mapParams = new HashMap<String, String>( );
444         mapParams.put( KEY_NEW_WIDGETS, NONE );
445 
446         return _cksNewWidgets.getKey( mapParams, PortalJspBean.MODE_HTML, null );
447     }
448 }