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.ArrayList;
38  import java.util.List;
39  import java.util.ListIterator;
40  import java.util.Locale;
41  
42  import javax.inject.Inject;
43  import javax.servlet.http.HttpServletRequest;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  import fr.paris.lutece.plugins.ticketing.business.assignee.AssigneeUnit;
48  import fr.paris.lutece.plugins.ticketing.business.assignee.AssigneeUser;
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.web.TicketingConstants;
52  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
53  import fr.paris.lutece.plugins.unittree.business.unit.UnitHome;
54  import fr.paris.lutece.plugins.workflow.modules.ticketing.business.resourcehistory.IResourceHistoryService;
55  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
56  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
57  import fr.paris.lutece.portal.business.user.AdminUser;
58  import fr.paris.lutece.portal.business.user.AdminUserHome;
59  import fr.paris.lutece.portal.service.i18n.I18nService;
60  import fr.paris.lutece.portal.service.util.AppPropertiesService;
61  
62  /**
63   * This class represents a task to respond to assigner after assigning up
64   *
65   */
66  public class TaskReplyAssignUpTicket extends AbstractTicketingTask
67  {
68      // Messages
69      private static final String MESSAGE_REPLY_ASSIGN_UP_TICKET = "module.workflow.ticketing.task_reply_assign_up_ticket.labelReplyAssignUpTicket";
70      private static final String MESSAGE_REPLY_ASSIGN_UP_TICKET_INFORMATION = "module.workflow.ticketing.task_reply_assign_up_ticket.information";
71      private static final String MESSAGE_REPLY_ASSIGN_TICKET_NO_CURRENT_USER = "module.workflow.ticketing.task_reply_assign_up_ticket.no_current_user";
72      private static final String MESSAGE_REPLY_ASSIGN_TICKET_NO_USER_FOUND = "module.workflow.ticketing.task_reply_assign_up_ticket.no_user_found";
73      private static final String PROPERTY_ASSIGN_UP_ACTION_ID = "workflow-ticketing.workflow.action.id.assignUp";
74      private static final String PROPERTY_ASSIGN_TO_UNIT_ACTION_ID = "workflow-ticketing.workflow.action.id.assignToUnit";
75      private static final int ASSIGN_UP_ACTION_ID = AppPropertiesService.getPropertyInt( PROPERTY_ASSIGN_UP_ACTION_ID, 304 );
76      private static final int ASSIGN_TO_UNIT_ACTION_ID = AppPropertiesService.getPropertyInt( PROPERTY_ASSIGN_TO_UNIT_ACTION_ID, 305 );
77  
78      // Services
79      @Inject
80      protected IResourceHistoryService _resourceHistoryServiceTicketing;
81  
82      @Override
83      public String processTicketingTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
84      {
85          String strTaskInformation = StringUtils.EMPTY;
86  
87          // We get the ticket to modify
88          Ticket ticket = getTicket( nIdResourceHistory );
89  
90          if ( ticket != null )
91          {
92              AssigneeUnit assigneeUnit = ticket.getAssigneeUnit( );
93              AssigneeUser assigneeUser = ticket.getAssigneeUser( );
94              String strCurrentUnit = null;
95  
96              if ( assigneeUnit != null )
97              {
98                  strCurrentUnit = assigneeUnit.getName( );
99              }
100 
101             if ( assigneeUser == null )
102             {
103                 assigneeUser = new AssigneeUser( );
104             }
105 
106             AdminUser user = getAssigner( nIdResourceHistory );
107             Unit unit = getAssignerUnitBeforeRelance( nIdResourceHistory );
108 
109             if ( ( user != null ) && ( user.getUserId( ) != assigneeUser.getAdminUserId( ) ) )
110             {
111                 assigneeUser.setAdminUserId( user.getUserId( ) );
112                 assigneeUser.setEmail( user.getEmail( ) );
113                 assigneeUser.setFirstname( user.getFirstName( ) );
114                 assigneeUser.setLastname( user.getLastName( ) );
115                 ticket.setAssigneeUser( assigneeUser );
116 
117                 if ( ticket.getAssignerUnit( ).getUnitId( ) != ticket.getAssigneeUnit( ).getUnitId( ) )
118                 {
119                     request.setAttribute( TicketingConstants.ATTRIBUTE_IS_UNIT_CHANGED, true );
120                 }
121 
122                 if ( unit != null )
123                 {
124                     AssigneeUnit assigneeUnitOld = new AssigneeUnit( unit );
125                     ticket.setAssigneeUnit( assigneeUnitOld );
126 
127                 }
128                 else
129                 {
130                     List<Unit> unitsList = UnitHome.findByIdUser( user.getUserId( ) );
131 
132                     if ( ( unitsList != null ) && ( !unitsList.isEmpty( ) ) )
133                     {
134                         assigneeUnit = new AssigneeUnit( unitsList.get( 0 ) );
135                         ticket.setAssigneeUnit( assigneeUnit );
136                     }
137                 }
138 
139                 ticket.setAssignerUser( null );
140                 ticket.setAssignerUnit( null );
141 
142                 TicketHome.update( ticket );
143 
144                 strTaskInformation = MessageFormat.format( I18nService.getLocalizedString( MESSAGE_REPLY_ASSIGN_UP_TICKET_INFORMATION, Locale.FRENCH ),
145                         ( strCurrentUnit != null ) ? strCurrentUnit : StringUtils.EMPTY,
146                         ( ticket.getAssigneeUser( ) != null ) ? ( ticket.getAssigneeUser( ).getFirstname( ) + " " + ticket.getAssigneeUser( ).getLastname( ) )
147                                 : I18nService.getLocalizedString( MESSAGE_REPLY_ASSIGN_TICKET_NO_CURRENT_USER, Locale.FRENCH ),
148                         ( ticket.getAssigneeUnit( ) != null ) ? ticket.getAssigneeUnit( ).getName( ) : StringUtils.EMPTY );
149             }
150             else
151             {
152                 strTaskInformation = I18nService.getLocalizedString( MESSAGE_REPLY_ASSIGN_TICKET_NO_USER_FOUND, Locale.FRENCH );
153             }
154         }
155 
156         if ( request != null )
157         {
158             request.setAttribute( TicketingConstants.ATTRIBUTE_REDIRECT_AFTER_WORKFLOW_ACTION, REDIRECT_TO_LIST );
159         }
160 
161         return strTaskInformation;
162     }
163 
164     /**
165      * Get the user assigning up the ticket corresponding to the resource of the resourceHistory id
166      *
167      * @param nIdResourceHistory
168      *            the resourceHistory id
169      * @return the user assigning up the ticket corresponding to the resource of the resourceHistory id , {@code null} otherwise
170      */
171     protected AdminUser getAssigner( int nIdResourceHistory )
172     {
173         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
174 
175         List<Integer> listIdResource = new ArrayList<>( );
176         listIdResource.add( resourceHistory.getIdResource( ) );
177 
178         List<Integer> listIdHistory = _resourceHistoryService.getListHistoryIdByListIdResourceId( listIdResource, resourceHistory.getResourceType( ),
179                 resourceHistory.getWorkflow( ).getId( ) );
180 
181         boolean isAssignUpActionFound = false;
182         ListIterator<Integer> iterator = listIdHistory.listIterator( listIdHistory.size( ) );
183 
184         while ( !isAssignUpActionFound && iterator.hasPrevious( ) )
185         {
186             resourceHistory = _resourceHistoryService.findByPrimaryKey( iterator.previous( ) );
187 
188             if ( ( resourceHistory.getAction( ).getId( ) == ASSIGN_UP_ACTION_ID ) || ( resourceHistory.getAction( ).getId( ) == ASSIGN_TO_UNIT_ACTION_ID ) )
189             {
190                 isAssignUpActionFound = true;
191             }
192         }
193 
194         return ( isAssignUpActionFound ? AdminUserHome.findUserByLogin( resourceHistory.getUserAccessCode( ) ) : null );
195 
196     }
197 
198     /**
199      * Get the unit assigning up the ticket corresponding to the resource of the resourceHistory id
200      *
201      * @param nIdResourceHistory
202      *            the resourceHistory id
203      * @return the unit assigning up the ticket corresponding to the resource of the resourceHistory id , {@code null} otherwise
204      */
205     protected Unit getAssignerUnitBeforeRelance( int nIdResourceHistory )
206     {
207         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
208 
209         List<Integer> listIdResource = new ArrayList<>( );
210         listIdResource.add( resourceHistory.getIdResource( ) );
211 
212         List<Integer> listIdHistory = _resourceHistoryService.getListHistoryIdByListIdResourceId( listIdResource, resourceHistory.getResourceType( ),
213                 resourceHistory.getWorkflow( ).getId( ) );
214 
215         boolean isAssignUpActionFound = false;
216         ListIterator<Integer> iterator = listIdHistory.listIterator( listIdHistory.size( ) );
217         fr.paris.lutece.plugins.workflow.modules.ticketing.business.resourcehistory.ResourceHistory resourceHistoryTicketing = null;
218 
219         while ( !isAssignUpActionFound && iterator.hasPrevious( ) )
220         {
221             resourceHistory = _resourceHistoryService.findByPrimaryKey( iterator.previous( ) );
222 
223             if ( ( resourceHistory.getAction( ).getId( ) == ASSIGN_UP_ACTION_ID ) || ( resourceHistory.getAction( ).getId( ) == ASSIGN_TO_UNIT_ACTION_ID ) )
224             {
225                 isAssignUpActionFound = true;
226                 resourceHistoryTicketing = _resourceHistoryServiceTicketing.findOldUnitByPrimaryKey( resourceHistory.getId( ), WorkflowUtils.getPlugin( ) );
227             }
228         }
229 
230         return ( resourceHistoryTicketing != null ? UnitHome.findByPrimaryKey( resourceHistoryTicketing.getIdUnitOld( ) ) : null );
231 
232     }
233 
234     @Override
235     public String getTitle( Locale locale )
236     {
237         return I18nService.getLocalizedString( MESSAGE_REPLY_ASSIGN_UP_TICKET, locale );
238     }
239 }