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.portal.service.portal;
35
36 import fr.paris.lutece.portal.business.style.Theme;
37 import fr.paris.lutece.portal.service.content.PageData;
38 import fr.paris.lutece.portal.service.database.AppConnectionService;
39 import fr.paris.lutece.portal.service.i18n.I18nService;
40 import fr.paris.lutece.portal.service.plugin.Plugin;
41 import fr.paris.lutece.portal.service.plugin.PluginService;
42 import fr.paris.lutece.portal.service.spring.SpringContextService;
43 import fr.paris.lutece.portal.service.util.AppLogService;
44 import fr.paris.lutece.portal.service.util.AppPropertiesService;
45 import fr.paris.lutece.util.ReferenceItem;
46 import fr.paris.lutece.util.ReferenceList;
47
48 import org.apache.commons.lang3.StringUtils;
49
50 import org.springframework.beans.factory.BeanDefinitionStoreException;
51 import org.springframework.beans.factory.CannotLoadBeanClassException;
52 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
53
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.Locale;
57
58 import javax.servlet.http.Cookie;
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
61
62
63
64
65 public final class ThemesService
66 {
67 public static final String GLOBAL_THEME = "default";
68 private static final String THEME_PLUGIN_NAME = "theme";
69 private static final String BEAN_THEME_SERVICE = "theme.themeService";
70 private static final String COOKIE_NAME = "theme";
71 private static final String THEME_TEST = "theme_test";
72
73
74 private static final String PROPERTY_USE_GLOBAL_THEME = "portal.style.label.useGlobalTheme";
75 private static final String PROPERTY_DEFAULT_CODE_THEME = "themes.default.code";
76 private static final String PROPERTY_DEFAULT_PATH_CSS = "themes.default.css";
77 private static final String PROPERTY_DEFAULT_PATH_JS = "themes.default.js";
78 private static final String PROPERTY_DEFAULT_PATH_IMAGE = "themes.default.images";
79 private static final String PROPERTY_DEFAULT_AUTHOR_URL = "themes.default.author_url";
80 private static final String PROPERTY_DEFAULT_AUTHOR = "themes.default.author";
81 private static final String PROPERTY_DEFAULT_LICENCE = "themes.default.licence";
82 private static final String PROPERTY_DEFAULT_DESCRIPTION = "themes.default.name";
83 private static final String PROPERTY_DEFAULT_VERSION = "themes.default.version";
84
85
86 private static final String MESSAGE_THEME_NOT_AVAILABLE = "Theme service not available.";
87
88
89
90
91 private ThemesService( )
92 {
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public static Theme getTheme( PageData data, HttpServletRequest request )
111 {
112 String strTheme = StringUtils.EMPTY;
113
114
115 String strPageTheme = data.getTheme( );
116
117 if ( ( strPageTheme != null ) && ( strPageTheme.compareToIgnoreCase( GLOBAL_THEME ) != 0 ) )
118 {
119 strTheme = strPageTheme;
120 }
121
122
123 String strUserTheme = getUserTheme( request );
124
125 if ( strUserTheme != null )
126 {
127 strTheme = strUserTheme;
128 }
129
130
131 String themeTest = request.getParameter( THEME_TEST );
132
133 if ( themeTest != null )
134 {
135 strTheme = themeTest;
136 }
137
138 return getGlobalTheme( strTheme );
139 }
140
141
142
143
144
145
146
147
148 public static String getUserTheme( HttpServletRequest request )
149 {
150 try
151 {
152 IThemeService themeService = getThemeService( );
153
154 return themeService.getUserTheme( request );
155 }
156 catch( ThemeNotAvailableException e )
157 {
158 return null;
159 }
160 }
161
162
163
164
165
166
167
168
169
170
171
172 public static void setUserTheme( HttpServletRequest request, HttpServletResponse response, String strTheme )
173 {
174 if ( isValid( strTheme ) )
175 {
176 Cookie cookie = new Cookie( COOKIE_NAME, strTheme );
177 cookie.setSecure( true );
178 cookie.setHttpOnly(true);
179 response.addCookie( cookie );
180 }
181 }
182
183
184
185
186
187
188
189
190 private static boolean isValid( String strTheme )
191 {
192 for ( ReferenceItem item : getThemes( ) )
193 {
194 if ( item.getCode( ).equals( strTheme ) )
195 {
196 return true;
197 }
198 }
199 return false;
200 }
201
202
203
204
205
206
207 public static String getGlobalTheme( )
208 {
209 Theme theme = getGlobalTheme( StringUtils.EMPTY );
210
211 return theme.getCodeTheme( );
212 }
213
214
215
216
217
218
219 public static Theme getGlobalThemeObject( )
220 {
221 return getGlobalTheme( StringUtils.EMPTY );
222 }
223
224
225
226
227
228
229
230
231 public static Theme getGlobalTheme( String strTheme )
232 {
233 Theme theme = null;
234
235 try
236 {
237 IThemeService themeService = getThemeService( );
238
239 if ( StringUtils.isBlank( strTheme ) )
240 {
241 theme = themeService.getGlobalTheme( );
242 }
243 else
244 {
245 theme = themeService.getTheme( strTheme );
246 }
247 }
248 catch( ThemeNotAvailableException e )
249 {
250 theme = getDefaultTheme( );
251 }
252
253 return theme;
254 }
255
256
257
258
259
260
261
262 public static void setGlobalTheme( String strGlobalTheme )
263 {
264 IThemeService themeService;
265
266 try
267 {
268 themeService = getThemeService( );
269 themeService.setGlobalTheme( strGlobalTheme );
270 }
271 catch( ThemeNotAvailableException e )
272 {
273 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
274 }
275 }
276
277
278
279
280
281
282
283
284 public static ReferenceList getPageThemes( Locale locale )
285 {
286 ReferenceListt.html#ReferenceList">ReferenceList listThemes = new ReferenceList( );
287
288 try
289 {
290 IThemeService themeService = getThemeService( );
291 listThemes = themeService.getThemes( );
292 }
293 catch( ThemeNotAvailableException e )
294 {
295 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
296 }
297
298 String strGlobalTheme = I18nService.getLocalizedString( PROPERTY_USE_GLOBAL_THEME, locale );
299 listThemes.addItem( GLOBAL_THEME, strGlobalTheme );
300
301 return listThemes;
302 }
303
304
305
306
307
308
309
310
311 private static IThemeService getThemeService( ) throws ThemeNotAvailableException
312 {
313 IThemeService themeService = null;
314
315 if ( !isAvailable( ) )
316 {
317 throw new ThemeNotAvailableException( );
318 }
319
320 try
321 {
322 themeService = SpringContextService.getBean( BEAN_THEME_SERVICE );
323 }
324 catch( BeanDefinitionStoreException | NoSuchBeanDefinitionException | CannotLoadBeanClassException e )
325 {
326 throw new ThemeNotAvailableException( );
327 }
328
329 return themeService;
330 }
331
332
333
334
335
336
337 private static Theme getDefaultTheme( )
338 {
339 Themeal/business/style/Theme.html#Theme">Theme theme = new Theme( );
340 String strCodeTheme = AppPropertiesService.getProperty( PROPERTY_DEFAULT_CODE_THEME );
341 String strPathCss = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PATH_CSS );
342 String strPathImages = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PATH_IMAGE );
343 String strPathJs = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PATH_JS );
344 String strThemeAuthor = AppPropertiesService.getProperty( PROPERTY_DEFAULT_AUTHOR );
345 String strThemeAuthorUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_AUTHOR_URL );
346 String strThemeDescription = AppPropertiesService.getProperty( PROPERTY_DEFAULT_DESCRIPTION );
347 String strThemeLicence = AppPropertiesService.getProperty( PROPERTY_DEFAULT_LICENCE );
348 String strThemeVersion = AppPropertiesService.getProperty( PROPERTY_DEFAULT_VERSION );
349
350 theme.setCodeTheme( strCodeTheme );
351 theme.setPathCss( strPathCss );
352 theme.setPathImages( strPathImages );
353 theme.setPathJs( strPathJs );
354 theme.setThemeAuthor( strThemeAuthor );
355 theme.setThemeAuthorUrl( strThemeAuthorUrl );
356 theme.setThemeDescription( strThemeDescription );
357 theme.setThemeLicence( strThemeLicence );
358 theme.setThemeVersion( strThemeVersion );
359
360 return theme;
361 }
362
363
364
365
366
367
368
369
370
371
372 public static boolean isAvailable( )
373 {
374 Plugin pluginTheme = PluginService.getPlugin( THEME_PLUGIN_NAME );
375
376 return PluginService.isPluginEnable( THEME_PLUGIN_NAME ) && ( pluginTheme.getDbPoolName( ) != null )
377 && !AppConnectionService.NO_POOL_DEFINED.equals( pluginTheme.getDbPoolName( ) );
378 }
379
380
381
382
383
384
385
386
387 public static Themef="../../../../../../fr/paris/lutece/portal/business/style/Theme.html#Theme">Theme create( Theme theme )
388 {
389 try
390 {
391 IThemeService themeService = getThemeService( );
392
393 return themeService.create( theme );
394 }
395 catch( ThemeNotAvailableException e )
396 {
397 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
398
399 return null;
400 }
401 }
402
403
404
405
406
407
408
409
410 public static Themef="../../../../../../fr/paris/lutece/portal/business/style/Theme.html#Theme">Theme update( Theme theme )
411 {
412 try
413 {
414 IThemeService themeService = getThemeService( );
415
416 return themeService.update( theme );
417 }
418 catch( ThemeNotAvailableException e )
419 {
420 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
421
422 return null;
423 }
424 }
425
426
427
428
429
430
431
432 public static void remove( String strCodeTheme )
433 {
434 try
435 {
436 IThemeService themeService = getThemeService( );
437 themeService.remove( strCodeTheme );
438 }
439 catch( ThemeNotAvailableException e )
440 {
441 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
442 }
443 }
444
445
446
447
448
449
450 public static Collection<Theme> getThemesList( )
451 {
452 Collection<Theme> listThemes = new ArrayList<>( );
453
454 try
455 {
456 IThemeService themeService = getThemeService( );
457 listThemes = themeService.getThemesList( );
458 }
459 catch( ThemeNotAvailableException e )
460 {
461 Theme theme = getDefaultTheme( );
462 listThemes.add( theme );
463 }
464
465 return listThemes;
466 }
467
468
469
470
471
472
473 public static ReferenceList getThemes( )
474 {
475 ReferenceListt.html#ReferenceList">ReferenceList listThemes = new ReferenceList( );
476
477 try
478 {
479 IThemeService themeService = getThemeService( );
480 listThemes = themeService.getThemes( );
481 }
482 catch( ThemeNotAvailableException e )
483 {
484 Theme theme = getDefaultTheme( );
485 listThemes.addItem( theme.getCodeTheme( ), theme.getThemeDescription( ) );
486 }
487
488 return listThemes;
489 }
490
491
492
493
494
495
496
497
498 public static boolean isValidTheme( String strCodeTheme )
499 {
500 boolean bIsValidTheme = false;
501
502 try
503 {
504 IThemeService themeService = getThemeService( );
505 bIsValidTheme = themeService.isValidTheme( strCodeTheme );
506 }
507 catch( ThemeNotAvailableException e )
508 {
509 Theme theme = getDefaultTheme( );
510 bIsValidTheme = theme.getCodeTheme( ).equals( strCodeTheme );
511 }
512
513 return bIsValidTheme;
514 }
515 }