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  
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  import org.apache.commons.lang3.StringUtils;
46  
47  /**
48   * The Task form generator generates all the html forms files needed for workflow task creation
49   */
50  public class TaskFormsGenerator extends AbstractGenerator
51  {
52  	private static final String PATH = "webapp/WEB-INF/templates/admin/plugins/workflow/modules/{plugin_name}/";
53  	private static final String EXT_HTML = ".html";
54  	private List<String> _suffix;
55  	
56  	
57  	/**
58  	 * 
59  	 */
60  	private void setSuffixList( PluginModel pluginModel ) 
61  	{
62  		_suffix = new ArrayList<>( );	
63  		_suffix.add( "_information" );
64  		
65      	if( StringUtils.equals( pluginModel.getConfiguration( ).getWorkflowFormConfigRequired( ), "1" ) )
66      	{  		
67      		_suffix.add( "_config" );
68      	}
69      	
70      	if( StringUtils.equals( pluginModel.getConfiguration( ).getWorkflowFormTaskRequired( ), "1" ) )
71      	{	
72      		_suffix.add( "_form" );
73      	}
74  	}
75  	
76      /**
77       * {@inheritDoc }
78       * 
79       */
80  	@Override
81  	public Map<String, String> generate( PluginModel pluginModel, String generationSchemeName ) 
82  	{
83  	
84  		setSuffixList( pluginModel );
85  		
86  		HashMap map = new HashMap( );
87  	    String strTaskName = pluginModel.getConfiguration( ).getWorkflowTaskName( ).toLowerCase( );
88  	    if( strTaskName.length( ) > 4 )
89  	    {
90  	    	strTaskName = strTaskName.substring( 0, strTaskName.length( ) - 4 );
91  	    }
92  	    
93          for ( String strSuffix : _suffix )
94          {
95                  String strFormFile = "task_" + strTaskName + strSuffix + EXT_HTML;
96                  String strPath = getFilePath( pluginModel, PATH, strFormFile );
97                  String strTemplateType = strSuffix.startsWith("_") ? strSuffix.substring( 1 ) : strSuffix;
98                  
99                  String strSourceCode = getFormFile( pluginModel.getConfiguration( ), pluginModel.getBusinessClasses( ).get( 0 ), strTemplateType, pluginModel.getPluginName( ), this.getTemplate( ) );
100                 strSourceCode = strSourceCode.replace( "&lt;", "<" );
101                 strSourceCode = strSourceCode.replace( "&gt;", ">" );
102                 
103                 map.put( strPath, strSourceCode.replace( "@@", "#" ) );
104         }
105 
106 	        return map;
107 	}
108 
109     /**
110      * Produces text content of html form file used to build a workflow task
111      * 
112      * @param configuration
113      *            The instance of a workflow task configuration
114      * @param businessClass
115      *            The instance of the businessClass attached to workflow task (MyTaskConfig class)
116      * @param strTemplateType
117      *            the Template type ( form, config or information)
118      * @param strPluginName
119      *            The plugin name
120      * @param strTemplate
121      *            The template of form file
122      * @return The content of the form file
123      */
124     private String getFormFile( Configuration configuration, BusinessClass businessClass, String strTemplateType, String strPluginName, String strTemplate )
125     {
126         Map<String, Object> model = new HashMap<>( );
127         
128         model.put( Markers.MARK_CONFIGURATION, configuration );
129         model.put( Markers.MARK_BUSINESS_CLASS, businessClass );
130         model.put( Markers.MARK_TASK_NAME, configuration.getWorkflowTaskName( ) );
131         model.put( Markers.MARK_TEMPLATE_TYPE, strTemplateType );
132         model.put( Markers.MARK_PLUGIN_NAME, strPluginName );
133         
134         model.put( Markers.MARK_I18N_BRACKETS_OPEN, "@@i18n{" );
135         model.put( Markers.MARK_I18N_BRACKETS_CLOSE, "}" );
136         model.put( Markers.MARK_MACRO, "@" );
137         model.put( Markers.MARK_VARIABLE, "#" );
138         model.put( Markers.MARK_BRACKETS_OPEN, "${" );
139         model.put( Markers.MARK_BRACKETS_CLOSE, "}" );
140         model.put( Markers.MARK_INCLUDE, "@@include" );
141 
142         return build( strTemplate, model );
143     }
144 }