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.wizards.any;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.client.console.rest.AnyTypeClassRestClient;
25  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
26  import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
27  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
30  import org.apache.syncope.common.lib.to.AnyTypeClassTO;
31  import org.apache.syncope.common.lib.to.GroupTO;
32  import org.apache.syncope.common.lib.to.TypeExtensionTO;
33  import org.apache.syncope.common.lib.types.AnyTypeKind;
34  import org.apache.wicket.PageReference;
35  import org.apache.wicket.extensions.wizard.WizardModel;
36  import org.apache.wicket.extensions.wizard.WizardStep;
37  import org.apache.wicket.markup.html.basic.Label;
38  import org.apache.wicket.model.PropertyModel;
39  import org.apache.wicket.model.util.ListModel;
40  
41  public class TypeExtensionWizardBuilder extends BaseAjaxWizardBuilder<TypeExtensionTO> {
42  
43      private static final long serialVersionUID = -7185214439144835423L;
44  
45      protected final GroupTO groupTO;
46  
47      protected final String anyTypeLabel;
48  
49      protected final String auxClassesLabel;
50  
51      protected final AnyTypeRestClient anyTypeRestClient;
52  
53      protected final AnyTypeClassRestClient anyTypeClassRestClient;
54  
55      public TypeExtensionWizardBuilder(
56              final GroupTO groupTO,
57              final TypeExtensionTO defaultItem,
58              final String anyTypeLabel,
59              final String auxClassesLabel,
60              final AnyTypeRestClient anyTypeRestClient,
61              final AnyTypeClassRestClient anyTypeClassRestClient,
62              final PageReference pageRef) {
63  
64          super(defaultItem, pageRef);
65  
66          this.groupTO = groupTO;
67          this.anyTypeLabel = anyTypeLabel;
68          this.auxClassesLabel = auxClassesLabel;
69          this.anyTypeRestClient = anyTypeRestClient;
70          this.anyTypeClassRestClient = anyTypeClassRestClient;
71      }
72  
73      @Override
74      protected WizardModel buildModelSteps(final TypeExtensionTO modelObject, final WizardModel wizardModel) {
75          wizardModel.add(new Details(modelObject));
76          return wizardModel;
77      }
78  
79      @Override
80      protected Serializable onApplyInternal(final TypeExtensionTO modelObject) {
81          List<TypeExtensionTO> typeExtensions = groupTO.getTypeExtensions().stream().
82                  filter(typeExt -> !typeExt.getAnyType().equals(modelObject.getAnyType())).collect(Collectors.toList());
83          typeExtensions.add(modelObject);
84          groupTO.getTypeExtensions().clear();
85          groupTO.getTypeExtensions().addAll(typeExtensions);
86          return groupTO;
87      }
88  
89      public class Details extends WizardStep {
90  
91          private static final long serialVersionUID = 6472869166547883903L;
92  
93          public Details(final TypeExtensionTO typeExtensionTO) {
94              super();
95              setOutputMarkupId(true);
96  
97              add(new Label("anyType.label", anyTypeLabel));
98  
99              if (typeExtensionTO.getAnyType() == null) {
100                 List<String> anyTypes = anyTypeRestClient.list();
101                 anyTypes.remove(AnyTypeKind.GROUP.name());
102                 anyTypes.removeAll(anyTypes.stream().
103                         filter(anyType -> groupTO.getTypeExtension(anyType).isPresent()).collect(Collectors.toList()));
104 
105                 AjaxDropDownChoicePanel<String> anyTypeComponent = new AjaxDropDownChoicePanel<>(
106                         "anyType.component", "anyType", new PropertyModel<>(typeExtensionTO, "anyType"));
107                 anyTypeComponent.setChoices(anyTypes);
108                 anyTypeComponent.addRequiredLabel();
109                 add(anyTypeComponent.hideLabel().setOutputMarkupId(true));
110             } else {
111                 AjaxTextFieldPanel anyTypeComponent = new AjaxTextFieldPanel(
112                         "anyType.component", "anyType", new PropertyModel<>(typeExtensionTO, "anyType"));
113                 anyTypeComponent.setEnabled(false);
114                 add(anyTypeComponent.hideLabel());
115             }
116 
117             add(new Label("auxClasses.label", auxClassesLabel));
118 
119             List<String> anyTypeClasses = anyTypeClassRestClient.list().stream().
120                     map(AnyTypeClassTO::getKey).collect(Collectors.toList());
121             AjaxPalettePanel<String> auxClassesPalette = new AjaxPalettePanel.Builder<String>().build(
122                     "auxClasses.palette",
123                     new PropertyModel<>(typeExtensionTO, "auxClasses"),
124                     new ListModel<>(anyTypeClasses));
125             add(auxClassesPalette.hideLabel().setOutputMarkupId(true));
126         }
127     }
128 }