-
Notifications
You must be signed in to change notification settings - Fork 188
/
ActivitiUtil.java
200 lines (177 loc) · 8.62 KB
/
ActivitiUtil.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
/*
* 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.processes.common;
import com.evolveum.midpoint.model.api.expr.MidpointFunctions;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.schema.util.WfContextUtil;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
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.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.wf.impl.processes.itemApproval.ApprovalLevel;
import com.evolveum.midpoint.wf.impl.processes.itemApproval.ProcessVariableNames;
import com.evolveum.midpoint.wf.impl.util.MiscDataUtil;
import com.evolveum.midpoint.wf.impl.util.SerializationSafeContainer;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ApprovalLevelType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WfContextType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemEventType;
import org.activiti.engine.delegate.DelegateExecution;
import org.jetbrains.annotations.NotNull;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.evolveum.midpoint.wf.impl.processes.common.CommonProcessVariableNames.*;
import static com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getMidpointFunctions;
import static com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getTaskManager;
/**
* General utilities that can be used from within processes.
*
* @author mederly
*/
public class ActivitiUtil implements Serializable {
private static final long serialVersionUID = 5183098710717369392L;
private static final Trace LOGGER = TraceManager.getTrace(ActivitiUtil.class);
@NotNull
public static ApprovalLevelType getAndVerifyCurrentStage(DelegateExecution execution, Task wfTask, boolean stageInContextSet,
PrismContext prismContext) {
int levelIndex = getRequiredVariable(execution, ProcessVariableNames.LEVEL_INDEX, Integer.class, prismContext);
int stageNumber = levelIndex+1;
ApprovalLevelType level;
if (stageInContextSet) {
level = WfContextUtil.getCurrentApprovalLevel(wfTask.getWorkflowContext());
if (level == null) {
throw new IllegalStateException("No current stage information in " + wfTask);
}
} else {
level = WfContextUtil.getApprovalLevel(wfTask.getWorkflowContext(), stageNumber);
if (level == null) {
throw new IllegalStateException("No stage #" + stageNumber + " in " + wfTask);
}
}
ApprovalLevel wfLevel = getRequiredVariable(execution, ProcessVariableNames.LEVEL, ApprovalLevel.class, prismContext);
if (!level.getOrder().equals(wfLevel.getOrder()) || level.getOrder() != stageNumber) {
throw new IllegalStateException("Current stage number in " + wfTask + " (" + level.getOrder()
+ "), number present in activiti process (" + wfLevel
+ "), and the stage number according to activiti process (" + stageNumber + ") do not match.");
}
return level;
}
public PrismContext getPrismContext() {
return SpringApplicationContextHolder.getPrismContext();
}
public void revive(SerializationSafeContainer<?> container) {
container.setPrismContext(SpringApplicationContextHolder.getPrismContext());
}
// todo - better name?
public MidpointFunctions midpoint() {
return getMidpointFunctions();
}
@Override
public String toString() {
return this.getClass().getName() + " object.";
}
@NotNull
public static String getTaskOid(Map<String, Object> variables) {
return ActivitiUtil.getRequiredVariable(variables, CommonProcessVariableNames.VARIABLE_MIDPOINT_TASK_OID, String.class, null);
}
@NotNull
public static Task getTask(DelegateExecution execution, OperationResult result) {
String oid = getTaskOid(execution.getVariables());
try {
return getTaskManager().getTask(oid, result);
} catch (ObjectNotFoundException|SchemaException|RuntimeException e) {
throw new SystemException("Couldn't get task " + oid + " corresponding to process " + execution.getProcessInstanceId(), e);
}
}
@SuppressWarnings("unchecked")
public static <T> T getRequiredVariable(DelegateExecution execution, String name, Class<T> clazz, PrismContext prismContext) {
Object value = getVariable(execution.getVariables(), name, clazz, prismContext);
if (value == null) {
throw new IllegalStateException("Required process variable " + name + " is missing in " + execution);
} else {
return (T) value;
}
}
public static <T> T getRequiredVariable(Map<String, Object> variables, String name, Class<T> clazz, PrismContext prismContext) {
Object value = getVariable(variables, name, clazz, prismContext);
if (value == null) {
throw new IllegalStateException("Required process variable " + name + " is missing");
} else {
return (T) value;
}
}
public static <T> T getVariable(DelegateExecution execution, String name, Class<T> clazz, PrismContext prismContext) {
return getVariable(execution.getVariables(), name, clazz, prismContext);
}
@SuppressWarnings("unchecked")
public static <T> T getVariable(Map<String, Object> variables, String name, Class<T> clazz, PrismContext prismContext) {
Object value = variables.get(name);
if (value instanceof SerializationSafeContainer && !SerializationSafeContainer.class.isAssignableFrom(clazz)) {
SerializationSafeContainer container = (SerializationSafeContainer) value;
if (container.getPrismContext() == null && prismContext != null) {
container.setPrismContext(prismContext);
}
value = container.getValue();
}
if (value != null && !(clazz.isAssignableFrom(value.getClass()))) {
throw new IllegalStateException("Process variable " + name + " should be of " + clazz + " but is of "
+ value.getClass() + " instead.");
} else {
return (T) value;
}
}
public static <T> T getVariable(Map<String, Object> variables, String name, Class<T> clazz) {
return getVariable(variables, name, clazz, null);
}
@NotNull
public static WfContextType getWorkflowContext(Task wfTask) {
if (wfTask == null) {
throw new IllegalArgumentException("No task");
} else if (wfTask.getWorkflowContext() == null) {
throw new IllegalArgumentException("No workflow context in task " + wfTask);
} else {
return wfTask.getWorkflowContext();
}
}
public static List<LightweightObjectRef> toLightweightReferences(Collection<ObjectReferenceType> refs) {
return refs.stream().map(ort -> new LightweightObjectRefImpl(ort)).collect(Collectors.toList());
}
// TODO move to better place (it is called also from WorkItemManager)
public static void fillInWorkItemEvent(WorkItemEventType event, MidPointPrincipal currentUser, String workItemId, Map<String, Object> variables) {
if (currentUser != null) {
event.setInitiatorRef(ObjectTypeUtil.createObjectRef(currentUser.getUser()));
}
event.setTimestamp(XmlTypeConverter.createXMLGregorianCalendar(new Date()));
event.setWorkItemId(workItemId);
String originalAssigneeString = ActivitiUtil.getVariable(variables, VARIABLE_ORIGINAL_ASSIGNEE, String.class, null);
if (originalAssigneeString != null) {
event.setOriginalAssigneeRef(MiscDataUtil.stringToRef(originalAssigneeString));
}
event.setStageDisplayName(ActivitiUtil.getVariable(variables, VARIABLE_STAGE_DISPLAY_NAME, String.class, null));
event.setStageName(ActivitiUtil.getVariable(variables, VARIABLE_STAGE_NAME, String.class, null));
event.setStageNumber(ActivitiUtil.getRequiredVariable(variables, VARIABLE_STAGE_NUMBER, Integer.class, null));
}
}