-
Notifications
You must be signed in to change notification settings - Fork 188
/
ItemWrapperFactoryImpl.java
269 lines (207 loc) · 10.2 KB
/
ItemWrapperFactoryImpl.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
/*
* Copyright (c) 2010-2018 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.factory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.evolveum.midpoint.gui.api.prism.ItemStatus;
import com.evolveum.midpoint.gui.api.prism.ItemWrapper;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.gui.impl.prism.PrismContainerValueWrapper;
import com.evolveum.midpoint.gui.impl.prism.PrismValueWrapper;
import com.evolveum.midpoint.gui.impl.registry.GuiComponentRegistryImpl;
import com.evolveum.midpoint.model.api.ModelInteractionService;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.QNameUtil;
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.prism.ValueStatus;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.VirtualContainerItemSpecificationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.VirtualContainersSpecificationType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
/**
* @author katka
*
*/
public abstract class ItemWrapperFactoryImpl<IW extends ItemWrapper, PV extends PrismValue, I extends Item, VW extends PrismValueWrapper> implements ItemWrapperFactory<IW, VW, PV> {
private static final Trace LOGGER = TraceManager.getTrace(ItemWrapperFactoryImpl.class);
@Autowired private GuiComponentRegistryImpl registry;
@Autowired private PrismContext prismContext;
@Autowired private ModelInteractionService modelInteractionService;
@Override
public IW createWrapper(PrismContainerValueWrapper<?> parent, ItemDefinition<?> def, WrapperContext context) throws SchemaException {
ItemName name = def.getItemName();
I childItem = null;
ItemStatus status = null;
if (CollectionUtils.isNotEmpty(context.getVirtualItemSpecification())) {
childItem = (I) def.instantiate();
status = ItemStatus.NOT_CHANGED;
} else {
childItem = (I) parent.getNewValue().findItem(name);
status = getStatus(childItem);
}
if (!skipCreateWrapper(def, status, context, childItem == null || CollectionUtils.isEmpty(childItem.getValues()))) {
LOGGER.trace("Skipping creating wrapper for non-existent item. It is not supported for {}", def);
return null;
}
if (childItem == null) {
childItem = (I) parent.getNewValue().findOrCreateItem(name);
childItem.setDefinition(def);
}
return createWrapper(parent, childItem, status, context);
}
private ItemStatus getStatus(I childItem) {
if (childItem == null) {
return ItemStatus.ADDED;
}
return ItemStatus.NOT_CHANGED;
}
public IW createWrapper(Item childItem, ItemStatus status, WrapperContext context) throws SchemaException {
return createWrapper(null, (I) childItem, status, context);
}
private IW createWrapper(PrismContainerValueWrapper<?> parent, I childItem, ItemStatus status, WrapperContext context) throws SchemaException {
IW itemWrapper = createWrapper(parent, childItem, status);
List<VW> valueWrappers = createValuesWrapper(itemWrapper, (I) childItem, context);
itemWrapper.getValues().addAll((Collection) valueWrappers);
itemWrapper.setShowEmpty(context.isShowEmpty(), false);
boolean readOnly = determineReadOnly(itemWrapper, context);
itemWrapper.setReadOnly(readOnly);
boolean showInVirtualContainer = determineShowInVirtualContainer(itemWrapper, context);
itemWrapper.setShowInVirtualContainer(showInVirtualContainer);
setupWrapper(itemWrapper);
return itemWrapper;
}
protected abstract void setupWrapper(IW wrapper);
protected <ID extends ItemDefinition<I>> List<VW> createValuesWrapper(IW itemWrapper, I item, WrapperContext context) throws SchemaException {
List<VW> pvWrappers = new ArrayList<>();
ID definition = (ID) item.getDefinition();
//TODO : prismContainer.isEmpty() interates and check is all prismcontainervalues are empty.. isn't it confusing?
if (item.isEmpty() && item.getValues().isEmpty()) {
if (shouldCreateEmptyValue(item, context)) {
PV prismValue = createNewValue(item);
VW valueWrapper = createValueWrapper(itemWrapper, prismValue, ValueStatus.ADDED, context);
pvWrappers.add(valueWrapper);
}
return pvWrappers;
}
for (PV pcv : (List<PV>)item.getValues()) {
if(canCreateValueWrapper(pcv)){
VW valueWrapper = createValueWrapper(itemWrapper, pcv, ValueStatus.NOT_CHANGED, context);
pvWrappers.add(valueWrapper);
}
}
return pvWrappers;
}
private boolean skipCreateWrapper(ItemDefinition<?> def, ItemStatus status, WrapperContext context, boolean isEmptyValue) {
if (QNameUtil.match(FocusType.F_LINK_REF, def.getItemName()) || QNameUtil.match(FocusType.F_PERSONA_REF, def.getItemName())) {
LOGGER.trace("Skip creating wrapper for {}, it is not supported", def);
return false;
}
if (ItemProcessing.IGNORE == def.getProcessing()) {
LOGGER.trace("Skip creating wrapper for {}, because item processig is set to IGNORE.", def);
return false;
}
if (def.isExperimental() && !WebModelServiceUtils.isEnableExperimentalFeature(modelInteractionService, context.getTask(), context.getResult())) {
LOGGER.trace("Skipping creating wrapper for {}, because experimental GUI features are turned off.", def);
return false;
}
if (ItemStatus.ADDED == status && def.isDeprecated()) {
LOGGER.trace("Skipping creating wrapeer for {}, because item is deprecated and doesn't contain any value.", def);
return false;
}
if (ItemStatus.ADDED == context.getObjectStatus() && !def.canAdd()) {
LOGGER.trace("Skipping creating wrapper for {}, because ADD operation is not supported.", def);
return false;
}
if (ItemStatus.NOT_CHANGED == context.getObjectStatus()) {
if (!def.canRead()) {
LOGGER.trace("Skipping creating wrapper for {}, because read operation is not supported.", def);
return false;
}
}
return canCreateWrapper(def, status, context, isEmptyValue);
}
protected boolean canCreateWrapper(ItemDefinition<?> def, ItemStatus status, WrapperContext context, boolean isEmptyValue) {
if (!context.isCreateOperational() && def.isOperational()) {
LOGGER.trace("Skipping creating wrapper for {}, because it is operational.", def.getItemName());
return false;
}
return true;
}
private boolean determineReadOnly(IW itemWrapper, WrapperContext context) {
Boolean readOnly = context.getReadOnly();
if (readOnly != null) {
LOGGER.trace("Setting {} as readonly because context said so.", itemWrapper);
return readOnly.booleanValue();
}
ItemStatus objectStatus = context.getObjectStatus();
if (ItemStatus.NOT_CHANGED == objectStatus) {
if (!itemWrapper.canModify()) {
LOGGER.trace("Setting {} as readonly because authZ said so.", itemWrapper);
return true;
}
}
return false;
}
private boolean determineShowInVirtualContainer(IW itemWrapper, WrapperContext context) {
Collection<VirtualContainersSpecificationType> virtualContainers = context.getVirtualContainers();
if (virtualContainers == null) {
return false;
}
for (VirtualContainersSpecificationType virtualContainer : virtualContainers) {
for (VirtualContainerItemSpecificationType item : virtualContainer.getItem()) {
ItemPathType itemPathType = item.getPath();
if (itemPathType == null) {
LOGGER.error("Bad virtual item specification, missing path. Skipping virtual item settings for {}", itemWrapper);
continue;
}
ItemPath itemPath = itemPathType.getItemPath();
if (itemPath.equivalent(itemWrapper.getPath())) {
return true;
}
}
}
return false;
}
protected boolean canCreateValueWrapper(PV pcv) {
return true;
}
protected abstract PV createNewValue(I item) throws SchemaException;
protected abstract IW createWrapper(PrismContainerValueWrapper<?> parent, I childContainer, ItemStatus status);
protected boolean shouldCreateEmptyValue(I item, WrapperContext context) {
if (item.getDefinition().isEmphasized()) {
return true;
}
if (context.isCreateIfEmpty()) {
return true;
}
return true;
}
/**
* @return the registry
*/
public GuiComponentRegistryImpl getRegistry() {
return registry;
}
/**
* @return the prismContext
*/
public PrismContext getPrismContext() {
return prismContext;
}
// @Override
// public VW createValueWrapper(PV value, IW parent, ValueStatus status, WrapperContext context) throws SchemaException {
// // TODO Auto-generated method stub
// return null;
// }
}