-
Notifications
You must be signed in to change notification settings - Fork 188
/
ModelEvent.java
225 lines (192 loc) · 9.05 KB
/
ModelEvent.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
/*
* Copyright (c) 2010-2017 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.api.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.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContext;
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.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 javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author mederly
*/
public class ModelEvent extends BaseEvent {
private static final Trace LOGGER = TraceManager.getTrace(ModelEvent.class);
// we can expect that modelContext != null and focus context != null as well
private ModelContext<?> modelContext;
public ModelEvent(LightweightIdentifierGenerator lightweightIdentifierGenerator, ModelContext modelContext) {
super(lightweightIdentifierGenerator);
this.modelContext = modelContext;
}
public ModelContext getModelContext() {
return modelContext;
}
public ModelElementContext<?> getFocusContext() {
return modelContext.getFocusContext();
}
public Collection<? extends ModelProjectionContext> getProjectionContexts() {
return modelContext.getProjectionContexts();
}
public List<? extends ObjectDeltaOperation> getFocusExecutedDeltas() {
return getFocusContext().getExecutedDeltas();
}
public List<ObjectDeltaOperation> getAllExecutedDeltas() {
List<ObjectDeltaOperation> retval = new ArrayList<>();
retval.addAll(getFocusContext().getExecutedDeltas());
for (Object o : modelContext.getProjectionContexts()) {
ModelProjectionContext modelProjectionContext = (ModelProjectionContext) o;
retval.addAll(modelProjectionContext.getExecutedDeltas());
}
return retval;
}
@Override
public boolean isStatusType(EventStatusType eventStatusType) {
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: anySuccess = true; allFailure = false; break;
case FATAL_ERROR: allSuccess = false; anyFailure = true; break;
case WARNING: anySuccess = true; allFailure = false; break;
case HANDLED_ERROR: anySuccess = true; allFailure = false; break;
case IN_PROGRESS: allSuccess = false; allFailure = false; anyInProgress = true; break;
case NOT_APPLICABLE: break;
case PARTIAL_ERROR: allSuccess = false; anyFailure = true; 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 (eventStatusType) {
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: " + eventStatusType);
}
}
// a bit of hack but ...
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 eventOperationType) {
// 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 eventOperationType == EventOperationType.ADD;
} else if (objectDeltaOperation.getObjectDelta().isDelete()) {
return eventOperationType == EventOperationType.DELETE;
}
}
return eventOperationType == EventOperationType.MODIFY;
}
@Override
public boolean isCategoryType(EventCategoryType eventCategoryType) {
return eventCategoryType == EventCategoryType.MODEL_EVENT;
}
public ObjectDelta<?> getFocusPrimaryDelta() {
return getFocusContext() != null ? getFocusContext().getPrimaryDelta() : null;
}
public ObjectDelta<?> getFocusSecondaryDelta() {
return getFocusContext() != null ? getFocusContext().getSecondaryDelta() : null;
}
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;
}
public ObjectDelta<? extends AssignmentHolderType> getSummarizedFocusDeltas() throws SchemaException {
return ObjectDeltaCollectionsUtil.summarize(getFocusDeltas());
}
public boolean hasFocusOfType(Class<? extends AssignmentHolderType> clazz) {
return getFocusContext() != null && clazz.isAssignableFrom(getFocusContext().getObjectTypeClass());
}
public boolean hasFocusOfType(QName focusType) {
PrismContext prismContext = getModelContext().getPrismContext();
if (prismContext == null) {
throw new IllegalStateException("No prismContext in model context");
}
PrismContainerDefinition pcd = prismContext.getSchemaRegistry().findContainerDefinitionByType(focusType);
if (pcd == null) {
LOGGER.warn("Couldn't find definition for type " + focusType);
return false;
}
Class expectedClass = pcd.getCompileTimeClass();
if (expectedClass == null) {
LOGGER.warn("Couldn't find class for type " + focusType);
return false;
}
return hasFocusOfType(expectedClass);
}
@Override
public boolean isRelatedToItem(ItemPath itemPath) {
return containsItem(getFocusDeltas(), itemPath);
}
@Override
public boolean isUserRelated() {
return hasFocusOfType(UserType.class);
}
public String getFocusTypeName() {
if (getFocusContext() == null || getFocusContext().getObjectTypeClass() == null) {
return null;
}
String simpleName = getFocusContext().getObjectTypeClass().getSimpleName();
return StringUtils.substringBeforeLast(simpleName, "Type"); // should usually work ;)
}
public String getContentAsFormattedList() {
return getContentAsFormattedList(false, false);
}
public String getContentAsFormattedList(boolean showSynchronizationItems, boolean showAuxiliaryAttributes) {
return getNotificationFunctions().getContentAsFormattedList(this, showSynchronizationItems, showAuxiliaryAttributes);
}
public String getFocusPassword() {
return getNotificationFunctions().getFocusPasswordFromEvent(this);
}
@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();
}
}