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.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import org.apache.syncope.client.console.rest.ConnectorRestClient;
26  import org.apache.syncope.common.lib.to.ResourceTO;
27  import org.apache.syncope.common.lib.types.ConnConfProperty;
28  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
29  import org.apache.wicket.model.LoadableDetachableModel;
30  import org.apache.wicket.model.PropertyModel;
31  import org.apache.wicket.spring.injection.annot.SpringBean;
32  
33  public abstract class ResourceConnConfPanel extends AbstractConnConfPanel<ResourceTO> {
34  
35      private static final long serialVersionUID = -7982691107029848579L;
36  
37      @SpringBean
38      protected ConnectorRestClient connectorRestClient;
39  
40      public ResourceConnConfPanel(final ResourceTO resourceTO) {
41          super(resourceTO);
42  
43          model = new LoadableDetachableModel<>() {
44  
45              private static final long serialVersionUID = -2965284931860212687L;
46  
47              @Override
48              protected List<ConnConfProperty> load() {
49                  List<ConnConfProperty> confOverride = getConnProperties(resourceTO);
50                  resourceTO.getConfOverride().clear();
51                  resourceTO.getConfOverride().addAll(confOverride);
52  
53                  return new PropertyModel<List<ConnConfProperty>>(modelObject, "confOverride") {
54  
55                      private static final long serialVersionUID = -7809699384012595307L;
56  
57                      @Override
58                      public List<ConnConfProperty> getObject() {
59                          List<ConnConfProperty> res = new ArrayList<>(super.getObject());
60  
61                          // re-order properties
62                          res.sort((left, right) -> {
63                              if (left == null) {
64                                  return -1;
65                              } else {
66                                  return left.compareTo(right);
67                              }
68                          });
69  
70                          return res;
71                      }
72                  }.getObject();
73              }
74          };
75  
76          setConfPropertyListView(model, true);
77  
78          check.setEnabled(!model.getObject().isEmpty());
79          check.setVisible(!model.getObject().isEmpty());
80      }
81  
82      /**
83       * Get overridable properties.
84       *
85       * @param resourceTO resource instance.
86       * @return overridable properties.
87       */
88      @Override
89      protected final List<ConnConfProperty> getConnProperties(final ResourceTO resourceTO) {
90          List<ConnConfProperty> props = new ArrayList<>();
91  
92          if (resourceTO.getConnector() != null) {
93              connectorRestClient.read(resourceTO.getConnector()).getConf().stream().
94                      filter(ConnConfProperty::isOverridable).
95                      forEachOrdered(props::add);
96          }
97          if (resourceTO.getConfOverride().isEmpty()) {
98              resourceTO.getConfOverride().clear();
99          } else {
100             Map<String, ConnConfProperty> valuedProps = new HashMap<>();
101             resourceTO.getConfOverride().forEach(prop -> valuedProps.put(prop.getSchema().getName(), prop));
102 
103             for (int i = 0; i < props.size(); i++) {
104                 if (valuedProps.containsKey(props.get(i).getSchema().getName())) {
105                     props.set(i, valuedProps.get(props.get(i).getSchema().getName()));
106                 }
107             }
108         }
109 
110         return props;
111     }
112 
113     public LoadableDetachableModel<List<ConnConfProperty>> getModel() {
114         return model;
115     }
116 
117     public AjaxButton getCheck() {
118         return check;
119     }
120 }