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  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * Admin Jsp Bean Generator
49   */
50  public class AdminJspBeanGenerator extends AbstractGenerator
51  {
52      private static final String PATH_JAVA = "java/fr/paris/lutece/plugins/{plugin_name}/web/";
53      private static final String PATH_JAVA_WORKFLOW = "java/fr/paris/lutece/plugins/workflow/modules/{plugin_name}/web/";
54      private static final String PATH_KOTLIN = "kotlin/fr/paris/lutece/plugins/{plugin_name}/web/";
55      private static final String PREFIX_JSPBEAN = "Abstract";
56      private static final String PREFIX_JSPBEAN_PATH = "src/";
57  
58      private static final String SUFFIX_JAVA_EXTENSION = ".java";
59      private static final String SUFFIX_KOTLIN_EXTENSION = ".kt";
60  
61      private static final String SUFFIX_JSPBEAN_CLASS = "JspBean";
62  
63      private String _strAbstractParentBeanTemplate = "/generators/default/jspbean/gt_jspbean_abstract.html";
64      private List<AdminJspBeanFileConfig> _listFiles;
65  
66      public void setFiles( List<AdminJspBeanFileConfig> listFiles )
67      {
68          if ( listFiles != null )
69          {
70              _listFiles = (List<AdminJspBeanFileConfig>) ( ( (ArrayList<AdminJspBeanFileConfig>) listFiles ).clone( ) );
71          }
72          else
73          {
74              _listFiles = null;
75          }
76      }
77  
78      /**
79       * Set the parent bean template
80       * 
81       * @param strParent
82       *            The parent bean template
83       */
84      public void setAbstractParentBeanTemplate( String strParent )
85      {
86          _strAbstractParentBeanTemplate = strParent;
87      }
88  
89      /**
90       * {@inheritDoc }
91       */
92      public Map<String, String> generate( PluginModel pm, String generationSchemeName )
93      {
94          HashMap<String, String> map = new HashMap<>( );
95  
96          String strFilesPath = ( isKotlin( ) ) ? PATH_KOTLIN : ( pm.isWorkflowTask( ) ? PATH_JAVA_WORKFLOW.replace( "{plugin_name}",pm.getPluginNameForRessource( ) ) : PATH_JAVA );
97          String strSuffix = SUFFIX_JSPBEAN_CLASS + ( ( isKotlin( ) ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
98  
99          for ( Feature feature : pm.getFeatures( ) )
100         {
101             Collection<BusinessClass> listBusinessClasses = ModelService.getBusinessClassesByFeature( pm, feature.getId( ) );
102 
103             if ( listBusinessClasses.isEmpty( ) )
104             {
105                 for ( AdminJspBeanFileConfig file : _listFiles )
106                 {
107                     String strSuffixConfig = SUFFIX_JSPBEAN_CLASS + file.getSuffix( ) + ( ( isKotlin( ) ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
108                     String strFilename = feature.getFeatureName( ) + strSuffixConfig;
109                     String strPath = getFilePath( pm, file.getSourcePath( ) + ( isKotlin( ) ? PATH_KOTLIN : ( pm.isWorkflowTask( ) ? PATH_JAVA_WORKFLOW.replace( "{plugin_name}",pm.getPluginNameForRessource( ) ) : PATH_JAVA ) ), strFilename );
110                     String strSourceCode = getJspBeanCode( pm, feature.getFeatureName( ), feature.getFeatureRight( ), file.getTemplate( ),
111                             pm.getPluginNameAsRadicalPackage( ), pm.getPluginName( ) );
112                     map.put( strPath, strSourceCode );
113                 }
114             }
115             else
116             {
117                 for ( BusinessClass business : listBusinessClasses )
118                 {
119                     for ( AdminJspBeanFileConfig file : _listFiles )
120                     {
121                         String strSuffixConfig = SUFFIX_JSPBEAN_CLASS + file.getSuffix( )
122                                 + ( ( isKotlin( ) ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
123                         String strFilename = business.getBusinessClassCapsFirst( ) + strSuffixConfig;
124                         String strPath = getFilePath( pm, file.getSourcePath( ) + ( isKotlin( ) ? PATH_KOTLIN : ( pm.isWorkflowTask( ) ? PATH_JAVA_WORKFLOW.replace( "{plugin_name}",pm.getPluginNameForRessource( ) ) : PATH_JAVA ) ), strFilename );
125                         String strSourceCode = getJspBeanCode( pm, feature.getFeatureName( ), feature.getFeatureRight( ), business, file.getTemplate( ),
126                                 pm.getPluginNameAsRadicalPackage( ), pm.getPluginName( ) );
127                         map.put( strPath, strSourceCode );
128                     }
129                 }
130             }
131         }
132         
133         String strPath = getFilePath( pm, PREFIX_JSPBEAN_PATH + strFilesPath, PREFIX_JSPBEAN + strSuffix );
134         String strSourceCode = getAbstractJspBeanCode( pm, pm.getPluginNameAsRadicalPackage( ),
135                 pm.getPluginName( ) );
136         map.put( strPath, strSourceCode );
137 
138         return map;
139     }
140 
141     /**
142      * Return JspBean code
143      *
144      * @param pm
145      *            The plugin model
146      * @param strFeatureName
147      *            The feature name
148      * @param strFeatureRight
149      *            The feature right
150      * @param business
151      *            The business classes
152      * @return the template The source code of the Jsp Bean
153      */
154     private String getJspBeanCode( PluginModel pm, String strFeatureName, String strFeatureRight, BusinessClass business, String strTemplate,
155             String strRadicalPackage, String strBeanName )
156     {
157         Map<String, Object> model = getModel( pm );
158 
159         model.put( Markers.MARK_BUSINESS_CLASS, business );
160         model.put( Markers.MARK_FEATURE_NAME, strFeatureName );
161         model.put( Markers.MARK_FEATURE_RIGHT, strFeatureRight );
162 
163         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
164         model.put( Markers.MARK_BEAN_NAME, strBeanName );
165 
166         return build( strTemplate, model );
167     }
168 
169     /**
170      * Return JspBean code
171      *
172      * @param pm
173      *            The plugin model
174      * @param strFeatureName
175      *            The feature name
176      * @param strFeatureRight
177      *            The feature right
178      * @param business
179      *            The business classes
180      * @return the template The source code of the Jsp Bean
181      */
182     private String getJspBeanCode( PluginModel pm, String strFeatureName, String strFeatureRight, String strTemplate, String strRadicalPackage,
183             String strBeanName )
184     {
185         Map<String, Object> model = getModel( pm );
186 
187         model.put( Markers.MARK_FEATURE_NAME, strFeatureName );
188         model.put( Markers.MARK_FEATURE_RIGHT, strFeatureRight );
189 
190         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
191         model.put( Markers.MARK_BEAN_NAME, strBeanName );
192 
193         return build( strTemplate, model );
194     }
195 
196     /**
197      * Return JspBean code
198      *
199      * @param pm
200      *            The plugin model
201      * @param strFeatureName
202      *            The feature name
203      * @param strFeatureRight
204      *            The feature right
205      * @return the template The source code of the Jsp Bean
206      */
207     private String getAbstractJspBeanCode( PluginModel pm, String strRadicalPackage, String strBeanName )
208     {
209         Map<String, Object> model = getModel( pm );
210 
211         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
212         model.put( Markers.MARK_BEAN_NAME, strBeanName );
213 
214         return build( _strAbstractParentBeanTemplate, model );
215     }
216 }