CommonsService.java

  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.template;

  35. import fr.paris.lutece.portal.business.template.CommonsInclude;
  36. import fr.paris.lutece.portal.service.datastore.DatastoreService;
  37. import fr.paris.lutece.portal.service.spring.SpringContextService;
  38. import fr.paris.lutece.portal.service.util.AppLogService;
  39. import fr.paris.lutece.util.ReferenceList;
  40. import java.util.List;

  41. /**
  42.  * CommonsService
  43.  */
  44. public class CommonsService
  45. {
  46.     private static final String DSKEY_CURRENT_COMMONS_INCLUDE = "core.templates.currentCommonsInclude";

  47.     private CommonsService( )
  48.     {
  49.         // Ctor
  50.     }

  51.     /**
  52.      * Get the list of commons includes
  53.      *
  54.      * @return The list
  55.      */
  56.     public static List<CommonsInclude> getCommonsIncludes( )
  57.     {
  58.         return SpringContextService.getBeansOfType( CommonsInclude.class );
  59.     }

  60.     /**
  61.      * Activate a commons library
  62.      *
  63.      * @param strKey
  64.      *            The commons key
  65.      */
  66.     public static void activateCommons( String strKey )
  67.     {
  68.         IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );

  69.         CommonsInclude ciNew = getCommonsInclude( strKey );

  70.         if ( ciNew == null )
  71.         {
  72.             return;
  73.         }

  74.         CommonsInclude ciCurrent = getCurrentCommonsInclude( );

  75.         // Remove auto-include of the current commons include
  76.         List<String> listAutoIncludes = serviceFMT.getAutoIncludes( );
  77.         if ( ciCurrent != null )
  78.         {
  79.             for ( String strExclude : ciCurrent.getFiles( ) )
  80.             {
  81.                 if ( ( listAutoIncludes != null ) && listAutoIncludes.contains( strExclude ) )
  82.                 {
  83.                     serviceFMT.removeAutoInclude( strExclude );
  84.                     AppLogService.info( "Existing Freemarker AutoInclude removed : {}", strExclude );
  85.                 }
  86.             }
  87.         }
  88.         // Add auto-include that aren't already present
  89.         for ( String strInclude : ciNew.getFiles( ) )
  90.         {
  91.             if ( ( listAutoIncludes != null ) && !listAutoIncludes.contains( strInclude ) )
  92.             {
  93.                 serviceFMT.addAutoInclude( strInclude );
  94.                 AppLogService.info( "New Freemarker AutoInclude added : {}", strInclude );
  95.             }
  96.         }

  97.         setNewCommonsInclude( ciNew );
  98.     }

  99.     /**
  100.      * Get the commons list
  101.      *
  102.      * @return The list
  103.      */
  104.     public static ReferenceList getCommonsIncludeList( )
  105.     {
  106.         ReferenceList list = new ReferenceList( );
  107.         for ( CommonsInclude ci : getCommonsIncludes( ) )
  108.         {
  109.             list.addItem( ci.getKey( ), ci.getName( ) );
  110.         }
  111.         return list;
  112.     }

  113.     /**
  114.      * Get the current commons key
  115.      *
  116.      * @return The key
  117.      */
  118.     public static String getCurrentCommonsKey( )
  119.     {
  120.         CommonsInclude ciCurrent = getCurrentCommonsInclude( );

  121.         if ( ciCurrent != null )
  122.         {
  123.             return ciCurrent.getKey( );
  124.         }
  125.         else
  126.         {
  127.             return null;
  128.         }
  129.     }

  130.     /**
  131.      * Get a commons include by its key
  132.      *
  133.      * @param strKey
  134.      *            The key
  135.      * @return The commons include object
  136.      */
  137.     public static CommonsInclude getCommonsInclude( String strKey )
  138.     {
  139.         for ( CommonsInclude ci : getCommonsIncludes( ) )
  140.         {
  141.             if ( ci.getKey( ).equals( strKey ) )
  142.             {
  143.                 return ci;
  144.             }
  145.         }
  146.         return null;
  147.     }

  148.     /**
  149.      * Get the default commons include
  150.      *
  151.      * @return The commons include object
  152.      */
  153.     public static CommonsInclude getDefaultCommonsInclude( )
  154.     {
  155.         // get default commons include
  156.         for ( CommonsInclude ci : getCommonsIncludes( ) )
  157.         {
  158.             if ( ci.isDefault( ) )
  159.             {
  160.                 return ci;
  161.             }
  162.         }

  163.         // if there's no default, returns the first one
  164.         if ( getCommonsIncludes( ).size( ) > 0 )
  165.         {
  166.             return getCommonsIncludes( ).get( 0 );
  167.         }

  168.         return null;
  169.     }

  170.     /**
  171.      * Get the current commons include
  172.      *
  173.      * @return The commons include object
  174.      */
  175.     public static CommonsInclude getCurrentCommonsInclude( )
  176.     {
  177.         String strCurrentCommonsIncludeKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );

  178.         if ( strCurrentCommonsIncludeKey != null )
  179.         {
  180.             CommonsInclude ci = getCommonsInclude( strCurrentCommonsIncludeKey );
  181.             if ( ci != null )
  182.             {
  183.                 return ci;
  184.             }
  185.         }

  186.         CommonsInclude ci = getDefaultCommonsInclude( );
  187.         if ( ci != null )
  188.         {
  189.             setNewCommonsInclude( ci );
  190.             return ci;
  191.         }
  192.         else
  193.         {
  194.             return null;
  195.         }

  196.     }

  197.     /**
  198.      * Define the new commons include
  199.      *
  200.      * @param ciNew
  201.      *            the new commons include
  202.      */
  203.     private static void setNewCommonsInclude( CommonsInclude ciNew )
  204.     {
  205.         DatastoreService.setDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, ciNew.getKey( ) );
  206.     }

  207. }