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.service;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import javax.inject.Inject;
41  import javax.inject.Named;
42  import javax.servlet.http.HttpServletRequest;
43  
44  import fr.paris.lutece.api.user.User;
45  import fr.paris.lutece.plugins.appointment.service.AppointmentResponseService;
46  import fr.paris.lutece.plugins.genericattributes.business.Response;
47  import fr.paris.lutece.plugins.workflow.modules.appointmentants.business.history.TaskAntsAppointmentHistory;
48  import fr.paris.lutece.plugins.workflow.modules.appointmentants.service.history.ITaskAntsAppointmentHistoryService;
49  import fr.paris.lutece.plugins.workflow.modules.appointmentants.service.history.TaskAntsAppointmentHistoryService;
50  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
51  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
52  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
53  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
54  import fr.paris.lutece.plugins.workflowcore.service.resource.ResourceHistoryService;
55  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
56  import fr.paris.lutece.portal.service.i18n.I18nService;
57  import fr.paris.lutece.portal.service.util.AppLogService;
58  
59  /**
60   * 
61   * Workflow task used to add an appointment in the ANTS' database, through their exposed API
62   * 
63   */
64  public class TaskAddAntsAppointment extends SimpleTask
65  {
66  	public static final String CLASS_NAME = WorkflowAppointmentAntsPlugin.PLUGIN_NAME + "TaskAddAntsAppointment";
67  
68  	@Inject
69  	@Named( ResourceHistoryService.BEAN_SERVICE )
70  	private IResourceHistoryService _resourceHistoryService;
71  
72  	/**
73  	 * Task's configuration service
74  	 */
75  	@Inject
76  	@Named( WorkflowAppointmentAntsPlugin.BEAN_CONFIG )
77  	private ITaskConfigService _config;
78  
79  	/**
80  	 * Task's service
81  	 */
82  	@Inject
83  	@Named( TaskAntsAppointmentService.BEAN_SERVICE )
84  	private ITaskAntsAppointmentService _antsAppointmentService;
85  
86  	/**
87  	 * Task's history service
88  	 */
89  	@Inject
90  	@Named( TaskAntsAppointmentHistoryService.BEAN_SERVICE )
91  	private ITaskAntsAppointmentHistoryService _antsAppointmentHistoryService;
92  
93  	/**
94  	 * Title of the task
95  	 */
96  	private static final String PROPERTY_LABEL_TITLE = "module.workflow.appointmentants.add_appointment.task_title";
97  
98  	/**
99       * {@inheritDoc}
100      */
101 	@Override
102 	public boolean processTaskWithResult( int nIdResourceHistory, HttpServletRequest request, Locale locale, User user )
103 	{
104 		// Get the resourceHistory to find the resource (i.e the appointment) to work with
105 		ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
106 
107 		// Task's execution result
108 		boolean isTaskResultPositive = false;
109 
110 		// Create the current task's history object
111 		TaskAntsAppointmentHistory antsAppointmentHistory = new TaskAntsAppointmentHistory( );
112 
113 		try
114 		{
115 			isTaskResultPositive = _antsAppointmentService.createAntsAppointment( request, resourceHistory.getIdResource( ), this.getId( ), antsAppointmentHistory );
116 		}
117 		catch ( Exception e )
118 		{
119 			AppLogService.error( CLASS_NAME, e );
120 		}
121 
122 		saveTaskHistory( antsAppointmentHistory, nIdResourceHistory, isTaskResultPositive );
123 		return isTaskResultPositive;
124 	}
125 
126 	/**
127 	 * Save the current task's history in the database
128 	 * 
129 	 * @param antsAppointmentHistory
130 	 *            Instance of TaskAntsAppointmentHistory object to save
131 	 * @param idResourceHistory
132 	 *            ID of the resource history used for the task
133 	 * @param isTaskSuccessful
134 	 *            Boolean result returned by the task
135 	 */
136 	private void saveTaskHistory( TaskAntsAppointmentHistory antsAppointmentHistory, int idResourceHistory, boolean isTaskSuccessful )
137 	{
138 		antsAppointmentHistory.setIdResourceHistory( idResourceHistory );
139 		antsAppointmentHistory.setIdTask( this.getId( ) );
140 		antsAppointmentHistory.setTaskSuccessState( isTaskSuccessful );
141 
142 		_antsAppointmentHistoryService.create( antsAppointmentHistory, WorkflowUtils.getPlugin( ) );
143 	}
144 
145 	/**
146      * {@inheritDoc}
147      */
148 	@Override
149 	public String getTitle( Locale locale )
150 	{
151 		return I18nService.getLocalizedString( PROPERTY_LABEL_TITLE, locale );
152 	}
153 
154 	/**
155      * {@inheritDoc}
156      */
157 	@Override
158 	public void doRemoveConfig( )
159 	{
160 		_config.remove( this.getId( ) );
161 		_antsAppointmentHistoryService.removeByTask( this.getId( ), WorkflowUtils.getPlugin( ) );
162 	}
163 
164 	/**
165      * {@inheritDoc}
166      */
167 	@Override
168 	public void doRemoveTaskInformation( int nIdHistory )
169 	{
170 		_antsAppointmentHistoryService.removeByHistory( nIdHistory, this.getId( ), WorkflowUtils.getPlugin( ) );
171 	}
172 }