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.wizards;
20  
21  import java.util.ArrayList;
22  import org.apache.syncope.client.console.panels.search.AnySelectionDirectoryPanel;
23  import org.apache.syncope.client.console.panels.search.SearchClausePanel;
24  import org.apache.syncope.client.console.panels.search.SearchUtils;
25  import org.apache.syncope.client.console.panels.search.UserSearchPanel;
26  import org.apache.syncope.client.console.panels.search.UserSelectionDirectoryPanel;
27  import org.apache.syncope.client.console.rest.AnyTypeClassRestClient;
28  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
29  import org.apache.syncope.client.console.rest.UserRestClient;
30  import org.apache.syncope.client.lib.SyncopeClient;
31  import org.apache.syncope.common.lib.to.AnyTypeTO;
32  import org.apache.syncope.common.lib.to.UserTO;
33  import org.apache.syncope.common.lib.types.AnyTypeKind;
34  import org.apache.wicket.Component;
35  import org.apache.wicket.PageReference;
36  import org.apache.wicket.ajax.AjaxRequestTarget;
37  import org.apache.wicket.event.IEvent;
38  import org.apache.wicket.extensions.wizard.WizardStep;
39  import org.apache.wicket.model.IModel;
40  import org.apache.wicket.model.util.ListModel;
41  import org.apache.wicket.spring.injection.annot.SpringBean;
42  
43  public class UserSelectionWizardStep extends WizardStep {
44  
45      private static final long serialVersionUID = 36221031226727L;
46  
47      @SpringBean
48      protected AnyTypeRestClient anyTypeRestClient;
49  
50      @SpringBean
51      protected AnyTypeClassRestClient anyTypeClassRestClient;
52  
53      @SpringBean
54      protected UserRestClient userRestClient;
55  
56      protected final IModel<String> model;
57  
58      protected final UserSearchPanel userSearchPanel;
59  
60      protected final UserSelectionDirectoryPanel userDirectoryPanel;
61  
62      public UserSelectionWizardStep(
63              final IModel<String> title, final IModel<String> model, final PageReference pageRef) {
64  
65          super();
66          setOutputMarkupId(true);
67  
68          this.model = model;
69          setTitleModel(title);
70  
71          userSearchPanel = UserSearchPanel.class.cast(new UserSearchPanel.Builder(
72                  new ListModel<>(new ArrayList<>()), pageRef).required(false).enableSearch(UserSelectionWizardStep.this).
73                  build("usersearch"));
74          add(userSearchPanel);
75  
76          AnyTypeTO anyTypeTO = anyTypeRestClient.read(AnyTypeKind.USER.name());
77          userDirectoryPanel = UserSelectionDirectoryPanel.class.cast(new UserSelectionDirectoryPanel.Builder(
78                  anyTypeClassRestClient.list(anyTypeTO.getClasses()), userRestClient, anyTypeTO.getKey(), pageRef).
79                  build("searchResult"));
80          add(userDirectoryPanel);
81      }
82  
83      @Override
84      public void onEvent(final IEvent<?> event) {
85          if (event.getPayload() instanceof SearchClausePanel.SearchEvent) {
86              AjaxRequestTarget target = SearchClausePanel.SearchEvent.class.cast(event.getPayload()).getTarget();
87              String fiql = SearchUtils.buildFIQL(
88                      userSearchPanel.getModel().getObject(), SyncopeClient.getUserSearchConditionBuilder());
89              userDirectoryPanel.search(fiql, target);
90          } else if (event.getPayload() instanceof AnySelectionDirectoryPanel.ItemSelection) {
91              @SuppressWarnings("unchecked")
92              AnySelectionDirectoryPanel.ItemSelection<UserTO> payload =
93                      (AnySelectionDirectoryPanel.ItemSelection<UserTO>) event.getPayload();
94  
95              UserTO selected = payload.getSelection();
96              this.model.setObject(selected.getKey());
97  
98              String tableId = ((Component) event.getSource()).
99                      get("container:content:searchContainer:resultTable:tablePanel:groupForm:checkgroup:dataTable").
100                     getMarkupId();
101             String js = "$('#" + tableId + " tr').removeClass('active');";
102             js += "$('#" + tableId + " td[title=" + selected.getKey() + "]').parent().addClass('active');";
103             payload.getTarget().prependJavaScript(js);
104         }
105     }
106 }