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.service.datastore.DatastoreService;
37  import fr.paris.lutece.portal.service.i18n.I18nService;
38  import fr.paris.lutece.portal.service.i18n.I18nTemplateMethod;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.plugin.PluginService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import java.util.Locale;
45  import java.util.Map;
46  
47  /**
48   * This Service is used to retreive HTML templates, stored as files in the WEB-INF/templates directory of the webapp, to build the user interface. It provides a
49   * cache feature to prevent from loading file each time it is asked.
50   */
51  public final class AppTemplateService
52  {
53      // Variables
54      private static String _strTemplateDefaultPath;
55      private static IFreeMarkerTemplateService _freeMarkerTemplateService;
56  
57      /**
58       * Protected constructor
59       */
60      private AppTemplateService( )
61      {
62      }
63  
64      /**
65       * Initializes the service with the templates's path
66       * 
67       * @param strTemplatePath
68       *            The template path
69       */
70      public static void init( String strTemplatePath )
71      {
72          _strTemplateDefaultPath = strTemplatePath;
73          getFreeMarkerTemplateService( ).setSharedVariable( "i18n", new I18nTemplateMethod( ) );
74          
75      }
76  
77      /**
78       * Initializes auto-includes and auto-imports for plugins.
79       */
80      public static void initMacros( )
81      {
82          // register core (commons declared in core.xml)
83          Plugin corePlugin = PluginService.getCore( );
84          addPluginAutoIncludes( corePlugin );
85          addPluginAutoImports( corePlugin );
86  
87          // register plugins
88          for ( Plugin plugin : PluginService.getPluginList( ) )
89          {
90              addPluginAutoIncludes( plugin );
91              addPluginAutoImports( plugin );
92          }
93  
94          // activate current commons stored in the datastore
95          CommonsService.activateCommons( CommonsService.getCurrentCommonsKey( ) );
96      }
97  
98      /**
99       * Adds the plugin auto-includes.
100      *
101      * @param plugin
102      *            the plugin
103      */
104     private static void addPluginAutoIncludes( Plugin plugin )
105     {
106         for ( String strFileName : plugin.getFreeMarkerAutoIncludes( ) )
107         {
108             AppLogService.info( "New freemarker autoinclude : {} from {}", strFileName, plugin.getName( ) );
109             getFreeMarkerTemplateService( ).addPluginAutoInclude( strFileName );
110         }
111     }
112 
113     /**
114      * Adds the plugin auto-imports.
115      *
116      * @param plugin
117      *            the plugin
118      */
119     private static void addPluginAutoImports( Plugin plugin )
120     {
121         for ( Map.Entry<String, String> importEntry : plugin.getFreeMarkerAutoImports( ).entrySet( ) )
122         {
123             AppLogService.info( "New freemarker autoimport : {} as {} from {}", importEntry.getValue( ), importEntry.getKey( ), plugin.getName( ) );
124             getFreeMarkerTemplateService( ).addPluginAutoImport( importEntry.getKey( ), importEntry.getValue( ) );
125         }
126     }
127 
128     /**
129      * Reset the cache
130      */
131     public static void resetCache( )
132     {
133         getFreeMarkerTemplateService( ).resetCache( );
134     }
135 
136     /**
137      * Resets the configuration cache
138      */
139     public static void resetConfiguration( )
140     {
141         getFreeMarkerTemplateService( ).resetConfiguration( );
142     }
143 
144     /**
145      * Returns a reference on a template object (load the template or get it from the cache if present.)
146      *
147      * @param strTemplate
148      *            The name of the template
149      * @return The template object.
150      */
151     public static HtmlTemplate getTemplate( String strTemplate )
152     {
153         return getTemplate( strTemplate, _strTemplateDefaultPath );
154     }
155 
156     /**
157      * Returns a reference on a template object (load the template or get it from the cache if present.)
158      *
159      * @param strTemplate
160      *            The name of the template
161      * @param strPath
162      *            The specific path to load the template
163      * @return The template object.
164      * @since 1.3.1
165      */
166     public static HtmlTemplate getTemplate( String strTemplate, String strPath )
167     {
168         return getTemplate( strTemplate, strPath, null, null );
169     }
170 
171     // //////////////////////////////////////////////////////////////////////////
172     // v1.5
173 
174     /**
175      * Returns a reference on a template object (load the template or get it from the cache if present.)
176      *
177      * @param strTemplate
178      *            The name of the template
179      * @param locale
180      *            The current locale to localize the template
181      * @return The template object.
182      * @since 1.5
183      */
184     public static HtmlTemplate getTemplate( String strTemplate, Locale locale )
185     {
186         return getTemplate( strTemplate, _strTemplateDefaultPath, locale, null );
187     }
188 
189     /**
190      * Returns a reference on a template object (load the template or get it from the cache if present.)
191      * 
192      * @param strTemplate
193      *            The name of the template
194      * @param locale
195      *            The current locale to localize the template
196      * @param model
197      *            the model to use for loading
198      * @return The template object.
199      * @since 1.5
200      */
201     public static HtmlTemplate getTemplate( String strTemplate, Locale locale, Object model )
202     {
203         HtmlTemplate template;
204 
205         // Load the template from the file
206         template = getTemplate( strTemplate, _strTemplateDefaultPath, locale, model );
207 
208         return template;
209     }
210 
211     /**
212      * Returns a reference on a template object (load the template or get it from the cache if present.)
213      *
214      * @param strTemplate
215      *            The name of the template
216      * @param strPath
217      *            The specific path to load the template
218      * @param locale
219      *            The current locale to localize the template
220      * @param model
221      *            the model to use for loading
222      * @return The template object.
223      * @since 1.5
224      */
225     public static HtmlTemplate getTemplate( String strTemplate, String strPath, Locale locale, Object model )
226     {
227         HtmlTemplate template;
228 
229         // Load the template from the file
230         template = loadTemplate( strPath, strTemplate, locale, model );
231 
232         return template;
233     }
234 
235     /**
236      * Returns a reference on a template object  using the template data passed in parameter or getting from cache if present
237      *
238      *
239      * @param strFreemarkerTemplateData
240      *            The content of the template
241      * @param locale
242      *            The current {@link Locale} to localize the template
243      * @param model
244      *            The model
245      * @return The template object
246      * @since 1.5
247      */
248     public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateData, Locale locale, Object model )
249     {
250         HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateData, locale, model );
251 
252         if ( locale != null )
253         {
254             String strLocalized = I18nService.localize( template.getHtml( ), locale );
255             template = new HtmlTemplate( strLocalized );
256         }
257         return template;
258     }
259     
260     
261     /**
262      * Returns a reference on a template object using the template data passed in parameter or getting from cache if present
263      *
264      * @param strFreemarkerTemplateName The name of the template ( Must be a Fully qualified name for example skin.plugins.myplugin.manage_my_objects )
265      * @param strFreemarkerTemplateData
266      *            The content of the template
267      * @param locale
268      *            The current {@link Locale} to localize the template
269      * @param model
270      *            The model
271      * @param bResetCache true if the template stored in cache must be updated by the content of the strFreemarkerTemplateDa            
272      * @return The template object
273      * @since 7.0.5
274      */
275     public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateName,String strFreemarkerTemplateData, Locale locale, Object model,boolean bResetCache )
276     {
277         HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateName,strFreemarkerTemplateData, locale, model,bResetCache );
278 
279         if ( locale != null )
280         {
281             String strLocalized = I18nService.localize( template.getHtml( ), locale );
282             template = new HtmlTemplate( strLocalized );
283         }
284         return template;
285     }
286 
287     /**
288      * Load the template from the file
289      * 
290      * @param strTemplate
291      *            The name of the template
292      * @param strPath
293      *            The specific path to load the template
294      * @param locale
295      *            The current locale to localize the template
296      * @param model
297      *            the model to use for loading
298      * @return The loaded template
299      */
300     private static HtmlTemplate loadTemplate( String strPath, String strTemplate, Locale locale, Object model )
301     {
302         HtmlTemplate template;
303         template = getFreeMarkerTemplateService( ).loadTemplate( strPath, strTemplate, locale, model );
304 
305         if ( locale != null )
306         {
307             String strLocalized = I18nService.localize( template.getHtml( ), locale );
308             template = new HtmlTemplate( strLocalized );
309         }
310 
311         template = new HtmlTemplate( DatastoreService.replaceKeys( template.getHtml( ) ) );
312 
313         return template;
314     }
315 
316     /**
317      * Get the instance of free marker template service
318      * 
319      * @return the instance of free marker template service
320      */
321     private static IFreeMarkerTemplateService getFreeMarkerTemplateService( )
322     {
323         if ( _freeMarkerTemplateService == null )
324         {
325             _freeMarkerTemplateService = FreeMarkerTemplateService.getInstance( );
326             _freeMarkerTemplateService.init( _strTemplateDefaultPath );
327         }
328 
329         return _freeMarkerTemplateService;
330     }
331 }