/
ObjectWrapper.java
626 lines (517 loc) · 18.7 KB
/
ObjectWrapper.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (c) 2010-2018 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.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.ConsistencyCheckScope;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.OriginType;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.Revivable;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.internals.InternalsConfig;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.util.DebugDumpable;
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.resource.capabilities_3.CapabilityType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
/**
* @author lazyman
* @author katkav
*/
public class ObjectWrapper<O extends ObjectType> extends PrismWrapper implements Serializable, Revivable, DebugDumpable {
private static final long serialVersionUID = 1L;
public static final String F_DISPLAY_NAME = "displayName";
public static final String F_SELECTED = "selected";
public static final String F_OBJECT = "object";
private static final Trace LOGGER = TraceManager.getTrace(ObjectWrapper.class);
public static final String PROPERTY_CONTAINERS = "containers";
private PrismObject<O> object;
private PrismObject<O> objectOld;
private ObjectDelta<O> oldDelta;
private ContainerStatus status;
private HeaderStatus headerStatus;
private String displayName;
private String description;
private List<ContainerWrapper<? extends Containerable>> containers;
private boolean selectable;
private boolean selected;
private boolean showAssignments = false;
// whether to show name and description properties and metadata container
private boolean showInheritedObjectAttributes = true;
// readonly flag is an override. false means "do not override"
private boolean readonly = false;
// whether to make wicket enforce that required fields are filled-in (if set
// to false, this check has to be done explicitly)
private boolean enforceRequiredFields = true;
private Collection<SelectorOptions<GetOperationOptions>> loadOptions;
private OperationResult result;
private Collection<PrismObject<OrgType>> parentOrgs = new ArrayList<>();
private OperationResult fetchResult;
// a "static" (non-refined) definition that reflects editability of the
// object in terms of midPoint schema limitations and security
// private PrismContainerDefinition objectDefinitionForEditing;
// a refined definition of an resource object class that reflects its
// editability; applicable for shadows only
private RefinedObjectClassDefinition objectClassDefinitionForEditing;
public ObjectWrapper(String dispayName, String descritpion, PrismObject<O> object, ContainerStatus status) {
Validate.notNull(object, "Object must not be null.");
Validate.notNull(status, "Container status must not be null.");
this.displayName = displayName;
this.description = description;
this.object = object;
this.objectOld = object.clone();
this.status = status;
}
public void revive(PrismContext prismContext) throws SchemaException {
if (object != null) {
object.revive(prismContext);
}
if (oldDelta != null) {
oldDelta.revive(prismContext);
}
if (containers != null) {
for (ContainerWrapper containerWrapper : containers) {
containerWrapper.revive(prismContext);
}
}
}
public Collection<PrismObject<OrgType>> getParentOrgs() {
return parentOrgs;
}
public OperationResult getFetchResult() {
return fetchResult;
}
public void setFetchResult(OperationResult fetchResult) {
this.fetchResult = fetchResult;
}
void setResult(OperationResult result) {
this.result = result;
}
public OperationResult getResult() {
return result;
}
public void clearResult() {
result = null;
}
public Collection<SelectorOptions<GetOperationOptions>> getLoadOptions() {
return loadOptions;
}
public void setLoadOptions(Collection<SelectorOptions<GetOperationOptions>> loadOptions) {
this.loadOptions = loadOptions;
}
public HeaderStatus getHeaderStatus() {
if (headerStatus == null) {
headerStatus = HeaderStatus.NORMAL;
}
return headerStatus;
}
public ObjectDelta<O> getOldDelta() {
return oldDelta;
}
public void setOldDelta(ObjectDelta<O> oldDelta) {
this.oldDelta = oldDelta;
}
public void setHeaderStatus(HeaderStatus headerStatus) {
this.headerStatus = headerStatus;
}
public PrismObject<O> getObject() {
return object;
}
public PrismObject<O> getObjectOld() {
return objectOld;
}
public String getDisplayName() {
if (displayName == null) {
return WebComponentUtil.getDisplayNameOrName(object);
}
return displayName;
}
public ContainerStatus getStatus() {
return status;
}
public String getDescription() {
return description;
}
public boolean isSelectable() {
return selectable;
}
public void setSelectable(boolean selectable) {
this.selectable = selectable;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public <C extends Containerable> List<ContainerWrapper<? extends Containerable>> getContainers() {
if (containers == null) {
containers = new ArrayList<>();
}
return containers;
}
public void setContainers(List<ContainerWrapper<? extends Containerable>> containers) {
this.containers = containers;
}
public <C extends Containerable> ContainerWrapper<C> findContainerWrapper(ItemPath path) {
if (path == null || path.isEmpty()) {
return (ContainerWrapper<C>) findMainContainerWrapper();
}
if (path.size() == 1) {
return (ContainerWrapper<C>) getContainers().stream().filter(wrapper -> path.equivalent(wrapper.getPath()))
.findFirst().orElse(null);
}
ContainerWrapper<C> containerWrapper = findContainerWrapper(path.head());
if (containerWrapper == null) {
return null;
}
return containerWrapper.findContainerWrapper(path);
}
public <C extends Containerable> ContainerValueWrapper<C> findContainerValueWrapper(ItemPath path) {
if (path == null || path.isEmpty()) {
ContainerWrapper<C> mainContainer = (ContainerWrapper<C>) findMainContainerWrapper();
if (mainContainer == null) {
return null;
}
return mainContainer.getValues().iterator().next();
}
ContainerWrapper<C> containerWrapper = findContainerWrapper(path.head());
if (containerWrapper == null) {
return null;
}
return containerWrapper.findContainerValueWrapper(path);
}
public ContainerWrapper<O> findMainContainerWrapper() {
for (ContainerWrapper wrapper : getContainers()) {
if (wrapper.isMain()) {
return wrapper;
}
}
return null;
}
public <IW extends ItemWrapper> IW findPropertyWrapper(ItemPath path) {
ContainerWrapper containerWrapper;
ItemPath propertyPath;
if (path.size() == 1) {
containerWrapper = findMainContainerWrapper();
propertyPath = path;
} else {
containerWrapper = findContainerWrapper(path.head());
propertyPath = path.tail();
}
if (containerWrapper == null) {
return null;
}
return (IW) containerWrapper.findPropertyWrapper(ItemPath.getFirstName(propertyPath));
}
public void normalize() throws SchemaException {
ObjectDelta delta = getObjectDelta();
if (ChangeType.ADD.equals(delta.getChangeType())) {
object = delta.getObjectToAdd();
} else {
delta.applyTo(object);
}
}
public void sort() {
getContainers().forEach(ContainerWrapper -> ContainerWrapper.sort());
// ContainerWrapper main = findMainContainerWrapper();
// if (main != null) {
// main.sort(pageBase);
// }
computeStripes();
}
@Override
public void computeStripes() {
getContainers().forEach(c -> c.computeStripes());
}
public ObjectDelta<O> getObjectDelta() throws SchemaException {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Wrapper before creating delta:\n{}", this.debugDump());
}
if (ContainerStatus.ADDING.equals(getStatus())) {
return createAddingObjectDelta();
}
ObjectDelta<O> delta = new ObjectDelta<O>(object.getCompileTimeClass(), ChangeType.MODIFY, object.getPrismContext());
delta.setOid(object.getOid());
List<ContainerWrapper<? extends Containerable>> containers = getContainers();
// sort containers by path size
Collections.sort(containers, new PathSizeComparator());
for (ContainerWrapper containerWrapper : getContainers()) {
containerWrapper.collectModifications(delta);
// containerWrapper.collectDeleteDelta(delta, object.getPrismContext());
// containerWrapper.collectAddDelta(delta, object.getPrismContext());
}
// returning container to previous order
Collections.sort(containers, new ItemWrapperComparator());
if (object.getPrismContext() != null) {
// Make sure we have all the definitions
object.getPrismContext().adopt(delta);
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Creating delta from wrapper {}: existing object, creating delta:\n{}", this, delta.debugDump());
}
return delta;
}
// TODO move to appropriate place!
public static PrismValue clone(PrismValue value) {
if (value == null) {
return null;
}
PrismValue cloned = value.clone();
cloned.setOriginType(OriginType.USER_ACTION);
if (value instanceof PrismPropertyValue) {
PrismPropertyValue ppValue = (PrismPropertyValue) value;
if (ppValue.getValue() instanceof ProtectedStringType) {
((PrismPropertyValue) cloned).setValue(((ProtectedStringType) ppValue.getValue()).clone());
}
if (ppValue.getValue() instanceof PolyString) {
PolyString poly = (PolyString) ppValue.getValue();
if (StringUtils.isEmpty(poly.getOrig())) {
return null;
}
((PrismPropertyValue) cloned).setValue(new PolyString(poly.getOrig(), poly.getNorm()));
}
} else if (value instanceof PrismReferenceValue) {
if (cloned == null) {
return null;
}
if (cloned.isEmpty()) {
return null;
}
}
return cloned;
}
protected boolean hasResourceCapability(ResourceType resource, Class<? extends CapabilityType> capabilityClass) {
if (resource == null) {
return false;
}
return ResourceTypeUtil.hasEffectiveCapability(resource, capabilityClass);
}
private ObjectDelta createAddingObjectDelta() throws SchemaException {
PrismObject object = this.object.clone();
List<ContainerWrapper<? extends Containerable>> containers = getContainers();
// sort containers by path size
Collections.sort(containers, new PathSizeComparator());
for (ContainerWrapper containerWrapper : getContainers()) {
if (!containerWrapper.hasChanged()) {
continue;
}
PrismContainer containerToAdd = containerWrapper.createContainerAddDelta();
if (containerWrapper.isMain()) {
object = (PrismObject) containerToAdd;
continue;
}
object.getValue().addReplaceExisting(containerToAdd);
}
// cleanup empty containers
cleanupEmptyContainers(object);
ObjectDelta delta = ObjectDelta.createAddDelta(object);
// returning container to previous order
Collections.sort(containers, new ItemWrapperComparator());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Creating delta from wrapper {}: adding object, creating complete ADD delta:\n{}", this,
delta.debugDump());
}
if (InternalsConfig.consistencyChecks) {
delta.checkConsistence(true, true, true, ConsistencyCheckScope.THOROUGH);
}
return delta;
}
private void cleanupEmptyContainers(PrismContainer container) {
List<PrismContainerValue> values = container.getValues();
List<PrismContainerValue> valuesToBeRemoved = new ArrayList<PrismContainerValue>();
for (PrismContainerValue value : values) {
List<? extends Item> items = value.getItems();
if (items != null) {
Iterator<? extends Item> iterator = items.iterator();
while (iterator.hasNext()) {
Item item = iterator.next();
if (item instanceof PrismContainer) {
cleanupEmptyContainers((PrismContainer) item);
if (item.isEmpty()) {
iterator.remove();
}
}
}
}
if (items == null || value.isEmpty()) {
valuesToBeRemoved.add(value);
}
}
container.removeAll(valuesToBeRemoved);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ObjectWrapper(");
if (displayName == null) {
if (object == null) {
builder.append("null");
} else {
builder.append(object.getElementName());
}
} else {
builder.append(displayName);
}
builder.append(" (");
builder.append(status);
builder.append(") ");
builder.append(getContainers() == null ? null : getContainers().size());
builder.append(" containers)");
return builder.toString();
}
public boolean isProtectedAccount() {
if (object == null || !(ShadowType.class.isAssignableFrom(object.getCompileTimeClass()))) {
return false;
}
PrismProperty<Boolean> protectedObject = object.findProperty(ShadowType.F_PROTECTED_OBJECT);
if (protectedObject == null) {
return false;
}
return protectedObject.getRealValue() != null ? protectedObject.getRealValue() : false;
}
private static class PathSizeComparator implements Comparator<ContainerWrapper> {
@Override
public int compare(ContainerWrapper c1, ContainerWrapper c2) {
int size1 = c1.getPath() != null ? c1.getPath().size() : 0;
int size2 = c2.getPath() != null ? c2.getPath().size() : 0;
return size1 - size2;
}
}
public String getOid() {
return object.getOid();
}
public boolean isShowAssignments() {
return showAssignments;
}
public void setShowAssignments(boolean showAssignments) {
this.showAssignments = showAssignments;
}
@Override
public void setShowEmpty(boolean showEmpty, boolean recursive) {
super.setShowEmpty(showEmpty, recursive);
}
public void setShowEmpty(boolean showEmpty) {
super.setShowEmpty(showEmpty, false);
getContainers().forEach(container -> container.setShowEmpty(showEmpty, true));
}
public boolean isReadonly() {
if (isProtectedAccount()) {
return true;
}
return readonly;
}
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}
public boolean isShowInheritedObjectAttributes() {
return showInheritedObjectAttributes;
}
public void setShowInheritedObjectAttributes(boolean showInheritedObjectAttributes) {
this.showInheritedObjectAttributes = showInheritedObjectAttributes;
}
public boolean isEnforceRequiredFields() {
return enforceRequiredFields;
}
public void setEnforceRequiredFields(boolean enforceRequiredFields) {
this.enforceRequiredFields = enforceRequiredFields;
}
public PrismContainerDefinition getDefinition() {
return object.getDefinition();
}
public PrismContainerDefinition getRefinedAttributeDefinition() {
if (objectClassDefinitionForEditing != null) {
return objectClassDefinitionForEditing.toResourceAttributeContainerDefinition();
}
return null;
}
public void copyRuntimeStateTo(ObjectWrapper<O> newWrapper) {
newWrapper.setMinimalized(this.isMinimalized());
// newWrapper.setShowEmpty(this.isShowEmpty());
newWrapper.setSorted(this.isSorted());
newWrapper.setSelectable(this.isSelectable());
newWrapper.setSelected(this.isSelected());
newWrapper.setShowAssignments(this.isShowAssignments());
newWrapper.setShowInheritedObjectAttributes(this.isShowInheritedObjectAttributes());
newWrapper.setReadonly(this.isReadonly());
}
/**
* To be used when enforceRequiredFields is false, so we want to check them
* only when really needed (e.g. MID-3876: when rejecting a work item,
* fields marked as required need not be present).
*
* Currently unused; we use DynamicFormPanel.checkRequiredFields instead. Might be
* useful in the future.
*
* @return true if everything is OK
*/
@SuppressWarnings("unused")
public boolean checkRequiredFields(PageBase pageBase) {
boolean rv = true;
for (ContainerWrapper<? extends Containerable> container : containers) {
if (!container.checkRequired(pageBase)) {
rv = false; // continuing to display messages for all missing fields
}
}
return rv;
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("ObjectWrapper:\n");
DebugUtil.debugDumpWithLabelLn(sb, "displayName", displayName, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "description", description, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "object", object == null ? null : object.toString(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "objectOld", objectOld == null ? null : objectOld.toString(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "oldDelta", oldDelta, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "status", status == null ? null : status.toString(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "headerStatus", headerStatus == null ? null : headerStatus.toString(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "loadOptions", loadOptions, indent + 1);
DebugUtil.debugDumpWithLabel(sb, "containers", containers, indent + 1);
return sb.toString();
}
}