AppTemplateService.java

  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. import fr.paris.lutece.portal.service.datastore.DatastoreService;
  36. import fr.paris.lutece.portal.service.i18n.I18nService;
  37. import fr.paris.lutece.portal.service.i18n.I18nTemplateMethod;
  38. import fr.paris.lutece.portal.service.plugin.Plugin;
  39. import fr.paris.lutece.portal.service.plugin.PluginService;
  40. import fr.paris.lutece.portal.service.util.AppLogService;
  41. import fr.paris.lutece.util.html.HtmlTemplate;

  42. import java.util.Locale;

  43. /**
  44.  * 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
  45.  * cache feature to prevent from loading file each time it is asked.
  46.  */
  47. public final class AppTemplateService
  48. {
  49.     // Variables
  50.     private static String _strTemplateDefaultPath;
  51.     private static IFreeMarkerTemplateService _freeMarkerTemplateService;

  52.     /**
  53.      * Protected constructor
  54.      */
  55.     private AppTemplateService( )
  56.     {
  57.     }

  58.     /**
  59.      * Initializes the service with the templates's path
  60.      *
  61.      * @param strTemplatePath
  62.      *            The template path
  63.      */
  64.     public static void init( String strTemplatePath )
  65.     {
  66.         _strTemplateDefaultPath = strTemplatePath;
  67.         getFreeMarkerTemplateService( ).setSharedVariable( "i18n", new I18nTemplateMethod( ) );
  68.     }

  69.     /**
  70.      * Initializes autoincludes for plugins.
  71.      */
  72.     public static void initAutoIncludes( )
  73.     {
  74.         // register core (commons declared in core.xml)
  75.         Plugin corePlugin = PluginService.getCore( );
  76.         addPluginMacros( corePlugin );

  77.         // register plugins
  78.         for ( Plugin plugin : PluginService.getPluginList( ) )
  79.         {
  80.             addPluginMacros( plugin );
  81.         }

  82.         // activate current commons stored in the datastore
  83.         CommonsService.activateCommons( CommonsService.getCurrentCommonsKey( ) );
  84.     }

  85.     /**
  86.      * Adds the plugin macros.
  87.      *
  88.      * @param plugin
  89.      *            the plugin
  90.      */
  91.     private static void addPluginMacros( Plugin plugin )
  92.     {
  93.         for ( String strFileName : plugin.getFreeMarkerMacrosFiles( ) )
  94.         {
  95.             AppLogService.info( "New freemarker autoinclude : {} from {}", strFileName, plugin.getName( ) );
  96.             getFreeMarkerTemplateService( ).addPluginMacros( strFileName );
  97.         }
  98.     }

  99.     /**
  100.      * Reset the cache
  101.      */
  102.     public static void resetCache( )
  103.     {
  104.         getFreeMarkerTemplateService( ).resetCache( );
  105.     }

  106.     /**
  107.      * Resets the configuration cache
  108.      */
  109.     public static void resetConfiguration( )
  110.     {
  111.         getFreeMarkerTemplateService( ).resetConfiguration( );
  112.     }

  113.     /**
  114.      * Returns a reference on a template object (load the template or get it from the cache if present.)
  115.      *
  116.      * @param strTemplate
  117.      *            The name of the template
  118.      * @return The template object.
  119.      */
  120.     public static HtmlTemplate getTemplate( String strTemplate )
  121.     {
  122.         return getTemplate( strTemplate, _strTemplateDefaultPath );
  123.     }

  124.     /**
  125.      * Returns a reference on a template object (load the template or get it from the cache if present.)
  126.      *
  127.      * @param strTemplate
  128.      *            The name of the template
  129.      * @param strPath
  130.      *            The specific path to load the template
  131.      * @return The template object.
  132.      * @since 1.3.1
  133.      */
  134.     public static HtmlTemplate getTemplate( String strTemplate, String strPath )
  135.     {
  136.         return getTemplate( strTemplate, strPath, null, null );
  137.     }

  138.     // //////////////////////////////////////////////////////////////////////////
  139.     // v1.5

  140.     /**
  141.      * Returns a reference on a template object (load the template or get it from the cache if present.)
  142.      *
  143.      * @param strTemplate
  144.      *            The name of the template
  145.      * @param locale
  146.      *            The current locale to localize the template
  147.      * @return The template object.
  148.      * @since 1.5
  149.      */
  150.     public static HtmlTemplate getTemplate( String strTemplate, Locale locale )
  151.     {
  152.         return getTemplate( strTemplate, _strTemplateDefaultPath, locale, null );
  153.     }

  154.     /**
  155.      * Returns a reference on a template object (load the template or get it from the cache if present.)
  156.      *
  157.      * @param strTemplate
  158.      *            The name of the template
  159.      * @param locale
  160.      *            The current locale to localize the template
  161.      * @param model
  162.      *            the model to use for loading
  163.      * @return The template object.
  164.      * @since 1.5
  165.      */
  166.     public static HtmlTemplate getTemplate( String strTemplate, Locale locale, Object model )
  167.     {
  168.         HtmlTemplate template;

  169.         // Load the template from the file
  170.         template = getTemplate( strTemplate, _strTemplateDefaultPath, locale, model );

  171.         return template;
  172.     }

  173.     /**
  174.      * Returns a reference on a template object (load the template or get it from the cache if present.)
  175.      *
  176.      * @param strTemplate
  177.      *            The name of the template
  178.      * @param strPath
  179.      *            The specific path to load the template
  180.      * @param locale
  181.      *            The current locale to localize the template
  182.      * @param model
  183.      *            the model to use for loading
  184.      * @return The template object.
  185.      * @since 1.5
  186.      */
  187.     public static HtmlTemplate getTemplate( String strTemplate, String strPath, Locale locale, Object model )
  188.     {
  189.         HtmlTemplate template;

  190.         // Load the template from the file
  191.         template = loadTemplate( strPath, strTemplate, locale, model );

  192.         return template;
  193.     }

  194.     /**
  195.      * Returns a reference on a template object  using the template data passed in parameter or getting from cache if present
  196.      *
  197.      *
  198.      * @param strFreemarkerTemplateData
  199.      *            The content of the template
  200.      * @param locale
  201.      *            The current {@link Locale} to localize the template
  202.      * @param model
  203.      *            The model
  204.      * @return The template object
  205.      * @since 1.5
  206.      */
  207.     public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateData, Locale locale, Object model )
  208.     {
  209.         HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateData, locale, model );

  210.         if ( locale != null )
  211.         {
  212.             String strLocalized = I18nService.localize( template.getHtml( ), locale );
  213.             template = new HtmlTemplate( strLocalized );
  214.         }
  215.         return template;
  216.     }
  217.    
  218.    
  219.     /**
  220.      * Returns a reference on a template object using the template data passed in parameter or getting from cache if present
  221.      *
  222.      * @param strFreemarkerTemplateName The name of the template ( Must be a Fully qualified name for example skin.plugins.myplugin.manage_my_objects )
  223.      * @param strFreemarkerTemplateData
  224.      *            The content of the template
  225.      * @param locale
  226.      *            The current {@link Locale} to localize the template
  227.      * @param model
  228.      *            The model
  229.      * @param bResetCache true if the template stored in cache must be updated by the content of the strFreemarkerTemplateDa            
  230.      * @return The template object
  231.      * @since 7.0.5
  232.      */
  233.     public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateName,String strFreemarkerTemplateData, Locale locale, Object model,boolean bResetCache )
  234.     {
  235.         HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateName,strFreemarkerTemplateData, locale, model,bResetCache );

  236.         if ( locale != null )
  237.         {
  238.             String strLocalized = I18nService.localize( template.getHtml( ), locale );
  239.             template = new HtmlTemplate( strLocalized );
  240.         }
  241.         return template;
  242.     }

  243.     /**
  244.      * Load the template from the file
  245.      *
  246.      * @param strTemplate
  247.      *            The name of the template
  248.      * @param strPath
  249.      *            The specific path to load the template
  250.      * @param locale
  251.      *            The current locale to localize the template
  252.      * @param model
  253.      *            the model to use for loading
  254.      * @return The loaded template
  255.      */
  256.     private static HtmlTemplate loadTemplate( String strPath, String strTemplate, Locale locale, Object model )
  257.     {
  258.         HtmlTemplate template;
  259.         template = getFreeMarkerTemplateService( ).loadTemplate( strPath, strTemplate, locale, model );

  260.         if ( locale != null )
  261.         {
  262.             String strLocalized = I18nService.localize( template.getHtml( ), locale );
  263.             template = new HtmlTemplate( strLocalized );
  264.         }

  265.         template = new HtmlTemplate( DatastoreService.replaceKeys( template.getHtml( ) ) );

  266.         return template;
  267.     }

  268.     /**
  269.      * Get the instance of free marker template service
  270.      *
  271.      * @return the instance of free marker template service
  272.      */
  273.     private static IFreeMarkerTemplateService getFreeMarkerTemplateService( )
  274.     {
  275.         if ( _freeMarkerTemplateService == null )
  276.         {
  277.             _freeMarkerTemplateService = FreeMarkerTemplateService.getInstance( );
  278.             _freeMarkerTemplateService.init( _strTemplateDefaultPath );
  279.         }

  280.         return _freeMarkerTemplateService;
  281.     }
  282. }