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.plugins.pluginwizard.service.generator;
35  
36  import fr.paris.lutece.plugins.pluginwizard.business.model.BusinessClass;
37  import fr.paris.lutece.plugins.pluginwizard.business.model.Feature;
38  import fr.paris.lutece.plugins.pluginwizard.business.model.PluginModel;
39  import fr.paris.lutece.plugins.pluginwizard.service.ModelService;
40  import fr.paris.lutece.plugins.pluginwizard.util.Utils;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import java.util.Collection;
45  import java.util.HashMap;
46  import java.util.Locale;
47  import java.util.Map;
48  
49  /**
50   *
51   * Generates Html files used as template to construct back office UI
52   *
53   */
54  public class AdminTemplateGenerator extends AbstractGenerator
55  {
56      private static final String PATH = "webapp/WEB-INF/templates/admin/plugins/{plugin_name}/";
57      private static String [ ] _template_prefix = {
58              "create_", "modify_", "manage_"
59      };
60      private String _strTabsTemplate;
61  
62      /**
63       * {@inheritDoc }
64       * 
65       * @param pm
66       */
67      @Override
68      public Map generate( PluginModel pm, String generationSchemeName )
69      {
70          HashMap map = new HashMap( );
71  
72          String strPluginName = pm.getPluginNameAsPackage( );
73          String strLink = pm.getPluginNameAsRadicalPath( );
74  
75          // for each feature,which business classes are attached to
76          Collection<Feature> listFeatures = pm.getFeatures( );
77  
78          for ( Feature feature : listFeatures )
79          {
80              Collection<BusinessClass> listBusinessClasses = ModelService.getBusinessClassesByFeature( pm, feature.getId( ) );
81  
82              for ( BusinessClass businessClass : listBusinessClasses )
83              {
84                  Feature admin_feature = ModelService.getFeatureByBusinessClass( pm, businessClass.getId( ) );
85  
86                  for ( int i = 0; i < _template_prefix.length; i++ )
87                  {
88                  	String strPath = "";
89                      String strSuffix = ".html";
90   
91                      //manage files case where plural business name is used 
92                      if ( i == 2 )
93                      {
94                          strPath = getFilePath( pm, PATH, _template_prefix [i] + businessClass.getPluralBusinessClass( ).toLowerCase( ) + strSuffix );
95                      }
96                      else
97                      {
98                          strPath = getFilePath( pm, PATH, _template_prefix [i] + businessClass.getBusinessClass( ).toLowerCase( ) + strSuffix );
99                      }
100 
101                     String strSourceCode = getCreateHtmlCode( listBusinessClasses, admin_feature, businessClass, i, strPluginName, strLink );
102                     map.put( strPath, strSourceCode );
103                 }
104             }
105 
106             // Add the main template where all the business management interface will be accessible
107             String strPath = getFilePath( pm, PATH, feature.getFeatureName( ).toLowerCase( ) + "_tabs.html" );
108 
109             String strSourceCode = getTabsHtmlCode( listBusinessClasses, strPluginName, feature, strLink );
110             map.put( strPath, strSourceCode );
111         }
112 
113         return map;
114     }
115 
116     /**
117      * Gets the code of a create template for a specific business object
118      *
119      * @param listAllBusinessClasses
120      *            A list of business classes attached to plugin
121      * @param businessClass
122      *            The instance of the business class
123      * @param nTemplateType
124      * @param strLinkJsp
125      *            The link to jsp
126      * @param strPluginName
127      *            The plugin name
128      * @return The html code of the create template
129      */
130     private String getCreateHtmlCode( Collection<BusinessClass> listAllBusinessClasses, Feature admin_feature, BusinessClass businessClass, int nTemplateType,
131             String strPluginName, String strLinkJsp )
132     {
133         Map<String, Object> model = new HashMap<String, Object>( );
134 
135         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
136         model.put( Markers.MARK_I18N_BRACKETS_OPEN, "@@i18n{" );
137         model.put( Markers.MARK_I18N_BRACKETS_CLOSE, "}" );
138         model.put( Markers.MARK_MACRO, "@" );
139         model.put( Markers.MARK_VARIABLE, "@@" );
140         model.put( Markers.MARK_BRACKETS_OPEN, "${" );
141         model.put( Markers.MARK_BRACKETS_CLOSE, "}" );
142         model.put( Markers.MARK_BUSINESS_CLASS, businessClass );
143         model.put( Markers.MARK_LIST_BUSINESS_CLASSES, listAllBusinessClasses );
144         model.put( Markers.MARK_ADMIN_FEATURE, admin_feature );
145         model.put( Markers.MARK_INCLUDE, "@@include" );
146         model.put( Markers.MARK_PLUGIN_PATH, strLinkJsp );
147 
148         model.put( Markers.MARK_TEMPLATE_TYPE, nTemplateType );
149 
150         HtmlTemplate template = AppTemplateService.getTemplate( getTemplate( ), Locale.getDefault( ), model );
151 
152         return template.getHtml( ).replace( "@@", "#" );
153     }
154 
155     /**
156      * Gets the code of a create template for a specific business object
157      *
158      * @param listAllBusinessClasses
159      *            A list of business classes attached to plugin
160      * @param strPluginName
161      * @param strLinkJsp
162      *            The link to jsp
163      * @param feature
164      *            The feature
165      * @return The html code of the create template
166      */
167     private String getTabsHtmlCode( Collection<BusinessClass> listAllBusinessClasses, String strPluginName, Feature feature, String strLinkJsp )
168     {
169         Map<String, Object> model = new HashMap<String, Object>( );
170 
171         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
172         model.put( Markers.MARK_FEATURE, feature );
173         model.put( Markers.MARK_I18N_BRACKETS_OPEN, "@@i18n{" );
174         model.put( Markers.MARK_I18N_BRACKETS_CLOSE, "}" );
175         model.put( Markers.MARK_MACRO, "@" );
176         model.put( Markers.MARK_VARIABLE, "@@" );
177         model.put( Markers.MARK_BRACKETS_OPEN, "${" );
178         model.put( Markers.MARK_BRACKETS_CLOSE, "}" );
179         model.put( Markers.MARK_MACRO_DEF, "@@macro" );
180         model.put( Markers.MARK_LIST_BUSINESS_CLASSES, listAllBusinessClasses );
181         model.put( Markers.MARK_PLUGIN_PATH, strLinkJsp );
182 
183         HtmlTemplate template = AppTemplateService.getTemplate( _strTabsTemplate, Locale.getDefault( ), model );
184 
185         return template.getHtml( ).replace( "@@", "#" );
186     }
187 
188     /**
189      * @param tabsTemplate
190      *            the tabsTemplate to set
191      */
192     public void setTabsTemplate( String tabsTemplate )
193     {
194         _strTabsTemplate = tabsTemplate;
195     }
196 }