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.clientapps;
20  
21  import java.util.List;
22  import java.util.Optional;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.SyncopeWebApplication;
26  import org.apache.syncope.client.console.panels.AbstractModalPanel;
27  import org.apache.syncope.client.console.panels.BeanPanel;
28  import org.apache.syncope.client.console.rest.ClientAppRestClient;
29  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
30  import org.apache.syncope.client.ui.commons.Constants;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
32  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
33  import org.apache.syncope.client.ui.commons.panels.WizardModalPanel;
34  import org.apache.syncope.client.ui.commons.wizards.AbstractModalPanelBuilder;
35  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
36  import org.apache.syncope.common.lib.clientapps.UsernameAttributeProviderConf;
37  import org.apache.syncope.common.lib.to.ClientAppTO;
38  import org.apache.syncope.common.lib.types.ClientAppType;
39  import org.apache.wicket.PageReference;
40  import org.apache.wicket.ajax.AjaxEventBehavior;
41  import org.apache.wicket.ajax.AjaxRequestTarget;
42  import org.apache.wicket.model.LoadableDetachableModel;
43  import org.apache.wicket.model.Model;
44  import org.apache.wicket.model.PropertyModel;
45  import org.springframework.util.ClassUtils;
46  
47  public class UsernameAttributeProviderModalPanelBuilder<T extends ClientAppTO> extends AbstractModalPanelBuilder<T> {
48  
49      private static final long serialVersionUID = -4106998301911573852L;
50  
51      protected final LoadableDetachableModel<List<String>> usernameAttributeProviderConfs =
52              new LoadableDetachableModel<>() {
53  
54          private static final long serialVersionUID = 5275935387613157437L;
55  
56          @Override
57          protected List<String> load() {
58              return SyncopeWebApplication.get().getLookup().getClasses(UsernameAttributeProviderConf.class).stream().
59                      map(Class::getName).sorted().collect(Collectors.toList());
60          }
61      };
62  
63      protected final BaseModal<T> modal;
64  
65      protected final ClientAppType type;
66  
67      protected final ClientAppRestClient clientAppRestClient;
68  
69      public UsernameAttributeProviderModalPanelBuilder(
70              final ClientAppType type,
71              final T defaultItem,
72              final BaseModal<T> modal,
73              final ClientAppRestClient clientAppRestClient,
74              final PageReference pageRef) {
75  
76          super(defaultItem, pageRef);
77          this.type = type;
78          this.modal = modal;
79          this.clientAppRestClient = clientAppRestClient;
80      }
81  
82      @Override
83      public WizardModalPanel<T> build(final String id, final int index, final AjaxWizard.Mode mode) {
84          return new Profile(newModelObject(), modal, pageRef);
85      }
86  
87      protected class Profile extends AbstractModalPanel<T> {
88  
89          private static final long serialVersionUID = 7647959917047450318L;
90  
91          protected final T clientAppTO;
92  
93          Profile(final T clientAppTO, final BaseModal<T> modal, final PageReference pageRef) {
94              super(modal, pageRef);
95              modal.setFormModel(clientAppTO);
96  
97              this.clientAppTO = clientAppTO;
98  
99              AjaxDropDownChoicePanel<String> conf = new AjaxDropDownChoicePanel<>(
100                     "conf", "conf", new Model<>());
101             Optional.ofNullable(clientAppTO.getUsernameAttributeProviderConf()).
102                     ifPresent(uapc -> conf.setModelObject(uapc.getClass().getName()));
103             conf.setChoices(usernameAttributeProviderConfs.getObject());
104             conf.setNullValid(true);
105             add(conf);
106 
107             PropertyModel<UsernameAttributeProviderConf> beanPanelModel =
108                     new PropertyModel<>(clientAppTO, "usernameAttributeProviderConf");
109             BeanPanel<UsernameAttributeProviderConf> bean = new BeanPanel<>("bean", beanPanelModel, pageRef);
110             add(bean.setRenderBodyOnly(false));
111 
112             conf.add(new AjaxEventBehavior(Constants.ON_CHANGE) {
113 
114                 private static final long serialVersionUID = -7133385027739964990L;
115 
116                 @SuppressWarnings("unchecked")
117                 @Override
118                 protected void onEvent(final AjaxRequestTarget target) {
119                     if (conf.getModelObject() == null) {
120                         beanPanelModel.setObject(null);
121                     } else {
122                         try {
123                             Class<? extends UsernameAttributeProviderConf> clazz =
124                                     (Class<? extends UsernameAttributeProviderConf>) ClassUtils.resolveClassName(
125                                             conf.getModelObject(), ClassUtils.getDefaultClassLoader());
126 
127                             beanPanelModel.setObject(clazz.getConstructor().newInstance());
128                         } catch (Exception e) {
129                             LOG.error("Cannot instantiate {}", conf.getModelObject(), e);
130                         }
131                     }
132 
133                     target.add(bean);
134                 }
135             });
136         }
137 
138         @Override
139         public void onSubmit(final AjaxRequestTarget target) {
140             try {
141                 clientAppRestClient.update(type, clientAppTO);
142 
143                 SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
144                 UsernameAttributeProviderModalPanelBuilder.Profile.this.modal.close(target);
145             } catch (Exception e) {
146                 LOG.error("While creating/updating clientApp", e);
147                 SyncopeConsoleSession.get().onException(e);
148             }
149             ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
150         }
151     }
152 }