1 /*
2 * Copyright (c) 2002-2014, Mairie de 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 /**
48 * This Service is used to retreive HTML templates, stored as files in the
49 * WEB-INF/templates directory of the webapp,
50 * to build the user interface. It provides a cache feature to prevent from
51 * loading file each time it is asked.
52 */
53 public final class AppTemplateService
54 {
55 // Variables
56 private static String _strTemplateDefaultPath;
57 private static IFreeMarkerTemplateService _freeMarkerTemplateService;
58
59 /**
60 * Protected constructor
61 */
62 private AppTemplateService( )
63 {
64 }
65
66 /**
67 * Initializes the service with the templates's path
68 * @param strTemplatePath 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 * Initializes autoincludes for plugins.
78 */
79 public static void initAutoIncludes( )
80 {
81 // register core
82 Plugin corePlugin = PluginService.getCore( );
83 addPluginMacros( corePlugin );
84
85 // register plugins
86 for ( Plugin plugin : PluginService.getPluginList( ) )
87 {
88 addPluginMacros( plugin );
89 }
90 }
91
92 /**
93 * Adds the plugin macros.
94 *
95 * @param plugin the plugin
96 */
97 private static void addPluginMacros( Plugin plugin )
98 {
99 for ( String strFileName : plugin.getFreeMarkerMacrosFiles( ) )
100 {
101 AppLogService.info( "New freemarker autoinclude : " + strFileName + " from " + plugin.getName( ) );
102 getFreeMarkerTemplateService( ).addPluginMacros( strFileName );
103 }
104 }
105
106 /**
107 * Reset the cache
108 */
109 public static void resetCache( )
110 {
111 getFreeMarkerTemplateService( ).resetCache( );
112 }
113
114 /**
115 * Resets the configuration cache
116 */
117 public static void resetConfiguration( )
118 {
119 getFreeMarkerTemplateService( ).resetConfiguration( );
120 }
121
122 /**
123 * Returns a reference on a template object (load the template or get it
124 * from the cache if present.)
125 *
126 * @param strTemplate The name of the template
127 * @return The template object.
128 */
129 public static HtmlTemplate getTemplate( String strTemplate )
130 {
131 return getTemplate( strTemplate, _strTemplateDefaultPath );
132 }
133
134 /**
135 * Returns a reference on a template object (load the template or get it
136 * from the cache if present.)
137 *
138 * @param strTemplate The name of the template
139 * @param strPath The specific path to load the template
140 * @return The template object.
141 * @since 1.3.1
142 */
143 public static HtmlTemplate getTemplate( String strTemplate, String strPath )
144 {
145 return getTemplate( strTemplate, strPath, null, null );
146 }
147
148 ////////////////////////////////////////////////////////////////////////////
149 // v1.5
150
151 /**
152 * Returns a reference on a template object (load the template or get it
153 * from the cache if present.)
154 *
155 * @param strTemplate The name of the template
156 * @param locale The current locale to localize the template
157 * @return The template object.
158 * @since 1.5
159 */
160 public static HtmlTemplate getTemplate( String strTemplate, Locale locale )
161 {
162 return getTemplate( strTemplate, _strTemplateDefaultPath, locale, null );
163 }
164
165 /**
166 * Returns a reference on a template object (load the template or get it
167 * from the cache if present.)
168 * @param strTemplate The name of the template
169 * @param locale The current locale to localize the template
170 * @param model the model to use for loading
171 * @return The template object.
172 * @since 1.5
173 */
174 public static HtmlTemplate getTemplate( String strTemplate, Locale locale, Object model )
175 {
176 HtmlTemplate template;
177
178 // Load the template from the file
179 template = getTemplate( strTemplate, _strTemplateDefaultPath, locale, model );
180
181 return template;
182 }
183
184 /**
185 * Returns a reference on a template object (load the template or get it
186 * from the cache if present.)
187 *
188 * @param strTemplate The name of the template
189 * @param strPath The specific path to load the template
190 * @param locale The current locale to localize the template
191 * @param model the model to use for loading
192 * @return The template object.
193 * @since 1.5
194 */
195 public static HtmlTemplate getTemplate( String strTemplate, String strPath, Locale locale, Object model )
196 {
197 HtmlTemplate template;
198
199 // Load the template from the file
200 template = loadTemplate( strPath, strTemplate, locale, model );
201
202 return template;
203 }
204
205 /**
206 * Returns a reference on a template object
207 *
208 * <br />
209 * <b>Deprecated</b> Using Freemarker without cache is huge CPU consuming
210 *
211 * @param strFreemarkerTemplateData The content of the template
212 * @param locale The current {@link Locale} to localize the template
213 * @param model The model
214 * @return The template object
215 * @since 1.5
216 */
217 @Deprecated
218 public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateData, Locale locale, Object model )
219 {
220 HtmlTemplate template;
221 // Load the template from the file
222 template = loadTemplate( strFreemarkerTemplateData, locale, model );
223
224 return template;
225 }
226
227 /**
228 * Load the template from the file
229 * @param strTemplate The name of the template
230 * @param strPath The specific path to load the template
231 * @param locale The current locale to localize the template
232 * @param model the model to use for loading
233 * @return The loaded template
234 */
235 private static HtmlTemplate loadTemplate( String strPath, String strTemplate, Locale locale, Object model )
236 {
237 HtmlTemplate template;
238 template = getFreeMarkerTemplateService( ).loadTemplate( strPath, strTemplate, locale, model );
239
240 if ( locale != null )
241 {
242 String strLocalized = I18nService.localize( template.getHtml( ), locale );
243 template = new HtmlTemplate( strLocalized );
244 }
245
246 template = new HtmlTemplate( DatastoreService.replaceKeys( template.getHtml( ) ) );
247
248 return template;
249 }
250
251 /**
252 * Load the template from the file
253 * WARNING : This method must not be used in front office (no cache
254 * management available).
255 *
256 * <br />
257 * <b>Deprecated</b> Using Freemarker without cache is huge CPU consuming
258 *
259 * @param strTemplateData The data of the template
260 * @param locale The current locale to localize the template
261 * @param model the model to use for loading
262 * @return The loaded template
263 */
264 @Deprecated
265 private static HtmlTemplate loadTemplate( String strTemplateData, Locale locale, Object model )
266 {
267 HtmlTemplate template;
268 template = getFreeMarkerTemplateService( ).loadTemplate( strTemplateData, locale, model );
269
270 if ( locale != null )
271 {
272 String strLocalized = I18nService.localize( template.getHtml( ), locale );
273 template = new HtmlTemplate( strLocalized );
274 }
275
276 return template;
277 }
278
279 /**
280 * Get the instance of free marker template service
281 * @return the instance of free marker template service
282 */
283 private static IFreeMarkerTemplateService getFreeMarkerTemplateService( )
284 {
285 if ( _freeMarkerTemplateService == null )
286 {
287 _freeMarkerTemplateService = FreeMarkerTemplateService.getInstance( );
288 _freeMarkerTemplateService.init( _strTemplateDefaultPath );
289 }
290
291 return _freeMarkerTemplateService;
292 }
293 }