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.resources;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.client.console.rest.ConnectorRestClient;
27  import org.apache.syncope.client.console.rest.ResourceRestClient;
28  import org.apache.syncope.client.console.topology.TopologyNode;
29  import org.apache.syncope.client.ui.commons.Constants;
30  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
31  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
32  import org.apache.syncope.common.lib.to.ResourceTO;
33  import org.apache.syncope.common.lib.types.ConnConfProperty;
34  import org.apache.wicket.PageReference;
35  import org.apache.wicket.ajax.AjaxRequestTarget;
36  import org.apache.wicket.extensions.wizard.WizardModel;
37  import org.apache.wicket.markup.ComponentTag;
38  import org.apache.wicket.model.LoadableDetachableModel;
39  import org.apache.wicket.model.PropertyModel;
40  
41  /**
42   * Resource wizard builder.
43   */
44  public class ResourceWizardBuilder extends AbstractResourceWizardBuilder<ResourceTO> {
45  
46      private static final long serialVersionUID = 1734415311027284221L;
47  
48      protected final ResourceRestClient resourceRestClient;
49  
50      protected final ConnectorRestClient connectorRestClient;
51  
52      protected boolean createFlag;
53  
54      public ResourceWizardBuilder(
55              final ResourceTO resourceTO,
56              final ResourceRestClient resourceRestClient,
57              final ConnectorRestClient connectorRestClient,
58              final PageReference pageRef) {
59  
60          super(resourceTO, pageRef);
61  
62          this.resourceRestClient = resourceRestClient;
63          this.connectorRestClient = connectorRestClient;
64      }
65  
66      @Override
67      public AjaxWizard<Serializable> build(final String id, final AjaxWizard.Mode mode) {
68          this.createFlag = mode == AjaxWizard.Mode.CREATE;
69          return super.build(id, mode);
70      }
71  
72      @Override
73      protected WizardModel buildModelSteps(final Serializable modelObject, final WizardModel wizardModel) {
74          ResourceTO resourceTO = ResourceTO.class.cast(modelObject);
75          ResourceDetailsPanel resourceDetailsPanel = new ResourceDetailsPanel(resourceTO, createFlag);
76  
77          ResourceConnConfPanel resourceConnConfPanel = new ResourceConnConfPanel(resourceTO) {
78  
79              private static final long serialVersionUID = -1128269449868933504L;
80  
81              @Override
82              protected Pair<Boolean, String> check(final AjaxRequestTarget target) {
83                  return resourceRestClient.check(modelObject);
84              }
85  
86              @Override
87              protected void onComponentTag(final ComponentTag tag) {
88                  tag.append("class", "scrollable-tab-content", " ");
89              }
90  
91          };
92  
93          if (createFlag && resourceDetailsPanel.getConnector() != null) {
94              resourceDetailsPanel.getConnector().getField().add(
95                      new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
96  
97                  private static final long serialVersionUID = 4600298808455564695L;
98  
99                  @Override
100                 protected void onUpdate(final AjaxRequestTarget target) {
101                     resourceTO.setConnector(resourceDetailsPanel.getConnector().getModelObject());
102 
103                     LoadableDetachableModel<List<ConnConfProperty>> model =
104                             new LoadableDetachableModel<>() {
105 
106                         private static final long serialVersionUID = -2965284931860212687L;
107 
108                         @Override
109                         protected List<ConnConfProperty> load() {
110                             List<ConnConfProperty> confOverride =
111                                     resourceConnConfPanel.getConnProperties(resourceTO);
112                             resourceTO.getConfOverride().clear();
113                             resourceTO.getConfOverride().addAll(confOverride);
114 
115                             return new PropertyModel<List<ConnConfProperty>>(modelObject, "confOverride") {
116 
117                                 private static final long serialVersionUID = -7809699384012595307L;
118 
119                                 @Override
120                                 public List<ConnConfProperty> getObject() {
121                                     List<ConnConfProperty> res = new ArrayList<>(super.getObject());
122 
123                                     // re-order properties
124                                     Collections.sort(res, (left, right) -> {
125                                         if (left == null) {
126                                             return -1;
127                                         } else {
128                                             return left.compareTo(right);
129                                         }
130                                     });
131 
132                                     return res;
133                                 }
134                             }.getObject();
135                         }
136                     };
137                     resourceConnConfPanel.setConfPropertyListView(model, true);
138                     target.add(resourceConnConfPanel.getCheck().setVisible(true).setEnabled(true));
139                 }
140             });
141         }
142         wizardModel.add(resourceDetailsPanel);
143         wizardModel.add(resourceConnConfPanel);
144         if (resourceTO.getConnector() != null) {
145             wizardModel.add(new ResourceConnCapabilitiesPanel(
146                     resourceTO, connectorRestClient.read(resourceTO.getConnector()).getCapabilities()));
147         } else {
148             wizardModel.add(new ResourceConnCapabilitiesPanel(resourceTO, Collections.emptySet()));
149         }
150 
151         wizardModel.add(new ResourceSecurityPanel(resourceTO));
152         return wizardModel;
153     }
154 
155     @Override
156     protected ResourceTO onApplyInternal(final Serializable modelObject) {
157         ResourceTO resourceTO = (ResourceTO) modelObject;
158         if (createFlag) {
159             resourceTO = resourceRestClient.create(resourceTO);
160         } else {
161             resourceRestClient.update(resourceTO);
162         }
163         return resourceTO;
164     }
165 
166     @Override
167     protected Serializable getCreateCustomPayloadEvent(final Serializable afterObject, final AjaxRequestTarget target) {
168         final ResourceTO actual = ResourceTO.class.cast(afterObject);
169         return new CreateEvent(
170                 actual.getKey(),
171                 actual.getKey(),
172                 TopologyNode.Kind.RESOURCE,
173                 actual.getConnector(),
174                 target);
175     }
176 }