View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.state.service;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import javax.inject.Inject;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
44  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
45  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
46  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
47  import fr.paris.lutece.plugins.workflowcore.business.state.State;
48  import fr.paris.lutece.plugins.workflowcore.business.state.StateFilter;
49  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
50  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
51  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
52  import fr.paris.lutece.plugins.workflowcore.service.state.IStateService;
53  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.service.workflow.WorkflowService;
56  import fr.paris.lutece.util.ReferenceList;
57  
58  public abstract class AbstractStateTaskService
59  {
60  
61      private static final String USER_AUTO = "auto";
62  
63      @Inject
64      protected IActionService _actionService;
65  
66      @Inject
67      protected IStateService _stateService;
68  
69      @Inject
70      protected IResourceHistoryService _resourceHistoryService;
71  
72      @Inject
73      protected IResourceWorkflowService _resourceWorkflowService;
74  
75      public ReferenceList getListStates( int nIdAction )
76      {
77          ReferenceList referenceListStates = new ReferenceList( );
78          Action action = _actionService.findByPrimaryKey( nIdAction );
79  
80          if ( ( action != null ) && ( action.getWorkflow( ) != null ) )
81          {
82              StateFilter stateFilter = new StateFilter( );
83              stateFilter.setIdWorkflow( action.getWorkflow( ).getId( ) );
84  
85              List<State> listStates = _stateService.getListStateByFilter( stateFilter );
86  
87              referenceListStates.addItem( -1, StringUtils.EMPTY );
88              referenceListStates.addAll( ReferenceList.convert( listStates, "id", "name", true ) );
89          }
90  
91          return referenceListStates;
92      }
93  
94      public ResourceWorkflow getResourceByHistory( int nIdHistory, int nIdWorkflow )
95      {
96          ResourceWorkflow resourceWorkflow = null;
97          ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdHistory );
98          if ( resourceHistory != null )
99          {
100             resourceWorkflow = _resourceWorkflowService.findByPrimaryKey( resourceHistory.getIdResource( ), resourceHistory.getResourceType( ), nIdWorkflow );
101         }
102         return resourceWorkflow;
103     }
104 
105     public void doChangeState( ITask task, int nIdResource, String strResourceType, int nIdWorkflow, int newState )
106     {
107         Locale locale = I18nService.getDefaultLocale( );
108         State state = _stateService.findByPrimaryKey( newState );
109         Action action = _actionService.findByPrimaryKey( task.getAction( ).getId( ) );
110 
111         if ( state != null && action != null )
112         {
113 
114             // Create Resource History
115             ResourceHistory resourceHistory = new ResourceHistory( );
116             resourceHistory.setIdResource( nIdResource );
117             resourceHistory.setResourceType( strResourceType );
118             resourceHistory.setAction( action );
119             resourceHistory.setWorkFlow( action.getWorkflow( ) );
120             resourceHistory.setCreationDate( WorkflowUtils.getCurrentTimestamp( ) );
121             resourceHistory.setUserAccessCode( USER_AUTO );
122             _resourceHistoryService.create( resourceHistory );
123 
124             // Update Resource
125             ResourceWorkflow resourceWorkflow = _resourceWorkflowService.findByPrimaryKey( nIdResource, strResourceType, nIdWorkflow );
126             resourceWorkflow.setState( state );
127             _resourceWorkflowService.update( resourceWorkflow );
128             saveTaskInformation( resourceHistory.getId( ), task, state );
129             // Execute the relative tasks of the state in the workflow
130             // We use AutomaticReflexiveActions because we don't want to change the state of the resource by executing actions.
131             WorkflowService.getInstance( ).doProcessAutomaticReflexiveActions( nIdResource, strResourceType, state.getId( ), null, locale, null );
132         }
133     }
134 
135     protected abstract void saveTaskInformation( int nIdResourceHistory, ITask task, State state );
136 }