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.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 if( pm.isWorkflowTask( ) )
118 {
119 strBasePath = "module-workflow-" + pm.getPluginName( ) + "/" + strPath.replace( "{plugin_name}", pm.getPluginName( ) );
120 }
121 else
122 {
123 strBasePath = strBasePath.replace( "{plugin_name}", pm.getPluginName( ) );
124 }
125
126 return strBasePath + strFilename;
127 }
128
129 /**
130 * Returns the value of a string with first letter in caps
131 *
132 * @param strValue
133 * The value to be transformed
134 * @return The first letter is in Capital
135 */
136 protected String getFirstCaps( String strValue )
137 {
138 String strFirstLetter = strValue.substring( 0, 1 );
139 String strLettersLeft = strValue.substring( 1 );
140 String strValueCap = strFirstLetter.toUpperCase( ) + strLettersLeft.toLowerCase( );
141
142 return strValueCap;
143 }
144
145 /**
146 * Return the model filled with the plugin model
147 *
148 * @param pm
149 * The plugin model
150 * @return The model
151 */
152 protected Map<String, Object> getModel( PluginModel pm )
153 {
154 Map<String, Object> model = new HashMap<>( );
155 model.put( Markers.MARK_PLUGIN_MODEL, pm );
156
157 return model;
158 }
159
160 /**
161 * Build the code
162 *
163 * @param pm
164 * The plugin model
165 * @return The code
166 */
167 protected String build( PluginModel pm )
168 {
169 return build( getTemplate( ), getModel( pm ) );
170 }
171
172 /**
173 * Build the code
174 *
175 * @param strTemplate
176 * The code template
177 * @param pm
178 * The plugin model
179 * @return The code
180 */
181 protected String build( String strTemplate, PluginModel pm )
182 {
183 return build( strTemplate, getModel( pm ) );
184 }
185
186 /**
187 * Build the code
188 *
189 * @param model
190 * The model
191 * @return The code
192 */
193 protected String build( Map<String, Object> model )
194 {
195 return build( getTemplate( ), model );
196 }
197
198 /**
199 * Build the code
200 *
201 * @param strTemplate
202 * The code template
203 * @param model
204 * The model
205 * @return The code
206 */
207 protected String build( String strTemplate, Map<String, Object> model )
208 {
209 HtmlTemplate template = AppTemplateService.getTemplate( strTemplate, Locale.getDefault( ), model );
210
211 return template.getHtml( );
212 }
213
214 }