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.daemon;
35  
36  import java.util.List;
37  import java.util.Locale;
38  import java.util.Optional;
39  
40  import fr.paris.lutece.plugins.workflow.modules.state.business.ChangeStateTaskConfig;
41  import fr.paris.lutece.plugins.workflow.modules.state.service.IChangeStateTaskService;
42  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
43  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
44  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
45  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflowFilter;
46  import fr.paris.lutece.plugins.workflowcore.business.task.ITaskType;
47  import fr.paris.lutece.plugins.workflowcore.business.workflow.Workflow;
48  import fr.paris.lutece.plugins.workflowcore.business.workflow.WorkflowFilter;
49  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
50  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
51  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
52  import fr.paris.lutece.plugins.workflowcore.service.resource.ResourceWorkflowService;
53  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
54  import fr.paris.lutece.plugins.workflowcore.service.task.ITaskService;
55  import fr.paris.lutece.plugins.workflowcore.service.task.TaskService;
56  import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
57  import fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService;
58  import fr.paris.lutece.portal.service.daemon.Daemon;
59  import fr.paris.lutece.portal.service.spring.SpringContextService;
60  import fr.paris.lutece.portal.service.util.AppLogService;
61  
62  public class ChangeStateDaemon extends Daemon
63  {
64  
65      private IChangeStateTaskService _changeStateTaskService = SpringContextService.getBean( "workflow.changeStateTaskService" );
66      private IWorkflowService _workflowService = SpringContextService.getBean( WorkflowService.BEAN_SERVICE );
67      private IResourceWorkflowService _resourceWorkflowService = SpringContextService.getBean( ResourceWorkflowService.BEAN_SERVICE );
68      private IActionService _actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
69      private ITaskService _taskService = SpringContextService.getBean( TaskService.BEAN_SERVICE );
70  
71      @Override
72      public void run( )
73      {
74          WorkflowFilter workflowFilter = new WorkflowFilter( );
75          workflowFilter.setIsEnabled( 1 );
76  
77          List<Workflow> listWorkflows = _workflowService.getListWorkflowsByFilter( workflowFilter );
78  
79          for ( Workflow workflow : listWorkflows )
80          {
81              ActionFilter filter = new ActionFilter( );
82              filter.setAutomaticReflexiveAction( true );
83              filter.setIdWorkflow( workflow.getId( ) );
84  
85              List<Action> listAutomaticActions = _actionService.getListActionByFilter( filter );
86  
87              for ( Action action : listAutomaticActions )
88              {
89                  ResourceWorkflowFilter filt = new ResourceWorkflowFilter( );
90                  filt.setListIdStateBefore( action.getListIdStateBefore( ) );
91                  filt.setIdWorkflow( workflow.getId( ) );
92  
93                  List<ITask> listActionTasks = _taskService.getListTaskByIdAction( action.getId( ), Locale.getDefault( ) );
94                  ITask task = findTaskToExecute( listActionTasks );
95  
96                  if ( task == null )
97                  {
98                      continue;
99                  }
100 
101                 ChangeStateTaskConfig config = _changeStateTaskService.loadConfig( task );
102 
103                 List<ResourceWorkflow> listResource = _resourceWorkflowService.getListResourceWorkflowByFilter( filt );
104 
105                 for ( ResourceWorkflow resource : listResource )
106                 {
107                     try
108                     {
109                         _changeStateTaskService.doChangeState( task, resource.getIdResource( ), resource.getResourceType( ), workflow.getId( ),
110                                 config.getIdNextState( ) );
111                     }
112                     catch( Exception e )
113                     {
114                         AppLogService.error( "Unexpected Error", e );
115                     }
116                 }
117             }
118         }
119     }
120 
121     private ITask findTaskToExecute( List<ITask> listActionTasks )
122     {
123         for ( ITask tsk : listActionTasks )
124         {
125             String beanName = Optional.ofNullable( tsk ).map( ITask::getTaskType ).map( ITaskType::getBeanName ).orElse( null );
126             if ( "workflow.changeStateTask".equals( beanName ) )
127             {
128                 return tsk;
129             }
130         }
131         return null;
132     }
133 }