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             response.addCookie( cookie );
179         }
180     }
181 
182     /**
183      * Check if a given theme code is among valid known theme codes
184      * 
185      * @param strTheme
186      *            The theme
187      * @return true if valid
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      * Returns the global theme
203      *
204      * @return the global theme
205      */
206     public static String getGlobalTheme( )
207     {
208         Theme theme = getGlobalTheme( StringUtils.EMPTY );
209 
210         return theme.getCodeTheme( );
211     }
212 
213     /**
214      * Returns the global theme Object
215      *
216      * @return the global theme Object
217      */
218     public static Theme getGlobalThemeObject( )
219     {
220         return getGlobalTheme( StringUtils.EMPTY );
221     }
222 
223     /**
224      * Returns the global theme
225      * 
226      * @param strTheme
227      *            The theme
228      * @return the global theme
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      * Sets the global theme
257      *
258      * @param strGlobalTheme
259      *            The global theme
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      * Returns a reference list which contains all the themes
278      * 
279      * @param locale
280      *            The Locale
281      * @return a reference list
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      * Get the theme service
305      * 
306      * @return the theme service
307      * @throws ThemeNotAvailableException
308      *             If the theme is not available
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      * Return the default theme in properties
333      * 
334      * @return the default theme
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      * Check if the theme service is available. It must have the following requirement to be available :
364      * <ul>
365      * <li>The <code>plugin-theme</code> is activated</li>
366      * <li>The pool of the <code>plugin-theme</code> is defined</li>
367      * </ul>
368      * 
369      * @return true if it is available, false otherwise
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      * Create a new theme
381      * 
382      * @param theme
383      *            the theme
384      * @return The theme
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      * Update a theme
404      * 
405      * @param theme
406      *            the theme to update
407      * @return The updated theme
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      * Remove a theme
427      * 
428      * @param strCodeTheme
429      *            the code theme
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      * Get a collection of themes
446      * 
447      * @return a collection of themes
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      * Get the list of themes as a {@link ReferenceList}
469      * 
470      * @return a {@link ReferenceList}
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      * Check if the theme is valid
492      * 
493      * @param strCodeTheme
494      *            the code theme
495      * @return true if it is valid, false otherwise
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 }