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.List;
22  import java.util.stream.Collectors;
23  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
24  import org.apache.syncope.client.console.status.ReconTaskPanel;
25  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
26  import org.apache.syncope.client.ui.commons.Constants;
27  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
28  import org.apache.syncope.client.ui.commons.panels.ModalPanel;
29  import org.apache.syncope.common.lib.SyncopeConstants;
30  import org.apache.syncope.common.lib.to.ConnObject;
31  import org.apache.syncope.common.lib.to.Provision;
32  import org.apache.syncope.common.lib.to.PullTaskTO;
33  import org.apache.syncope.common.lib.to.ResourceTO;
34  import org.apache.syncope.common.lib.types.AnyTypeKind;
35  import org.apache.wicket.PageReference;
36  import org.apache.wicket.ajax.AjaxRequestTarget;
37  import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
38  import org.apache.wicket.markup.html.panel.Panel;
39  import org.apache.wicket.model.Model;
40  import org.apache.wicket.model.StringResourceModel;
41  
42  public class ConnObjects extends Panel implements ModalPanel {
43  
44      private static final long serialVersionUID = -1143512993584984838L;
45  
46      private final AjaxDropDownChoicePanel<String> anyTypes;
47  
48      private final MultilevelPanel connObjects;
49  
50      public ConnObjects(final ResourceTO resource, final PageReference pageRef) {
51          super(BaseModal.CONTENT_ID);
52  
53          List<String> availableAnyTypes = resource.getProvisions().stream().
54                  map(Provision::getAnyType).
55                  sorted(AnyTypeRestClient.KEY_COMPARATOR).
56                  collect(Collectors.toList());
57          if (resource.getOrgUnit() != null) {
58              availableAnyTypes.add(0, SyncopeConstants.REALM_ANYTYPE);
59          }
60  
61          anyTypes = new AjaxDropDownChoicePanel<>("anyTypes", "anyTypes", new Model<>());
62          anyTypes.setChoices(availableAnyTypes);
63          anyTypes.hideLabel();
64          anyTypes.setNullValid(false);
65          if (availableAnyTypes.contains(AnyTypeKind.USER.name())) {
66              anyTypes.setDefaultModelObject(AnyTypeKind.USER.name());
67          } else if (availableAnyTypes.contains(AnyTypeKind.GROUP.name())) {
68              anyTypes.setDefaultModelObject(AnyTypeKind.GROUP.name());
69          } else if (!availableAnyTypes.isEmpty()) {
70              anyTypes.setDefaultModelObject(availableAnyTypes.get(0));
71          }
72          add(anyTypes);
73  
74          connObjects = new MultilevelPanel("connObjects") {
75  
76              private static final long serialVersionUID = 1473786800290434002L;
77  
78              @Override
79              public void prev(final AjaxRequestTarget target) {
80                  anyTypes.setEnabled(true);
81                  target.add(anyTypes);
82  
83                  super.prev(target);
84              }
85  
86          };
87          connObjects.setFirstLevel(new NextableConnObjectDirectoryPanel(
88                  resource, anyTypes.getField().getModelObject(), pageRef));
89          connObjects.setOutputMarkupId(true);
90          add(connObjects);
91  
92          anyTypes.getField().add(new AjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
93  
94              private static final long serialVersionUID = -1107858522700306810L;
95  
96              @Override
97              protected void onUpdate(final AjaxRequestTarget target) {
98                  connObjects.setFirstLevel(new NextableConnObjectDirectoryPanel(
99                          resource, anyTypes.getField().getModelObject(), pageRef));
100                 target.add(connObjects);
101             }
102         });
103     }
104 
105     private class NextableConnObjectDirectoryPanel extends ConnObjectListViewPanel {
106 
107         private static final long serialVersionUID = 956427874406567048L;
108 
109         NextableConnObjectDirectoryPanel(
110                 final ResourceTO resource,
111                 final String anyType,
112                 final PageReference pageRef) {
113 
114             super(MultilevelPanel.FIRST_LEVEL_ID, resource, anyType, pageRef);
115         }
116 
117         @Override
118         protected void viewConnObject(final ConnObject connObjectTO, final AjaxRequestTarget target) {
119             anyTypes.setEnabled(false);
120             target.add(anyTypes);
121 
122             connObjects.next(
123                     new StringResourceModel("connObject.view", this, new Model<>(connObjectTO)).getObject(),
124                     new ConnObjectDetails(connObjectTO),
125                     target);
126         }
127 
128         @Override
129         protected void pullConnObject(
130                 final String fiql,
131                 final AjaxRequestTarget target,
132                 final String resource,
133                 final String anyType,
134                 final String realm,
135                 final boolean isOnSyncope,
136                 final PageReference pageRef) {
137 
138             anyTypes.setEnabled(false);
139             target.add(anyTypes);
140 
141             final PullTaskTO pullTaskTO = new PullTaskTO();
142             pullTaskTO.setDestinationRealm(realm);
143             connObjects.next("PULL " + resource,
144                     new ReconTaskPanel(
145                             resource,
146                             pullTaskTO,
147                             anyType,
148                             null,
149                             fiql,
150                             isOnSyncope,
151                             connObjects,
152                             pageRef),
153                     target
154             );
155         }
156     }
157 }