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 org.apache.commons.lang3.StringUtils;
23  import org.apache.syncope.client.console.SyncopeConsoleSession;
24  import org.apache.syncope.client.console.pages.BasePage;
25  import org.apache.syncope.client.console.panels.search.AnyObjectSearchPanel;
26  import org.apache.syncope.client.console.panels.search.GroupSearchPanel;
27  import org.apache.syncope.client.console.panels.search.MapOfListModel;
28  import org.apache.syncope.client.console.panels.search.UserSearchPanel;
29  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
30  import org.apache.syncope.client.console.rest.DynRealmRestClient;
31  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
32  import org.apache.syncope.client.console.wizards.DynRealmWrapper;
33  import org.apache.syncope.client.ui.commons.Constants;
34  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
35  import org.apache.syncope.client.ui.commons.wicket.markup.html.bootstrap.tabs.Accordion;
36  import org.apache.syncope.common.lib.to.AnyTypeTO;
37  import org.apache.wicket.PageReference;
38  import org.apache.wicket.ajax.AjaxRequestTarget;
39  import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
40  import org.apache.wicket.markup.html.list.ListItem;
41  import org.apache.wicket.markup.html.list.ListView;
42  import org.apache.wicket.markup.html.panel.Panel;
43  import org.apache.wicket.model.LoadableDetachableModel;
44  import org.apache.wicket.model.Model;
45  import org.apache.wicket.model.PropertyModel;
46  import org.apache.wicket.spring.injection.annot.SpringBean;
47  
48  public class DynRealmModalPanel extends AbstractModalPanel<DynRealmWrapper> {
49  
50      private static final long serialVersionUID = -3773196441177699452L;
51  
52      @SpringBean
53      protected AnyTypeRestClient anyTypeRestClient;
54  
55      @SpringBean
56      protected DynRealmRestClient dynRealmRestClient;
57  
58      protected final DynRealmWrapper dynRealmWrapper;
59  
60      protected final boolean create;
61  
62      public DynRealmModalPanel(
63              final DynRealmWrapper dynRealmWrapper,
64              final boolean create,
65              final BaseModal<DynRealmWrapper> modal,
66              final PageReference pageRef) {
67  
68          super(modal, pageRef);
69          this.dynRealmWrapper = dynRealmWrapper;
70          this.create = create;
71          modal.setFormModel(dynRealmWrapper);
72  
73          AjaxTextFieldPanel key = new AjaxTextFieldPanel(
74                  Constants.KEY_FIELD_NAME,
75                  Constants.KEY_FIELD_NAME,
76                  new PropertyModel<>(dynRealmWrapper.getInnerObject(), Constants.KEY_FIELD_NAME), false);
77          key.setReadOnly(!create);
78          key.setRequired(true);
79          add(key);
80  
81          final LoadableDetachableModel<List<AnyTypeTO>> types = new LoadableDetachableModel<>() {
82  
83              private static final long serialVersionUID = 5275935387613157437L;
84  
85              @Override
86              protected List<AnyTypeTO> load() {
87                  return anyTypeRestClient.listAnyTypes();
88              }
89          };
90  
91          add(new ListView<>("dynMembershipCond", types) {
92  
93              private static final long serialVersionUID = 9101744072914090143L;
94  
95              @Override
96              protected void populateItem(final ListItem<AnyTypeTO> item) {
97                  final String key = item.getModelObject().getKey();
98                  item.add(new Accordion("dynMembershipCond", List.of(
99                          new AbstractTab(Model.of(key + " Dynamic Condition")) {
100 
101                     private static final long serialVersionUID = 1037272333056449378L;
102 
103                     @Override
104                     public Panel getPanel(final String panelId) {
105                         switch (item.getModelObject().getKind()) {
106                             case USER:
107                                 return new UserSearchPanel.Builder(
108                                         new MapOfListModel<>(dynRealmWrapper, "dynClauses", key), pageRef).
109                                         required(false).build(panelId);
110 
111                             case GROUP:
112                                 return new GroupSearchPanel.Builder(
113                                         new MapOfListModel<>(dynRealmWrapper, "dynClauses", key), pageRef).
114                                         required(false).build(panelId);
115 
116                             case ANY_OBJECT:
117                             default:
118                                 return new AnyObjectSearchPanel.Builder(
119                                         key,
120                                         new MapOfListModel<>(dynRealmWrapper, "dynClauses", key), pageRef).
121                                         required(false).build(panelId);
122                         }
123                     }
124                 }), Model.of(StringUtils.isBlank(dynRealmWrapper.getDynMembershipConds().get(key)) ? -1 : 0)).
125                         setOutputMarkupId(true));
126             }
127         });
128     }
129 
130     @Override
131     public DynRealmWrapper getItem() {
132         return dynRealmWrapper;
133     }
134 
135     @Override
136     public void onSubmit(final AjaxRequestTarget target) {
137         try {
138             dynRealmWrapper.fillDynamicConditions();
139             if (create) {
140                 dynRealmRestClient.create(dynRealmWrapper.getInnerObject());
141             } else {
142                 dynRealmRestClient.update(dynRealmWrapper.getInnerObject());
143             }
144             SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
145             this.modal.close(target);
146         } catch (Exception e) {
147             LOG.error("While creating/updating dynamic realm", e);
148             SyncopeConsoleSession.get().onException(e);
149         }
150         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
151     }
152 }