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