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.PluginModel;
37  import fr.paris.lutece.portal.service.template.AppTemplateService;
38  import fr.paris.lutece.util.html.HtmlTemplate;
39  
40  import java.util.HashMap;
41  import java.util.Locale;
42  import java.util.Map;
43  
44  /**
45   * AbstractGenerator
46   *
47   */
48  public abstract class AbstractGenerator implements Generator
49  {
50      protected static final String MARK_KOTLIN = "kotlin";
51  
52      private String _strTemplate;
53      private boolean _bKotlin;
54  
55      /**
56       * Set kotlin
57       * 
58       * @param bKotlin
59       *            true if kotlin generation
60       */
61      public void setKotlin( boolean bKotlin )
62      {
63          _bKotlin = bKotlin;
64      }
65  
66      /**
67       * Return true if generation is for Kotlin
68       * 
69       * @return true if generation is for Kotlin
70       */
71      protected boolean isKotlin( )
72      {
73          return _bKotlin;
74      }
75  
76      /**
77       * Returns the Template
78       *
79       * @return The Template
80       */
81      public String getTemplate( )
82      {
83          return _strTemplate;
84      }
85  
86      /**
87       * Sets the Template
88       *
89       * @param strTemplate
90       *            The Template
91       */
92      public void setTemplate( String strTemplate )
93      {
94          _strTemplate = strTemplate;
95      }
96  
97      /**
98       * Build the file path
99       * 
100      * @param pm
101      *            The Plugin Model
102      * @param strPath
103      *            The relative path
104      * @param strFilename
105      *            The file name
106      * @return The full path
107      */
108     protected String getFilePath( PluginModel pm, String strPath, String strFilename )
109     {
110         String strBasePath = "plugin-{plugin_name}/" + strPath;
111 
112         if ( pm.isModule( ) )
113         {
114             strBasePath = "module-" + pm.getPluginName( ) + "/"
115                     + strPath.replace( "{plugin_name}", ( pm.getPluginName( ).split( "-" ) [0] + "/modules/" + pm.getPluginName( ).split( "-" ) [1] ) );
116         }
117         else
118         {
119             strBasePath = strBasePath.replace( "{plugin_name}", pm.getPluginName( ) );
120         }
121 
122         return strBasePath + strFilename;
123     }
124 
125     /**
126      * Returns the value of a string with first letter in caps
127      * 
128      * @param strValue
129      *            The value to be transformed
130      * @return The first letter is in Capital
131      */
132     protected String getFirstCaps( String strValue )
133     {
134         String strFirstLetter = strValue.substring( 0, 1 );
135         String strLettersLeft = strValue.substring( 1 );
136         String strValueCap = strFirstLetter.toUpperCase( ) + strLettersLeft.toLowerCase( );
137 
138         return strValueCap;
139     }
140 
141     /**
142      * Return the model filled with the plugin model
143      * 
144      * @param pm
145      *            The plugin model
146      * @return The model
147      */
148     protected Map<String, Object> getModel( PluginModel pm )
149     {
150         Map<String, Object> model = new HashMap<>( );
151         model.put( Markers.MARK_PLUGIN_MODEL, pm );
152 
153         return model;
154     }
155 
156     /**
157      * Build the code
158      * 
159      * @param pm
160      *            The plugin model
161      * @return The code
162      */
163     protected String build( PluginModel pm )
164     {
165         return build( getTemplate( ), getModel( pm ) );
166     }
167 
168     /**
169      * Build the code
170      * 
171      * @param strTemplate
172      *            The code template
173      * @param pm
174      *            The plugin model
175      * @return The code
176      */
177     protected String build( String strTemplate, PluginModel pm )
178     {
179         return build( strTemplate, getModel( pm ) );
180     }
181 
182     /**
183      * Build the code
184      * 
185      * @param model
186      *            The model
187      * @return The code
188      */
189     protected String build( Map<String, Object> model )
190     {
191         return build( getTemplate( ), model );
192     }
193 
194     /**
195      * Build the code
196      * 
197      * @param strTemplate
198      *            The code template
199      * @param model
200      *            The model
201      * @return The code
202      */
203     protected String build( String strTemplate, Map<String, Object> model )
204     {
205         HtmlTemplate template = AppTemplateService.getTemplate( strTemplate, Locale.getDefault( ), model );
206 
207         return template.getHtml( );
208     }
209 
210 }