-
Notifications
You must be signed in to change notification settings - Fork 188
/
ItemWrapperImpl.java
754 lines (602 loc) · 20.3 KB
/
ItemWrapperImpl.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright (c) 2010-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.gui.impl.prism.wrapper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.xml.namespace.QName;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.prism.ItemStatus;
import com.evolveum.midpoint.gui.api.prism.wrapper.*;
import com.evolveum.midpoint.gui.api.util.ModelServiceLocator;
import com.evolveum.midpoint.gui.api.util.WebPrismUtil;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.MiscUtil;
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.web.component.data.column.ColumnUtils;
import com.evolveum.midpoint.web.component.prism.ItemVisibility;
import com.evolveum.midpoint.web.component.prism.ValueStatus;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
/**
* @author katka
*/
public abstract class ItemWrapperImpl<I extends Item, VW extends PrismValueWrapper> implements ItemWrapper<I, VW>, Serializable {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(ItemWrapperImpl.class);
private PrismContainerValueWrapper<?> parent;
private ItemStatus status;
private String displayName;
private List<VW> values = new ArrayList<>();
private I oldItem;
private I newItem;
private boolean column;
private boolean stripe;
private boolean showEmpty;
private boolean showInVirtualContainer;
//consider
private boolean readOnly;
private UserInterfaceElementVisibilityType visibleOverwrite;
public ItemWrapperImpl(PrismContainerValueWrapper<?> parent, I item, ItemStatus status) {
Validate.notNull(item, "Item must not be null.");
Validate.notNull(status, "Item status must not be null.");
this.parent = parent;
this.newItem = item;
this.oldItem = (I) item.clone();
this.status = status;
}
@Override
public <D extends ItemDelta<? extends PrismValue, ? extends ItemDefinition>> Collection<D> getDelta() throws SchemaException {
LOGGER.trace("Start computing delta for {}", newItem);
if (isOperational()) {
return null;
}
D delta;
if (parent != null && ValueStatus.ADDED == parent.getStatus()) {
delta = (D) createEmptyDelta(getItemName());
} else {
delta = (D) createEmptyDelta(getPath());
}
for (VW value : values) {
value.addToDelta((ItemDelta) delta);
}
if (delta.isEmpty()) {
LOGGER.trace("There is no delta for {}", newItem);
return null;
}
LOGGER.trace("Returning delta {}", delta);
return MiscUtil.createCollection(delta);
}
@Override
public String getDisplayName() {
if (displayName == null) {
displayName = getLocalizedDisplayName();
}
return displayName;
}
@Override
public String getHelp() {
return WebPrismUtil.getHelpText(getItemDefinition());
}
@Override
public boolean isExperimental() {
return getItemDefinition().isExperimental();
}
@Override
public String getDeprecatedSince() {
return getItemDefinition().getDeprecatedSince();
}
@Override
public boolean isDeprecated() {
return getItemDefinition().isDeprecated();
}
@Override
public boolean isMandatory() {
return getItemDefinition().isMandatory();
}
public ItemStatus getStatus() {
return status;
}
@Override
public I getItem() {
return newItem;
}
@Override
public void setColumn(boolean column) {
this.column = column;
}
@Override
public boolean isColumn() {
return column;
}
public PrismContainerValueWrapper<?> getParent() {
return parent;
}
@Override
public boolean isMultiValue() {
return getItemDefinition().isMultiValue();
}
@Override
public boolean isReadOnly() {
return readOnly;
}
@Override
public ItemPath getPath() {
return newItem.getPath();
}
@Override
public ExpressionType getFormComponentValidator() {
FormItemValidationType formItemValidation = getItemDefinition().getAnnotation(ItemRefinedDefinitionType.F_VALIDATION);
if (formItemValidation == null) {
return null;
}
List<FormItemServerValidationType> serverValidators = formItemValidation.getServer();
if (CollectionUtils.isNotEmpty(serverValidators)) {
return serverValidators.iterator().next().getExpression();
}
return null;
}
<ID extends ItemDefinition<I>> ID getItemDefinition() {
return (ID) newItem.getDefinition();
}
@Override
public String debugDump(int indent) {
StringBuilder sb = DebugUtil.createIndentedStringBuilder(indent);
sb.append(toString());
sb.append("Original definition: ").append(newItem.getDefinition()).append("\n");
sb.append("Display nam: ").append(displayName).append("\n");
sb.append("Item status: ").append(status).append("\n");
sb.append("Read only: ").append(isReadOnly()).append("\n");
sb.append("New item: \n").append(newItem).append("\n");
sb.append("Old item: \n").append(oldItem).append("\n");
sb.append("Values: \n");
for (VW value : values) {
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append(value.debugDump());
}
return sb.toString();
}
private String getLocalizedDisplayName() {
Validate.notNull(newItem, "Item must not be null.");
String displayName = newItem.getDisplayName();
if (!StringUtils.isEmpty(displayName)) {
return localizeName(displayName, displayName);
}
QName name = newItem.getElementName();
if (name != null) {
displayName = name.getLocalPart();
PrismContainerValue<?> val = newItem.getParent();
if (val != null && val.getDefinition() != null
&& val.getDefinition().isRuntimeSchema()) {
return localizeName(displayName, displayName);
}
if (val != null && val.getTypeName() != null) {
if (val.getRealClass() != null) {
displayName = val.getRealClass().getSimpleName() + "." + displayName;
} else {
displayName = val.getTypeName().getLocalPart() + "." + displayName;
}
}
} else {
displayName = newItem.getDefinition().getTypeName().getLocalPart();
}
return localizeName(displayName, name.getLocalPart());
}
private String localizeName(String nameKey, String defaultString) {
Validate.notNull(nameKey, "Null localization key");
return ColumnUtils.createStringResource(nameKey, defaultString).getString();
}
@Override
public ItemStatus findObjectStatus() {
if (parent == null) {
return status;
}
ItemWrapper parentWrapper = parent.getParent();
PrismObjectWrapper<?> objectWrapper = findObjectWrapper(parentWrapper);
if (objectWrapper == null) {
return status;
}
return objectWrapper.getStatus();
}
@Override
public <OW extends PrismObjectWrapper<O>, O extends ObjectType> OW findObjectWrapper() {
if (parent == null) {
return null;
}
ItemWrapper parentWrapper = parent.getParent();
return findObjectWrapper(parentWrapper);
}
private <OW extends PrismObjectWrapper<O>, O extends ObjectType> OW findObjectWrapper(ItemWrapper parent) {
if (parent != null) {
if (parent instanceof PrismObjectWrapper) {
return (OW) parent;
}
if (parent.getParent() != null) {
return findObjectWrapper(parent.getParent().getParent());
}
}
return null;
}
@Override
public List<VW> getValues() {
return values;
}
@Override
public VW getValue() throws SchemaException {
if (CollectionUtils.isEmpty(getValues())) {
return null;
}
if (isMultiValue()) {
throw new SchemaException("Attempt to get sngle value from multi-value property.");
}
return getValues().iterator().next();
}
@Override
public boolean checkRequired(PageBase pageBase) {
return newItem.getDefinition().isMandatory();
}
@Override
public boolean isShowEmpty() {
return showEmpty;
}
@Override
public void setShowEmpty(boolean isShowEmpty, boolean recursive) {
this.showEmpty = isShowEmpty;
}
@Override
public boolean isShowInVirtualContainer() {
return showInVirtualContainer;
}
@Override
public void setShowInVirtualContainer(boolean showInVirtualContainer) {
this.showInVirtualContainer = showInVirtualContainer;
}
@Override
public void setVisibleOverwrite(UserInterfaceElementVisibilityType visibleOverwrite) {
this.visibleOverwrite = visibleOverwrite;
}
public boolean isEmpty() {
return newItem.isEmpty();
}
ItemStatus getItemStatus() {
return status;
}
@NotNull
@Override
public ItemName getItemName() {
return getItemDefinition().getItemName();
}
@Override
public String getNamespace() {
return getItemDefinition().getNamespace();
}
@Override
public int getMinOccurs() {
return getItemDefinition().getMinOccurs();
}
@Override
public int getMaxOccurs() {
return getItemDefinition().getMaxOccurs();
}
@Override
public boolean isSingleValue() {
return getItemDefinition().isSingleValue();
}
@Override
public boolean isOptional() {
return getItemDefinition().isOptional();
}
@Override
public boolean isOperational() {
return getItemDefinition().isOperational();
}
@Override
public boolean isInherited() {
return getItemDefinition().isInherited();
}
@Override
public boolean isDynamic() {
return getItemDefinition().isDynamic();
}
@Override
public boolean canRead() {
return getItemDefinition().canRead();
}
@Override
public boolean canModify() {
return getItemDefinition().canModify();
}
@Override
public boolean canAdd() {
return getItemDefinition().canAdd();
}
@Override
public QName getSubstitutionHead() {
return getItemDefinition().getSubstitutionHead();
}
@Override
public boolean isHeterogeneousListItem() {
return getItemDefinition().isHeterogeneousListItem();
}
@Override
public PrismReferenceValue getValueEnumerationRef() {
return getItemDefinition().getValueEnumerationRef();
}
@Override
public boolean isValidFor(QName elementQName, Class<? extends ItemDefinition> clazz) {
return getItemDefinition().isValidFor(elementQName, clazz);
}
@Override
public boolean isValidFor(@NotNull QName elementQName, @NotNull Class<? extends ItemDefinition> clazz, boolean caseInsensitive) {
return getItemDefinition().isValidFor(elementQName, clazz, caseInsensitive);
}
@Override
public void adoptElementDefinitionFrom(ItemDefinition otherDef) {
getItemDefinition().adoptElementDefinitionFrom(otherDef);
}
@NotNull
@Override
public I instantiate() throws SchemaException {
return getItemDefinition().instantiate();
}
@NotNull
@Override
public I instantiate(QName name) throws SchemaException {
return getItemDefinition().instantiate();
}
@Override
public <T extends ItemDefinition> T findItemDefinition(@NotNull ItemPath path, @NotNull Class<T> clazz) {
return getItemDefinition().findItemDefinition(path, clazz);
}
@Override
public ItemDelta createEmptyDelta(ItemPath path) {
return getItemDefinition().createEmptyDelta(path);
}
@Override
public @NotNull ItemDefinition<I> clone() {
return getItemDefinition().clone();
}
@Override
public ItemDefinition<I> deepClone(boolean ultraDeep, Consumer<ItemDefinition> postCloneAction) {
return getItemDefinition().deepClone(ultraDeep, postCloneAction);
}
@Override
public ItemDefinition<I> deepClone(Map<QName, ComplexTypeDefinition> ctdMap, Map<QName, ComplexTypeDefinition> onThisPath,
Consumer<ItemDefinition> postCloneAction) {
return getItemDefinition().deepClone(ctdMap, onThisPath, postCloneAction);
}
@Override
public void revive(PrismContext prismContext) {
getItemDefinition().revive(prismContext);
}
@Override
public void debugDumpShortToString(StringBuilder sb) {
//TODO implement for wrappers
getItemDefinition().debugDumpShortToString(sb);
}
@Override
public boolean canBeDefinitionOf(I item) {
return getItemDefinition().canBeDefinitionOf(item);
}
@Override
public boolean canBeDefinitionOf(PrismValue pvalue) {
return getItemDefinition().canBeDefinitionOf(pvalue);
}
@Override
public MutableItemDefinition<I> toMutable() {
return getItemDefinition().toMutable();
}
@Override
public QName getTypeName() {
return getItemDefinition().getTypeName();
}
@Override
public boolean isRuntimeSchema() {
return getItemDefinition().isRuntimeSchema();
}
@Override
@Deprecated
public boolean isIgnored() {
return getItemDefinition().isIgnored();
}
@Override
public ItemProcessing getProcessing() {
return getItemDefinition().getProcessing();
}
@Override
public boolean isAbstract() {
return getItemDefinition().isAbstract();
}
@Override
public String getPlannedRemoval() {
return getItemDefinition().getPlannedRemoval();
}
@Override
public boolean isElaborate() {
return getItemDefinition().isElaborate();
}
@Override
public boolean isEmphasized() {
return getItemDefinition().isEmphasized();
}
@Override
public Integer getDisplayOrder() {
return getItemDefinition().getDisplayOrder();
}
@Override
public String getDocumentation() {
return getItemDefinition().getDocumentation();
}
@Override
public String getDocumentationPreview() {
return getItemDefinition().getDocumentationPreview();
}
@Override
public PrismContext getPrismContext() {
return getItemDefinition().getPrismContext();
}
@Override
public Class getTypeClassIfKnown() {
return getItemDefinition().getTypeClassIfKnown();
}
@Override
public Class getTypeClass() {
return getItemDefinition().getTypeClass();
}
@Override
public <A> A getAnnotation(QName qname) {
return getItemDefinition().getAnnotation(qname);
}
@Override
public <A> void setAnnotation(QName qname, A value) {
getItemDefinition().setAnnotation(qname, value);
}
@Override
public List<SchemaMigration> getSchemaMigrations() {
return getItemDefinition().getSchemaMigrations();
}
@Override
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
@Override
public boolean isStripe() {
return stripe;
}
@Override
public void setStripe(boolean stripe) {
this.stripe = stripe;
}
public I getOldItem() {
return oldItem;
}
@Override
public boolean isIndexOnly() {
return false; // todo
}
public UserInterfaceElementVisibilityType getVisibleOverwrite() {
return visibleOverwrite;
}
@Override
public boolean isVisible(PrismContainerValueWrapper<?> parent, ItemVisibilityHandler visibilityHandler) {
if (!isVisibleByVisibilityHandler(parent.isExpanded(), visibilityHandler)) {
return false;
}
if (!parent.isVirtual() && isShowInVirtualContainer()) {
return false;
}
ItemStatus objectStatus = findObjectStatus();
switch (objectStatus) {
case NOT_CHANGED:
return isVisibleForModify(parent.isShowEmpty());
case ADDED:
return isVisibleForAdd(parent.isShowEmpty());
case DELETED:
return false;
}
return false;
}
protected boolean isVisibleByVisibilityHandler(boolean parentExpanded, ItemVisibilityHandler visibilityHandler) {
if (!parentExpanded) {
return false;
}
if (visibilityHandler != null) {
ItemVisibility visible = visibilityHandler.isVisible(this);
if (visible != null) {
switch (visible) {
case HIDDEN:
return false;
default:
// automatic, go on ...
}
}
}
return true;
}
private boolean isVisibleForModify(boolean parentShowEmpty) {
if (parentShowEmpty) {
return true;
}
return isEmphasized() || !isEmpty();
}
private boolean isVisibleForAdd(boolean parentShowEmpty) {
if (parentShowEmpty) {
return true;
}
return isEmphasized() || !isEmpty();
}
@Override
public void remove(VW valueWrapper, ModelServiceLocator locator) throws SchemaException {
removeValue(valueWrapper);
int count = countUsableValues(values);
if (count == 0 && !hasEmptyPlaceholder(values)) {
add(createNewEmptyValue(locator), locator);
}
}
@Override
public void removeAll(ModelServiceLocator locator) throws SchemaException {
for (VW value : new ArrayList<>(values)) {
removeValue(value);
}
if (!hasEmptyPlaceholder(values)) {
add(createNewEmptyValue(locator), locator);
}
}
private void removeValue(VW valueWrapper) {
switch (valueWrapper.getStatus()) {
case ADDED:
values.remove(valueWrapper);
getItem().remove(valueWrapper.getOldValue());
getItem().remove(valueWrapper.getNewValue());
break;
case NOT_CHANGED:
getItem().remove(valueWrapper.getNewValue());
valueWrapper.setStatus(ValueStatus.DELETED);
break;
}
}
protected abstract <PV extends PrismValue> PV createNewEmptyValue(ModelServiceLocator locator);
@Override
public <PV extends PrismValue> void add(PV newValue, ModelServiceLocator locator) throws SchemaException {
getItem().add(newValue);
VW newContainerValue = WebPrismUtil.createNewValueWrapper(this, newValue, locator);
values.add(newContainerValue);
}
private int countUsableValues(List<VW> values) {
int count = 0;
for (VW value : values) {
if (ValueStatus.DELETED.equals(value.getStatus())) {
continue;
}
if (ValueStatus.ADDED.equals(value.getStatus())) {
continue;
}
count++;
}
return count;
}
private boolean hasEmptyPlaceholder(List<VW> values) {
for (VW value : values) {
if (ValueStatus.ADDED.equals(value.getStatus())) {//&& !value.hasValueChanged()) {
return true;
}
}
return false;
}
}