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