/
HeterogenousContainerWrapperFactory.java
217 lines (173 loc) · 7.45 KB
/
HeterogenousContainerWrapperFactory.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
/*
* Copyright (c) 2010-2019 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.gui.impl.factory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.xml.namespace.QName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.gui.api.prism.ItemStatus;
import com.evolveum.midpoint.gui.api.prism.ItemWrapper;
import com.evolveum.midpoint.gui.api.prism.PrismContainerWrapper;
import com.evolveum.midpoint.gui.api.registry.GuiComponentRegistry;
import com.evolveum.midpoint.gui.impl.prism.PrismContainerPanel;
import com.evolveum.midpoint.gui.impl.prism.PrismContainerValueWrapper;
import com.evolveum.midpoint.gui.impl.prism.PrismContainerValueWrapperImpl;
import com.evolveum.midpoint.gui.impl.prism.PrismContainerWrapperImpl;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.path.ItemName;
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;
/**
* @author katka
*
*/
@Component
public class HeterogenousContainerWrapperFactory<C extends Containerable> implements PrismContainerWrapperFactory<C> {
private static final transient Trace LOGGER = TraceManager.getTrace(HeterogenousContainerWrapperFactory.class);
@Autowired private GuiComponentRegistry registry;
@Override
public PrismContainerWrapper<C> createWrapper(PrismContainerValueWrapper<?> parent,
ItemDefinition<?> def, WrapperContext context) throws SchemaException {
ItemName name = def.getItemName();
PrismContainer<C> childItem = parent.getNewValue().findContainer(name);
ItemStatus status = ItemStatus.NOT_CHANGED;
if (childItem == null) {
childItem = (PrismContainer<C>) def.instantiate();
status = ItemStatus.ADDED;
}
PrismContainerWrapper<C> itemWrapper = new PrismContainerWrapperImpl<C>(parent, childItem, status);
registry.registerWrapperPanel(childItem.getDefinition().getTypeName(), PrismContainerPanel.class);
List<PrismContainerValueWrapper<C>> valueWrappers = createValuesWrapper(itemWrapper, childItem, context);
LOGGER.trace("valueWrappers {}", itemWrapper.getValues());
itemWrapper.getValues().addAll((Collection) valueWrappers);
return itemWrapper;
}
@Override
public PrismContainerValueWrapper<C> createValueWrapper(PrismContainerWrapper<C> parent,
PrismContainerValue<C> value, ValueStatus status, WrapperContext context)
throws SchemaException {
PrismContainerValueWrapper<C> containerValueWrapper = new PrismContainerValueWrapperImpl<C>(parent, value, status);
containerValueWrapper.setShowEmpty(context.isShowEmpty());
containerValueWrapper.setExpanded(shouldBeExpanded(parent, value, context));
containerValueWrapper.setHeterogenous(true);
List<ItemWrapper<?,?,?,?>> wrappers = new ArrayList<>();
for (ItemDefinition<?> def : value.getDefinition().getDefinitions()) {
Item<?,?> childItem = value.findItem(def.getItemName());
if (childItem == null && def instanceof PrismContainerDefinition) {
LOGGER.trace("Skipping craeting wrapper for {}, only property and refernce wrappers are created for heterogenous containers.");
continue;
}
ItemWrapperFactory<?,?,?> factory = registry.findWrapperFactory(def);
ItemWrapper<?, ?, ?, ?> wrapper = factory.createWrapper(containerValueWrapper, def, context);
if (wrapper != null) {
wrappers.add(wrapper);
}
}
containerValueWrapper.getItems().addAll((Collection) wrappers);
return containerValueWrapper;
}
protected boolean shouldBeExpanded(PrismContainerWrapper<C> parent, PrismContainerValue<C> value, WrapperContext context) {
if (value.isEmpty()) {
return context.isShowEmpty() || containsEmphasizedItems(parent.getDefinitions());
}
return true;
}
private boolean containsEmphasizedItems(List<? extends ItemDefinition> definitions) {
for (ItemDefinition def : definitions) {
if (def.isEmphasized()) {
return true;
}
}
return false;
}
protected List<PrismContainerValueWrapper<C>> createValuesWrapper(PrismContainerWrapper<C> itemWrapper, PrismContainer<C> item, WrapperContext context) throws SchemaException {
List<PrismContainerValueWrapper<C>> pvWrappers = new ArrayList<>();
// PrismContainerDefinition<C> definition = item.getDefinition();
if (item.getValues() == null || item.getValues().isEmpty()) {
PrismContainerValueWrapper<C> valueWrapper = createValueWrapper(itemWrapper, item.createNewValue(), ValueStatus.ADDED, context);
pvWrappers.add(valueWrapper);
return pvWrappers;
}
for (PrismContainerValue<C> pcv : item.getValues()) {
PrismContainerValueWrapper<C> valueWrapper = createValueWrapper(itemWrapper, pcv, ValueStatus.NOT_CHANGED, context);
pvWrappers.add(valueWrapper);
}
return pvWrappers;
}
/**
*
* match single value containers which contains a looot of other conainers, e.g. policy rule, policy action, notification configuration, etc
*/
@Override
public boolean match(ItemDefinition<?> def) {
QName defName = def.getTypeName();
if (!(def instanceof PrismContainerDefinition)) {
return false;
}
PrismContainerDefinition<?> containerDef = (PrismContainerDefinition<?>) def;
if (containerDef.isMultiValue()) {
return false;
}
List<? extends ItemDefinition> defs = containerDef.getDefinitions();
int containers = 0;
for (ItemDefinition<?> itemDef : defs) {
if (itemDef instanceof PrismContainerDefinition<?> && itemDef.isMultiValue()) {
containers++;
}
}
if (containers > 2) {
return true;
}
return false;
// if (def.isSingleValue() && )
// return PolicyConstraintPresentationType.COMPLEX_TYPE.equals(defName)
// || StatePolicyConstraintType.COMPLEX_TYPE.equals(defName)
// || HasAssignmentPolicyConstraintType.COMPLEX_TYPE.equals(defName)
// || ExclusionPolicyConstraintType.COMPLEX_TYPE.equals(defName)
// || PolicyConstraintsType.COMPLEX_TYPE.equals(defName);
}
@Override
@PostConstruct
public void register() {
registry.addToRegistry(this);
}
@Override
public int getOrder() {
return 110;
}
@Override
public PrismContainerValueWrapper<C> createContainerValueWrapper(PrismContainerWrapper<C> objectWrapper,
PrismContainerValue<C> objectValue, ValueStatus status, WrapperContext context) {
// TODO Auto-generated method stub
return null;
}
@Override
public PrismContainerWrapper<C> createWrapper(Item childContainer, ItemStatus status, WrapperContext context)
throws SchemaException {
// TODO Auto-generated method stub
return null;
}
}