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.template;
35  
36  import fr.paris.lutece.portal.business.template.CommonsInclude;
37  import fr.paris.lutece.portal.service.datastore.DatastoreService;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.portal.service.util.AppLogService;
40  import fr.paris.lutece.util.ReferenceList;
41  import java.util.List;
42  
43  /**
44   * CommonsService
45   */
46  public class CommonsService
47  {
48      private static final String DSKEY_CURRENT_COMMONS_INCLUDE = "core.templates.currentCommonsInclude";
49  
50      private CommonsService( )
51      {
52          // Ctor
53      }
54  
55      /**
56       * Get the list of commons includes
57       * 
58       * @return The list
59       */
60      public static List<CommonsInclude> getCommonsIncludes( )
61      {
62          return SpringContextService.getBeansOfType( CommonsInclude.class );
63      }
64  
65      /**
66       * Activate a commons library
67       * 
68       * @param strKey
69       *            The commons key
70       */
71      public static void activateCommons( String strKey )
72      {
73          IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );
74  
75          CommonsInclude ciNew = getCommonsInclude( strKey );
76  
77          if ( ciNew == null )
78          {
79              return;
80          }
81  
82          CommonsInclude ciCurrent = getCurrentCommonsInclude( );
83  
84          // Remove auto-include of the current commons include
85          List<String> listAutoIncludes = serviceFMT.getAutoIncludes( );
86          if ( ciCurrent != null )
87          {
88              for ( String strExclude : ciCurrent.getFiles( ) )
89              {
90                  if ( ( listAutoIncludes != null ) && listAutoIncludes.contains( strExclude ) )
91                  {
92                      serviceFMT.removeAutoInclude( strExclude );
93                      AppLogService.info( "Existing Freemarker AutoInclude removed : {}", strExclude );
94                  }
95              }
96          }
97          // Add auto-include that aren't already present
98          for ( String strInclude : ciNew.getFiles( ) )
99          {
100             if ( ( listAutoIncludes != null ) && !listAutoIncludes.contains( strInclude ) )
101             {
102                 serviceFMT.addAutoInclude( strInclude );
103                 AppLogService.info( "New Freemarker AutoInclude added : {}", strInclude );
104             }
105         }
106 
107         setNewCommonsInclude( ciNew );
108     }
109 
110     /**
111      * Get the commons list
112      * 
113      * @return The list
114      */
115     public static ReferenceList getCommonsIncludeList( )
116     {
117         ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
118         for ( CommonsInclude ci : getCommonsIncludes( ) )
119         {
120             list.addItem( ci.getKey( ), ci.getName( ) );
121         }
122         return list;
123     }
124 
125     /**
126      * Get the current commons key
127      * 
128      * @return The key
129      */
130     public static String getCurrentCommonsKey( )
131     {
132         CommonsInclude ciCurrent = getCurrentCommonsInclude( );
133 
134         if ( ciCurrent != null )
135         {
136             return ciCurrent.getKey( );
137         }
138         else
139         {
140             return null;
141         }
142     }
143 
144     /**
145      * Get a commons include by its key
146      * 
147      * @param strKey
148      *            The key
149      * @return The commons include object
150      */
151     public static CommonsInclude getCommonsInclude( String strKey )
152     {
153         for ( CommonsInclude ci : getCommonsIncludes( ) )
154         {
155             if ( ci.getKey( ).equals( strKey ) )
156             {
157                 return ci;
158             }
159         }
160         return null;
161     }
162 
163     /**
164      * Get the default commons include
165      * 
166      * @return The commons include object
167      */
168     public static CommonsInclude getDefaultCommonsInclude( )
169     {
170         // get default commons include
171         for ( CommonsInclude ci : getCommonsIncludes( ) )
172         {
173             if ( ci.isDefault( ) )
174             {
175                 return ci;
176             }
177         }
178 
179         // if there's no default, returns the first one
180         if ( getCommonsIncludes( ).size( ) > 0 )
181         {
182             return getCommonsIncludes( ).get( 0 );
183         }
184 
185         return null;
186     }
187 
188     /**
189      * Get the current commons include
190      * 
191      * @return The commons include object
192      */
193     public static CommonsInclude getCurrentCommonsInclude( )
194     {
195         String strCurrentCommonsIncludeKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );
196 
197         if ( strCurrentCommonsIncludeKey != null )
198         {
199             CommonsInclude ci = getCommonsInclude( strCurrentCommonsIncludeKey );
200             if ( ci != null )
201             {
202                 return ci;
203             }
204         }
205 
206         CommonsInclude ci = getDefaultCommonsInclude( );
207         if ( ci != null )
208         {
209             setNewCommonsInclude( ci );
210             return ci;
211         }
212         else
213         {
214             return null;
215         }
216 
217     }
218 
219     /**
220      * Define the new commons include
221      * 
222      * @param ciNew
223      *            the new commons include
224      */
225     private static void setNewCommonsInclude( CommonsInclude ciNew )
226     {
227         DatastoreService.setDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, ciNew.getKey( ) );
228     }
229 
230 }