View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.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   * ThemesService
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      // PROPERTIES
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      // MESSAGES
86      private static final String MESSAGE_THEME_NOT_AVAILABLE = "Theme service not available.";
87  
88      /**
89       * Private constructor
90       */
91      private ThemesService( )
92      {
93      }
94  
95      /**
96       * Get the theme code depending of the different priorities. The priorities are :
97       * <ol>
98       * <li>the theme of test (in case you want to test a page with a specific theme)</li>
99       * <li>the theme choosen by the user</li>
100      * <li>the global theme : the one choosen in the back office for the whole site</li>
101      * <li>the page theme : a theme specified for a page</li>
102      * </ol>
103      *
104      * @param data
105      *            The PageData object
106      * @param request
107      *            The HttpServletRequest
108      * @return the theme
109      */
110     public static Theme getTheme( PageData data, HttpServletRequest request )
111     {
112         String strTheme = StringUtils.EMPTY;
113 
114         // The code_theme of the page
115         String strPageTheme = data.getTheme( );
116 
117         if ( ( strPageTheme != null ) && ( strPageTheme.compareToIgnoreCase( GLOBAL_THEME ) != 0 ) )
118         {
119             strTheme = strPageTheme;
120         }
121 
122         // The theme of the user
123         String strUserTheme = getUserTheme( request );
124 
125         if ( strUserTheme != null )
126         {
127             strTheme = strUserTheme;
128         }
129 
130         // the test theme (choosen for a page to test the different theme from the backoffice theme section)
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      * Gets the theme selected by the user
143      *
144      * @param request
145      *            The HTTP request
146      * @return The theme if available otherwise null
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      * Sets the users theme using a cookie
164      * 
165      * @param request
166      *            The HTTP request
167      * @param response
168      *            The HTTP response
169      * @param strTheme
170      *            The Theme code
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      * Check if a given theme code is among valid known theme codes
185      * 
186      * @param strTheme
187      *            The theme
188      * @return true if valid
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      * Returns the global theme
204      *
205      * @return the global theme
206      */
207     public static String getGlobalTheme( )
208     {
209         Theme theme = getGlobalTheme( StringUtils.EMPTY );
210 
211         return theme.getCodeTheme( );
212     }
213 
214     /**
215      * Returns the global theme Object
216      *
217      * @return the global theme Object
218      */
219     public static Theme getGlobalThemeObject( )
220     {
221         return getGlobalTheme( StringUtils.EMPTY );
222     }
223 
224     /**
225      * Returns the global theme
226      * 
227      * @param strTheme
228      *            The theme
229      * @return the global theme
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      * Sets the global theme
258      *
259      * @param strGlobalTheme
260      *            The global theme
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      * Returns a reference list which contains all the themes
279      * 
280      * @param locale
281      *            The Locale
282      * @return a reference list
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      * Get the theme service
306      * 
307      * @return the theme service
308      * @throws ThemeNotAvailableException
309      *             If the theme is not available
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      * Return the default theme in properties
334      * 
335      * @return the default theme
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      * Check if the theme service is available. It must have the following requirement to be available :
365      * <ul>
366      * <li>The <code>plugin-theme</code> is activated</li>
367      * <li>The pool of the <code>plugin-theme</code> is defined</li>
368      * </ul>
369      * 
370      * @return true if it is available, false otherwise
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      * Create a new theme
382      * 
383      * @param theme
384      *            the theme
385      * @return The theme
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      * Update a theme
405      * 
406      * @param theme
407      *            the theme to update
408      * @return The updated theme
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      * Remove a theme
428      * 
429      * @param strCodeTheme
430      *            the code theme
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      * Get a collection of themes
447      * 
448      * @return a collection of themes
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      * Get the list of themes as a {@link ReferenceList}
470      * 
471      * @return a {@link ReferenceList}
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      * Check if the theme is valid
493      * 
494      * @param strCodeTheme
495      *            the code theme
496      * @return true if it is valid, false otherwise
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 }