View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.panels;
20  
21  import java.util.List;
22  import org.apache.commons.lang3.BooleanUtils;
23  import org.apache.commons.lang3.time.DateFormatUtils;
24  import org.apache.syncope.client.console.wicket.markup.html.form.BinaryFieldPanel;
25  import org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel;
26  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDateTimeFieldPanel;
27  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxSpinnerFieldPanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
31  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
32  import org.apache.syncope.common.lib.to.PlainSchemaTO;
33  import org.apache.wicket.extensions.wizard.WizardStep;
34  import org.apache.wicket.markup.html.WebMarkupContainer;
35  import org.apache.wicket.markup.html.form.IChoiceRenderer;
36  import org.apache.wicket.markup.html.list.ListItem;
37  import org.apache.wicket.markup.html.list.ListView;
38  import org.apache.wicket.markup.html.panel.Panel;
39  import org.apache.wicket.model.IModel;
40  import org.apache.wicket.model.LoadableDetachableModel;
41  import org.apache.wicket.model.Model;
42  import org.apache.wicket.model.PropertyModel;
43  
44  public class ParametersWizardAttrStep extends WizardStep {
45  
46      private static final long serialVersionUID = -7843275202297616553L;
47  
48      private final AjaxTextFieldPanel schema;
49  
50      public ParametersWizardAttrStep(
51              final AjaxWizard.Mode mode,
52              final ParametersWizardPanel.ParametersForm modelObject) {
53  
54          this.setOutputMarkupId(true);
55  
56          WebMarkupContainer content = new WebMarkupContainer("content");
57          content.setOutputMarkupId(true);
58          add(content);
59  
60          schema = new AjaxTextFieldPanel(
61                  "schema", getString("schema"), new PropertyModel<>(modelObject.getParam(), "schema"));
62          schema.setRequired(true);
63          schema.setReadOnly(mode != AjaxWizard.Mode.CREATE);
64          content.add(schema);
65  
66          LoadableDetachableModel<List<PlainSchemaTO>> schemas = new LoadableDetachableModel<>() {
67  
68              private static final long serialVersionUID = 7172461137064525667L;
69  
70              @Override
71              protected List<PlainSchemaTO> load() {
72                  return List.of(modelObject.getSchema());
73              }
74          };
75  
76          ListView<PlainSchemaTO> attrs = new ListView<>("attrs", schemas) {
77  
78              private static final long serialVersionUID = 9101744072914090143L;
79  
80              @Override
81              protected void populateItem(final ListItem<PlainSchemaTO> item) {
82                  item.add(getFieldPanel("panel", modelObject.getParam(), item.getModelObject()));
83              }
84          };
85          content.add(attrs);
86      }
87  
88      @SuppressWarnings({ "rawtypes", "unchecked" })
89      private Panel getFieldPanel(final String id, final ConfParam param, final PlainSchemaTO plainSchemaTO) {
90          String valueHeaderName = getString("values");
91  
92          final FieldPanel panel;
93          switch (plainSchemaTO.getType()) {
94              case Date:
95                  panel = new AjaxDateTimeFieldPanel(
96                          id, valueHeaderName, new Model<>(),
97                          DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT);
98                  break;
99  
100             case Boolean:
101                 panel = new AjaxDropDownChoicePanel<Boolean>(id, valueHeaderName, new Model<>(), false);
102                 ((AjaxDropDownChoicePanel<Boolean>) panel).setChoices(List.of(true, false));
103 
104                 if (!param.getValues().isEmpty()) {
105                     ((AjaxDropDownChoicePanel) panel).setChoiceRenderer(new IChoiceRenderer<Boolean>() {
106 
107                         private static final long serialVersionUID = -8223314361351275865L;
108 
109                         @Override
110                         public Object getDisplayValue(final Boolean object) {
111                             return BooleanUtils.toStringTrueFalse(object);
112                         }
113 
114                         @Override
115                         public String getIdValue(final Boolean object, final int index) {
116                             return BooleanUtils.toStringTrueFalse(object);
117                         }
118 
119                         @Override
120                         public Boolean getObject(
121                                 final String id, final IModel<? extends List<? extends Boolean>> choices) {
122 
123                             return BooleanUtils.toBoolean(id);
124                         }
125                     });
126                 }
127                 ((AjaxDropDownChoicePanel<Boolean>) panel).setNullValid(false);
128                 break;
129 
130             case Long:
131                 panel = new AjaxSpinnerFieldPanel.Builder<Long>().
132                         convertValuesToString(false).
133                         build(id, valueHeaderName, Long.class, new Model<>());
134                 break;
135 
136             case Double:
137                 panel = new AjaxSpinnerFieldPanel.Builder<Double>().
138                         convertValuesToString(false).
139                         build(id, valueHeaderName, Double.class, new Model<>());
140                 break;
141 
142             case Binary:
143                 panel = new BinaryFieldPanel(id, valueHeaderName, new Model<>(),
144                         plainSchemaTO.getMimeType(), schema.getModelObject());
145                 break;
146 
147             default:
148                 panel = new AjaxTextFieldPanel(id, valueHeaderName, new Model<>(), false);
149         }
150 
151         if (plainSchemaTO.isMultivalue()) {
152             return new MultiFieldPanel.Builder<>(
153                     new PropertyModel<>(param, "values")).build(id, valueHeaderName, panel);
154         }
155 
156         panel.setNewModel(param.getValues());
157         return panel;
158     }
159 }