-
Notifications
You must be signed in to change notification settings - Fork 188
/
CertCampaignTypeUtil.java
351 lines (313 loc) · 16.1 KB
/
CertCampaignTypeUtil.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (c) 2010-2015 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.schema.util;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.builder.QueryBuilder;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.StringUtils;
import javax.xml.namespace.QName;
import java.util.*;
import java.util.stream.Collectors;
import static com.evolveum.midpoint.prism.PrismConstants.T_PARENT;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationResponseType.ACCEPT;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationResponseType.NO_RESPONSE;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationResponseType.REDUCE;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationResponseType.REVOKE;
/**
* @author mederly
*/
public class CertCampaignTypeUtil {
public static AccessCertificationStageType getCurrentStage(AccessCertificationCampaignType campaign) {
for (AccessCertificationStageType stage : campaign.getStage()) {
if (stage.getNumber() == campaign.getStageNumber()) {
return stage;
}
}
return null;
}
public static AccessCertificationStageDefinitionType getCurrentStageDefinition(AccessCertificationCampaignType campaign) {
return findStageDefinition(campaign, campaign.getStageNumber());
}
public static AccessCertificationStageDefinitionType findStageDefinition(AccessCertificationCampaignType campaign, int stageNumber) {
for (AccessCertificationStageDefinitionType stage : campaign.getStageDefinition()) {
if (stage.getNumber() == stageNumber) {
return stage;
}
}
throw new IllegalStateException("No stage " + stageNumber + " in " + ObjectTypeUtil.toShortString(campaign));
}
public static AccessCertificationStageType findStage(AccessCertificationCampaignType campaign, int stageNumber) {
for (AccessCertificationStageType stage : campaign.getStage()) {
if (stage.getNumber() == stageNumber) {
return stage;
}
}
throw new IllegalStateException("No stage " + stageNumber + " in " + ObjectTypeUtil.toShortString(campaign));
}
public static AccessCertificationCaseType findCase(AccessCertificationCampaignType campaign, long caseId) {
for (AccessCertificationCaseType _case : campaign.getCase()) {
if (_case.asPrismContainerValue().getId() != null && _case.asPrismContainerValue().getId() == caseId) {
return _case;
}
}
return null;
}
// to be used in tests (beware: there could be more work items)
// TODO move to a test class
public static AccessCertificationWorkItemType findWorkItem(AccessCertificationCaseType _case, int stageNumber, String reviewerOid) {
return _case.getWorkItem().stream()
.filter(wi -> wi.getStageNumber() == stageNumber && ObjectTypeUtil.containsOid(wi.getReviewerRef(), reviewerOid))
.findFirst().orElse(null);
}
public static AccessCertificationWorkItemType findWorkItem(AccessCertificationCaseType _case, long workItemId) {
return _case.getWorkItem().stream()
.filter(wi -> wi.getId() != null && wi.getId() == workItemId)
.findFirst().orElse(null);
}
public static int getNumberOfStages(AccessCertificationCampaignType campaign) {
return campaign.getStageDefinition().size();
}
public static AccessCertificationDefinitionType getDefinition(AccessCertificationCampaignType campaign) {
if (campaign.getDefinitionRef() == null) {
throw new IllegalStateException("No definition reference in " + ObjectTypeUtil.toShortString(campaign));
}
PrismReferenceValue referenceValue = campaign.getDefinitionRef().asReferenceValue();
if (referenceValue.getObject() == null) {
throw new IllegalStateException("No definition object in " + ObjectTypeUtil.toShortString(campaign));
}
return (AccessCertificationDefinitionType) (referenceValue.getObject().asObjectable());
}
public static boolean isRemediationAutomatic(AccessCertificationCampaignType campaign) {
return campaign.getRemediationDefinition() != null &&
AccessCertificationRemediationStyleType.AUTOMATED.equals(campaign.getRemediationDefinition().getStyle());
}
public static boolean isCampaignClosed(AccessCertificationCampaignType campaign) {
int currentStage = campaign.getStageNumber();
int stages = getNumberOfStages(campaign);
return AccessCertificationCampaignStateType.CLOSED.equals(campaign.getState()) || currentStage > stages;
}
// TODO rework signalling problems
public static void checkStageDefinitionConsistency(List<AccessCertificationStageDefinitionType> stages) {
int count = stages.size();
boolean[] numberPresent = new boolean[count];
for (AccessCertificationStageDefinitionType stage : stages) {
int num = stage.getNumber();
if (num < 1 || num > count) {
throw new IllegalStateException("Invalid stage number: " + num + " (stage count: " + count +")");
}
if (numberPresent[num-1]) {
throw new IllegalStateException("Stage with number " + num + " is defined multiple times");
}
numberPresent[num-1] = true;
}
// maybe redundant test
for (int i = 0; i < numberPresent.length; i++) {
if (!numberPresent[i]) {
throw new IllegalStateException("Stage with number " + (i+1) + " was not defined");
}
}
}
// expects that the currentStageNumber is reasonable
public static AccessCertificationStageType findCurrentStage(AccessCertificationCampaignType campaign) {
return findStage(campaign, campaign.getStageNumber());
}
// active cases = cases that are to be responded to in this stage
public static int getActiveCases(List<AccessCertificationCaseType> caseList, int campaignStageNumber, AccessCertificationCampaignStateType state) {
int open = 0;
if (state == AccessCertificationCampaignStateType.IN_REMEDIATION || state == AccessCertificationCampaignStateType.CLOSED) {
campaignStageNumber = campaignStageNumber - 1; // move to last campaign state
}
for (AccessCertificationCaseType aCase : caseList) {
if (aCase.getCurrentStageNumber() != campaignStageNumber) {
continue;
}
open++;
}
return open;
}
// unanswered cases = cases where one or more answers from reviewers are missing
// "no reviewers" cases are treated as answered, because no answer can be provided
public static int getUnansweredCases(List<AccessCertificationCaseType> caseList, int campaignStageNumber, AccessCertificationCampaignStateType state) {
int unansweredCases = 0;
for (AccessCertificationCaseType aCase : caseList) {
for (AccessCertificationWorkItemType workItem : aCase.getWorkItem()) {
if (workItem.getStageNumber() != campaignStageNumber) {
continue;
}
if (workItem.getClosedTimestamp() == null && (workItem.getResponse() == null || workItem.getResponse() == NO_RESPONSE)) {
unansweredCases++; // TODO what about delegations?
break;
}
}
}
return unansweredCases;
}
// backwards compatibility (for reports)
public static int getPercentComplete(List<AccessCertificationCaseType> caseList, int campaignStageNumber, AccessCertificationCampaignStateType state) {
return Math.round(getCasesCompletedPercentage(caseList, campaignStageNumber, state));
}
// what % of cases is fully decided? (i.e. either they have all the decisions or they are not in the current stage at all)
public static float getCasesCompletedPercentage(AccessCertificationCampaignType campaign) {
return getCasesCompletedPercentage(campaign.getCase(), campaign.getStageNumber(), campaign.getState());
}
public static float getCasesCompletedPercentage(List<AccessCertificationCaseType> caseList, int campaignStageNumber, AccessCertificationCampaignStateType state) {
int cases = caseList.size();
if (cases > 0) {
int unanswered = getUnansweredCases(caseList, campaignStageNumber, state);
return 100 * ((float) cases - (float) unanswered) / (float) cases;
} else {
return 100.0f;
}
}
// what % of cases is effectively decided? (i.e. their preliminary outcome is ACCEPT, REJECT, or REVOKE)
public static float getCasesDecidedPercentage(AccessCertificationCampaignType campaign) {
return getCasesDecidedPercentage(campaign.getCase());
}
public static float getCasesDecidedPercentage(List<AccessCertificationCaseType> caseList) {
if (caseList.isEmpty()) {
return 100.0f;
}
int decided = 0;
for (AccessCertificationCaseType aCase : caseList) {
if (aCase.getOverallOutcome() == ACCEPT || aCase.getOverallOutcome() == REVOKE || aCase.getOverallOutcome() == REDUCE) {
decided++;
}
}
return 100.0f * (float) decided / (float) caseList.size();
}
// what % of decisions is complete?
public static float getDecisionsDonePercentage(AccessCertificationCampaignType campaign) {
return getDecisionsDonePercentage(campaign.getCase(), campaign.getStageNumber(), campaign.getState());
}
public static float getDecisionsDonePercentage(List<AccessCertificationCaseType> caseList, int campaignStageNumber, AccessCertificationCampaignStateType state) {
int decisionsRequested = 0;
int decisionsDone = 0;
if (state == AccessCertificationCampaignStateType.IN_REMEDIATION || state == AccessCertificationCampaignStateType.CLOSED) {
campaignStageNumber = campaignStageNumber - 1; // move to last campaign state
}
for (AccessCertificationCaseType aCase : caseList) {
for (AccessCertificationWorkItemType workItem : aCase.getWorkItem()) {
if (workItem.getStageNumber() != campaignStageNumber) {
continue;
}
decisionsRequested++;
if (workItem.getResponse() != null && workItem.getResponse() != NO_RESPONSE) { // TODO what about delegations?
decisionsDone++;
}
}
}
if (decisionsRequested == 0) {
return 100.0f;
} else {
return 100.0f * (float) decisionsDone / (float) decisionsRequested;
}
}
public static Date getReviewedTimestamp(List<AccessCertificationWorkItemType> workItems) {
Date lastDate = null;
for (AccessCertificationWorkItemType workItem : workItems) {
if (hasNoResponse(workItem)) {
continue;
}
Date responseDate = XmlTypeConverter.toDate(workItem.getTimestamp());
if (lastDate == null || responseDate.after(lastDate)) {
lastDate = responseDate;
}
}
return lastDate;
}
private static boolean hasNoResponse(AccessCertificationWorkItemType workItem) {
return (workItem.getResponse() == null || workItem.getResponse() == NO_RESPONSE) && StringUtils.isEmpty(workItem.getComment());
}
public static List<ObjectReferenceType> getReviewedBy(List<AccessCertificationWorkItemType> workItems) {
List<ObjectReferenceType> rv = new ArrayList<>();
for (AccessCertificationWorkItemType workItem : workItems) {
if (hasNoResponse(workItem)) {
continue;
}
rv.add(workItem.getResponderRef());
}
return rv;
}
public static List<String> getComments(List<AccessCertificationWorkItemType> workItems) {
List<String> rv = new ArrayList<>();
for (AccessCertificationWorkItemType workItem : workItems) {
if (StringUtils.isEmpty(workItem.getComment())) {
continue;
}
rv.add(workItem.getComment());
}
return rv;
}
// TODO find a better place for this
public static ItemPath getOrderBy(QName oldName) {
if (QNameUtil.match(oldName, AccessCertificationCaseType.F_TARGET_REF)) {
return new ItemPath(AccessCertificationCaseType.F_TARGET_REF, PrismConstants.T_OBJECT_REFERENCE, ObjectType.F_NAME);
} else if (QNameUtil.match(oldName, AccessCertificationCaseType.F_OBJECT_REF)) {
return new ItemPath(AccessCertificationCaseType.F_OBJECT_REF, PrismConstants.T_OBJECT_REFERENCE, ObjectType.F_NAME);
} else if (QNameUtil.match(oldName, AccessCertificationCaseType.F_TENANT_REF)) {
return new ItemPath(AccessCertificationCaseType.F_TENANT_REF, PrismConstants.T_OBJECT_REFERENCE, ObjectType.F_NAME);
} else if (QNameUtil.match(oldName, AccessCertificationCaseType.F_ORG_REF)) {
return new ItemPath(AccessCertificationCaseType.F_ORG_REF, PrismConstants.T_OBJECT_REFERENCE, ObjectType.F_NAME);
} else if (QNameUtil.match(oldName, AccessCertificationCaseType.F_CAMPAIGN_REF)) {
return new ItemPath(T_PARENT, ObjectType.F_NAME);
} else {
return new ItemPath(oldName);
}
}
public static ObjectQuery createCasesForCampaignQuery(String campaignOid, PrismContext prismContext) throws SchemaException {
return QueryBuilder.queryFor(AccessCertificationCaseType.class, prismContext)
.ownerId(campaignOid)
.build();
}
public static AccessCertificationCaseStageOutcomeType getStageOutcome(AccessCertificationCaseType aCase, int stageNumber) {
for (AccessCertificationCaseStageOutcomeType outcome : aCase.getCompletedStageOutcome()) {
if (outcome.getStageNumber() == stageNumber) {
return outcome;
}
}
throw new IllegalStateException("No outcome registered for stage " + stageNumber + " in case " + aCase);
}
public static List<AccessCertificationResponseType> getOutcomesToStopOn(List<AccessCertificationResponseType> stopReviewOn, List<AccessCertificationResponseType> advanceToNextStageOn) {
if (!stopReviewOn.isEmpty()) {
return stopReviewOn;
}
List<AccessCertificationResponseType> rv = new ArrayList<>(Arrays.asList(AccessCertificationResponseType.values()));
rv.removeAll(advanceToNextStageOn);
return rv;
}
// useful e.g. for tests
public static Set<ObjectReferenceType> getReviewers(AccessCertificationCaseType aCase) {
return aCase.getWorkItem().stream()
.flatMap(wi -> wi.getReviewerRef().stream())
.collect(Collectors.toSet());
}
public static AccessCertificationCaseType getCase(AccessCertificationWorkItemType workItem) {
PrismContainerable<AccessCertificationWorkItemType> parent = workItem.asPrismContainerValue().getParent();
if (!(parent instanceof PrismContainer)) {
return null;
}
PrismValue parentParent = ((PrismContainer<AccessCertificationWorkItemType>) parent).getParent();
if (!(parentParent instanceof PrismContainerValue)) {
return null;
}
return ((PrismContainerValue<AccessCertificationCaseType>) parentParent).asContainerable();
}
}