-
Notifications
You must be signed in to change notification settings - Fork 188
/
ACAttributeValuePanel.java
198 lines (161 loc) · 7.19 KB
/
ACAttributeValuePanel.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
/*
* 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.assignment;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.component.password.PasswordPanel;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.xml.XsdTypeMapper;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.web.component.input.DatePanel;
import com.evolveum.midpoint.web.component.input.TextPanel;
import com.evolveum.midpoint.web.component.input.TriStateComboPanel;
import com.evolveum.midpoint.web.component.prism.InputPanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import org.apache.commons.lang.ClassUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import java.util.List;
/**
* @author lazyman
*/
public class ACAttributeValuePanel extends BasePanel<ACValueConstructionDto> {
private static final String ID_INPUT = "input";
private static final String ID_ADD = "add";
private static final String ID_REMOVE = "remove";
public ACAttributeValuePanel(String id, IModel<ACValueConstructionDto> iModel,
boolean ignoreMandatoryAttributes, Form form) {
super(id, iModel);
initLayout(form, ignoreMandatoryAttributes);
}
private void initLayout(Form form, boolean ignoreMandatoryAttributes) {
ACValueConstructionDto dto = getModel().getObject();
PrismPropertyDefinition definition = dto.getAttribute().getDefinition();
InputPanel input = createTypedInputComponent(ID_INPUT, definition);
for (FormComponent comp : input.getFormComponents()) {
comp.setLabel(new PropertyModel(dto.getAttribute(), ACAttributeDto.F_NAME));
if (!ignoreMandatoryAttributes){
comp.setRequired(definition.getMinOccurs() > 0);
}
comp.add(new AjaxFormComponentUpdatingBehavior("blur") {
@Override
protected void onUpdate(AjaxRequestTarget target) {}
});
}
add(input);
AjaxLink<Void> addLink = new AjaxLink<Void>(ID_ADD) {
@Override
public void onClick(AjaxRequestTarget target) {
addPerformed(target);
}
};
add(addLink);
addLink.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return isAddVisible();
}
});
AjaxLink removeLink = new AjaxLink(ID_REMOVE) {
@Override
public void onClick(AjaxRequestTarget target) {
removePerformed(target);
}
};
add(removeLink);
removeLink.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return isRemoveVisible();
}
});
}
private InputPanel createTypedInputComponent(String id, PrismPropertyDefinition definition) {
QName valueType = definition.getTypeName();
final String baseExpression = ACValueConstructionDto.F_VALUE;
InputPanel panel;
if (DOMUtil.XSD_DATETIME.equals(valueType)) {
panel = new DatePanel(id, new PropertyModel<>(getModel(), baseExpression));
} else if (ProtectedStringType.COMPLEX_TYPE.equals(valueType)) {
panel = new PasswordPanel(id, new PropertyModel<>(getModel(), baseExpression));
} else if (DOMUtil.XSD_BOOLEAN.equals(valueType)) {
panel = new TriStateComboPanel(id, new PropertyModel<>(getModel(), baseExpression));
} else if (SchemaConstants.T_POLY_STRING_TYPE.equals(valueType)) {
panel = new TextPanel<>(id, new PropertyModel<>(getModel(), baseExpression + ".orig"), String.class);
} else {
Class type = XsdTypeMapper.getXsdToJavaMapping(valueType);
if (type != null && type.isPrimitive()) {
type = ClassUtils.primitiveToWrapper(type);
}
panel = new TextPanel<>(id, new PropertyModel<>(getModel(), baseExpression),
type);
if (ObjectType.F_NAME.equals(definition.getItemName())) {
panel.getBaseFormComponent().setRequired(true);
}
}
return panel;
}
private boolean isAddVisible() {
ACValueConstructionDto dto = getModel().getObject();
ACAttributeDto attributeDto = dto.getAttribute();
PrismPropertyDefinition def = attributeDto.getDefinition();
List<ACValueConstructionDto> values = attributeDto.getValues();
if (def.getMaxOccurs() != -1 && values.size() >= def.getMaxOccurs()) {
return false;
}
//we want to show add on last item only
if (values.indexOf(dto) + 1 != values.size()) {
return false;
}
return true;
}
private boolean isRemoveVisible() {
ACValueConstructionDto dto = getModel().getObject();
ACAttributeDto attributeDto = dto.getAttribute();
PrismPropertyDefinition def = attributeDto.getDefinition();
List<ACValueConstructionDto> values = attributeDto.getValues();
if (values.size() <= 1) {
return false;
}
if (values.size() <= def.getMinOccurs()) {
return false;
}
return true;
}
private void addPerformed(AjaxRequestTarget target) {
ACValueConstructionDto dto = getModel().getObject();
ACAttributeDto attributeDto = dto.getAttribute();
attributeDto.getValues().add(new ACValueConstructionDto(attributeDto, null));
target.add(findParent(ACAttributePanel.class).getParent());
//todo implement add to account construction
}
private void removePerformed(AjaxRequestTarget target) {
ACValueConstructionDto dto = getModel().getObject();
ACAttributeDto attributeDto = dto.getAttribute();
attributeDto.getValues().remove(dto);
//todo implement remove from acctount construction
target.add(findParent(ACAttributePanel.class).getParent());
}
}