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 = "AbstractPaginator";
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          String strFilesPath = ( isKotlin( ) ) ? PATH_KOTLIN : PATH_JAVA;
95          String strSuffix = SUFFIX_JSPBEAN_CLASS + ( ( isKotlin( ) ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
96          
97          for ( Feature feature : pm.getFeatures( ) )
98          {
99              Collection<BusinessClass> listBusinessClasses = ModelService.getBusinessClassesByFeature( pm, feature.getId( ) );
100 
101             if ( listBusinessClasses.isEmpty( ) )
102             {
103                 for ( AdminJspBeanFileConfig file : _listFiles )
104                 {
105                     String strSuffixConfig = SUFFIX_JSPBEAN_CLASS + file.getSuffix( ) + ( ( isKotlin( ) ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
106                     String strFilename = feature.getFeatureName( ) + strSuffixConfig;
107                     String strPath = getFilePath( pm, file.getSourcePath( ) + ( isKotlin( ) ? PATH_KOTLIN : PATH_JAVA ), strFilename );
108                     String strSourceCode = getJspBeanCode( pm, feature.getFeatureName( ), feature.getFeatureRight( ), file.getTemplate( ),
109                             pm.getPluginNameAsRadicalPackage( ), pm.getPluginName( ) );
110                     map.put( strPath, strSourceCode );
111                 }
112             }
113             else
114             {
115                 for ( BusinessClass business : listBusinessClasses )
116                 {
117                     for ( AdminJspBeanFileConfig file : _listFiles )
118                     {
119                         String strSuffixConfig = SUFFIX_JSPBEAN_CLASS + file.getSuffix( )
120                                 + ( ( isKotlin( ) ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
121                         String strFilename = business.getBusinessClassCapsFirst( ) + strSuffixConfig;
122                         String strPath = getFilePath( pm, file.getSourcePath( ) + ( isKotlin( ) ? PATH_KOTLIN : PATH_JAVA ), strFilename );
123                         String strSourceCode = getJspBeanCode( pm, feature.getFeatureName( ), feature.getFeatureRight( ), business, file.getTemplate( ),
124                                 pm.getPluginNameAsRadicalPackage( ), pm.getPluginName( ) );
125                         map.put( strPath, strSourceCode );
126                     }
127                 }
128             }
129         }
130         String strPath = getFilePath( pm, PREFIX_JSPBEAN_PATH + strFilesPath, PREFIX_JSPBEAN + strSuffix );
131         String strSourceCode = getAbstractJspBeanCode( pm, pm.getPluginNameAsRadicalPackage( ),
132                 pm.getPluginName( ) );
133         map.put( strPath, strSourceCode );
134 
135         return map;
136     }
137 
138     /**
139      * Return JspBean code
140      *
141      * @param pm
142      *            The plugin model
143      * @param strFeatureName
144      *            The feature name
145      * @param strFeatureRight
146      *            The feature right
147      * @param business
148      *            The business classes
149      * @return the template The source code of the Jsp Bean
150      */
151     private String getJspBeanCode( PluginModel pm, String strFeatureName, String strFeatureRight, BusinessClass business, String strTemplate,
152             String strRadicalPackage, String strBeanName )
153     {
154         Map<String, Object> model = getModel( pm );
155 
156         model.put( Markers.MARK_BUSINESS_CLASS, business );
157         model.put( Markers.MARK_FEATURE_NAME, strFeatureName );
158         model.put( Markers.MARK_FEATURE_RIGHT, strFeatureRight );
159         
160         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
161         model.put( Markers.MARK_BEAN_NAME, strBeanName );
162 
163         return build( strTemplate, model );
164     }
165 
166     /**
167      * Return JspBean code
168      *
169      * @param pm
170      *            The plugin model
171      * @param strFeatureName
172      *            The feature name
173      * @param strFeatureRight
174      *            The feature right
175      * @param business
176      *            The business classes
177      * @return the template The source code of the Jsp Bean
178      */
179     private String getJspBeanCode( PluginModel pm, String strFeatureName, String strFeatureRight, String strTemplate, String strRadicalPackage,
180             String strBeanName )
181     {
182         Map<String, Object> model = getModel( pm );
183 
184         model.put( Markers.MARK_FEATURE_NAME, strFeatureName );
185         model.put( Markers.MARK_FEATURE_RIGHT, strFeatureRight );
186         
187         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
188         model.put( Markers.MARK_BEAN_NAME, strBeanName );
189 
190         return build( strTemplate, model );
191     }
192 
193     /**
194      * Return JspBean code
195      *
196      * @param pm
197      *            The plugin model
198      * @param strFeatureName
199      *            The feature name
200      * @param strFeatureRight
201      *            The feature right
202      * @return the template The source code of the Jsp Bean
203      */
204     private String getAbstractJspBeanCode( PluginModel pm, String strRadicalPackage, String strBeanName )
205     {
206         Map<String, Object> model = getModel( pm );
207 
208         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
209         model.put( Markers.MARK_BEAN_NAME, strBeanName );
210 
211         return build( _strAbstractParentBeanTemplate, model );
212     }
213 }