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.Iterator;
22  import java.util.List;
23  import java.util.Optional;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.client.console.SyncopeWebApplication;
27  import org.apache.syncope.client.console.commons.RealmsUtils;
28  import org.apache.syncope.client.console.rest.RealmRestClient;
29  import org.apache.syncope.client.console.wicket.markup.html.form.AjaxSearchFieldPanel;
30  import org.apache.syncope.client.ui.commons.Constants;
31  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
32  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
33  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxSpinnerFieldPanel;
34  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
35  import org.apache.syncope.common.lib.to.ConnIdBundle;
36  import org.apache.syncope.common.lib.to.ConnInstanceTO;
37  import org.apache.syncope.common.lib.to.ConnPoolConfTO;
38  import org.apache.syncope.common.lib.to.RealmTO;
39  import org.apache.wicket.ajax.AjaxRequestTarget;
40  import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
41  import org.apache.wicket.extensions.wizard.WizardStep;
42  import org.apache.wicket.model.PropertyModel;
43  import org.apache.wicket.spring.injection.annot.SpringBean;
44  
45  public class ConnectorDetailsPanel extends WizardStep {
46  
47      private static final long serialVersionUID = -2435937897614232137L;
48  
49      @SpringBean
50      protected RealmRestClient realmRestClient;
51  
52      public ConnectorDetailsPanel(final ConnInstanceTO connInstanceTO, final List<ConnIdBundle> bundles) {
53          super();
54          setOutputMarkupId(true);
55  
56          boolean fullRealmsTree = SyncopeWebApplication.get().fullRealmsTree(realmRestClient);
57  
58          AutoCompleteSettings settings = new AutoCompleteSettings();
59          settings.setShowCompleteListOnFocusGain(fullRealmsTree);
60          settings.setShowListOnEmptyInput(fullRealmsTree);
61  
62          AjaxSearchFieldPanel realm = new AjaxSearchFieldPanel(
63                  "adminRealm", "adminRealm", new PropertyModel<>(connInstanceTO, "adminRealm"), settings) {
64  
65              private static final long serialVersionUID = -6390474600233486704L;
66  
67              @Override
68              protected Iterator<String> getChoices(final String input) {
69                  return (RealmsUtils.checkInput(input)
70                          ? (realmRestClient.search(fullRealmsTree
71                                  ? RealmsUtils.buildRootQuery()
72                                  : RealmsUtils.buildKeywordQuery(input)).getResult())
73                          : List.<RealmTO>of()).stream().
74                          map(RealmTO::getFullPath).collect(Collectors.toList()).iterator();
75              }
76          };
77          add(realm.addRequiredLabel().setOutputMarkupId(true));
78  
79          AjaxTextFieldPanel displayName = new AjaxTextFieldPanel(
80                  "displayName",
81                  "displayName",
82                  new PropertyModel<>(connInstanceTO, "displayName"), false);
83          add(displayName.addRequiredLabel().setOutputMarkupId(true));
84  
85          AjaxTextFieldPanel location = new AjaxTextFieldPanel(
86                  "location", "location", new PropertyModel<>(connInstanceTO, "location"), false);
87          add(location.addRequiredLabel().setOutputMarkupId(true).setEnabled(false));
88  
89          AjaxDropDownChoicePanel<String> bundleName = new AjaxDropDownChoicePanel<>(
90                  "bundleName",
91                  "bundleName",
92                  new PropertyModel<>(connInstanceTO, "bundleName"), false);
93          bundleName.setEnabled(connInstanceTO.getKey() == null || connInstanceTO.isErrored());
94          bundleName.setChoices(bundles.stream().map(ConnIdBundle::getBundleName).
95                  distinct().sorted().collect(Collectors.toList()));
96          bundleName.getField().setOutputMarkupId(true);
97          add(bundleName.addRequiredLabel().setOutputMarkupId(true));
98  
99          AjaxDropDownChoicePanel<String> connectorName = new AjaxDropDownChoicePanel<>(
100                 "connectorName",
101                 "connectorName",
102                 new PropertyModel<>(connInstanceTO, "connectorName"), false);
103         connectorName.setEnabled(connInstanceTO.getBundleName() == null || connInstanceTO.isErrored());
104         Optional.ofNullable(connInstanceTO.getConnectorName()).ifPresent(v -> connectorName.setChoices(List.of(v)));
105         connectorName.getField().setOutputMarkupId(true);
106         add(connectorName.addRequiredLabel().setOutputMarkupId(true));
107 
108         AjaxDropDownChoicePanel<String> version = new AjaxDropDownChoicePanel<>(
109                 "version", "version", new PropertyModel<>(connInstanceTO, "version"), false);
110         version.setEnabled(connInstanceTO.getConnectorName() == null || connInstanceTO.isErrored());
111         Optional.ofNullable(connInstanceTO.getVersion()).ifPresent(v -> version.setChoices(List.of(v)));
112         version.getField().setOutputMarkupId(true);
113         add(version.addRequiredLabel().setOutputMarkupId(true));
114 
115         bundleName.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
116 
117             private static final long serialVersionUID = -1107858522700306810L;
118 
119             @Override
120             protected void onUpdate(final AjaxRequestTarget target) {
121                 connectorName.setEnabled(true);
122 
123                 List<Pair<String, String>> connectors = bundles.stream().
124                         filter(bundle -> bundle.getBundleName().equals(connInstanceTO.getBundleName())).
125                         map(bundle -> Pair.of(bundle.getConnectorName(), bundle.getVersion())).
126                         collect(Collectors.toList());
127                 if (connectors.size() == 1) {
128                     Pair<String, String> entry = connectors.get(0);
129 
130                     connInstanceTO.setConnectorName(entry.getLeft());
131                     connectorName.getField().setModelObject(entry.getLeft());
132                     connectorName.setChoices(List.of(entry.getLeft()));
133 
134                     connInstanceTO.setVersion(entry.getRight());
135                     version.getField().setModelObject(entry.getRight());
136                     version.setChoices(List.of(entry.getRight()));
137                 } else {
138                     connectorName.setChoices(connectors.stream().
139                             map(Pair::getLeft).distinct().sorted().collect(Collectors.toList()));
140 
141                     List<String> versions = connectors.stream().
142                             map(Pair::getRight).distinct().sorted().collect(Collectors.toList());
143                     version.setChoices(versions);
144 
145                     if (versions.size() == 1) {
146                         connInstanceTO.setVersion(versions.get(0));
147                         version.getField().setModelObject(versions.get(0));
148                     } else {
149                         connInstanceTO.setVersion(null);
150                         version.getField().setModelObject(null);
151                     }
152                 }
153 
154                 target.add(version);
155                 target.add(connectorName);
156             }
157         });
158 
159         connectorName.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
160 
161             private static final long serialVersionUID = -1107858522700306810L;
162 
163             @Override
164             protected void onUpdate(final AjaxRequestTarget target) {
165                 List<String> versions = bundles.stream().
166                         filter(bundle -> bundle.getBundleName().equals(connInstanceTO.getBundleName())
167                         && bundle.getConnectorName().equals(connInstanceTO.getConnectorName())).
168                         map(ConnIdBundle::getVersion).collect(Collectors.toList());
169                 if (versions.size() == 1) {
170                     connInstanceTO.setVersion(versions.get(0));
171                     version.getField().setModelObject(versions.get(0));
172                 }
173                 version.setChoices(versions);
174 
175                 target.add(version);
176             }
177         });
178 
179         if (connInstanceTO.getPoolConf() == null) {
180             connInstanceTO.setPoolConf(new ConnPoolConfTO());
181         }
182 
183         add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).max(Integer.MAX_VALUE).build(
184                 "connRequestTimeout", "connRequestTimeout", Integer.class,
185                 new PropertyModel<>(connInstanceTO, "connRequestTimeout")));
186 
187         add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).max(Integer.MAX_VALUE).build(
188                 "poolMaxObjects", "poolMaxObjects", Integer.class,
189                 new PropertyModel<>(connInstanceTO.getPoolConf(), "maxObjects")));
190 
191         add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).max(Integer.MAX_VALUE).build(
192                 "poolMinIdle", "poolMinIdle", Integer.class,
193                 new PropertyModel<>(connInstanceTO.getPoolConf(), "minIdle")));
194 
195         add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).max(Integer.MAX_VALUE).build(
196                 "poolMaxIdle", "poolMaxIdle", Integer.class,
197                 new PropertyModel<>(connInstanceTO.getPoolConf(), "maxIdle")));
198 
199         add(new AjaxSpinnerFieldPanel.Builder<Long>().min(0L).max(Long.MAX_VALUE).build(
200                 "poolMaxWait", "poolMaxWait", Long.class,
201                 new PropertyModel<>(connInstanceTO.getPoolConf(), "maxWait")));
202 
203         add(new AjaxSpinnerFieldPanel.Builder<Long>().min(0L).max(Long.MAX_VALUE).build(
204                 "poolMinEvictableIdleTime", "poolMinEvictableIdleTime", Long.class,
205                 new PropertyModel<>(connInstanceTO.getPoolConf(), "minEvictableIdleTimeMillis")));
206     }
207 }