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          if (createFlag && resourceDetailsPanel.getConnector() != null) {
93              resourceDetailsPanel.getConnector().getField().add(
94                      new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
95  
96                  private static final long serialVersionUID = 4600298808455564695L;
97  
98                  @Override
99                  protected void onUpdate(final AjaxRequestTarget target) {
100                     resourceTO.setConnector(resourceDetailsPanel.getConnector().getModelObject());
101 
102                     LoadableDetachableModel<List<ConnConfProperty>> model = new LoadableDetachableModel<>() {
103 
104                         private static final long serialVersionUID = -2965284931860212687L;
105 
106                         @Override
107                         protected List<ConnConfProperty> load() {
108                             List<ConnConfProperty> confOverride =
109                                     resourceConnConfPanel.getConnProperties(resourceTO);
110                             resourceTO.getConfOverride().clear();
111                             resourceTO.getConfOverride().addAll(confOverride);
112 
113                             return new PropertyModel<List<ConnConfProperty>>(modelObject, "confOverride") {
114 
115                                 private static final long serialVersionUID = -7809699384012595307L;
116 
117                                 @Override
118                                 public List<ConnConfProperty> getObject() {
119                                     List<ConnConfProperty> res = new ArrayList<>(super.getObject());
120 
121                                     // re-order properties
122                                     Collections.sort(res, (left, right) -> {
123                                         if (left == null) {
124                                             return -1;
125                                         } else {
126                                             return left.compareTo(right);
127                                         }
128                                     });
129 
130                                     return res;
131                                 }
132                             }.getObject();
133                         }
134                     };
135                     resourceConnConfPanel.setConfPropertyListView(model, true);
136                     target.add(resourceConnConfPanel.getCheck().setVisible(true).setEnabled(true));
137                 }
138             });
139         }
140         wizardModel.add(resourceDetailsPanel);
141         wizardModel.add(resourceConnConfPanel);
142         if (resourceTO.getConnector() != null) {
143             wizardModel.add(new ResourceConnCapabilitiesPanel(
144                     resourceTO, connectorRestClient.read(resourceTO.getConnector()).getCapabilities()));
145         } else {
146             wizardModel.add(new ResourceConnCapabilitiesPanel(resourceTO, Collections.emptySet()));
147         }
148 
149         wizardModel.add(new ResourceSecurityPanel(resourceTO));
150         return wizardModel;
151     }
152 
153     @Override
154     protected ResourceTO onApplyInternal(final Serializable modelObject) {
155         ResourceTO resourceTO = (ResourceTO) modelObject;
156         if (createFlag) {
157             resourceTO = resourceRestClient.create(resourceTO);
158         } else {
159             resourceRestClient.update(resourceTO);
160         }
161         return resourceTO;
162     }
163 
164     @Override
165     protected Serializable getCreateCustomPayloadEvent(final Serializable afterObject, final AjaxRequestTarget target) {
166         final ResourceTO actual = ResourceTO.class.cast(afterObject);
167         return new CreateEvent(
168                 actual.getKey(),
169                 actual.getKey(),
170                 TopologyNode.Kind.RESOURCE,
171                 actual.getConnector(),
172                 target);
173     }
174 }