/
PrismPropertyPanel.java
383 lines (316 loc) · 13.6 KB
/
PrismPropertyPanel.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
/*
* 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 com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.util.exception.SchemaException;
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.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.util.InfoTooltipBehavior;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.*;
import java.util.List;
/**
* @author lazyman
*/
public class PrismPropertyPanel<IW extends ItemWrapper> extends Panel {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(PrismPropertyPanel.class);
private static final String ID_HAS_PENDING_MODIFICATION = "hasPendingModification";
private static final String ID_HELP = "help";
private static final String ID_DEPRECATED = "deprecated";
private static final String ID_LABEL = "label";
private static final String ID_LABEL_CONTAINER = "labelContainer";
private IModel<IW> model;
private PageBase pageBase;
private boolean labelContainerVisible = true;
public PrismPropertyPanel(String id, final IModel<IW> model, Form form, ItemVisibilityHandler visibilityHandler, PageBase pageBase) {
super(id, model);
Validate.notNull(model, "no model");
this.model = model;
this.pageBase = pageBase;
LOGGER.trace("Creating property panel for {}", model.getObject());
setOutputMarkupId(true);
add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
IW propertyWrapper = model.getObject();
if (visibilityHandler != null && !visibilityHandler.isVisible(propertyWrapper)) {
return false;
}
boolean visible = propertyWrapper.isVisible();
LOGGER.trace("isVisible: {}: {}", propertyWrapper, visible);
return visible;
}
@Override
public boolean isEnabled() {
return !model.getObject().isReadonly();
}
});
initLayout(model, form);
}
public IModel<IW> getModel() {
return model;
}
private void initLayout(final IModel<IW> model, final Form form) {
WebMarkupContainer labelContainer = new WebMarkupContainer(ID_LABEL_CONTAINER);
labelContainer.setOutputMarkupId(true);
labelContainer.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override public boolean isVisible() {
return labelContainerVisible;
}
});
add(labelContainer);
final IModel<String> label = createDisplayName(model);
Label displayName = new Label(ID_LABEL, label);
displayName.add(new AttributeModifier("style", new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
if (model.getObject().isDeprecated()) {
return "text-decoration: line-through;";
}
return "text-decoration: none;";
}
}));
labelContainer.add(displayName);
final IModel<String> helpText = new LoadableModel<String>(false) {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
return loadHelpText(model);
}
};
Label help = new Label(ID_HELP);
help.add(AttributeModifier.replace("title", helpText));
help.add(new InfoTooltipBehavior());
help.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return StringUtils.isNotEmpty(helpText.getObject());
}
});
labelContainer.add(help);
Label deprecated = new Label(ID_DEPRECATED);
deprecated.add(AttributeModifier.replace("deprecated", new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
return model.getObject().getDeprecatedSince();
}
}));
deprecated.add(new InfoTooltipBehavior() {
private static final long serialVersionUID = 1L;
@Override
public String getCssClass() {
return "fa fa-fw fa-warning text-warning";
}
});
deprecated.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return model.getObject().isDeprecated();
}
});
labelContainer.add(deprecated);
WebMarkupContainer required = new WebMarkupContainer("required");
required.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
IW wrapper = model.getObject();
ItemDefinition def = wrapper.getItemDefinition();
if (ObjectType.F_NAME.equals(def.getName()) && model.getObject().getParent() != null &&
model.getObject().getParent().isMain()) {
//fix for "name as required" MID-789
return true;
}
return def.isMandatory();
}
});
labelContainer.add(required);
WebMarkupContainer hasOutbound = new WebMarkupContainer("hasOutbound");
hasOutbound.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return hasOutbound(model);
}
});
labelContainer.add(hasOutbound);
WebMarkupContainer hasPendingModification = new WebMarkupContainer(ID_HAS_PENDING_MODIFICATION);
hasPendingModification.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return hasPendingModification(model);
}
});
labelContainer.add(hasPendingModification);
ListView<ValueWrapper> values = new ListView<ValueWrapper>("values",
new PropertyModel<List<ValueWrapper>>(model, "values")) {
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(final ListItem<ValueWrapper> item) {
PrismValuePanel panel = new PrismValuePanel("value", item.getModel(), label, form, getValueCssClass(), getInputCssClass());
item.add(panel);
item.add(AttributeModifier.append("class", createStyleClassModel(item.getModel())));
item.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return isVisibleValue(item.getModel());
}
});
}
};
values.add(new AttributeModifier("class", getValuesClass()));
values.setReuseItems(true);
add(values);
}
protected String getInputCssClass() {
return"col-xs-10";
}
protected String getValuesClass() {
return "col-md-6";
}
protected String getValueCssClass() {
return "row";
}
private String loadHelpText(IModel<IW> model) {
IW property = (IW) model.getObject();
ItemDefinition def = property.getItemDefinition();
String doc = def.getHelp();
if (StringUtils.isEmpty(doc)) {
return null;
}
return PageBase.createStringResourceStatic(this, doc).getString();
// return StringResourceModelMigration.of(doc, null, doc).getString();
}
protected IModel<String> createStyleClassModel(final IModel<ValueWrapper> value) {
return new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
if (getIndexOfValue(value.getObject()) > 0) {
return "col-md-offset-2 prism-value";
}
return null;
}
};
}
private int getIndexOfValue(ValueWrapper value) {
ItemWrapper property = value.getItem();
List<ValueWrapper> values = property.getValues();
for (int i = 0; i < values.size(); i++) {
if (values.get(i).equals(value)) {
return i;
}
}
return -1;
}
private boolean hasOutbound(IModel<IW> model) {
ItemWrapper wrapper = model.getObject();
// Item property = wrapper.getItem();
ItemDefinition def = wrapper.getItemDefinition();
if (!(def instanceof RefinedAttributeDefinition)) {
return false;
}
RefinedAttributeDefinition refinedDef = (RefinedAttributeDefinition) def;
return refinedDef.hasOutboundMapping();
}
private boolean hasPendingModification(IModel<IW> model) {
ItemWrapper propertyWrapper = model.getObject();
ContainerWrapper containerWrapper = propertyWrapper.getParent();
if (containerWrapper == null) {
return false; // TODO - ok?
}
if (!containerWrapper.isMain()) {
return false;
}
PrismContainer prismContainer = containerWrapper.getItem();
if (prismContainer.getCompileTimeClass() == null ||
!ShadowType.class.isAssignableFrom(prismContainer.getCompileTimeClass())) {
return false;
}
PrismProperty objectChange = prismContainer.findProperty(ShadowType.F_OBJECT_CHANGE);
if (objectChange == null || objectChange.getValue() == null) {
return false;
}
ItemPath path = propertyWrapper.getItem().getPath();
ObjectDeltaType delta = (ObjectDeltaType) objectChange.getValue().getValue();
try {
for (ItemDeltaType itemDelta : delta.getItemDelta()) {
//noinspection unchecked
ItemDelta iDelta = DeltaConvertor.createItemDelta(itemDelta, (Class<? extends Containerable>)
prismContainer.getCompileTimeClass(), prismContainer.getPrismContext());
if (iDelta.getPath().equivalent(path)) {
return true;
}
}
} catch (SchemaException ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't check if property has pending modification", ex);
}
return false;
}
private IModel<String> createDisplayName(final IModel<IW> model) {
return new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
IW wrapper = model.getObject();
String displayName = wrapper.getDisplayName();
String displayNameValueByKey = PageBase.createStringResourceStatic(PrismPropertyPanel.this, displayName).getString();
return StringUtils.isEmpty(displayNameValueByKey) ?
getString(displayName, null, displayName) : displayNameValueByKey;
}
};
}
private boolean isVisibleValue(IModel<ValueWrapper> model) {
ValueWrapper value = model.getObject();
return !ValueStatus.DELETED.equals(value.getStatus());
}
public boolean isLabelContainerVisible() {
return labelContainerVisible;
}
public void setLabelContainerVisible(boolean labelContainerVisible) {
this.labelContainerVisible = labelContainerVisible;
}
}