-
Notifications
You must be signed in to change notification settings - Fork 188
/
ProcessInstanceDtoProvider.java
170 lines (144 loc) · 6.73 KB
/
ProcessInstanceDtoProvider.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
/*
* 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.web.page.admin.workflow.dto;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.WorkflowService;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.builder.*;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.SchemaException;
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.web.component.data.BaseSortableDataProvider;
import com.evolveum.midpoint.web.security.SecurityUtils;
import com.evolveum.midpoint.wf.api.WorkflowManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WfContextType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WfProcessInstanceType;
import org.apache.wicket.Component;
import java.util.Iterator;
import java.util.List;
import static com.evolveum.midpoint.gui.api.util.WebComponentUtil.*;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType.F_OBJECT_REF;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType.F_WORKFLOW_CONTEXT;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.WfContextType.F_PROCESS_INSTANCE_ID;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.WfContextType.F_REQUESTER_REF;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.WfContextType.F_START_TIMESTAMP;
/**
* @author lazyman
* @author mederly
*/
public class ProcessInstanceDtoProvider extends BaseSortableDataProvider<ProcessInstanceDto> {
private static final transient Trace LOGGER = TraceManager.getTrace(ProcessInstanceDtoProvider.class);
private static final String DOT_CLASS = ProcessInstanceDtoProvider.class.getName() + ".";
private static final String OPERATION_LIST_ITEMS = DOT_CLASS + "listItems";
private static final String OPERATION_COUNT_ITEMS = DOT_CLASS + "countItems";
/*
* requestedBy:
* - true = we are interested in process instances REQUESTED BY a user (e.g. the user has requested granting a role to another user)
* - false = we are interested in process instances REQUESTED FOR a user (e.g. the user is to be granted a role)
*/
private boolean requestedBy;
private boolean requestedFor;
public static String currentUser() {
MidPointPrincipal principal = SecurityUtils.getPrincipalUser();
if (principal == null) {
return "Unknown";
}
return principal.getOid();
}
public ProcessInstanceDtoProvider(Component component, boolean requestedBy, boolean requestedFor) {
super(component);
this.requestedBy = requestedBy;
this.requestedFor = requestedFor;
}
@Override
public Iterator<? extends ProcessInstanceDto> internalIterator(long first, long count) {
getAvailableData().clear();
Task opTask = getTaskManager().createTaskInstance(OPERATION_LIST_ITEMS);
OperationResult result = opTask.getResult();
try {
// SortParam sortParam = getSort();
// OrderDirectionType order;
// if (sortParam.isAscending()) {
// order = OrderDirectionType.ASCENDING;
// } else {
// order = OrderDirectionType.DESCENDING;
// }
ObjectQuery query = getObjectQuery();
query.setPaging(ObjectPaging.createPaging(safeLongToInteger(first), safeLongToInteger(count)));
List<PrismObject<TaskType>> tasks = getModel().searchObjects(TaskType.class, query, null, opTask, result);
for (PrismObject<TaskType> task : tasks) {
try {
getAvailableData().add(new ProcessInstanceDto(task.asObjectable()));
} catch (Exception e) {
LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing workflow task {}", e, task);
result.recordPartialError("Couldn't list process instance.", e);
}
}
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing wf-related tasks", ex);
result.recordFatalError("Couldn't list wf-related tasks.", ex);
}
if (result.isUnknown()) {
result.computeStatus();
}
if (!result.isSuccess()) {
getPage().showResult(result);
}
return getAvailableData().iterator();
}
private ObjectQuery getObjectQuery() throws SchemaException {
String currentUserOid = currentUser();
S_FilterEntry q = QueryBuilder.queryFor(TaskType.class, getPrismContext());
if (requestedBy) {
q = q.item(F_WORKFLOW_CONTEXT, F_REQUESTER_REF).ref(currentUserOid).and();
}
if (requestedFor) {
q = q.item(F_OBJECT_REF).ref(currentUserOid).and();
}
return q
.not().item(F_WORKFLOW_CONTEXT, F_PROCESS_INSTANCE_ID).isNull()
.desc(F_WORKFLOW_CONTEXT, F_START_TIMESTAMP)
.build();
}
@Override
protected int internalSize() {
int count = 0;
Task opTask = getTaskManager().createTaskInstance(OPERATION_COUNT_ITEMS);
OperationResult result = opTask.getResult();
try {
ObjectQuery query = getObjectQuery();
count = getModel().countObjects(TaskType.class, query, null, opTask, result);
} catch (Exception ex) {
String msg = "Couldn't count process instances";
LoggingUtils.logUnexpectedException(LOGGER, msg, ex);
result.recordFatalError(msg, ex);
}
if (result.isUnknown()) {
result.computeStatus();
}
if (!result.isSuccess()) {
getPage().showResult(result);
}
return count;
}
}