1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
60
61 public class WidgetService
62 {
63
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
71 private static final String COMMA = ",";
72 private static final String TRUE = "true";
73 private static final String NONE = "-";
74
75
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
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
90
91 public WidgetService( )
92 {
93 init( );
94 }
95
96
97
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
116
117
118
119
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
137
138
139
140 public Collection<Widget> getWidgetsList( )
141 {
142 return WidgetHome.getWidgetsList( );
143 }
144
145
146
147
148
149
150
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
172
173
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
195
196
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
218
219
220
221
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
235
236
237
238
239 public void createWidget( Widget widget )
240 {
241 _cacheWidget.resetCache( );
242 WidgetHome.create( widget );
243 }
244
245
246
247
248
249
250
251 public void removeWidget( int nWidgetId )
252 {
253 _cacheWidget.resetCache( );
254 _widgetContentService.removeCache( nWidgetId );
255 WidgetHome.remove( nWidgetId );
256 }
257
258
259
260
261
262
263
264 public void updateWidget( Widget widget )
265 {
266 _cacheWidget.resetCache( );
267 _widgetContentService.removeCache( widget.getIdWidget( ) );
268 WidgetHome.update( widget );
269 }
270
271
272
273
274
275
276
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
301
302
303
304 public List<Widget> getPublicMandatoryWidgets( )
305 {
306 return WidgetHome.getPublicMandatoryWidgets( );
307 }
308
309
310
311
312
313
314
315
316 public void setWidgetCacheKeyService( ICacheKeyService cacheKeyService )
317 {
318 _cksWidget = cacheKeyService;
319 }
320
321
322
323
324
325
326
327 public void setIconCacheKeyService( ICacheKeyService cacheKeyService )
328 {
329 _cksIcon = cacheKeyService;
330 }
331
332
333
334
335
336
337
338 public void setEssentialWidgetsCacheKeyService( ICacheKeyService cacheKeyService )
339 {
340 _cksEssentialWidgets = cacheKeyService;
341 }
342
343
344
345
346
347
348
349 public void setNewWidgetsCacheKeyService( ICacheKeyService cacheKeyService )
350 {
351 _cksNewWidgets = cacheKeyService;
352 }
353
354
355
356
357
358
359
360 public void setCategoryWidgetsCacheKeyService( ICacheKeyService cacheKeyService )
361 {
362 _cksCategoryWidgets = cacheKeyService;
363 }
364
365
366
367
368
369
370
371 public void setWidgetContentService( WidgetContentService widgetContentService )
372 {
373 _widgetContentService = widgetContentService;
374 }
375
376
377
378
379
380
381
382
383
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
395
396
397
398
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
410
411
412
413
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
425
426
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
438
439
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 }