View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.appointment.modules.resource.service.workflow;
35  
36  import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
37  import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentFormResourceType;
38  import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentFormResourceTypeHome;
39  import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentResource;
40  import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentResourceHome;
41  import fr.paris.lutece.plugins.appointment.modules.resource.business.workflow.SetAppointmentResourceHistory;
42  import fr.paris.lutece.plugins.appointment.modules.resource.business.workflow.SetAppointmentResourceHistoryHome;
43  import fr.paris.lutece.plugins.appointment.modules.resource.business.workflow.TaskSetAppointmentResourceConfig;
44  import fr.paris.lutece.plugins.appointment.service.AppointmentService;
45  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
46  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
47  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
48  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  
51  import org.apache.commons.lang.StringUtils;
52  
53  import java.util.Locale;
54  
55  import javax.inject.Inject;
56  import javax.inject.Named;
57  import javax.servlet.http.HttpServletRequest;
58  
59  /**
60   * Workflow task to update a resource associated with an appointment
61   */
62  public class TaskSetAppointmentResource extends SimpleTask
63  {
64      /**
65       * Name of the bean of the config service of this task
66       */
67      public static final String CONFIG_SERVICE_BEAN_NAME = "appointment-resource.taskSetAppointmentResourceConfigService";
68  
69      // MESSAGES
70      private static final String MESSAGE_SET_APPOINTMENT_RESOURCE_TASK_DESCRIPTION = "module.appointment.resource.task_set_appointment_resource_config.taskDescription";
71  
72      // PARAMETERS
73      private static final String PARAMETER_ID_RESOURCE = "idResource_";
74  
75      // SERVICES
76      @Inject
77      private IResourceHistoryService _resourceHistoryService;
78      @Inject
79      @Named( CONFIG_SERVICE_BEAN_NAME )
80      private ITaskConfigService _taskSetAppointmentResourceConfigService;
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
87      {
88          ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
89          TaskSetAppointmentResourceConfig config = _taskSetAppointmentResourceConfigService.findByPrimaryKey( this.getId( ) );
90  
91          if ( config == null )
92          {
93              return;
94          }
95  
96          String strIdResource = request.getParameter( PARAMETER_ID_RESOURCE + config.getIdFormResourceType( ) );
97  
98          if ( StringUtils.isNotEmpty( strIdResource ) )
99          {
100             Appointment appointment = AppointmentService.findAppointmentById( resourceHistory.getIdResource( ) );
101 
102             AppointmentResource appResource = AppointmentResourceHome.findByPrimaryKey( appointment.getIdAppointment( ), config.getIdFormResourceType( ) );
103 
104             if ( appResource != null )
105             {
106                 appResource.setIdResource( strIdResource );
107                 AppointmentResourceHome.update( appResource );
108             }
109             else
110             {
111                 appResource = new AppointmentResource( );
112                 appResource.setIdAppointment( appointment.getIdAppointment( ) );
113                 appResource.setIdAppointmentFormResourceType( config.getIdFormResourceType( ) );
114                 appResource.setIdResource( strIdResource );
115                 AppointmentResourceHome.insert( appResource );
116             }
117 
118             SetAppointmentResourceHistoryodules/resource/business/workflow/SetAppointmentResourceHistory.html#SetAppointmentResourceHistory">SetAppointmentResourceHistory history = new SetAppointmentResourceHistory( );
119             history.setIdHistory( nIdResourceHistory );
120             history.setIdAppointment( appResource.getIdAppointment( ) );
121             history.setIdFormResourceType( appResource.getIdAppointmentFormResourceType( ) );
122             history.setIdResource( appResource.getIdResource( ) );
123             SetAppointmentResourceHistoryHome.create( history );
124         }
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public String getTitle( Locale locale )
132     {
133         TaskSetAppointmentResourceConfig config = _taskSetAppointmentResourceConfigService.findByPrimaryKey( getId( ) );
134 
135         if ( config == null )
136         {
137             return StringUtils.EMPTY;
138         }
139 
140         AppointmentFormResourceType resourceType = AppointmentFormResourceTypeHome.findByPrimaryKey( config.getIdFormResourceType( ) );
141         Object [ ] args = {
142             ( resourceType != null ) ? resourceType.getDescription( ) : StringUtils.EMPTY
143         };
144 
145         return I18nService.getLocalizedString( MESSAGE_SET_APPOINTMENT_RESOURCE_TASK_DESCRIPTION, args, locale );
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public void doRemoveConfig( )
153     {
154         _taskSetAppointmentResourceConfigService.remove( getId( ) );
155     }
156 }