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