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.Application;
37  import fr.paris.lutece.plugins.pluginwizard.business.model.BusinessClass;
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   *
49   * Class generating the Xpages
50   *
51   */
52  public class XPageGenerator extends AbstractGenerator
53  {
54      private static final String PATH_JAVA = "java/fr/paris/lutece/plugins/{plugin_name}/web/";
55      private static final String PATH_KOTLIN = "kotlin/fr/paris/lutece/plugins/{plugin_name}/web/";
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_XPAGE_CLASS = "XPage";
61      private static final String SUFFIX_XPAGE = "";
62  
63      private List<XPageFileConfig> _listFiles;
64  
65      /**
66       * Set the list of configuration files
67       * 
68       * @param listFiles
69       *            The list of files
70       */
71      public void setFiles( List<XPageFileConfig> listFiles )
72      {
73          if ( listFiles != null )
74          {
75              _listFiles = (List<XPageFileConfig>) ( ( (ArrayList<XPageFileConfig>) listFiles ).clone( ) );
76          }
77          else
78          {
79              _listFiles = null;
80          }
81      }
82  
83      /**
84       * {@inheritDoc }
85       * 
86       * @param pm
87       */
88      @Override
89      public Map<String, String> generate( PluginModel pm, String generationSchemeName )
90      {
91          HashMap<String, String> map = new HashMap<>( );
92          String strFilesPath = ( isKotlin( ) ) ? PATH_KOTLIN : PATH_JAVA;
93  
94          for ( Application application : pm.getApplications( ) )
95          {
96              Collection<BusinessClass> listBusinessClasses = ModelService.getBusinessClassesByApplication( pm, application.getId( ) );
97  
98              if ( listBusinessClasses.isEmpty( ) )
99              {
100                 for ( XPageFileConfig file : _listFiles )
101                 {
102                     String strSuffix = SUFFIX_XPAGE + file.getSuffix( ) + ( isKotlin( ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
103                     String strFilename = application.getApplicationClass( ) + strSuffix;
104                     String strPath = getFilePath( pm, file.getSourcePath( ) + ( isKotlin( ) ? PATH_KOTLIN : PATH_JAVA ), strFilename );
105                     String strSourceCode = getXPageCode( pm, application.getApplicationName( ), application.getId( ), application,
106                             pm.getPluginNameAsRadicalPackage( ), file.getTemplate( ) );
107                     map.put( strPath, strSourceCode );
108                 }
109             }
110             else
111             {
112                 for ( BusinessClass businessClass : listBusinessClasses )
113                 {
114                     for ( XPageFileConfig file : _listFiles )
115                     {
116                         String strSuffix = SUFFIX_XPAGE_CLASS + file.getSuffix( ) + ( isKotlin( ) ? SUFFIX_KOTLIN_EXTENSION : SUFFIX_JAVA_EXTENSION );
117                         String strFilename = businessClass.getBusinessClassCapsFirst( ) + strSuffix;
118                         String strPath = getFilePath( pm, file.getSourcePath( ) + ( isKotlin( ) ? PATH_KOTLIN : PATH_JAVA ), strFilename );
119                         String strSourceCode = getXPageCode( pm, application.getApplicationName( ), application.getId( ), application, businessClass,
120                                 pm.getPluginNameAsRadicalPackage( ), file.getTemplate( ) );
121                         map.put( strPath, strSourceCode );
122                     }
123                 }
124             }
125         }
126 
127         return map;
128     }
129 
130     /**
131      * Generates the XPage code
132      * 
133      * @param pm
134      *            The plugin model
135      * @param nApplicationId
136      *            id of the plugin application
137      * @param strApplicationName
138      *            the name of the application
139      * @param application
140      *            the application
141      * @param businessClass
142      *            the business class
143      * @return The code of the XPage generated
144      */
145     private String getXPageCode( PluginModel pm, String strApplicationName, int nApplicationId, Application application, BusinessClass businessClass,
146             String strRadicalPackage, String strTemplate )
147     {
148         Map<String, Object> model = getModel( pm );
149         model.put( Markers.MARK_PLUGIN, pm );
150         model.put( Markers.MARK_PLUGIN_APPLICATION, ModelService.getApplication( pm, nApplicationId ) );
151         model.put( Markers.MARK_APPLICATION, application );
152         model.put( Markers.MARK_APPLICATION_NAME, strApplicationName );
153         model.put( Markers.MARK_BUSINESS_CLASS, businessClass );
154         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
155 
156         return build( strTemplate, model );
157     }
158 
159     /**
160      * Generates the XPage code
161      * 
162      * @param pm
163      *            The plugin model
164      * @param nApplicationId
165      *            id of the plugin application
166      * @param strApplicationName
167      *            the name of the application
168      * @param application
169      *            the application
170      * @return The code of the XPage generated
171      */
172     private String getXPageCode( PluginModel pm, String strApplicationName, int nApplicationId, Application application, String strRadicalPackage,
173             String strTemplate )
174     {
175         Map<String, Object> model = getModel( pm );
176         model.put( Markers.MARK_PLUGIN, pm );
177         model.put( Markers.MARK_PLUGIN_APPLICATION, ModelService.getApplication( pm, nApplicationId ) );
178         model.put( Markers.MARK_APPLICATION, application );
179         model.put( Markers.MARK_APPLICATION_NAME, strApplicationName );
180         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
181 
182         return build( strTemplate, model );
183     }
184 }