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.archive.daemon;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Optional;
40  
41  import org.apache.commons.collections.CollectionUtils;
42  
43  import fr.paris.lutece.plugins.workflow.modules.archive.business.ArchiveConfig;
44  import fr.paris.lutece.plugins.workflow.modules.archive.service.ArchiveService;
45  import fr.paris.lutece.plugins.workflow.modules.archive.service.IArchiveService;
46  import fr.paris.lutece.plugins.workflow.modules.archive.service.TaskArchive;
47  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
48  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
49  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
50  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflowFilter;
51  import fr.paris.lutece.plugins.workflowcore.business.task.ITaskType;
52  import fr.paris.lutece.plugins.workflowcore.business.workflow.Workflow;
53  import fr.paris.lutece.plugins.workflowcore.business.workflow.WorkflowFilter;
54  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
55  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
56  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
57  import fr.paris.lutece.plugins.workflowcore.service.resource.ResourceWorkflowService;
58  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
59  import fr.paris.lutece.plugins.workflowcore.service.task.ITaskService;
60  import fr.paris.lutece.plugins.workflowcore.service.task.TaskService;
61  import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
62  import fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService;
63  import fr.paris.lutece.portal.service.daemon.Daemon;
64  import fr.paris.lutece.portal.service.spring.SpringContextService;
65  import fr.paris.lutece.portal.service.util.AppLogService;
66  
67  public class ArchiveDaemon extends Daemon
68  {
69      private IWorkflowService _workflowService = SpringContextService.getBean( WorkflowService.BEAN_SERVICE );
70      private IActionService _actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
71      private ITaskService _taskService = SpringContextService.getBean( TaskService.BEAN_SERVICE );
72      private IResourceWorkflowService _resourceWorkflowService = SpringContextService.getBean( ResourceWorkflowService.BEAN_SERVICE );
73      private IArchiveService _archiveService = SpringContextService.getBean( ArchiveService.BEAN_SERVICE );
74  
75      @Override
76      public void run( )
77      {
78          WorkflowFilter workflowFilter = new WorkflowFilter( );
79          workflowFilter.setIsEnabled( 1 );
80  
81          List<Workflow> listWorkflows = _workflowService.getListWorkflowsByFilter( workflowFilter );
82  
83          for ( Workflow wf : listWorkflows )
84          {
85              ActionFilter filter = new ActionFilter( );
86              filter.setAutomaticReflexiveAction( true );
87              filter.setIdWorkflow( wf.getId( ) );
88  
89              List<Action> listAutomaticActions = _actionService.getListActionByFilter( filter );
90  
91              for ( Action action : listAutomaticActions )
92              {
93                  processAction( action, wf );
94              }
95          }
96      }
97  
98      private void processAction( Action action, Workflow wf )
99      {
100         List<ITask> listTasks = getListTaskByIdActionAndTaskType( action.getId( ), "taskTypeArchive", Locale.getDefault( ) );
101 
102         if ( CollectionUtils.isNotEmpty( listTasks ) )
103         {
104             ResourceWorkflowFilter filt = new ResourceWorkflowFilter( );
105             filt.setListIdStateBefore( action.getListIdStateBefore( ) );
106             filt.setIdWorkflow( wf.getId( ) );
107 
108             List<ResourceWorkflow> listResource = _resourceWorkflowService.getListResourceWorkflowByFilter( filt );
109             if ( CollectionUtils.isNotEmpty( listResource ) )
110             {
111                 for ( ITask task : listTasks )
112                 {
113                     ArchiveConfig config = _archiveService.loadConfig( task );
114                     if ( config == null )
115                     {
116                         AppLogService.error( "No config for archive task: " + task.getId( ) );
117                         continue;
118                     }
119                     for ( ResourceWorkflow resourceWorkflow : listResource )
120                     {
121                         ( (TaskArchive) task ).doArchiveResource( resourceWorkflow, config );
122                     }
123                 }
124             }
125         }
126     }
127 
128     private List<ITask> getListTaskByIdActionAndTaskType( int nIdAction, String taskType, Locale locale )
129     {
130         List<ITask> result = new ArrayList<>( );
131         List<ITask> allTasks = _taskService.getListTaskByIdAction( nIdAction, locale );
132         for ( ITask task : allTasks )
133         {
134             String currentType = Optional.ofNullable( task ).map( ITask::getTaskType ).map( ITaskType::getKey ).orElse( null );
135             if ( taskType.equals( currentType ) )
136             {
137                 result.add( task );
138             }
139         }
140         return result;
141     }
142 }