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.workflow.modules.ticketing.service.task;
35  
36  import java.text.MessageFormat;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import javax.inject.Inject;
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  import fr.paris.lutece.plugins.ticketing.business.assignee.AssigneeUnit;
46  import fr.paris.lutece.plugins.ticketing.business.assignee.AssigneeUser;
47  import fr.paris.lutece.plugins.ticketing.business.resourcehistory.IResourceWorkflowHistoryDAO;
48  import fr.paris.lutece.plugins.ticketing.business.search.IndexerActionHome;
49  import fr.paris.lutece.plugins.ticketing.business.ticket.Ticket;
50  import fr.paris.lutece.plugins.ticketing.business.ticket.TicketHome;
51  import fr.paris.lutece.plugins.ticketing.service.TicketReassignUnitResourceService;
52  import fr.paris.lutece.plugins.ticketing.web.TicketingConstants;
53  import fr.paris.lutece.plugins.ticketing.web.util.TicketIndexerActionUtil;
54  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
55  import fr.paris.lutece.plugins.unittree.business.unit.UnitHome;
56  import fr.paris.lutece.plugins.workflow.modules.ticketing.business.resourcehistory.IResourceHistoryService;
57  import fr.paris.lutece.plugins.workflow.modules.ticketing.business.resourcehistory.ResourceHistory;
58  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
59  import fr.paris.lutece.portal.business.user.AdminUser;
60  import fr.paris.lutece.portal.business.user.AdminUserHome;
61  import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
62  import fr.paris.lutece.portal.service.i18n.I18nService;
63  import fr.paris.lutece.portal.service.spring.SpringContextService;
64  
65  /**
66   * This class represents a task to assign a unit
67   *
68   */
69  public class TaskReassignTicketToUnitForAUserNotAvailable extends AbstractTicketingTask
70  {
71      // Messages
72      private static final String MESSAGE_ASSIGN_TICKET_TO_UNIT = "module.workflow.ticketing.task_assign_ticket_to_unit.labelAssignTicketToUnit";
73      private static final String MESSAGE_ASSIGN_TICKET_TO_UNIT_INFORMATION = "module.workflow.ticketing.task_reassign_ticket_to_unit.information";
74      private static final String MESSAGE_REASSIGN_TICKET_NO_MODIFICATIONS_INFORMATION = "module.workflow.ticketing.task_reassign_ticket.no_modifications_information";
75  
76      // PARAMETERS
77      public static final String PARAMETER_ASSIGNEE_UNIT = "id_unit";
78  
79      private TicketReassignUnitResourceService _ticketReassignUnitResourceService = SpringContextService.getBean( TicketReassignUnitResourceService.BEAN_NAME );
80  
81      IResourceWorkflowHistoryDAO dao = SpringContextService.getBean( "ticketing.resourceWorkflowHistoryDAO" );
82  
83      // Services
84      @Inject
85      protected IResourceHistoryService _resourceHistoryServiceTicketing;
86  
87      @Override
88      public String processTicketingTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
89      {
90          String strTaskInformation = StringUtils.EMPTY;
91          String strUserId = request.getParameter( "id_user" );
92          int nUserId = Integer.parseInt( strUserId );
93  
94          AdminUser user = AdminUserHome.findByPrimaryKey( nUserId );
95  
96          AdminUser adminUser = AdminAuthenticationService.getInstance( ).getRegisteredUser( request );
97  
98          if ( null != user )
99          {
100             // unite pour le tranfsert des tickets
101             Unit unitToTransfer = null;
102 
103             // cas 1 : le user appatient a une unite
104             boolean isUserInUnit = _ticketReassignUnitResourceService.checkUserInUnit( nUserId );
105 
106             Ticket ticket = TicketHome.findByPrimaryKey( dao.getidRessourceFromResourceWorkflowHistory( nIdResourceHistory ) );
107 
108             if ( ticket != null )
109             {
110                 if ( isUserInUnit )
111                 {
112                     // cas 1 : il appartient a une entite
113                     List<Unit> unitsOfUser = UnitHome.findByIdUser( nUserId );
114                     unitToTransfer = unitsOfUser.get( 0 );
115                 }
116                 else
117                 {
118                     // cas 2 : il n'appartient pas a une entite
119                     unitToTransfer = _ticketReassignUnitResourceService.findUnitToTransfer( ticket, nUserId );
120                 }
121 
122                 if ( ( unitToTransfer != null ) )
123                 {
124                     AssigneeUnit assigneeOldUnit = null;
125                     if ( null != ticket.getAssigneeUnit( ) )
126                     {
127                         assigneeOldUnit = ticket.getAssigneeUnit( );
128                     }
129 
130                     AssigneeUnit assigneeNewUnit = new AssigneeUnit( unitToTransfer );
131                     AssigneeUser assigner = new AssigneeUser( adminUser );
132 
133                     ticket.setAssignerUser( assigner );
134                     ticket.setAssignerUnit( assigneeOldUnit );
135                     assigneeNewUnit.setUnitId( unitToTransfer.getIdUnit( ) );
136                     assigneeNewUnit.setName( unitToTransfer.getLabel( ) );
137                     ticket.setAssigneeUnit( assigneeNewUnit );
138                     ticket.setAssigneeUser( null );
139 
140                     TicketHome.update( ticket );
141 
142                     immediateTicketIndexingWaiting( ticket );
143 
144                     request.setAttribute( TicketingConstants.ATTRIBUTE_IS_UNIT_CHANGED, ( assigneeNewUnit != assigneeOldUnit ) );
145                     request.setAttribute( TicketingConstants.ATTRIBUTE_REDIRECT_AFTER_WORKFLOW_ACTION, REDIRECT_TO_LIST );
146 
147                     strTaskInformation = MessageFormat.format( I18nService.getLocalizedString( MESSAGE_ASSIGN_TICKET_TO_UNIT_INFORMATION, Locale.FRENCH ),
148                             user.getFirstName( ), user.getLastName( ), unitToTransfer.getLabel( ) );
149 
150                     // insert in workflow_resource_history_ticketing
151                     ResourceHistoryw/modules/ticketing/business/resourcehistory/ResourceHistory.html#ResourceHistory">ResourceHistory resourceHistory = new ResourceHistory( );
152                     resourceHistory.setIdHistory( nIdResourceHistory );
153                     resourceHistory.setIdChannel( ticket.getChannel( ).getId( ) );
154                     if ( assigneeOldUnit == null )
155                     {
156                         resourceHistory.setIdUnitOld( -1 );
157                     }
158                     else
159                     {
160                         resourceHistory.setIdUnitOld( assigneeOldUnit.getUnitId( ) );
161                     }
162                     resourceHistory.setIdUnitNew( unitToTransfer.getIdUnit( ) );
163                     _resourceHistoryServiceTicketing.create( resourceHistory, WorkflowUtils.getPlugin( ) );
164                 }
165             }
166             // In the case when there are no modifications
167             if ( strTaskInformation.equals( StringUtils.EMPTY ) )
168             {
169                 strTaskInformation = I18nService.getLocalizedString( MESSAGE_REASSIGN_TICKET_NO_MODIFICATIONS_INFORMATION, locale );
170             }
171         }
172         return strTaskInformation;
173     }
174 
175     @Override
176     public String getTitle( Locale locale )
177     {
178         return I18nService.getLocalizedString( MESSAGE_ASSIGN_TICKET_TO_UNIT, locale );
179     }
180 
181     /**
182      * Immediate indexation of a Ticket for the Backoffice
183      *
184      * @param idTicket
185      *            the id of the Ticket to index
186      */
187     protected void immediateTicketIndexingWaiting( Ticket ticket )
188     {
189         IndexerActionHome.create( TicketIndexerActionUtil.createIndexerActionFromTicket( ticket ) );
190     }
191 }