/
WorkItemDtoProvider.java
208 lines (175 loc) · 8.67 KB
/
WorkItemDtoProvider.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
/*
* Copyright (c) 2010-2017 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.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.ModelAuthorizationAction;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.builder.S_FilterEntryOrEmpty;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
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.*;
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.page.admin.workflow.WorkItemsPageType;
import com.evolveum.midpoint.web.page.error.PageError;
import com.evolveum.midpoint.web.security.SecurityUtils;
import com.evolveum.midpoint.wf.util.QueryUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.model.IModel;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import static com.evolveum.midpoint.gui.api.util.WebComponentUtil.isAuthorized;
import static com.evolveum.midpoint.gui.api.util.WebComponentUtil.safeLongToInteger;
import static com.evolveum.midpoint.prism.PrismConstants.T_PARENT;
import static com.evolveum.midpoint.prism.query.OrderDirection.DESCENDING;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ASSIGNEE_REF;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_CREATE_TIMESTAMP;
/**
* @author lazyman
*/
public class WorkItemDtoProvider extends BaseSortableDataProvider<WorkItemDto> {
private static final transient Trace LOGGER = TraceManager.getTrace(WorkItemDtoProvider.class);
private static final String DOT_CLASS = WorkItemDtoProvider.class.getName() + ".";
private static final String OPERATION_LIST_ITEMS = DOT_CLASS + "listItems";
private static final String OPERATION_COUNT_ITEMS = DOT_CLASS + "countItems";
private WorkItemsPageType workItemsPageType;
private IModel<PrismObject<UserType>> donorModel;
public WorkItemDtoProvider(Component component, WorkItemsPageType workItemsPageType,
IModel<PrismObject<UserType>> donorModel) {
super(component);
this.workItemsPageType = workItemsPageType;
this.donorModel = donorModel;
}
private String currentUserOid() {
MidPointPrincipal principal = SecurityUtils.getPrincipalUser();
if (principal == null) {
return "Unknown";
}
return principal.getOid();
}
@Override
public Iterator<? extends WorkItemDto> iterator(long first, long count) {
assumePowerOfAttorneyIfRequested();
try {
return super.iterator(first, count);
} finally {
dropPowerOfAttorneyIfRequested();
}
}
@Override
public long size() {
assumePowerOfAttorneyIfRequested();
try {
return super.size();
} finally {
dropPowerOfAttorneyIfRequested();
}
}
private void assumePowerOfAttorneyIfRequested() {
if (workItemsPageType == WorkItemsPageType.ATTORNEY) {
WebModelServiceUtils.assumePowerOfAttorney(donorModel.getObject(), getModelInteractionService(), getTaskManager(), null);
}
}
private void dropPowerOfAttorneyIfRequested() {
if (workItemsPageType == WorkItemsPageType.ATTORNEY) {
WebModelServiceUtils.dropPowerOfAttorney(getModelInteractionService(), getTaskManager(), null);
}
}
@Override
public Iterator<? extends WorkItemDto> internalIterator(long first, long count) {
getAvailableData().clear();
Task task = getTaskManager().createTaskInstance();
OperationResult result = new OperationResult(OPERATION_LIST_ITEMS);
try {
ObjectQuery query = createQuery(first, count, result);
Collection<SelectorOptions<GetOperationOptions>> options = getOperationOptionsBuilder()
.item(F_ASSIGNEE_REF).resolve()
.item(T_PARENT, CaseType.F_OBJECT_REF).resolve()
.item(T_PARENT, CaseType.F_TARGET_REF).resolve()
.build();
List<CaseWorkItemType> items = getModel().searchContainers(CaseWorkItemType.class, query, options, task, result);
for (CaseWorkItemType item : items) {
try {
getAvailableData().add(new WorkItemDto(item, getPage()));
} catch (Exception e) {
LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing work item {}", e, item);
result.recordFatalError(getPage().createStringResource("WorkItemDtoProvider.message.couldNotListWorkItem").getString(), e);
}
}
} catch (CommonException | RuntimeException ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing work items", ex);
result.recordFatalError(getPage().createStringResource("WorkItemDtoProvider.message.couldNotListWorkItem").getString(), ex);
}
if (result.isUnknown()) {
result.computeStatus();
}
if (!WebComponentUtil.isSuccessOrHandledError(result)) {
handleError(result);
}
return getAvailableData().iterator();
}
private void handleError(OperationResult result) {
getPage().showResult(result);
throw new RestartResponseException(PageError.class);
}
private ObjectQuery createQuery(long first, long count, OperationResult result) throws SchemaException {
ObjectQuery query = createQuery(result);
query.setPaging(getPrismContext().queryFactory().createPaging(safeLongToInteger(first), safeLongToInteger(count), F_CREATE_TIMESTAMP, DESCENDING));
return query;
}
private ObjectQuery createQuery(OperationResult result) throws SchemaException {
boolean authorizedToSeeAll = isAuthorized(ModelAuthorizationAction.READ_ALL_WORK_ITEMS.getUrl());
S_FilterEntryOrEmpty q = getPrismContext().queryFor(CaseWorkItemType.class);
if (WorkItemsPageType.ALL.equals(workItemsPageType) && authorizedToSeeAll) {
return q.build();
} else if (WorkItemsPageType.CLAIMABLE.equals(workItemsPageType)) {
return QueryUtils.filterForClaimableItems(q, currentUserOid(), getRepositoryService(), getRelationRegistry(), result).build();
} else {
// not authorized to see all => sees only allocated to him (not quite what is expected, but sufficient for the time being)
return QueryUtils.filterForAssignees(q, SecurityUtils.getPrincipalUser(),
OtherPrivilegesLimitationType.F_APPROVAL_WORK_ITEMS, getRelationRegistry()).build();
}
}
@Override
protected int internalSize() {
int count;
Task task = getTaskManager().createTaskInstance();
OperationResult result = new OperationResult(OPERATION_COUNT_ITEMS);
try {
ObjectQuery query = createQuery(result);
count = getModel().countContainers(CaseWorkItemType.class, query, null, task, result);
} catch (SchemaException | SecurityViolationException | ObjectNotFoundException | ExpressionEvaluationException | RuntimeException | CommunicationException | ConfigurationException e) {
throw new SystemException("Couldn't count work items: " + e.getMessage(), e);
}
if (result.isUnknown()) {
result.computeStatus();
}
if (!WebComponentUtil.isSuccessOrHandledError(result)) {
handleError(result);
}
return count;
}
}