1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.service;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Locale;
39
40 import javax.inject.Inject;
41 import javax.inject.Named;
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.apache.commons.lang3.StringUtils;
45
46 import fr.paris.lutece.plugins.forms.business.FormQuestionResponse;
47 import fr.paris.lutece.plugins.forms.business.FormQuestionResponseHome;
48 import fr.paris.lutece.plugins.forms.business.FormResponse;
49 import fr.paris.lutece.plugins.forms.business.FormResponseHome;
50 import fr.paris.lutece.plugins.genericattributes.business.Response;
51 import fr.paris.lutece.plugins.workflow.modules.assignment.service.IAssignmentHistoryService;
52 import fr.paris.lutece.plugins.workflow.modules.assignment.service.IWorkgroupConfigService;
53 import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.business.AutomaticAssignment;
54 import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.business.TaskAutomaticAssignmentConfig;
55 import fr.paris.lutece.plugins.workflow.service.WorkflowPlugin;
56 import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
57 import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
58 import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
59 import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
60 import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
61 import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
62 import fr.paris.lutece.portal.service.plugin.Plugin;
63 import fr.paris.lutece.portal.service.plugin.PluginService;
64
65
66
67
68
69
70 public class TaskAutomaticAssignment extends SimpleTask
71 {
72
73 @Inject
74 @Named( TaskAutomaticAssignmentConfigService.BEAN_SERVICE )
75 private ITaskConfigService _taskAutomaticAssignmentConfigService;
76 @Inject
77 private IAutomaticAssignmentService _automaticAssignmentService;
78 @Inject
79 private IResourceHistoryService _resourceHistoryService;
80 @Inject
81 private IResourceWorkflowService _resourceWorkflowService;
82 @Inject
83 private IAssignmentHistoryService _assignmentHistoryService;
84 @Inject
85 private IWorkgroupConfigService _workgroupConfigService;
86
87
88
89
90 @Override
91 public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
92 {
93 Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
94
95 ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
96
97 FormResponse formResponse = FormResponseHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
98 if ( formResponse == null )
99 {
100 return;
101 }
102
103 List<String> listWorkgroup = new ArrayList<>( );
104
105 List<AutomaticAssignment> automaticAssignementList = _automaticAssignmentService.findByTask( this.getId( ), autoAssignPlugin );
106 List<FormQuestionResponse> listFormQuestionResponse = FormQuestionResponseHome.getFormQuestionResponseListByFormResponse( formResponse.getId( ) );
107
108 for ( FormQuestionResponse formQuestionResponse : listFormQuestionResponse )
109 {
110 for ( AutomaticAssignment automaticAssignment : automaticAssignementList )
111 {
112 if ( formQuestionResponse.getQuestion( ).getId( ) == automaticAssignment.getIdQuestion( ) )
113 {
114 for ( Response response : formQuestionResponse.getEntryResponse( ) )
115 {
116 if ( response.getField( ) != null && response.getField( ).getIdField( ) == automaticAssignment.getIdField( ) )
117 {
118 listWorkgroup.add( automaticAssignment.getWorkgroupKey( ) );
119 }
120 }
121 }
122 }
123
124 }
125
126 TaskAutomaticAssignmentConfig config = _taskAutomaticAssignmentConfigService.findByPrimaryKey( this.getId( ) );
127
128 if ( ( config != null ) && config.isNotify( ) )
129 {
130 _automaticAssignmentService.notify( listFormQuestionResponse, config, listWorkgroup, resourceHistory, request, locale, this );
131 }
132
133
134 ResourceWorkflow resourceWorkflow = _resourceWorkflowService.findByPrimaryKey( resourceHistory.getIdResource( ), resourceHistory.getResourceType( ),
135 resourceHistory.getWorkflow( ).getId( ) );
136 resourceWorkflow.setWorkgroups( listWorkgroup );
137 _resourceWorkflowService.update( resourceWorkflow );
138 }
139
140
141
142
143 @Override
144 public void doRemoveConfig( )
145 {
146 Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
147 Plugin workflowPlugin = PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
148
149 _taskAutomaticAssignmentConfigService.remove( this.getId( ) );
150 _automaticAssignmentService.removeByTask( this.getId( ), autoAssignPlugin );
151
152 _assignmentHistoryService.removeByTask( this.getId( ), workflowPlugin );
153 _workgroupConfigService.removeByTask( this.getId( ), workflowPlugin );
154 }
155
156
157
158
159 @Override
160 public void doRemoveTaskInformation( int nIdHistory )
161 {
162 Plugin workflowPlugin = PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
163 _assignmentHistoryService.removeByHistory( nIdHistory, this.getId( ), workflowPlugin );
164 }
165
166
167
168
169 @Override
170 public String getTitle( Locale locale )
171 {
172 TaskAutomaticAssignmentConfig config = _taskAutomaticAssignmentConfigService.findByPrimaryKey( this.getId( ) );
173
174 if ( config != null )
175 {
176 return config.getTitle( );
177 }
178
179 return StringUtils.EMPTY;
180 }
181 }