-
Notifications
You must be signed in to change notification settings - Fork 188
/
ObjectWrapperFactory.java
353 lines (289 loc) · 16.3 KB
/
ObjectWrapperFactory.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
352
353
/*
* 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.component.prism;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.commons.lang.Validate;
import com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition;
import com.evolveum.midpoint.gui.api.util.ModelServiceLocator;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.ModelInteractionService;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismReference;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.exception.SystemException;
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.ApprovalSchemaType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthorizationPhaseType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NonceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationExecutionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.PasswordHistoryEntryType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityQuestionsCredentialsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAssociationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SubjectedObjectSelectorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TriggerType;
/**
* @author Viliam Repan (lazyman)
*/
public class ObjectWrapperFactory {
private static final Trace LOGGER = TraceManager.getTrace(ObjectWrapperFactory.class);
private static final String DOT_CLASS = ObjectWrapperFactory.class.getName() + ".";
private static final String CREATE_CONTAINERS = DOT_CLASS + "createContainers";
private static final String CREATE_OBJECT_WRAPPER = DOT_CLASS + "createObjectWrapper";
private static final List<QName> CONTAINERS_TO_IGNORE = Arrays.asList(
SubjectedObjectSelectorType.COMPLEX_TYPE,
TriggerType.COMPLEX_TYPE,
OperationExecutionType.COMPLEX_TYPE,
ApprovalSchemaType.COMPLEX_TYPE,
PasswordHistoryEntryType.COMPLEX_TYPE,
SecurityQuestionsCredentialsType.COMPLEX_TYPE,
NonceType.COMPLEX_TYPE);
private ModelServiceLocator modelServiceLocator;
private OperationResult result;
public ObjectWrapperFactory(ModelServiceLocator modelServiceLocator) {
Validate.notNull(modelServiceLocator, "Service locator must not be null");
this.modelServiceLocator = modelServiceLocator;
}
public OperationResult getResult() {
return result;
}
public <O extends ObjectType> ObjectWrapper<O> createObjectWrapper(String displayName,
String description, PrismObject<O> object, ContainerStatus status, Task task) {
return createObjectWrapper(displayName, description, object, status, AuthorizationPhaseType.REQUEST, false, task);
}
/**
*
* @param displayName
* @param description
* @param object
* @param status
* @param entirelyReadonly every child item wrapper will be set to this value
* @param task
* @param <O>
* @return
*/
public <O extends ObjectType> ObjectWrapper<O> createObjectWrapper(String displayName,
String description, PrismObject<O> object, ContainerStatus status, boolean entirelyReadonly, Task task) {
return createObjectWrapper(displayName, description, object, status, AuthorizationPhaseType.REQUEST, entirelyReadonly, task);
}
public <O extends ObjectType> ObjectWrapper<O> createObjectWrapper(String displayName, String description,
PrismObject<O> object, ContainerStatus status, AuthorizationPhaseType authorizationPhase, Task task) {
return createObjectWrapper(displayName, description, object, status, authorizationPhase, false, task);
}
public <O extends ObjectType> ObjectWrapper<O> createObjectWrapper(String displayName, String description,
PrismObject<O> object, ContainerStatus status, AuthorizationPhaseType authorizationPhase, boolean entirelyReadonly, Task task) {
if (authorizationPhase == null) {
authorizationPhase = AuthorizationPhaseType.REQUEST;
}
try {
OperationResult result = task.getResult();
PrismObjectDefinition<O> objectDefinitionForEditing = modelServiceLocator.getModelInteractionService()
.getEditObjectDefinition(object, authorizationPhase, task, result);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Edit definition for {}:\n{}", object, objectDefinitionForEditing.debugDump(1));
}
if (objectDefinitionForEditing != null) {
object.setDefinition(objectDefinitionForEditing);
}
RefinedObjectClassDefinition objectClassDefinitionForEditing = null;
if (isShadow(object)) {
PrismReference resourceRef = object.findReference(ShadowType.F_RESOURCE_REF);
PrismObject<ResourceType> resource = resourceRef.getValue().getObject();
Validate.notNull(resource, "No resource object in the resourceRef");
objectClassDefinitionForEditing = modelServiceLocator.getModelInteractionService().getEditObjectClassDefinition(
(PrismObject<ShadowType>) object, resource, authorizationPhase, task, result);
if (objectClassDefinitionForEditing != null) {
object.findOrCreateContainer(ShadowType.F_ATTRIBUTES).applyDefinition((PrismContainerDefinition) objectClassDefinitionForEditing.toResourceAttributeContainerDefinition());;
}
}
return createObjectWrapper(displayName, description, object, objectDefinitionForEditing,
objectClassDefinitionForEditing, status, entirelyReadonly, task, result);
} catch (SchemaException | ConfigurationException | ObjectNotFoundException | ExpressionEvaluationException | CommunicationException | SecurityViolationException ex) {
throw new SystemException(ex);
}
}
public <O extends ObjectType> ObjectWrapper<O> createObjectWrapper(String displayName,
String description, PrismObject<O> object, PrismObjectDefinition<O> objectDefinitionForEditing,
RefinedObjectClassDefinition objectClassDefinitionForEditing, ContainerStatus status, Task task) throws SchemaException {
return createObjectWrapper(displayName, description, object, objectDefinitionForEditing,
objectClassDefinitionForEditing, status, false, task, null);
}
private <O extends ObjectType> ObjectWrapper<O> createObjectWrapper(String displayName,
String description, PrismObject<O> object, PrismObjectDefinition<O> objectDefinitionForEditing,
RefinedObjectClassDefinition objectClassDefinitionForEditing, ContainerStatus status, boolean entirelyReadonly,
Task task, OperationResult result) throws SchemaException {
if (result == null) {
this.result = new OperationResult(CREATE_OBJECT_WRAPPER);
} else {
this.result = result;
}
ObjectWrapper<O> objectWrapper = new ObjectWrapper<>(displayName, description, object,
status);
List<ContainerWrapper<? extends Containerable>> containerWrappers = createContainerWrappers(objectWrapper, object,
objectDefinitionForEditing, status, task, this.result);
objectWrapper.setContainers(containerWrappers);
if (entirelyReadonly) {
setObjectWrapperValuesReadOnlyState(objectWrapper, entirelyReadonly);
}
this.result.computeStatusIfUnknown();
objectWrapper.setResult(this.result);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Created object wrapper:\n{}", objectWrapper.debugDump());
}
return objectWrapper;
}
private <O extends ObjectType> List<ContainerWrapper<? extends Containerable>> createContainerWrappers(ObjectWrapper<O> oWrapper,
PrismObject<O> object, PrismObjectDefinition<O> objectDefinitionForEditing, ContainerStatus cStatus, Task task, OperationResult pResult) throws SchemaException {
OperationResult result = pResult.createSubresult(CREATE_CONTAINERS);
List<ContainerWrapper<? extends Containerable>> containerWrappers = new ArrayList<>();
ContainerWrapperFactory cwf = new ContainerWrapperFactory(modelServiceLocator);
try {
ContainerWrapper<O> mainContainerWrapper = cwf.createContainerWrapper(object, oWrapper.getStatus(), cStatus, ItemPath.EMPTY_PATH, task);
mainContainerWrapper.setDisplayName("prismContainer.mainPanelDisplayName");
result.addSubresult(cwf.getResult());
containerWrappers.add(mainContainerWrapper);
addContainerWrappers(containerWrappers, oWrapper, object, null, task, result);
} catch (SchemaException | RuntimeException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Error occurred during container wrapping" + e.getMessage(), e);
result.recordFatalError("Error occurred during container wrapping, reason: " + e.getMessage(), e);
throw e;
}
containerWrappers.sort(new ItemWrapperComparator());
result.recomputeStatus();
result.recordSuccessIfUnknown();
return containerWrappers;
}
private <O extends ObjectType, C extends Containerable> void addContainerWrappers(
List<ContainerWrapper<? extends Containerable>> containerWrappers, ObjectWrapper<O> oWrapper,
PrismContainer<C> parentContainer, ItemPath path, Task task, OperationResult result) throws SchemaException {
PrismContainerDefinition<C> parentContainerDefinition = parentContainer.getDefinition();
List<ItemPathSegment> segments = new ArrayList<>();
if (path != null) {
segments.addAll(path.getSegments());
}
ItemPath parentPath = new ItemPath(segments);
for (ItemDefinition def : (Collection<ItemDefinition>) parentContainerDefinition.getDefinitions()) {
if (!(def instanceof PrismContainerDefinition)) {
continue;
}
if (isIgnoreContainer(def.getTypeName())) {
continue;
}
if (def.isExperimental() && !WebModelServiceUtils.isEnableExperimentalFeature(task, modelServiceLocator)) {
LOGGER.trace("Skipping creating wrapper for container {} because it is experimental a experimental features are not enabled.", def.getName());
continue;
}
LOGGER.trace("ObjectWrapper.createContainerWrapper processing definition: {}", def);
PrismContainerDefinition<C> containerDef = (PrismContainerDefinition) def;
ItemPath newPath = createPropertyPath(parentPath, containerDef.getName());
ContainerWrapperFactory cwf = new ContainerWrapperFactory(modelServiceLocator);
PrismContainer<C> prismContainer = parentContainer.findContainer(def.getName());
ContainerWrapper<C> container = createContainerWrapper(oWrapper.getObject(), oWrapper.getStatus(), prismContainer, containerDef, cwf, newPath, task);
result.addSubresult(cwf.getResult());
if (container != null) {
containerWrappers.add(container);
}
}
}
private <O extends ObjectType, C extends Containerable> ContainerWrapper<C> createContainerWrapper(PrismObject<O> object, ContainerStatus objectStatus, PrismContainer<C> prismContainer,
PrismContainerDefinition<C> containerDef, ContainerWrapperFactory cwf, ItemPath newPath, Task task) throws SchemaException{
if (ShadowAssociationType.COMPLEX_TYPE.equals(containerDef.getTypeName())) {
ObjectType objectType = object.asObjectable();
ShadowType shadow;
if (objectType instanceof ShadowType) {
shadow = (ShadowType) objectType;
} else {
throw new SchemaException("Something very strange happenned. Association contianer in the " + objectType.getClass().getSimpleName() + "?");
}
// Task task = modelServiceLocator.createSimpleTask("Load resource ref");
//TODO: is it safe to case modelServiceLocator to pageBase?
PrismObject<ResourceType> resource = WebModelServiceUtils.loadObject(shadow.getResourceRef(), modelServiceLocator, task, result);
result.computeStatusIfUnknown();
if (!result.isAcceptable()) {
LOGGER.error("Cannot find resource referenced from shadow. {}", result.getMessage());
result.recordPartialError("Could not find resource referenced from shadow.");
return null;
}
if (prismContainer != null) {
return (ContainerWrapper<C>) cwf.createAssociationWrapper(resource, shadow.getKind(), shadow.getIntent(), (PrismContainer<ShadowAssociationType>) prismContainer, objectStatus, ContainerStatus.MODIFYING, newPath);
}
prismContainer = containerDef.instantiate();
return (ContainerWrapper<C>) cwf.createAssociationWrapper(resource, shadow.getKind(), shadow.getIntent(), (PrismContainer<ShadowAssociationType>) prismContainer, objectStatus, ContainerStatus.ADDING, newPath);
}
if (prismContainer != null) {
return cwf.createContainerWrapper(prismContainer, objectStatus, ContainerStatus.MODIFYING, newPath, task);
}
prismContainer = containerDef.instantiate();
return cwf.createContainerWrapper(prismContainer, objectStatus, ContainerStatus.ADDING, newPath, task);
}
private boolean isIgnoreContainer(QName containerDefinitionName) {
for (QName container : CONTAINERS_TO_IGNORE) {
if (container.equals(containerDefinitionName)){
return true;
}
}
return false;
}
private ItemPath createPropertyPath(ItemPath path, QName element) {
return path.append(element);
}
private boolean isShadow(PrismObject object) {
return (object.getCompileTimeClass() != null && ShadowType.class.isAssignableFrom(object
.getCompileTimeClass()))
|| (object.getDefinition() != null && object.getDefinition().getName()
.equals(ShadowType.COMPLEX_TYPE));
}
private <O extends ObjectType> void setObjectWrapperValuesReadOnlyState(ObjectWrapper<O> objectWrapper, boolean entirelyReadonly) {
for (ContainerWrapper<? extends Containerable> container : objectWrapper.getContainers()) {
setContainerValuesReadOnlyState(container, entirelyReadonly);
}
}
private void setContainerValuesReadOnlyState(ContainerWrapper<? extends Containerable> container, boolean entirelyReadonly) {
container.setReadonly(entirelyReadonly);
container.getValues().forEach(v -> {
v.getItems().forEach(item -> {
if (item instanceof ContainerWrapper) {
setContainerValuesReadOnlyState((ContainerWrapper) item, entirelyReadonly);
} else if (item instanceof PropertyOrReferenceWrapper) {
((PropertyOrReferenceWrapper) item).setReadonly(entirelyReadonly);
}
});
((ContainerValueWrapper) v).setReadonly(entirelyReadonly);
});
}
}