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 response.addCookie( cookie );
179 }
180 }
181
182
183
184
185
186
187
188
189 private static boolean isValid( String strTheme )
190 {
191 for ( ReferenceItem item : getThemes( ) )
192 {
193 if ( item.getCode( ).equals( strTheme ) )
194 {
195 return true;
196 }
197 }
198 return false;
199 }
200
201
202
203
204
205
206 public static String getGlobalTheme( )
207 {
208 Theme theme = getGlobalTheme( StringUtils.EMPTY );
209
210 return theme.getCodeTheme( );
211 }
212
213
214
215
216
217
218 public static Theme getGlobalThemeObject( )
219 {
220 return getGlobalTheme( StringUtils.EMPTY );
221 }
222
223
224
225
226
227
228
229
230 public static Theme getGlobalTheme( String strTheme )
231 {
232 Theme theme = null;
233
234 try
235 {
236 IThemeService themeService = getThemeService( );
237
238 if ( StringUtils.isBlank( strTheme ) )
239 {
240 theme = themeService.getGlobalTheme( );
241 }
242 else
243 {
244 theme = themeService.getTheme( strTheme );
245 }
246 }
247 catch( ThemeNotAvailableException e )
248 {
249 theme = getDefaultTheme( );
250 }
251
252 return theme;
253 }
254
255
256
257
258
259
260
261 public static void setGlobalTheme( String strGlobalTheme )
262 {
263 IThemeService themeService;
264
265 try
266 {
267 themeService = getThemeService( );
268 themeService.setGlobalTheme( strGlobalTheme );
269 }
270 catch( ThemeNotAvailableException e )
271 {
272 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
273 }
274 }
275
276
277
278
279
280
281
282
283 public static ReferenceList getPageThemes( Locale locale )
284 {
285 ReferenceListt.html#ReferenceList">ReferenceList listThemes = new ReferenceList( );
286
287 try
288 {
289 IThemeService themeService = getThemeService( );
290 listThemes = themeService.getThemes( );
291 }
292 catch( ThemeNotAvailableException e )
293 {
294 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
295 }
296
297 String strGlobalTheme = I18nService.getLocalizedString( PROPERTY_USE_GLOBAL_THEME, locale );
298 listThemes.addItem( GLOBAL_THEME, strGlobalTheme );
299
300 return listThemes;
301 }
302
303
304
305
306
307
308
309
310 private static IThemeService getThemeService( ) throws ThemeNotAvailableException
311 {
312 IThemeService themeService = null;
313
314 if ( !isAvailable( ) )
315 {
316 throw new ThemeNotAvailableException( );
317 }
318
319 try
320 {
321 themeService = SpringContextService.getBean( BEAN_THEME_SERVICE );
322 }
323 catch( BeanDefinitionStoreException | NoSuchBeanDefinitionException | CannotLoadBeanClassException e )
324 {
325 throw new ThemeNotAvailableException( );
326 }
327
328 return themeService;
329 }
330
331
332
333
334
335
336 private static Theme getDefaultTheme( )
337 {
338 Themeal/business/style/Theme.html#Theme">Theme theme = new Theme( );
339 String strCodeTheme = AppPropertiesService.getProperty( PROPERTY_DEFAULT_CODE_THEME );
340 String strPathCss = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PATH_CSS );
341 String strPathImages = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PATH_IMAGE );
342 String strPathJs = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PATH_JS );
343 String strThemeAuthor = AppPropertiesService.getProperty( PROPERTY_DEFAULT_AUTHOR );
344 String strThemeAuthorUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_AUTHOR_URL );
345 String strThemeDescription = AppPropertiesService.getProperty( PROPERTY_DEFAULT_DESCRIPTION );
346 String strThemeLicence = AppPropertiesService.getProperty( PROPERTY_DEFAULT_LICENCE );
347 String strThemeVersion = AppPropertiesService.getProperty( PROPERTY_DEFAULT_VERSION );
348
349 theme.setCodeTheme( strCodeTheme );
350 theme.setPathCss( strPathCss );
351 theme.setPathImages( strPathImages );
352 theme.setPathJs( strPathJs );
353 theme.setThemeAuthor( strThemeAuthor );
354 theme.setThemeAuthorUrl( strThemeAuthorUrl );
355 theme.setThemeDescription( strThemeDescription );
356 theme.setThemeLicence( strThemeLicence );
357 theme.setThemeVersion( strThemeVersion );
358
359 return theme;
360 }
361
362
363
364
365
366
367
368
369
370
371 public static boolean isAvailable( )
372 {
373 Plugin pluginTheme = PluginService.getPlugin( THEME_PLUGIN_NAME );
374
375 return PluginService.isPluginEnable( THEME_PLUGIN_NAME ) && ( pluginTheme.getDbPoolName( ) != null )
376 && !AppConnectionService.NO_POOL_DEFINED.equals( pluginTheme.getDbPoolName( ) );
377 }
378
379
380
381
382
383
384
385
386 public static Themef="../../../../../../fr/paris/lutece/portal/business/style/Theme.html#Theme">Theme create( Theme theme )
387 {
388 try
389 {
390 IThemeService themeService = getThemeService( );
391
392 return themeService.create( theme );
393 }
394 catch( ThemeNotAvailableException e )
395 {
396 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
397
398 return null;
399 }
400 }
401
402
403
404
405
406
407
408
409 public static Themef="../../../../../../fr/paris/lutece/portal/business/style/Theme.html#Theme">Theme update( Theme theme )
410 {
411 try
412 {
413 IThemeService themeService = getThemeService( );
414
415 return themeService.update( theme );
416 }
417 catch( ThemeNotAvailableException e )
418 {
419 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
420
421 return null;
422 }
423 }
424
425
426
427
428
429
430
431 public static void remove( String strCodeTheme )
432 {
433 try
434 {
435 IThemeService themeService = getThemeService( );
436 themeService.remove( strCodeTheme );
437 }
438 catch( ThemeNotAvailableException e )
439 {
440 AppLogService.info( MESSAGE_THEME_NOT_AVAILABLE );
441 }
442 }
443
444
445
446
447
448
449 public static Collection<Theme> getThemesList( )
450 {
451 Collection<Theme> listThemes = new ArrayList<>( );
452
453 try
454 {
455 IThemeService themeService = getThemeService( );
456 listThemes = themeService.getThemesList( );
457 }
458 catch( ThemeNotAvailableException e )
459 {
460 Theme theme = getDefaultTheme( );
461 listThemes.add( theme );
462 }
463
464 return listThemes;
465 }
466
467
468
469
470
471
472 public static ReferenceList getThemes( )
473 {
474 ReferenceListt.html#ReferenceList">ReferenceList listThemes = new ReferenceList( );
475
476 try
477 {
478 IThemeService themeService = getThemeService( );
479 listThemes = themeService.getThemes( );
480 }
481 catch( ThemeNotAvailableException e )
482 {
483 Theme theme = getDefaultTheme( );
484 listThemes.addItem( theme.getCodeTheme( ), theme.getThemeDescription( ) );
485 }
486
487 return listThemes;
488 }
489
490
491
492
493
494
495
496
497 public static boolean isValidTheme( String strCodeTheme )
498 {
499 boolean bIsValidTheme = false;
500
501 try
502 {
503 IThemeService themeService = getThemeService( );
504 bIsValidTheme = themeService.isValidTheme( strCodeTheme );
505 }
506 catch( ThemeNotAvailableException e )
507 {
508 Theme theme = getDefaultTheme( );
509 bIsValidTheme = theme.getCodeTheme( ).equals( strCodeTheme );
510 }
511
512 return bIsValidTheme;
513 }
514 }