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.plugins.pluginwizard.service.generator;
35  
36  import fr.paris.lutece.plugins.pluginwizard.business.model.Application;
37  import fr.paris.lutece.plugins.pluginwizard.business.model.BusinessClass;
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 front office
52   *
53   */
54  public class XPageTemplateGenerator extends AbstractGenerator
55  {
56      private static final String PATH = "webapp/WEB-INF/templates/skin/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          String strPluginName = pm.getPluginNameAsPackage( );
72  
73          // for each application,which business classes are attached to
74          Collection<Application> listApplication = pm.getApplications( );
75  
76          for ( Application application : listApplication )
77          {
78              Collection<BusinessClass> listBusinessClasses = ModelService.getBusinessClassesByApplication( pm, application.getId( ) );
79  
80              if ( listBusinessClasses.isEmpty( ) )
81              {
82                  String strPath = getFilePath( pm, PATH, application.getApplicationName( ) + ".html" );
83                  String strSourceCode = getCreateHtmlCode( application, strPluginName );
84                  map.put( strPath, strSourceCode );
85              }
86              else
87              {
88                  for ( BusinessClass businessClass : listBusinessClasses )
89                  {
90                      for ( int i = 0; i < _template_prefix.length; i++ )
91                      {
92                          String strSuffix = ( i == 2 ) ? "s.html" : ".html";
93                          String strPath = getFilePath( pm, PATH, _template_prefix [i] + businessClass.getBusinessClass( ).toLowerCase( ) + strSuffix );
94  
95                          String strSourceCode = getCreateHtmlCode( listBusinessClasses, application, businessClass, i, strPluginName );
96                          map.put( strPath, strSourceCode );
97                      }
98                  }
99  
100                 // Add the main template where all the business management interface will be accessible
101                 String strPath = getFilePath( pm, PATH, application.getApplicationName( ).toLowerCase( ) + "_tabs.html" );
102 
103                 String strSourceCode = getTabsHtmlCode( listBusinessClasses, strPluginName, application );
104                 map.put( strPath, strSourceCode );
105             }
106         }
107 
108         return map;
109     }
110 
111     /**
112      * Gets the code of a create template for a specific business object
113      *
114      * @param listAllBusinessClasses
115      *            A list of business classes attached to plugin
116      * @param businessClass
117      *            The instance of the business class
118      * @param nTemplateType
119      *            The type of template
120      * @param application
121      *            The application
122      * @param strPluginName
123      *            The plugin name
124      * @return The html code of the create template
125      */
126     private String getCreateHtmlCode( Collection<BusinessClass> listAllBusinessClasses, Application application, BusinessClass businessClass, int nTemplateType,
127             String strPluginName )
128     {
129         Map<String, Object> model = new HashMap<>( );
130 
131         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
132         model.put( Markers.MARK_I18N_BRACKETS_OPEN, "@@i18n{" );
133         model.put( Markers.MARK_I18N_BRACKETS_CLOSE, "}" );
134         model.put( Markers.MARK_MACRO, "@" );
135         model.put( Markers.MARK_VARIABLE, "@@" );
136         model.put( Markers.MARK_BRACKETS_OPEN, "${" );
137         model.put( Markers.MARK_BRACKETS_CLOSE, "}" );
138         model.put( Markers.MARK_BUSINESS_CLASS, businessClass );
139         model.put( Markers.MARK_LIST_BUSINESS_CLASSES, listAllBusinessClasses );
140         model.put( Markers.MARK_APPLICATION, application );
141         model.put( Markers.MARK_INCLUDE, "@@include" );
142 
143         model.put( Markers.MARK_TEMPLATE_TYPE, nTemplateType );
144 
145         HtmlTemplate template = AppTemplateService.getTemplate( getTemplate( ), Locale.getDefault( ), model );
146 
147         return template.getHtml( ).replace( "@@", "#" );
148     }
149 
150     /**
151      * Gets the code of a tab template for a specific business object
152      *
153      * @param listAllBusinessClasses
154      *            A list of business classes attached to plugin
155      * @param strPluginName
156      *            The plugin name
157      * @param application
158      *            The application
159      * @return The html code of the tab template
160      */
161     private String getTabsHtmlCode( Collection<BusinessClass> listAllBusinessClasses, String strPluginName, Application application )
162     {
163         Map<String, Object> model = new HashMap<>( );
164 
165         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
166         model.put( Markers.MARK_APPLICATION, application );
167         model.put( Markers.MARK_I18N_BRACKETS_OPEN, "@@i18n{" );
168         model.put( Markers.MARK_I18N_BRACKETS_CLOSE, "}" );
169         model.put( Markers.MARK_MACRO, "@" );
170         model.put( Markers.MARK_VARIABLE, "@@" );
171         model.put( Markers.MARK_BRACKETS_OPEN, "${" );
172         model.put( Markers.MARK_BRACKETS_CLOSE, "}" );
173         model.put( Markers.MARK_MACRO_DEF, "@@macro" );
174         model.put( Markers.MARK_LIST_BUSINESS_CLASSES, listAllBusinessClasses );
175 
176         HtmlTemplate template = AppTemplateService.getTemplate( _strTabsTemplate, Locale.getDefault( ), model );
177 
178         return template.getHtml( ).replace( "@@", "#" );
179     }
180 
181     /**
182      * Gets the code of a create template for a specific business object
183      *
184      * @param application
185      *            The application
186      * @param strPluginName
187      *            The plugin name
188      * @return The html code of the create template
189      */
190     private String getCreateHtmlCode( Application application, String strPluginName )
191     {
192         Map<String, Object> model = new HashMap<>( );
193 
194         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
195         model.put( Markers.MARK_I18N_BRACKETS_OPEN, "@@i18n{" );
196         model.put( Markers.MARK_I18N_BRACKETS_CLOSE, "}" );
197         model.put( Markers.MARK_MACRO, "@" );
198         model.put( Markers.MARK_VARIABLE, "@@" );
199         model.put( Markers.MARK_BRACKETS_OPEN, "${" );
200         model.put( Markers.MARK_BRACKETS_CLOSE, "}" );
201         model.put( Markers.MARK_APPLICATION, application );
202         model.put( Markers.MARK_INCLUDE, "@@include" );
203 
204         HtmlTemplate template = AppTemplateService.getTemplate( getTemplate( ), Locale.getDefault( ), model );
205 
206         return template.getHtml( ).replace( "@@", "#" );
207     }
208 
209     /**
210      * @param tabsTemplate
211      *            the tabsTemplate to set
212      */
213     public void setTabsTemplate( String tabsTemplate )
214     {
215         _strTabsTemplate = tabsTemplate;
216     }
217 }