-
Notifications
You must be signed in to change notification settings - Fork 188
/
DynamicFieldGroupPanel.java
182 lines (142 loc) · 6.66 KB
/
DynamicFieldGroupPanel.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
/*
* 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 java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.impl.util.GuiImplUtil;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.ItemDefinitionImpl;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.xml.XsdTypeMapper;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractFormItemType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormDefinitionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormFieldGroupType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormItemDisplayType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import com.evolveum.midpoint.schema.util.FormTypeUtil;
public class DynamicFieldGroupPanel<O extends ObjectType> extends BasePanel<ObjectWrapper<O>>{
private static final long serialVersionUID = 1L;
private static final transient Trace LOGGER = TraceManager.getTrace(DynamicFieldGroupPanel.class);
private static final String ID_PROPERTY = "property";
private static final String ID_HEADER = "header";
private List<AbstractFormItemType> formItems;
public DynamicFieldGroupPanel(String id, String groupName, IModel<ObjectWrapper<O>> objectWrapper, List<AbstractFormItemType> formItems, Form<?> mainForm, PageBase parentPage) {
super(id,objectWrapper);
setParent(parentPage);
this.formItems = formItems;
initLayout(groupName, formItems, mainForm);
}
public DynamicFieldGroupPanel(String id, IModel<ObjectWrapper<O>> objectWrapper, FormDefinitionType formDefinition, Form<?> mainForm, PageBase parentPage) {
super(id, objectWrapper);
setParent(parentPage);
String groupName = "Basic";
if (formDefinition.getDisplay() != null) {
groupName = formDefinition.getDisplay().getLabel();
}
this.formItems = FormTypeUtil.getFormItems(formDefinition.getFormItems());
initLayout(groupName, formItems, mainForm);
}
private void initLayout(String groupName, List<AbstractFormItemType> formItems, Form<?> mainForm) {
Label header = new Label(ID_HEADER, groupName);
add(header);
RepeatingView itemView = new RepeatingView(ID_PROPERTY);
add(itemView);
int i = 0;
for (AbstractFormItemType formItem : formItems) {
if (formItem instanceof FormFieldGroupType) {
DynamicFieldGroupPanel<O> dynamicFieldGroupPanel = new DynamicFieldGroupPanel<O>(itemView.newChildId(), formItem.getName(), getModel(), FormTypeUtil.getFormItems(((FormFieldGroupType) formItem).getFormItems()), mainForm, getPageBase());
dynamicFieldGroupPanel.setOutputMarkupId(true);
itemView.add(dynamicFieldGroupPanel);
continue;
}
ItemWrapper itemWrapper = createItemWrapper(formItem, getObjectWrapper());
if (itemWrapper instanceof ContainerWrapper) {
PrismContainerPanel containerPanel = new PrismContainerPanel(itemView.newChildId(),
Model.of((ContainerWrapper) itemWrapper), true, mainForm, getPageBase());
containerPanel.setOutputMarkupId(true);
itemView.add(containerPanel);
} else {
PrismPropertyPanel propertyPanel = new PrismPropertyPanel(itemView.newChildId(),
Model.of(itemWrapper), mainForm, getPageBase());
propertyPanel.setOutputMarkupId(true);
itemView.add(propertyPanel);
}
i++;
}
}
private ItemWrapper createItemWrapper(AbstractFormItemType formField, ObjectWrapper objectWrapper) {
ItemPathType itemPathType = GuiImplUtil.getPathType(formField);
if (itemPathType == null) {
getSession().error("Bad form item definition. It has to contain reference to the real attribute");
LOGGER.error("Bad form item definition. It has to contain reference to the real attribute");
throw new RestartResponseException(getPageBase());
}
ItemPath path = itemPathType.getItemPath();
ItemDefinition itemDef = objectWrapper.getObject().getDefinition().findItemDefinition(path);
ItemWrapper itemWrapper = null;
if (itemDef instanceof PrismContainerDefinition) {
itemWrapper = objectWrapper.findContainerWrapper(path);
} else {
itemWrapper = objectWrapper.findPropertyWrapper(path);
}
if (itemWrapper == null) {
getSession().error("Bad form item definition. No attribute with path: " + path + " was found");
LOGGER.error("Bad form item definition. No attribute with path: " + path + " was found");
throw new RestartResponseException(getPageBase());
}
applyFormDefinition(itemWrapper, formField);
return itemWrapper;
}
private void applyFormDefinition(ItemWrapper itemWrapper, AbstractFormItemType formField) {
FormItemDisplayType displayType = formField.getDisplay();
if (displayType == null) {
return;
}
ItemDefinitionImpl itemDef = (ItemDefinitionImpl) itemWrapper.getItemDefinition();
if (StringUtils.isNotEmpty(displayType.getLabel())) {
itemDef.setDisplayName(displayType.getLabel());
}
if (StringUtils.isNotEmpty(displayType.getHelp())) {
itemDef.setHelp(displayType.getHelp());
}
if (StringUtils.isNotEmpty(displayType.getMaxOccurs())) {
itemDef.setMaxOccurs(XsdTypeMapper.multiplicityToInteger(displayType.getMaxOccurs()));
}
if (StringUtils.isNotEmpty(displayType.getMinOccurs())) {
itemDef.setMinOccurs(XsdTypeMapper.multiplicityToInteger(displayType.getMinOccurs()));
}
}
public ObjectWrapper<O> getObjectWrapper() {
return getModelObject();
}
public List<AbstractFormItemType> getFormItems() {
return formItems;
}
}