-
Notifications
You must be signed in to change notification settings - Fork 188
/
ActivitiInterface.java
228 lines (195 loc) · 9.96 KB
/
ActivitiInterface.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.wf.impl.activiti;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskExecutionStatus;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.wf.impl.activiti.dao.WorkItemProvider;
import com.evolveum.midpoint.wf.impl.messages.*;
import com.evolveum.midpoint.wf.impl.processes.common.ActivitiUtil;
import com.evolveum.midpoint.wf.impl.tasks.WfTaskController;
import com.evolveum.midpoint.wf.impl.processes.ProcessInterfaceFinder;
import com.evolveum.midpoint.wf.impl.processes.common.CommonProcessVariableNames;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricDetailQuery;
import org.activiti.engine.history.HistoricFormProperty;
import org.activiti.engine.history.HistoricVariableUpdate;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceBuilder;
import org.activiti.engine.task.IdentityLink;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* Transports messages between midPoint and Activiti. (Originally via Camel, currently using direct java calls.)
*/
@Component
public class ActivitiInterface {
private static final Trace LOGGER = TraceManager.getTrace(ActivitiInterface.class);
private static final String DOT_CLASS = ActivitiInterface.class.getName() + ".";
@Autowired
private ActivitiEngine activitiEngine;
@Autowired
private TaskManager taskManager;
@Autowired
private WfTaskController wfTaskController;
@Autowired
private ProcessInterfaceFinder processInterfaceFinder;
@Autowired
private WorkItemProvider workItemProvider;
public void startActivitiProcessInstance(StartProcessCommand spic, Task task, OperationResult result)
throws SchemaException, ObjectNotFoundException, ObjectAlreadyExistsException {
String owner = spic.getProcessOwner();
if (owner != null) {
activitiEngine.getIdentityService().setAuthenticatedUserId(owner);
}
RuntimeService rs = activitiEngine.getProcessEngine().getRuntimeService();
ProcessInstanceBuilder builder = rs.createProcessInstanceBuilder()
.processDefinitionKey(spic.getProcessName())
.processInstanceName(spic.getProcessInstanceName());
for (Map.Entry<String, Object> varEntry : spic.getVariables().entrySet()) {
builder.addVariable(varEntry.getKey(), varEntry.getValue());
}
ProcessInstance pi = builder.start();
if (spic.isSendStartConfirmation()) { // let us send a reply back (useful for listener-free processes)
ProcessStartedEvent event = new ProcessStartedEvent(
pi.getProcessInstanceId(),
((ExecutionEntity) pi).getVariables(), // a bit of hack...
processInterfaceFinder);
event.setRunning(!pi.isEnded());
LOGGER.trace("Event to be sent to IDM: {}", event);
wfTaskController.onProcessEvent(event, task, result);
}
}
public void queryActivitiProcessInstance(QueryProcessCommand qpc, Task task, OperationResult result)
throws ObjectAlreadyExistsException, ObjectNotFoundException, SchemaException {
String pid = qpc.getPid();
LOGGER.trace("Querying process instance id {}", pid);
HistoryService hs = activitiEngine.getHistoryService();
HistoricDetailQuery hdq = hs.createHistoricDetailQuery()
.variableUpdates()
.processInstanceId(pid)
.orderByTime().desc();
Map<String,Object> variables = new HashMap<>();
for (HistoricDetail hd : hdq.list()) {
HistoricVariableUpdate hvu = (HistoricVariableUpdate) hd;
String varname = hvu.getVariableName();
Object value = hvu.getValue();
LOGGER.trace(" - found historic variable update: {} <- {}", varname, value);
if (!variables.containsKey(varname)) {
variables.put(varname, value);
}
}
HistoricDetailQuery hdq2 = hs.createHistoricDetailQuery()
.formProperties()
.processInstanceId(pid)
.orderByVariableRevision().desc();
for (HistoricDetail hd : hdq2.list()) {
HistoricFormProperty hfp = (HistoricFormProperty) hd;
String varname = hfp.getPropertyId();
Object value = hfp.getPropertyValue();
LOGGER.trace(" - found historic form property: {} <- {}", varname, value);
variables.put(varname, value);
}
QueryProcessResponse qpr = new QueryProcessResponse(pid, variables, processInterfaceFinder);
ProcessInstance pi = activitiEngine.getProcessEngine().getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult();
qpr.setRunning(pi != null && !pi.isEnded());
LOGGER.trace("Running process instance = {}, isRunning: {}", pi, qpr.isRunning());
LOGGER.trace("Response to be sent to midPoint: {}", qpr);
wfTaskController.onProcessEvent(qpr, task, result);
}
public void notifyMidpointAboutProcessFinishedEvent(DelegateExecution execution) {
notifyMidpointAboutProcessEvent(new ProcessFinishedEvent(execution, processInterfaceFinder));
}
public void notifyMidpointAboutProcessEvent(DelegateExecution execution) {
notifyMidpointAboutProcessEvent(new ProcessEvent(execution, processInterfaceFinder));
}
private void notifyMidpointAboutProcessEvent(ProcessEvent event) {
OperationResult result = new OperationResult(DOT_CLASS + "notifyMidpointAboutProcessEvent");
String taskOid = ActivitiUtil.getTaskOid(event.getVariables());
Task task;
try {
task = taskManager.getTaskWithResult(taskOid, result);
} catch (ObjectNotFoundException|SchemaException|RuntimeException e) {
throw new SystemException("Couldn't get task " + taskOid + " from repository: " + e.getMessage(), e);
}
if (task.getExecutionStatus() != TaskExecutionStatus.WAITING) {
LOGGER.trace("Asynchronous message received in a state different from WAITING (actual state: {}), ignoring it. Task = {}", task.getExecutionStatus(), task);
return;
}
try {
wfTaskController.onProcessEvent(event, task, result);
} catch (SchemaException|ObjectNotFoundException|ObjectAlreadyExistsException|RuntimeException e) {
throw new SystemException("Couldn't process a process-related event: " + e.getMessage(), e);
}
}
public void notifyMidpointAboutTaskEvent(DelegateTask delegateTask) {
OperationResult result = new OperationResult(DOT_CLASS + "notifyMidpointAboutTaskEvent");
TaskEvent taskEvent;
if (TaskListener.EVENTNAME_CREATE.equals(delegateTask.getEventName())) {
taskEvent = new TaskCreatedEvent(); // TODO distinguish created vs. assigned event
} else if (TaskListener.EVENTNAME_COMPLETE.equals(delegateTask.getEventName())) {
taskEvent = new TaskCompletedEvent();
} else if (TaskListener.EVENTNAME_DELETE.equals(delegateTask.getEventName())) {
taskEvent = new TaskDeletedEvent();
} else {
return; // ignoring other events
}
taskEvent.setVariables(delegateTask.getVariables());
taskEvent.setAssigneeOid(delegateTask.getAssignee());
taskEvent.setTaskId(delegateTask.getId());
taskEvent.setTaskName(delegateTask.getName());
taskEvent.setProcessInstanceName((String) delegateTask.getVariable(CommonProcessVariableNames.VARIABLE_PROCESS_INSTANCE_NAME));
taskEvent.setProcessInstanceId(delegateTask.getProcessInstanceId());
taskEvent.setCreateTime(delegateTask.getCreateTime());
taskEvent.setDueDate(delegateTask.getDueDate());
taskEvent.setExecutionId(delegateTask.getExecutionId());
taskEvent.setOwner(delegateTask.getOwner());
for (IdentityLink identityLink : delegateTask.getCandidates()) {
if (identityLink.getUserId() != null) {
taskEvent.getCandidateUsers().add(identityLink.getUserId());
} else if (identityLink.getGroupId() != null) {
taskEvent.getCandidateGroups().add(identityLink.getGroupId());
} else {
throw new IllegalStateException("Neither candidate user nor group id is provided in delegateTask: " + delegateTask);
}
}
try {
WorkItemType workItem = workItemProvider.taskEventToWorkItemNew(taskEvent, null, true, true, true, result);
wfTaskController.onTaskEvent(workItem, taskEvent, result);
} catch (Exception e) { // todo fix the exception processing e.g. think about situation where an event cannot be audited - should we allow to proceed?
String message = "Couldn't process an event coming from the workflow management system";
LoggingUtils.logUnexpectedException(LOGGER, message, e);
result.recordFatalError(message, e);
}
}
}