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.Configuration;
38  import fr.paris.lutece.plugins.pluginwizard.business.model.PluginModel;
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   *
47   * The business classes representing the business layer of the plugin is generated
48   *
49   */
50  public class BusinessClassGenerator extends AbstractGenerator
51  {
52      private static final String PATH = "SOURCE/java/fr/paris/lutece/plugins/";
53      private static final String PATH_SUFFIX = "/business/";
54      private static final String SUFFIX_FILE_NAME_ABSTRACT_FILTER_DAO = "abstractFilterDao.html"; // must be unique
55      private List<BusinessFileConfig> _listFiles;
56  
57      /**
58       * Set the list of files config
59       * 
60       * @param listFiles
61       *            The list of files
62       */
63      public void setFiles( List<BusinessFileConfig> listFiles )
64      {
65          if ( listFiles != null )
66          {
67              _listFiles = (List<BusinessFileConfig>) ( ( (ArrayList<BusinessFileConfig>) listFiles ).clone( ) );
68          }
69          else
70          {
71              _listFiles = null;
72          }
73      }
74  
75      /**
76       * {@inheritDoc }
77       */
78      @Override
79      public Map<String, String> generate( PluginModel pm, String generationSchemeName )
80      {
81          HashMap<String, String> map = new HashMap<>( );
82          Collection<BusinessClass> listAllBusinessClasses = pm.getBusinessClasses( );
83  
84          String strPluginName = pm.getPluginNameAsPackage( );
85          String strRadicalPackage = pm.getPluginNameAsRadicalPackage( );
86          String strRadicalPath = pm.getPluginNameAsRadicalPath( );
87  
88          int counterFilterDao = 0; // Must be max at one because we want an unic general abstractFilterDao file
89  
90          for ( BusinessClass businessClass : listAllBusinessClasses )
91          {
92              for ( BusinessFileConfig file : _listFiles )
93              {
94                  String strClassName;
95  
96                  // Corresponds to the first time the abstractFilterDao file is created.
97                  if ( file.getTemplate( ).endsWith( SUFFIX_FILE_NAME_ABSTRACT_FILTER_DAO ) && counterFilterDao < 1 )
98                  {
99                      strClassName = file.getPrefix( );
100                     counterFilterDao = 1;
101                     // If abstractFilterDao already exists.
102                 }
103                 else
104                     if ( file.getTemplate( ).endsWith( SUFFIX_FILE_NAME_ABSTRACT_FILTER_DAO ) && counterFilterDao >= 1 )
105                     {
106                         continue;
107                         // Creation of each business file except abstractFilterDao
108                     }
109                     else
110                     {
111                         strClassName = file.getPrefix( ) + businessClass.getBusinessClass( ) + file.getSuffix( );
112                     }
113 
114                 String strFilename = strClassName + ".java";
115                 String strSourceCode;
116                 
117                 if ( pm.isWorkflowTask( ) ) 
118                 {
119                 	strSourceCode = getSourceCode( strPluginName, businessClass, file.getTemplate( ), strRadicalPackage, pm.getPluginName( ), pm.getType( ), pm.getConfiguration( ) );
120                 }
121                 else
122                 {
123                 	strSourceCode = getSourceCode( strPluginName, businessClass, file.getTemplate( ), strRadicalPackage, pm.getPluginName( ), pm.getType( ) );
124                 }                
125                 
126                 strSourceCode = strSourceCode.replace( "&lt;", "<" );
127                 strSourceCode = strSourceCode.replace( "&gt;", ">" );
128                 strSourceCode = strSourceCode.replace( "@i18n", "#i18n" );
129 
130                 String strPath = PATH.replace( "SOURCE", file.getSourcePath( ) ) + strRadicalPath + PATH_SUFFIX;
131 
132                 map.put( getFilePath( pm, strPath, strFilename ), strSourceCode );
133             }
134         }
135 
136         return map;
137     }
138 
139     /**
140      * Returns the source code of a business object
141      * 
142      * @param strPluginName
143      *            The plugin name
144      * @param businessClass
145      *            The business class
146      * @param strTemplate
147      *            The type of generation(DAO,Home,etc)
148      * @return The java source code of the business object
149      */
150     private String getSourceCode( String strPluginName, BusinessClass businessClass, String strTemplate, String strRadicalPackage, String strBeanName, String strProjectType )
151     {
152         Map<String, Object> model = new HashMap<>( );
153         model.put( Markers.MARK_BUSINESS_CLASS, businessClass );
154         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
155         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
156         model.put( Markers.MARK_BEAN_NAME, strBeanName );
157         model.put( Markers.MARK_PROJECT_TYPE , strProjectType );
158 
159         return build( strTemplate, model );
160     }
161     
162     /**
163      * Returns the source code of a business object
164      * 
165      * @param strPluginName
166      *            The plugin name
167      * @param businessClass
168      *            The business class
169      * @param strTemplate
170      *            The type of generation(DAO,Home,etc)
171      * @return The java source code of the business object
172      */
173     private String getSourceCode( String strPluginName, BusinessClass businessClass, String strTemplate, String strRadicalPackage, String strBeanName, String strProjectType, Configuration configuration )
174     {
175         Map<String, Object> model = new HashMap<>( );
176         model.put( Markers.MARK_BUSINESS_CLASS, businessClass );
177         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
178         model.put( Markers.MARK_RADICAL_PACKAGE, strRadicalPackage );
179         model.put( Markers.MARK_BEAN_NAME, strBeanName );
180         model.put( Markers.MARK_PROJECT_TYPE , strProjectType );
181         model.put( Markers.MARK_CONFIGURATION, configuration );
182 
183         return build( strTemplate, model );
184     }
185 }