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.panels;
20  
21  import java.util.ArrayList;
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.rest.SchemaRestClient;
27  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
29  import org.apache.syncope.common.lib.to.AnyTypeClassTO;
30  import org.apache.syncope.common.lib.to.AnyTypeTO;
31  import org.apache.syncope.common.lib.to.PlainSchemaTO;
32  import org.apache.syncope.common.lib.to.Provision;
33  import org.apache.syncope.common.lib.types.SchemaType;
34  import org.apache.wicket.markup.html.panel.Panel;
35  import org.apache.wicket.model.IModel;
36  import org.apache.wicket.model.PropertyModel;
37  import org.apache.wicket.model.ResourceModel;
38  import org.apache.wicket.model.util.ListModel;
39  import org.apache.wicket.spring.injection.annot.SpringBean;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  public class ProvisionAuxClassesPanel extends Panel {
44  
45      private static final long serialVersionUID = -3962956154520358784L;
46  
47      private static final Logger LOG = LoggerFactory.getLogger(ProvisionAuxClassesPanel.class);
48  
49      @SpringBean
50      protected AnyTypeRestClient anyTypeRestClient;
51  
52      @SpringBean
53      protected AnyTypeClassRestClient anyTypeClassRestClient;
54  
55      @SpringBean
56      protected SchemaRestClient schemaRestClient;
57  
58      protected final Provision provision;
59  
60      public ProvisionAuxClassesPanel(final String id, final Provision provision) {
61          super(id);
62          setOutputMarkupId(true);
63  
64          this.provision = provision;
65      }
66  
67      @Override
68      protected void onBeforeRender() {
69          super.onBeforeRender();
70  
71          AnyTypeTO anyType = null;
72          IModel<List<String>> model;
73          List<String> choices;
74          if (provision == null) {
75              model = new ListModel<>(List.of());
76              choices = List.of();
77          } else {
78              model = new PropertyModel<>(provision, "auxClasses");
79              choices = new ArrayList<>();
80  
81              try {
82                  anyType = anyTypeRestClient.read(provision.getAnyType());
83              } catch (Exception e) {
84                  LOG.error("Could not read AnyType {}", provision.getAnyType(), e);
85              }
86              if (anyType != null) {
87                  for (AnyTypeClassTO aux : anyTypeClassRestClient.list()) {
88                      if (!anyType.getClasses().contains(aux.getKey())) {
89                          choices.add(aux.getKey());
90                      }
91                  }
92              }
93          }
94  
95          addOrReplace(
96                  new AjaxPalettePanel.Builder<String>().build("auxClasses", model, new ListModel<>(choices)).
97                          hideLabel().
98                          setOutputMarkupId(true).
99                          setEnabled(provision != null));
100 
101         AjaxTextFieldPanel uidOnCreate = new AjaxTextFieldPanel(
102                 "uidOnCreate", new ResourceModel("uidOnCreate", "uidOnCreate").getObject(),
103                 new PropertyModel<>(provision, "uidOnCreate"));
104         uidOnCreate.setChoices(getSchemas(anyType, model.getObject()));
105         uidOnCreate.setOutputMarkupId(true).setEnabled(provision != null);
106         addOrReplace(uidOnCreate);
107     }
108 
109     protected List<String> getSchemas(final AnyTypeTO anyType, final List<String> anyTypeClasses) {
110         List<String> classes = new ArrayList<>(anyType.getClasses());
111         classes.addAll(anyTypeClasses);
112 
113         return schemaRestClient.<PlainSchemaTO>getSchemas(
114                 SchemaType.PLAIN, null, classes.toArray(String[]::new)).
115                 stream().map(PlainSchemaTO::getKey).collect(Collectors.toList());
116     }
117 }