View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.CommonsImport;
37  import fr.paris.lutece.portal.business.template.CommonsInclude;
38  import fr.paris.lutece.portal.service.datastore.DatastoreService;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.util.ReferenceList;
42  import java.util.List;
43  import java.util.Map;
44  
45  import org.apache.commons.lang3.StringUtils;
46  
47  /**
48   * CommonsService
49   */
50  public class CommonsService
51  {
52      private static final String DSKEY_CURRENT_COMMONS_INCLUDE = "core.templates.currentCommonsInclude";
53  
54      private CommonsService( )
55      {
56          // Ctor
57      }
58  
59      /**
60       * Get the list of commons includes
61       * 
62       * @return The list
63       */
64      public static List<CommonsInclude> getCommonsIncludes( )
65      {
66          return SpringContextService.getBeansOfType( CommonsInclude.class );
67      }
68  
69      /**
70       * Get the list of commons imports
71       * 
72       * @return The list
73       */
74      public static List<CommonsImport> getCommonsImports( )
75      {
76          return SpringContextService.getBeansOfType( CommonsImport.class );
77      }
78  
79      /**
80       * Activate a commons library
81       * 
82       * @param strKey
83       *            The commons key
84       */
85      public static void activateCommons( String strKey )
86      {
87          CommonsInclude comIncNew = getCommonsInclude( strKey );
88          activateCommonsInclude( comIncNew );
89          CommonsImport comImpNew = getCommonsImport( strKey );
90          activateCommonsImport( comImpNew );
91  
92          if ( comIncNew != null || comImpNew != null )
93          {
94              setNewCommonsKey( strKey );
95          }
96      }
97  
98      /**
99       * Activate a commons include
100      * 
101      * @param ciNew
102      *            The new commons include
103      */
104     private static void activateCommonsInclude( CommonsInclude ciNew )
105     {
106         if ( ciNew == null )
107         {
108             return;
109         }
110 
111         IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );
112 
113         CommonsInclude ciCurrent = getCurrentCommonsInclude( );
114 
115         // Remove auto-include of the current commons include
116         List<String> listAutoIncludes = serviceFMT.getAutoIncludes( );
117         if ( ciCurrent != null )
118         {
119             for ( String strExclude : ciCurrent.getFiles( ) )
120             {
121                 if ( ( listAutoIncludes != null ) && listAutoIncludes.contains( strExclude ) )
122                 {
123                     serviceFMT.removeAutoInclude( strExclude );
124                     AppLogService.info( "Existing Freemarker AutoInclude removed : {}", strExclude );
125                 }
126             }
127         }
128         // Add auto-include that aren't already present
129         for ( String strInclude : ciNew.getFiles( ) )
130         {
131             if ( ( listAutoIncludes != null ) && !listAutoIncludes.contains( strInclude ) )
132             {
133                 serviceFMT.addAutoInclude( strInclude );
134                 AppLogService.info( "New Freemarker AutoInclude added : {}", strInclude );
135             }
136         }
137     }
138 
139     /**
140      * Activate a commons import
141      * 
142      * @param ciNew
143      *            The new commons import
144      */
145     private static void activateCommonsImport( CommonsImport ciNew )
146     {
147         if ( ciNew == null )
148         {
149             return;
150         }
151 
152         IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );
153 
154         CommonsImport ciCurrent = getCurrentCommonsImport( );
155 
156         // Remove auto-import of the current commons import
157         Map<String,String> mapAutoImports = serviceFMT.getAutoImports( );
158         if ( ciCurrent != null )
159         {
160             for ( Map.Entry<String, String> mapFilesEntry : ciCurrent.getMapFiles( ).entrySet( ) )
161             {
162                 serviceFMT.removeAutoImport( mapFilesEntry.getKey( ) );
163                 AppLogService.info( "Existing Freemarker AutoImport removed : {} as {}", mapFilesEntry.getValue( ), mapFilesEntry.getKey( ) );
164             }
165         }
166         // Add auto-import that aren't already present
167         for ( Map.Entry<String, String> mapFilesEntry : ciNew.getMapFiles( ).entrySet( ) )
168         {
169             serviceFMT.addAutoImport( mapFilesEntry.getKey( ), mapFilesEntry.getValue( ) );
170             AppLogService.info( "New Freemarker AutoImport added : {} as {}", mapFilesEntry.getValue( ), mapFilesEntry.getKey( ) );
171         }
172     }
173 
174     /**
175      * Get the commons list
176      * 
177      * @return The list
178      */
179     public static ReferenceList getCommonsList( )
180     {
181         ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
182         for ( CommonsInclude ci : getCommonsIncludes( ) )
183         {
184             list.addItem( ci.getKey( ), ci.getName( ) );
185         }
186         for ( CommonsImport ci : getCommonsImports( ) )
187         {
188             if ( !list.stream( ).anyMatch( item -> StringUtils.equals( item.getCode( ), ci.getKey( ) ) ) )
189             {
190                 list.addItem( ci.getKey( ), ci.getName( ) );
191             }
192         }
193         return list;
194     }
195 
196     /**
197      * Get the current commons key
198      * 
199      * @return The key
200      */
201     public static String getCurrentCommonsKey( )
202     {
203         String strCurrentKey = null;
204 
205         CommonsInclude comIncCurrent = getCurrentCommonsInclude( );
206 
207         if ( comIncCurrent != null )
208         {
209             strCurrentKey = comIncCurrent.getKey( );
210         }
211 
212         CommonsImport comImpCurrent = getCurrentCommonsImport( );
213 
214         if ( comImpCurrent != null )
215         {
216             strCurrentKey = comImpCurrent.getKey( );
217         }
218 
219         return strCurrentKey;
220     }
221 
222     /**
223      * Get a commons include by its key
224      * 
225      * @param strKey
226      *            The key
227      * @return The commons include object
228      */
229     public static CommonsInclude getCommonsInclude( String strKey )
230     {
231         for ( CommonsInclude ci : getCommonsIncludes( ) )
232         {
233             if ( ci.getKey( ).equals( strKey ) )
234             {
235                 return ci;
236             }
237         }
238         return null;
239     }
240 
241     /**
242      * Get a commons import by its key
243      * 
244      * @param strKey
245      *            The key
246      * @return The commons import object
247      */
248     public static CommonsImport getCommonsImport( String strKey )
249     {
250         for ( CommonsImport ci : getCommonsImports( ) )
251         {
252             if ( ci.getKey( ).equals( strKey ) )
253             {
254                 return ci;
255             }
256         }
257         return null;
258     }
259 
260     /**
261      * Get the default commons include
262      * 
263      * @return The commons include object
264      */
265     public static CommonsInclude getDefaultCommonsInclude( )
266     {
267         // get default commons include
268         for ( CommonsInclude ci : getCommonsIncludes( ) )
269         {
270             if ( ci.isDefault( ) )
271             {
272                 return ci;
273             }
274         }
275 
276         // if there's no default, returns the first one
277         if ( getCommonsIncludes( ).size( ) > 0 )
278         {
279             return getCommonsIncludes( ).get( 0 );
280         }
281 
282         return null;
283     }
284 
285     /**
286      * Get the default commons import
287      * 
288      * @return The commons import object
289      */
290     public static CommonsImport getDefaultCommonsImport( )
291     {
292         // get default commons import
293         for ( CommonsImport ci : getCommonsImports( ) )
294         {
295             if ( ci.isDefault( ) )
296             {
297                 return ci;
298             }
299         }
300 
301         // if there's no default, returns the first one
302         if ( getCommonsImports( ).size( ) > 0 )
303         {
304             return getCommonsImports( ).get( 0 );
305         }
306 
307         return null;
308     }
309 
310     /**
311      * Get the current commons include
312      * 
313      * @return The commons include object
314      */
315     public static CommonsInclude getCurrentCommonsInclude( )
316     {
317         String strCurrentCommonsKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );
318 
319         if ( strCurrentCommonsKey != null )
320         {
321             CommonsInclude ci = getCommonsInclude( strCurrentCommonsKey );
322             if ( ci != null )
323             {
324                 return ci;
325             }
326         }
327 
328         CommonsInclude ci = getDefaultCommonsInclude( );
329         if ( ci != null )
330         {
331             setNewCommonsKey( ci.getKey( ) );
332             return ci;
333         }
334 
335         return null;
336     }
337 
338     /**
339      * Get the current commons import
340      * 
341      * @return The commons import object
342      */
343     public static CommonsImport getCurrentCommonsImport( )
344     {
345         String strCurrentCommonsKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );
346 
347         if ( strCurrentCommonsKey != null )
348         {
349             CommonsImport ci = getCommonsImport( strCurrentCommonsKey );
350             if ( ci != null )
351             {
352                 return ci;
353             }
354         }
355 
356         CommonsImport ci = getDefaultCommonsImport( );
357         if ( ci != null )
358         {
359             setNewCommonsKey( ci.getKey( ) );
360             return ci;
361         }
362 
363         return null;
364     }
365 
366     /**
367      * Define the new commons key
368      * 
369      * @param strNewKey
370      *            the new commons key
371      */
372     private static void setNewCommonsKey( String strNewKey )
373     {
374         DatastoreService.setDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, strNewKey );
375     }
376 
377 }