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.io.Serializable;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.pages.BasePage;
26  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
30  import org.apache.syncope.common.lib.SyncopeClientException;
31  import org.apache.syncope.common.lib.to.GroupTO;
32  import org.apache.syncope.common.lib.types.AnyTypeKind;
33  import org.apache.wicket.PageReference;
34  import org.apache.wicket.ajax.AjaxRequestTarget;
35  import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
36  import org.apache.wicket.markup.html.form.Form;
37  import org.apache.wicket.markup.html.form.IChoiceRenderer;
38  import org.apache.wicket.model.LoadableDetachableModel;
39  import org.apache.wicket.model.Model;
40  import org.apache.wicket.model.ResourceModel;
41  import org.apache.wicket.spring.injection.annot.SpringBean;
42  
43  public abstract class MembersTogglePanel extends TogglePanel<Serializable> {
44  
45      private static final long serialVersionUID = -3195479265440591519L;
46  
47      @SpringBean
48      protected AnyTypeRestClient anyTypeRestClient;
49  
50      protected GroupTO groupTO;
51  
52      protected final Form<?> form;
53  
54      protected final Model<String> typeModel = new Model<>();
55  
56      private final LoadableDetachableModel<List<String>> anyTypes = new LoadableDetachableModel<>() {
57  
58          private static final long serialVersionUID = 5275935387613157437L;
59  
60          @Override
61          protected List<String> load() {
62              return anyTypeRestClient.list().stream().
63                      filter(anyType -> !AnyTypeKind.GROUP.name().equals(anyType)).collect(Collectors.toList());
64          }
65      };
66  
67      public MembersTogglePanel(final PageReference pageRef) {
68          super(Constants.OUTER, "groupMembers", pageRef);
69  
70          form = new Form<>("membersForm");
71          addInnerObject(form);
72  
73          FieldPanel<String> type = new AjaxDropDownChoicePanel<>("type", "type", typeModel, false).
74                  setChoices(anyTypes).
75                  setChoiceRenderer(new IChoiceRenderer<String>() {
76  
77                      private static final long serialVersionUID = -200150326532439794L;
78  
79                      @Override
80                      public Object getDisplayValue(final String anyType) {
81                          return new ResourceModel("anyType." + anyType, anyType).getObject();
82                      }
83                  }).
84                  setStyleSheet("form-control").
85                  setRequired(true);
86  
87          type.hideLabel();
88          form.add(type);
89  
90          form.add(new AjaxSubmitLink("changeit", form) {
91  
92              private static final long serialVersionUID = -7978723352517770644L;
93  
94              @Override
95              protected void onSubmit(final AjaxRequestTarget target) {
96                  try {
97                      onApplyInternal(groupTO, typeModel.getObject(), target);
98                      toggle(target, false);
99                  } catch (SyncopeClientException e) {
100                     LOG.error("While inspecting group memebers of type {}", typeModel.getObject(), e);
101                     SyncopeConsoleSession.get().onException(e);
102                 }
103                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
104             }
105 
106             @Override
107             protected void onError(final AjaxRequestTarget target) {
108                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
109             }
110         });
111     }
112 
113     protected abstract Serializable onApplyInternal(
114             GroupTO groupTO, String type, AjaxRequestTarget target);
115 
116     public void setTargetObject(final GroupTO groupTO) {
117         this.groupTO = groupTO;
118     }
119 }