View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.workflow.modules.appointmentants.web;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang3.math.NumberUtils;
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.appointment.service.EntryService;
47  import fr.paris.lutece.plugins.appointment.service.FormService;
48  import fr.paris.lutece.plugins.genericattributes.business.Entry;
49  import fr.paris.lutece.plugins.workflow.modules.appointmentants.business.TaskAntsAppointmentConfig;
50  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
51  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
52  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
53  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.portal.service.util.AppPathService;
56  import fr.paris.lutece.util.ReferenceList;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  import fr.paris.lutece.util.url.UrlItem;
59  
60  /**
61   * 
62   * Task Component used to display a task's configuration page and allow its modification
63   *
64   */
65  public abstract class AbstractTaskAntsAppointmentComponent extends NoFormTaskComponent {
66  
67  	// MARKS
68  	private static final String MARK_CONFIG = "config";
69  	private static final String MARK_FORMS_LIST = "forms_list";
70  	private static final String MARK_FORM_FIELDS_LIST = "form_fields_list";
71  	private static final String MARK_FORM_ID = "id_form";
72  	private static final String MARK_TASK_TITLE = "taskTitle";
73  
74  	// PARAMETERS
75  	private static final String PARAMETER_SELECT_FORM_ID = "id_form_selection";
76  	public static final String PARAMETER_SELECT_FIELD_ENTRY_TITLE = "id_form_field_entry_selection";
77  	public static final String PARAMETER_SELECT_FORM = "selectForm";
78  	private static final String PARAMETER_ID_TASK = "id_task";
79  	private static final String PARAMETER_ID_FORM = "id_form";
80  
81  	// TEMPLATES
82  	private static final String TEMPLATE_TASK_ANTS_APPOINTMENT_CONFIG = "admin/plugins/workflow/modules/appointmentants/task_ants_appointment_config.html";
83  
84  	// JSPs
85  	private static final String JSP_MODIFY_TASK = "jsp/admin/plugins/workflow/ModifyTask.jsp";
86  
87  	/**
88  	 * Build and display the configuration page for the current task
89  	 * 
90  	 * @param taskTitle
91  	 * 				Title of the task
92  	 * @param locale
93  	 * 				Language used
94  	 * @param task
95  	 * 				The task to configure
96  	 * @param configService
97  	 * 				Service used for the task configuration
98  	 * @return
99  	 * 				A String containing the HTML page of the task configuration 
100 	 */
101 	public String getDisplayConfigForm( HttpServletRequest request, String taskTitle, Locale locale, ITask task, ITaskConfigService configService )
102 	{
103 		// Retrieve the task's config
104 		TaskAntsAppointmentConfig config = configService.findByPrimaryKey( task.getId( ) );
105 
106 		// Get the selected form's ID for this task
107 		int idForm = getCurrentFormId( request, config );
108 
109 		// Get the list of existing forms		
110 		ReferenceList formsList = FormService.findAllInReferenceList( );
111 		// Get the list of entries for the given form. Empty if no form as been selected yet
112 		ReferenceList entriesList = getFieldsList( idForm );
113 
114 		Map<String, Object> model = new HashMap<>( );
115 
116 		model.put( MARK_TASK_TITLE, taskTitle );
117 		model.put( MARK_FORM_ID, idForm );
118 		model.put( MARK_CONFIG, config );
119 		model.put( MARK_FORMS_LIST, formsList );
120 		model.put( MARK_FORM_FIELDS_LIST, entriesList );
121 
122 		HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_ANTS_APPOINTMENT_CONFIG, locale, model );
123 
124 		return template.getHtml( );
125 	}	
126 
127 	/**
128 	 * Save the configuration of this task
129 	 * 
130 	 * @param request
131 	 * 				HTTP request
132 	 * @param task
133 	 * 				The task getting its configuration set
134 	 * @param configService
135 	 * 				The service in charge of handling the configuration
136 	 * @return
137 	 * 				The URL to reach after the task configuration is done
138 	 */
139 	public String doSaveConfig( HttpServletRequest request, ITask task, ITaskConfigService configService )
140 	{
141 		// Retrieve the values of the selected form and its entry 
142 		String paramFormId = request.getParameter( PARAMETER_SELECT_FORM_ID );
143 		String paramFieldEntryId = request.getParameter( PARAMETER_SELECT_FIELD_ENTRY_TITLE );
144 
145 		String paramActionSelect = request.getParameter( PARAMETER_SELECT_FORM );
146 
147 		// If a new form has been selected and the selection button has been clicked to apply the changes
148 		if( StringUtils.equals( paramActionSelect, PARAMETER_SELECT_FORM ) &&
149 				( Integer.parseInt( paramFormId ) != WorkflowUtils.CONSTANT_ID_NULL ) )
150 		{
151 			int formId = Integer.parseInt( paramFormId );
152 			// Reload the page with the new data (form's ID)
153 			return getUrlToTaskModificationPage( request, task.getId( ), formId );
154 		}
155 
156 		TaskAntsAppointmentConfig config = configService.findByPrimaryKey( task.getId( ) );
157 
158 		/* If we are using an existing config, then this will remain false. It will be 
159 		 * set to true if a new config is being created */
160 		boolean bCreate = false;
161 
162 		// If the config wasn't found, then initialize a new one
163 		if( config == null )
164 		{
165 			config = new TaskAntsAppointmentConfig( );
166 			config.setIdTask( task.getId( ) );
167 			bCreate = true;
168 		}
169 
170 		// Set the selected form Entry's ID in the config
171 		if( StringUtils.isNotBlank( paramFieldEntryId ) )
172 		{
173 			config.setIdFieldEntry( NumberUtils.toInt( paramFieldEntryId ) );
174 		}
175 
176 		// Set the selected form in the config
177 		config.setIdForm( NumberUtils.toInt( paramFormId ) );
178 
179 		// If the config is new, then create it in DB
180 		if ( bCreate )
181 		{
182 			configService.create( config );
183 		}
184 		// If the config already exists, then update it in DB
185 		else
186 		{
187 			configService.update( config );
188 		}
189 		return null;
190 	}
191 
192 	/**
193 	 * Get a ReferenceList containing all the entries of the specified form. Each item of the ReferenceList
194 	 * will contain the title value of the entries
195 	 * 
196 	 * @param idForm
197 	 * 				ID of the form to process
198 	 * @return
199 	 * 				A ReferenceList Object with all the entries available in the form, or an empty ReferenceList
200 	 * 				if the form has no entry
201 	 */
202 	private ReferenceList getFieldsList( int idForm )
203 	{
204 		// If the ID is not -1, then retrieve the entries of the corresponding form
205 		if( idForm != -1 )
206 		{
207 			List<Entry> entriesList = EntryService.findListEntry( idForm );
208 
209 			ReferenceList entriesRefList = new ReferenceList( );
210 			for ( Entry entry : entriesList )
211 			{
212 				/* Only retrieve the Entry if it has a title. Their title is used to identify them
213 				 * when they are in a drop-down list
214 				 * */
215 				if( StringUtils.isNotEmpty( entry.getTitle( ) ) )
216 				{
217 					entriesRefList.addItem( entry.getIdEntry( ), entry.getTitle( ) );
218 				}
219 			}
220 			return entriesRefList;
221 		}
222 		else
223 		{
224 			return new ReferenceList( );
225 		}
226 	}
227 
228 	/**
229 	 * Build and return the URL to modify a task (reloads the page)
230 	 * 
231 	 * @param request
232 	 * 				The HTTP request
233 	 * @param idTask
234 	 * 				The ID of the task whose configuration is getting modified
235 	 * @param idForm
236 	 * 				ID of the form selected by the user
237 	 * @return
238 	 * 				The URL of this config's modification page
239 	 */
240 	private String getUrlToTaskModificationPage( HttpServletRequest request, int idTask, int idForm )
241 	{
242 		StringBuilder redirectUrl = new StringBuilder( AppPathService.getBaseUrl( request ) );
243 		redirectUrl.append( JSP_MODIFY_TASK );
244 
245 		UrlItem url = new UrlItem( redirectUrl.toString( ) );
246 		url.addParameter( PARAMETER_ID_TASK, idTask );
247 		url.addParameter( PARAMETER_ID_FORM, idForm );
248 
249 		return url.getUrl( );
250 	}
251 
252 	/**
253 	 * Get the ID of the form currently selected
254 	 * 
255 	 * @param request
256 	 * 				The current HTTP request
257 	 * @param config
258 	 * 				The task's configuration
259 	 * @return
260 	 * 				The ID of the form saved in the config or selected by the user. Otherwise the default
261 	 * 				value -1 is returned
262 	 */
263 	private int getCurrentFormId( HttpServletRequest request, TaskAntsAppointmentConfig config )
264 	{
265 		// Get the form's ID from the URL's parameters if the user previously selected it
266 		String strIdForm = request.getParameter( PARAMETER_ID_FORM );
267 
268 		if( StringUtils.isNumeric( strIdForm ) )
269 		{
270 			// Return the selected form's ID
271 			return Integer.parseInt( strIdForm );
272 		}
273 
274 		// If the config exists, then get its current selected form ID
275 		if( config != null && config.getIdForm( ) != 0 )
276 		{
277 			return config.getIdForm( );
278 		}
279 
280 		/* If there are no config and the user didn't select a form yet, then we consider
281 		 * that this page is loaded for the first time, so we set the default values
282 		 * */
283 		return -1;
284 	}
285 
286 	/**
287 	 * {@inheritDoc}
288 	 */
289 	@Override
290 	public String getDisplayTaskInformation(int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
291 	{
292 		return null;
293 	}
294 
295 	/**
296 	 * {@inheritDoc}
297 	 */
298 	@Override
299 	public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
300 	{
301 		return null;
302 	}
303 }