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.mapping;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.Comparator;
24  import java.util.List;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.syncope.client.console.init.ClassPathScanImplementationLookup;
27  import org.apache.syncope.client.console.rest.AnyTypeClassRestClient;
28  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
29  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
30  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
31  import org.apache.syncope.client.ui.commons.Constants;
32  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
33  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
34  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
35  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTripleStateButtonPanel;
36  import org.apache.syncope.common.lib.scim.SCIMItem;
37  import org.apache.syncope.common.lib.scim.SCIMReturned;
38  import org.apache.wicket.ajax.AjaxRequestTarget;
39  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
40  import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
41  import org.apache.wicket.markup.html.WebMarkupContainer;
42  import org.apache.wicket.markup.html.basic.Label;
43  import org.apache.wicket.markup.html.list.ListItem;
44  import org.apache.wicket.markup.html.list.ListView;
45  import org.apache.wicket.markup.html.panel.Panel;
46  import org.apache.wicket.model.IModel;
47  import org.apache.wicket.model.Model;
48  import org.apache.wicket.model.PropertyModel;
49  import org.apache.wicket.spring.injection.annot.SpringBean;
50  
51  public class SCIMExtensionMappingPanel extends Panel {
52  
53      private static final long serialVersionUID = -5268147603868322754L;
54  
55      @SpringBean
56      protected AnyTypeRestClient anyTypeRestClient;
57  
58      @SpringBean
59      protected AnyTypeClassRestClient anyTypeClassRestClient;
60  
61      protected final Label intAttrNameInfo;
62  
63      protected final WebMarkupContainer mandatoryHeader;
64  
65      protected final Label caseExactLabel;
66  
67      protected final Label mutabilityLabel;
68  
69      protected final Label returnedLabel;
70  
71      protected final Label uniquenessLabel;
72  
73      protected final Label multiValuedLabel;
74  
75      protected final AjaxButton addMappingBtn;
76  
77      protected final ListView<SCIMItem> mappings;
78  
79      protected final WebMarkupContainer mappingContainer;
80  
81      protected final String anyTypeKey;
82  
83      public SCIMExtensionMappingPanel(
84              final String id,
85              final IModel<List<SCIMItem>> model,
86              final String anyTypeKey) {
87  
88          super(id);
89          this.anyTypeKey = anyTypeKey;
90          setOutputMarkupId(true);
91  
92          mappingContainer = new WebMarkupContainer("mappingContainer");
93          mappingContainer.setOutputMarkupId(true);
94          add(mappingContainer);
95  
96          intAttrNameInfo = new Label("intAttrNameInfo", Model.of());
97          mappingContainer.add(intAttrNameInfo);
98  
99          mandatoryHeader = new WebMarkupContainer("mandatoryHeader");
100         mandatoryHeader.setOutputMarkupId(true);
101         mappingContainer.add(mandatoryHeader);
102 
103         caseExactLabel = new Label("caseExactLabel", Model.of());
104         mappingContainer.add(caseExactLabel);
105         mutabilityLabel = new Label("mutabilityLabel", Model.of());
106         mappingContainer.add(mutabilityLabel);
107         returnedLabel = new Label("returnedLabel", Model.of());
108         mappingContainer.add(returnedLabel);
109         uniquenessLabel = new Label("uniquenessLabel", Model.of());
110         mappingContainer.add(uniquenessLabel);
111         multiValuedLabel = new Label("multiValuedLabel", Model.of());
112         mappingContainer.add(multiValuedLabel);
113 
114         mappings = new ListView<>("mappings", model) {
115 
116             private static final long serialVersionUID = -8749412138042656239L;
117 
118             @Override
119             protected void populateItem(final ListItem<SCIMItem> item) {
120                 final SCIMItem itemTO = item.getModelObject();
121 
122                 //--------------------------------
123                 // Internal attribute
124                 // -------------------------------
125                 AjaxTextFieldPanel intAttrName = new AjaxTextFieldPanel(
126                         "intAttrName",
127                         "intAttrName",
128                         new PropertyModel<>(itemTO, "intAttrName"),
129                         false);
130                 intAttrName.setChoices(List.of());
131                 intAttrName.setRequired(true).hideLabel();
132                 item.add(intAttrName);
133                 // -------------------------------
134 
135                 //--------------------------------
136                 // External attribute
137                 // -------------------------------
138                 AjaxTextFieldPanel extAttrName = new AjaxTextFieldPanel(
139                         "extAttrName",
140                         "extAttrName",
141                         new PropertyModel<>(itemTO, "extAttrName"));
142                 extAttrName.setChoices(getExtAttrNames().getObject());
143 
144                 extAttrName.setRequired(true).hideLabel();
145                 extAttrName.setEnabled(true);
146                 item.add(extAttrName);
147                 // -------------------------------
148 
149                 //--------------------------------
150                 // mandatoryCondition
151                 // -------------------------------
152                 AjaxTripleStateButtonPanel mandatoryCondition = new AjaxTripleStateButtonPanel(
153                         "mandatoryCondition",
154                         "mandatoryCondition",
155                         new PropertyModel<>(itemTO, "mandatoryCondition"));
156                 mandatoryCondition.setRequired(true);
157                 mandatoryCondition.hideLabel();
158                 mandatoryCondition.setEnabled(true);
159                 item.add(mandatoryCondition);
160                 // -------------------------------
161 
162                 //--------------------------------
163                 // CaseExact
164                 // -------------------------------
165                 AjaxTripleStateButtonPanel caseExact = new AjaxTripleStateButtonPanel(
166                         "caseExact",
167                         "caseExact",
168                         new PropertyModel<>(itemTO, "caseExact"));
169                 caseExact.setRequired(true);
170                 caseExact.hideLabel();
171                 caseExact.setEnabled(true);
172                 item.add(caseExact);
173                 // -------------------------------
174 
175                 //--------------------------------
176                 // Mutability
177                 // -------------------------------
178                 AjaxTripleStateButtonPanel mutability = new AjaxTripleStateButtonPanel(
179                         "mutability",
180                         "mutability",
181                         new PropertyModel<>(itemTO, "mutability"));
182                 mutability.setRequired(true);
183                 mutability.hideLabel();
184                 mutability.setEnabled(true);
185                 item.add(mutability);
186                 // -------------------------------
187 
188                 //--------------------------------
189                 // Returned
190                 // -------------------------------
191                 AjaxDropDownChoicePanel<SCIMReturned> returned = new AjaxDropDownChoicePanel<>(
192                         "returned",
193                         "returned",
194                         new PropertyModel<>(itemTO, "returned"));
195                 returned.hideLabel();
196                 returned.setChoices(List.of(SCIMReturned.values()));
197                 returned.setEnabled(true);
198                 item.add(returned);
199                 // -------------------------------
200 
201                 //--------------------------------
202                 // Uniqueness
203                 // -------------------------------
204                 AjaxTripleStateButtonPanel uniqueness = new AjaxTripleStateButtonPanel(
205                         "uniqueness",
206                         "uniqueness",
207                         new PropertyModel<>(itemTO, "uniqueness"));
208                 uniqueness.setRequired(true);
209                 uniqueness.hideLabel();
210                 uniqueness.setEnabled(true);
211                 item.add(uniqueness);
212                 // -------------------------------
213 
214                 //--------------------------------
215                 // MultiValued
216                 // -------------------------------
217                 AjaxTripleStateButtonPanel multiValued = new AjaxTripleStateButtonPanel(
218                         "multiValued",
219                         "multiValued",
220                         new PropertyModel<>(itemTO, "multiValued"));
221                 multiValued.setRequired(true);
222                 multiValued.hideLabel();
223                 multiValued.setEnabled(true);
224                 item.add(multiValued);
225                 // -------------------------------
226 
227                 //--------------------------------
228                 // Remove
229                 // -------------------------------
230                 ActionsPanel<Serializable> actions = new ActionsPanel<>("toRemove", null);
231                 actions.add(new ActionLink<>() {
232 
233                     private static final long serialVersionUID = -4097030429755746419L;
234 
235                     @Override
236                     public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
237                         model.getObject().remove(item.getIndex());
238                         item.getParent().removeAll();
239                         target.add(SCIMExtensionMappingPanel.this);
240                     }
241                 }, ActionLink.ActionType.DELETE, StringUtils.EMPTY, true).hideLabel();
242                 item.add(actions);
243                 // -------------------------------
244 
245                 intAttrName.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
246 
247                     private static final long serialVersionUID = 6890150953186587184L;
248 
249                     @Override
250                     protected void onUpdate(final AjaxRequestTarget target) {
251                     }
252                 });
253             }
254         };
255 
256         mappings.setReuseItems(true);
257         mappingContainer.add(mappings);
258 
259         addMappingBtn = new IndicatingAjaxButton("addMappingBtn") {
260 
261             private static final long serialVersionUID = -971427869417596230L;
262 
263             @Override
264             protected void onSubmit(final AjaxRequestTarget target) {
265                 model.getObject().add(new SCIMItem());
266                 target.add(SCIMExtensionMappingPanel.this);
267             }
268         };
269         addMappingBtn.setDefaultFormProcessing(false);
270         addMappingBtn.setEnabled(true);
271         mappingContainer.add(addMappingBtn);
272     }
273 
274     protected IModel<List<String>> getExtAttrNames() {
275         List<String> choices = new ArrayList<>();
276         switch (anyTypeKey) {
277             case "USER":
278                 choices.addAll(ClassPathScanImplementationLookup.USER_FIELD_NAMES);
279                 break;
280             case "GROUP":
281                 choices.addAll(ClassPathScanImplementationLookup.GROUP_FIELD_NAMES);
282                 break;
283             default:
284                 choices.addAll(ClassPathScanImplementationLookup.ANY_OBJECT_FIELD_NAMES);
285         }
286         anyTypeClassRestClient.list(anyTypeRestClient.read(anyTypeKey).getClasses()).
287                 forEach(anyTypeClassTO -> {
288                     choices.addAll(anyTypeClassTO.getPlainSchemas());
289                     choices.addAll(anyTypeClassTO.getDerSchemas());
290                 });
291 
292         choices.sort(Comparator.naturalOrder());
293         return Model.ofList(choices);
294     }
295 }