1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.console.panels;
20
21 import de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverBehavior;
22 import de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig;
23 import de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipConfig;
24 import io.swagger.v3.oas.annotations.media.Schema;
25 import java.io.Serializable;
26 import java.lang.reflect.Field;
27 import java.lang.reflect.ParameterizedType;
28 import java.time.Duration;
29 import java.time.OffsetDateTime;
30 import java.time.ZonedDateTime;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import org.apache.commons.lang3.time.DateFormatUtils;
38 import org.apache.commons.lang3.tuple.Pair;
39 import org.apache.commons.lang3.tuple.Triple;
40 import org.apache.syncope.client.console.SyncopeConsoleSession;
41 import org.apache.syncope.client.console.SyncopeWebApplication;
42 import org.apache.syncope.client.console.panels.search.AnyObjectSearchPanel;
43 import org.apache.syncope.client.console.panels.search.GroupSearchPanel;
44 import org.apache.syncope.client.console.panels.search.SearchClause;
45 import org.apache.syncope.client.console.panels.search.SearchUtils;
46 import org.apache.syncope.client.console.panels.search.UserSearchPanel;
47 import org.apache.syncope.client.console.rest.SchemaRestClient;
48 import org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel;
49 import org.apache.syncope.client.lib.SyncopeClient;
50 import org.apache.syncope.client.ui.commons.DateOps;
51 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
52 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDateTimeFieldPanel;
53 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
54 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxGridFieldPanel;
55 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
56 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxSpinnerFieldPanel;
57 import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
58 import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
59 import org.apache.syncope.common.lib.report.SearchCondition;
60 import org.apache.syncope.common.lib.search.AbstractFiqlSearchConditionBuilder;
61 import org.apache.syncope.common.lib.to.SchemaTO;
62 import org.apache.syncope.common.lib.types.SchemaType;
63 import org.apache.wicket.PageReference;
64 import org.apache.wicket.core.util.lang.PropertyResolver;
65 import org.apache.wicket.core.util.lang.PropertyResolverConverter;
66 import org.apache.wicket.markup.html.basic.Label;
67 import org.apache.wicket.markup.html.list.ListItem;
68 import org.apache.wicket.markup.html.list.ListView;
69 import org.apache.wicket.markup.html.panel.Fragment;
70 import org.apache.wicket.markup.html.panel.Panel;
71 import org.apache.wicket.model.IModel;
72 import org.apache.wicket.model.LoadableDetachableModel;
73 import org.apache.wicket.model.Model;
74 import org.apache.wicket.model.PropertyModel;
75 import org.apache.wicket.model.ResourceModel;
76 import org.apache.wicket.model.util.ListModel;
77 import org.apache.wicket.spring.injection.annot.SpringBean;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80 import org.springframework.beans.BeanWrapper;
81 import org.springframework.beans.PropertyAccessorFactory;
82 import org.springframework.util.ClassUtils;
83 import org.springframework.util.ReflectionUtils;
84
85 public class BeanPanel<T extends Serializable> extends Panel {
86
87 private static final long serialVersionUID = 3905038169553185171L;
88
89 protected static final Logger LOG = LoggerFactory.getLogger(BeanPanel.class);
90
91 @SpringBean
92 protected SchemaRestClient schemaRestClient;
93
94 protected final List<String> excluded;
95
96 protected final Map<String, Pair<AbstractFiqlSearchConditionBuilder<?, ?, ?>, List<SearchClause>>> sCondWrapper;
97
98 public BeanPanel(final String id, final IModel<T> bean, final PageReference pageRef, final String... excluded) {
99 this(id, bean, null, pageRef, excluded);
100 }
101
102 public BeanPanel(
103 final String id,
104 final IModel<T> bean,
105 final Map<String, Pair<AbstractFiqlSearchConditionBuilder<?, ?, ?>, List<SearchClause>>> sCondWrapper,
106 final PageReference pageRef,
107 final String... excluded) {
108
109 super(id, bean);
110 setOutputMarkupId(true);
111
112 this.sCondWrapper = sCondWrapper;
113
114 this.excluded = new ArrayList<>(List.of(excluded));
115 this.excluded.add("serialVersionUID");
116 this.excluded.add("class");
117
118 LoadableDetachableModel<List<Field>> model = new LoadableDetachableModel<>() {
119
120 private static final long serialVersionUID = 5275935387613157437L;
121
122 @Override
123 protected List<Field> load() {
124 List<Field> result = new ArrayList<>();
125
126 if (BeanPanel.this.getDefaultModelObject() != null) {
127 ReflectionUtils.doWithFields(
128 BeanPanel.this.getDefaultModelObject().getClass(),
129 result::add,
130 field -> !field.isSynthetic() && !BeanPanel.this.excluded.contains(field.getName()));
131 }
132
133 return result;
134 }
135 };
136
137 add(new ListView<>("propView", model) {
138
139 private static final long serialVersionUID = 9101744072914090143L;
140
141 private void setRequired(final ListItem<Field> item, final boolean required) {
142 if (required) {
143 Fragment fragment = new Fragment("required", "requiredFragment", this);
144 fragment.add(new Label("requiredLabel", "*"));
145 item.replace(fragment);
146 }
147 }
148
149 private void setDescription(final ListItem<Field> item, final String description) {
150 Fragment fragment = new Fragment("description", "descriptionFragment", this);
151 fragment.add(new Label("descriptionLabel", Model.of()).add(new PopoverBehavior(
152 Model.<String>of(),
153 Model.of(description),
154 new PopoverConfig().withPlacement(TooltipConfig.Placement.right)) {
155
156 private static final long serialVersionUID = -7867802555691605021L;
157
158 @Override
159 protected String createRelAttribute() {
160 return "description";
161 }
162 }).setRenderBodyOnly(false));
163 item.replace(fragment);
164 }
165
166 @SuppressWarnings({ "unchecked", "rawtypes" })
167 @Override
168 protected void populateItem(final ListItem<Field> item) {
169 item.add(new Fragment("required", "emptyFragment", this));
170 item.add(new Fragment("description", "emptyFragment", this));
171
172 Field field = item.getModelObject();
173
174 item.add(new Label("fieldName", new ResourceModel(field.getName(), field.getName())));
175
176 Panel panel;
177
178 SearchCondition scondAnnot = field.getAnnotation(SearchCondition.class);
179 if (scondAnnot != null) {
180 BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean.getObject());
181 String fiql = (String) wrapper.getPropertyValue(field.getName());
182
183 List<SearchClause> clauses = Optional.ofNullable(fiql).
184 map(f -> SearchUtils.getSearchClauses(f.replaceAll(
185 SearchUtils.getTypeConditionPattern(scondAnnot.type()).pattern(), ""))).
186 orElse(new ArrayList<>());
187
188 AbstractFiqlSearchConditionBuilder<?, ?, ?> builder;
189 switch (scondAnnot.type()) {
190 case "USER":
191 panel = new UserSearchPanel.Builder(
192 new ListModel<>(clauses), pageRef).required(false).build("value");
193 builder = SyncopeClient.getUserSearchConditionBuilder();
194 break;
195
196 case "GROUP":
197 panel = new GroupSearchPanel.Builder(
198 new ListModel<>(clauses), pageRef).required(false).build("value");
199 builder = SyncopeClient.getGroupSearchConditionBuilder();
200 break;
201
202 default:
203 panel = new AnyObjectSearchPanel.Builder(
204 scondAnnot.type(),
205 new ListModel<>(clauses), pageRef).required(false).build("value");
206 builder = SyncopeClient.getAnyObjectSearchConditionBuilder(scondAnnot.type());
207 }
208
209 Optional.ofNullable(BeanPanel.this.sCondWrapper).
210 ifPresent(scw -> scw.put(field.getName(), Pair.of(builder, clauses)));
211 } else if (List.class.equals(field.getType())) {
212 Class<?> listItemType = field.getGenericType() instanceof ParameterizedType
213 ? (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]
214 : String.class;
215
216 org.apache.syncope.common.lib.Schema schema =
217 field.getAnnotation(org.apache.syncope.common.lib.Schema.class);
218 if (listItemType.equals(String.class) && schema != null) {
219 List<SchemaTO> choices = new ArrayList<>();
220
221 for (SchemaType type : schema.type()) {
222 switch (type) {
223 case PLAIN:
224 choices.addAll(
225 schemaRestClient.getSchemas(SchemaType.PLAIN, schema.anyTypeKind()));
226 break;
227
228 case DERIVED:
229 choices.addAll(
230 schemaRestClient.getSchemas(SchemaType.DERIVED, schema.anyTypeKind()));
231 break;
232
233 case VIRTUAL:
234 choices.addAll(
235 schemaRestClient.getSchemas(SchemaType.VIRTUAL, schema.anyTypeKind()));
236 break;
237
238 default:
239 }
240 }
241
242 panel = new AjaxPalettePanel.Builder<>().setName(field.getName()).build(
243 "value",
244 new PropertyModel<>(bean.getObject(), field.getName()),
245 new ListModel<>(choices.stream().map(SchemaTO::getKey).collect(Collectors.toList()))).
246 hideLabel();
247 } else if (listItemType.isEnum()) {
248 panel = new AjaxPalettePanel.Builder<>().setName(field.getName()).build(
249 "value",
250 new PropertyModel<>(bean.getObject(), field.getName()),
251 new ListModel(List.of(listItemType.getEnumConstants()))).hideLabel();
252 } else {
253 Triple<FieldPanel, Boolean, Optional<String>> single =
254 buildSinglePanel(bean.getObject(), field.getType(), field.getName(),
255 field.getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class), "panel");
256
257 setRequired(item, single.getMiddle());
258 single.getRight().ifPresent(description -> setDescription(item, description));
259
260 panel = new MultiFieldPanel.Builder<>(
261 new PropertyModel<>(bean.getObject(), field.getName())).build(
262 "value",
263 field.getName(),
264 single.getLeft()).hideLabel();
265 }
266 } else if (Map.class.equals(field.getType())) {
267 panel = new AjaxGridFieldPanel(
268 "value", field.getName(), new PropertyModel<>(bean, field.getName())).hideLabel();
269 Optional.ofNullable(field.getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class)).
270 ifPresent(annot -> setDescription(item, annot.description()));
271 } else {
272 Triple<FieldPanel, Boolean, Optional<String>> single =
273 buildSinglePanel(bean.getObject(), field.getType(), field.getName(),
274 field.getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class), "value");
275
276 setRequired(item, single.getMiddle());
277 single.getRight().ifPresent(description -> setDescription(item, description));
278
279 panel = single.getLeft().hideLabel();
280 }
281
282 item.add(panel.setRenderBodyOnly(true));
283 }
284 }.setReuseItems(false));
285 }
286
287 @SuppressWarnings({ "unchecked", "rawtypes" })
288 private Triple<FieldPanel, Boolean, Optional<String>> buildSinglePanel(
289 final Serializable bean, final Class<?> type, final String fieldName,
290 final io.swagger.v3.oas.annotations.media.Schema schema, final String id) {
291
292 PropertyModel model = new PropertyModel<>(bean, fieldName);
293
294 FieldPanel panel;
295 if (ClassUtils.isAssignable(Boolean.class, type)) {
296 panel = new AjaxCheckBoxPanel(id, fieldName, model);
297 } else if (ClassUtils.isAssignable(Number.class, type)) {
298 panel = new AjaxSpinnerFieldPanel.Builder<>().build(
299 id, fieldName, (Class<Number>) ClassUtils.resolvePrimitiveIfNecessary(type), model);
300 } else if (Date.class.equals(type)) {
301 panel = new AjaxDateTimeFieldPanel(id, fieldName, model,
302 DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT);
303 } else if (OffsetDateTime.class.equals(type)) {
304 panel = new AjaxDateTimeFieldPanel(id, fieldName, DateOps.WrappedDateModel.ofOffset(model),
305 DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT);
306 } else if (ZonedDateTime.class.equals(type)) {
307 panel = new AjaxDateTimeFieldPanel(id, fieldName, DateOps.WrappedDateModel.ofZoned(model),
308 DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT);
309 } else if (type.isEnum()) {
310 panel = new AjaxDropDownChoicePanel(id, fieldName, model).
311 setChoices(List.of(type.getEnumConstants()));
312 } else if (Duration.class.equals(type)) {
313 panel = new AjaxTextFieldPanel(id, fieldName, new IModel<>() {
314
315 private static final long serialVersionUID = 807008909842554829L;
316
317 @Override
318 public String getObject() {
319 return Optional.ofNullable(PropertyResolver.getValue(fieldName, bean)).
320 map(Object::toString).orElse(null);
321 }
322
323 @Override
324 public void setObject(final String object) {
325 PropertyResolverConverter prc = new PropertyResolverConverter(
326 SyncopeWebApplication.get().getConverterLocator(),
327 SyncopeConsoleSession.get().getLocale());
328 PropertyResolver.setValue(fieldName, bean, Duration.parse(object), prc);
329 }
330 });
331 } else {
332
333 panel = new AjaxTextFieldPanel(id, fieldName, model);
334 }
335
336 boolean required = false;
337 Optional<String> description = Optional.empty();
338
339 if (schema != null) {
340 panel.setReadOnly(schema.accessMode() == Schema.AccessMode.READ_ONLY);
341
342 required = schema.requiredMode() == Schema.RequiredMode.REQUIRED;
343 panel.setRequired(required);
344
345 Optional.ofNullable(schema.example()).ifPresent(panel::setPlaceholder);
346
347 description = Optional.ofNullable(schema.description());
348
349 if (panel instanceof AjaxTextFieldPanel
350 && panel.getModelObject() == null
351 && schema.defaultValue() != null) {
352
353 ((AjaxTextFieldPanel) panel).setModelObject(schema.defaultValue());
354 }
355 }
356
357 return Triple.of(panel, required, description);
358 }
359 }