-
Notifications
You must be signed in to change notification settings - Fork 188
/
ModelEventImpl.java
332 lines (296 loc) · 13.2 KB
/
ModelEventImpl.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
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.notifications.impl.events;
import com.evolveum.midpoint.model.api.context.ModelContext;
import com.evolveum.midpoint.model.api.context.ModelElementContext;
import com.evolveum.midpoint.model.api.context.ModelProjectionContext;
import com.evolveum.midpoint.notifications.api.events.ModelEvent;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.ObjectDeltaCollectionsUtil;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.ObjectDeltaOperation;
import com.evolveum.midpoint.task.api.LightweightIdentifierGenerator;
import com.evolveum.midpoint.util.DebugUtil;
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.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static java.util.Collections.singletonList;
public class ModelEventImpl extends BaseEventImpl implements ModelEvent {
private static final Trace LOGGER = TraceManager.getTrace(ModelEventImpl.class);
@NotNull private final ModelContext<?> modelContext;
@NotNull private final ModelElementContext<?> focusContext;
public ModelEventImpl(LightweightIdentifierGenerator lightweightIdentifierGenerator, @NotNull ModelContext<?> modelContext) {
super(lightweightIdentifierGenerator);
this.modelContext = modelContext;
this.focusContext = modelContext.getFocusContext();
}
@NotNull
@Override
public ModelContext<?> getModelContext() {
return modelContext;
}
@NotNull
@Override
public ModelElementContext<?> getFocusContext() {
return focusContext;
}
@Override
public Collection<? extends ModelProjectionContext> getProjectionContexts() {
return modelContext.getProjectionContexts();
}
@Override
public List<? extends ObjectDeltaOperation> getFocusExecutedDeltas() {
return getFocusContext().getExecutedDeltas();
}
@Override
public List<ObjectDeltaOperation> getAllExecutedDeltas() {
List<ObjectDeltaOperation> retval = new ArrayList<>(focusContext.getExecutedDeltas());
for (Object o : modelContext.getProjectionContexts()) {
ModelProjectionContext modelProjectionContext = (ModelProjectionContext) o;
retval.addAll(modelProjectionContext.getExecutedDeltas());
}
return retval;
}
@Override
public boolean isStatusType(EventStatusType eventStatus) {
boolean allSuccess = true, anySuccess = false, allFailure = true, anyFailure = false, anyInProgress = false;
for (ObjectDeltaOperation objectDeltaOperation : getAllExecutedDeltas()) {
if (objectDeltaOperation.getExecutionResult() != null) {
switch (objectDeltaOperation.getExecutionResult().getStatus()) {
case SUCCESS:
case WARNING:
case HANDLED_ERROR:
anySuccess = true; allFailure = false;
break;
case FATAL_ERROR:
case PARTIAL_ERROR:
allSuccess = false; anyFailure = true;
break;
case IN_PROGRESS:
allSuccess = false; allFailure = false; anyInProgress = true;
break;
case NOT_APPLICABLE:
break;
case UNKNOWN:
allSuccess = false; allFailure = false;
break;
default: LOGGER.warn("Unknown execution result: {}", objectDeltaOperation.getExecutionResult().getStatus());
}
} else {
allSuccess = false;
allFailure = false;
anyInProgress = true;
}
}
switch (eventStatus) {
case ALSO_SUCCESS: return anySuccess;
case SUCCESS: return allSuccess;
case FAILURE: return anyFailure;
case ONLY_FAILURE: return allFailure;
case IN_PROGRESS: return anyInProgress;
default: throw new IllegalStateException("Invalid eventStatusType: " + eventStatus);
}
}
// a bit of hack but ...
@Override
public ChangeType getChangeType() {
if (isOperationType(EventOperationType.ADD)) {
return ChangeType.ADD;
} else if (isOperationType(EventOperationType.DELETE)) {
return ChangeType.DELETE;
} else {
return ChangeType.MODIFY;
}
}
@Override
public boolean isOperationType(EventOperationType eventOperation) {
// we consider an operation to be 'add' when there is 'add' delta among deltas
// in a similar way with 'delete'
//
// alternatively, we could summarize deltas and then decide based on the type of summarized delta (would be a bit inefficient)
for (Object o : getFocusExecutedDeltas()) {
ObjectDeltaOperation objectDeltaOperation = (ObjectDeltaOperation) o;
if (objectDeltaOperation.getObjectDelta().isAdd()) {
return eventOperation == EventOperationType.ADD;
} else if (objectDeltaOperation.getObjectDelta().isDelete()) {
return eventOperation == EventOperationType.DELETE;
}
}
return eventOperation == EventOperationType.MODIFY;
}
@Override
public boolean isCategoryType(EventCategoryType eventCategory) {
return eventCategory == EventCategoryType.MODEL_EVENT;
}
@Override
public ObjectDelta<?> getFocusPrimaryDelta() {
return focusContext.getPrimaryDelta();
}
@Override
public ObjectDelta<?> getFocusSecondaryDelta() {
return focusContext.getSecondaryDelta();
}
@Override
public List<ObjectDelta<AssignmentHolderType>> getFocusDeltas() {
List<ObjectDelta<AssignmentHolderType>> retval = new ArrayList<>();
Class c = modelContext.getFocusClass();
if (c != null && AssignmentHolderType.class.isAssignableFrom(c)) {
for (Object o : getFocusExecutedDeltas()) {
ObjectDeltaOperation objectDeltaOperation = (ObjectDeltaOperation) o;
//noinspection unchecked
retval.add(objectDeltaOperation.getObjectDelta());
}
}
return retval;
}
@Override
public ObjectDelta<? extends AssignmentHolderType> getSummarizedFocusDeltas() throws SchemaException {
return ObjectDeltaCollectionsUtil.summarize(getFocusDeltas());
}
@Override
public boolean hasFocusOfType(Class<? extends AssignmentHolderType> clazz) {
return focusContext.isOfType(clazz);
}
@Override
public boolean hasFocusOfType(QName focusType) {
PrismContainerDefinition<? extends AssignmentHolderType> pcd =
getPrismContext().getSchemaRegistry().findContainerDefinitionByType(focusType);
if (pcd != null) {
Class<? extends AssignmentHolderType> expectedClass = pcd.getCompileTimeClass();
if (expectedClass != null) {
return hasFocusOfType(expectedClass);
} else {
LOGGER.warn("Couldn't find class for type {}", focusType);
return false;
}
} else {
LOGGER.warn("Couldn't find definition for type {}", focusType);
return false;
}
}
@Override
public boolean isRelatedToItem(ItemPath itemPath) {
return containsItem(getFocusDeltas(), itemPath);
}
@Override
public boolean isUserRelated() {
return hasFocusOfType(UserType.class);
}
@Override
public String getFocusTypeName() {
if (focusContext.getObjectTypeClass() == null) {
return null;
} else {
String simpleName = getFocusContext().getObjectTypeClass().getSimpleName();
return StringUtils.substringBeforeLast(simpleName, "Type"); // should usually work ;)
}
}
@Override
public boolean hasContentToShow(boolean watchAuxiliaryAttributes) {
ObjectDelta<? extends ObjectType> summarizedDelta;
try {
summarizedDelta = getSummarizedFocusDeltas();
if (!summarizedDelta.isModify()) {
return true;
} else if (!getTextFormatter().containsVisibleModifiedItems(summarizedDelta.getModifications(),
false, watchAuxiliaryAttributes)) {
LOGGER.trace("No relevant attributes in modify delta (watchAux={})", watchAuxiliaryAttributes);
return false;
} else {
return true;
}
} catch (Throwable t) {
LoggingUtils.logUnexpectedException(LOGGER, "Unable to check if there's content to show; focus context = {}", t, focusContext.debugDump());
return false;
}
}
@Override
public String getContentAsFormattedList(boolean showAuxiliaryAttributes) {
try {
ObjectDelta<? extends ObjectType> summarizedDelta = getSummarizedFocusDeltas();
if (summarizedDelta.isAdd()) {
return getTextFormatter().formatObject(summarizedDelta.getObjectToAdd(), false, showAuxiliaryAttributes);
} else if (summarizedDelta.isModify()) {
ModelElementContext<?> focusContext = modelContext.getFocusContext();
return getTextFormatter().formatObjectModificationDelta(summarizedDelta, false, showAuxiliaryAttributes, focusContext.getObjectOld(),
focusContext.getObjectNew());
} else {
return "";
}
} catch (Throwable t) {
LoggingUtils.logUnexpectedException(LOGGER, "Unable to determine the focus change; focus context = {}", t, focusContext.debugDump());
return("(unable to determine the change because of schema exception: " + t.getMessage() + ")\n");
}
}
public String getFocusPassword() {
List<ObjectDelta<AssignmentHolderType>> focusDeltas = getFocusDeltas();
if (focusDeltas.isEmpty()) {
LOGGER.trace("getFocusPasswordFromEvent: No user deltas in event");
return null;
}
if (!hasFocusOfType(UserType.class)) {
LOGGER.trace("getFocusPasswordFromEvent: Not a UserType context");
return null;
}
//noinspection unchecked,rawtypes
String passwordFromDeltas = getPasswordFromDeltas((List) focusDeltas);
if (passwordFromDeltas != null) {
LOGGER.trace("getFocusPasswordFromEvent: Found password in user executed delta(s)");
return passwordFromDeltas;
}
//noinspection unchecked,rawtypes
ObjectDelta<UserType> focusPrimaryDelta = (ObjectDelta) getFocusPrimaryDelta();
//noinspection unchecked,rawtypes
ObjectDelta<UserType> focusSecondaryDelta = (ObjectDelta) getFocusSecondaryDelta();
if (focusPrimaryDelta == null && focusSecondaryDelta == null) {
LOGGER.trace("getFocusPasswordFromEvent: No password in executed delta(s) and no primary/secondary deltas");
return null;
}
if (focusPrimaryDelta != null) {
String password = getPasswordFromDeltas(singletonList(focusPrimaryDelta));
if (password != null) {
LOGGER.trace("getFocusPasswordFromEvent: Found password in user primary delta, continuing");
return password;
}
}
if (focusSecondaryDelta != null) {
String password = getPasswordFromDeltas(singletonList(focusSecondaryDelta));
if (password != null) {
LOGGER.trace("getFocusPasswordFromEvent: Found password in user secondary delta(s)");
return password;
}
}
LOGGER.trace("getFocusPasswordFromEvent: No password in executed delta(s) nor in primary/secondary deltas");
return null;
}
private String getPasswordFromDeltas(List<ObjectDelta<UserType>> deltas) {
try {
return getMidpointFunctions().getPlaintextUserPasswordFromDeltas(deltas);
} catch (EncryptionException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't decrypt password from user deltas: {}", e, DebugUtil.debugDump(deltas));
return null;
}
}
@Override
public String debugDump(int indent) {
StringBuilder sb = DebugUtil.createTitleStringBuilderLn(this.getClass(), indent);
debugDumpCommon(sb, indent);
DebugUtil.debugDumpWithLabelToString(sb, "modelContext", modelContext, indent + 1);
return sb.toString();
}
}