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.any;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import org.apache.syncope.client.console.SyncopeWebApplication;
27  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
28  import org.apache.syncope.client.console.commons.IdMConstants;
29  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
30  import org.apache.syncope.client.console.pages.BasePage;
31  import org.apache.syncope.client.console.panels.DirectoryPanel;
32  import org.apache.syncope.client.console.rest.ResourceRestClient;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
34  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
35  import org.apache.syncope.client.console.wizards.any.MergeLinkedAccountsResourcesPanel.ResourceSelectionDirectoryPanel.ResourcesDataProvider;
36  import org.apache.syncope.common.lib.to.ResourceTO;
37  import org.apache.syncope.common.lib.types.IdMEntitlement;
38  import org.apache.wicket.PageReference;
39  import org.apache.wicket.ajax.AjaxRequestTarget;
40  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
41  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
42  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
43  import org.apache.wicket.extensions.wizard.WizardModel.ICondition;
44  import org.apache.wicket.extensions.wizard.WizardStep;
45  import org.apache.wicket.model.CompoundPropertyModel;
46  import org.apache.wicket.model.IModel;
47  import org.apache.wicket.model.Model;
48  import org.apache.wicket.model.ResourceModel;
49  import org.apache.wicket.model.StringResourceModel;
50  import org.apache.wicket.spring.injection.annot.SpringBean;
51  
52  public class MergeLinkedAccountsResourcesPanel extends WizardStep implements ICondition {
53  
54      private static final long serialVersionUID = 1221037007528732347L;
55  
56      @SpringBean
57      protected ResourceRestClient resourceRestClient;
58  
59      protected final MergeLinkedAccountsWizardModel wizardModel;
60  
61      public MergeLinkedAccountsResourcesPanel(
62              final MergeLinkedAccountsWizardModel wizardModel,
63              final PageReference pageRef) {
64  
65          super();
66          setOutputMarkupId(true);
67          this.wizardModel = wizardModel;
68          add(new ResourceSelectionDirectoryPanel("resources", resourceRestClient, pageRef));
69      }
70  
71      @Override
72      public boolean evaluate() {
73          return SyncopeWebApplication.get().getSecuritySettings().getAuthorizationStrategy().
74                  isActionAuthorized(this, RENDER);
75      }
76  
77      @Override
78      public String getTitle() {
79          setSummaryModel(new StringResourceModel("mergeLinkedAccounts.searchResource.summary",
80                  Model.of(wizardModel.getMergingUser())));
81          setTitleModel(new StringResourceModel("mergeLinkedAccounts.searchResource.title",
82                  Model.of(wizardModel.getMergingUser())));
83          return super.getTitle();
84      }
85  
86      protected class ResourceSelectionDirectoryPanel extends
87              DirectoryPanel<ResourceTO, ResourceTO, ResourcesDataProvider, ResourceRestClient> {
88  
89          private static final long serialVersionUID = 6005711052393825472L;
90  
91          ResourceSelectionDirectoryPanel(
92                  final String id,
93                  final ResourceRestClient restClient,
94                  final PageReference pageRef) {
95  
96              super(id, restClient, pageRef, true);
97  
98              modal.size(Modal.Size.Large);
99              setOutputMarkupId(true);
100             disableCheckBoxes();
101             initResultTable();
102         }
103 
104         @Override
105         protected ResourcesDataProvider dataProvider() {
106             return new ResourcesDataProvider(this.rows);
107         }
108 
109         @Override
110         protected String paginatorRowsKey() {
111             return IdMConstants.PREF_RESOURCES_PAGINATOR_ROWS;
112         }
113 
114         @Override
115         protected List<IColumn<ResourceTO, String>> getColumns() {
116             List<IColumn<ResourceTO, String>> columns = new ArrayList<>();
117             columns.add(new PropertyColumn<>(new ResourceModel("resource"), "key", "key"));
118             return columns;
119         }
120 
121         @Override
122         protected ActionsPanel<ResourceTO> getActions(final IModel<ResourceTO> model) {
123             final ActionsPanel<ResourceTO> panel = super.getActions(model);
124             panel.add(new ActionLink<>() {
125 
126                 private static final long serialVersionUID = -7978723352517770644L;
127 
128                 @Override
129                 public void onClick(final AjaxRequestTarget target, final ResourceTO resource) {
130                     MergeLinkedAccountsWizardModel model = MergeLinkedAccountsResourcesPanel.this.wizardModel;
131                     String connObjectKeyValue = restClient.getConnObjectKeyValue(
132                             resource.getKey(),
133                             model.getMergingUser().getType(),
134                             model.getMergingUser().getKey());
135                     if (connObjectKeyValue != null) {
136                         model.setResource(resource);
137                         String tableId = MergeLinkedAccountsResourcesPanel.this.
138                                 get("resources:container:content:searchContainer:resultTable"
139                                         + ":tablePanel:groupForm:checkgroup:dataTable").
140                                 getMarkupId();
141                         String js = "$('#" + tableId + "').removeClass('active');";
142                         js += "$('#" + tableId + " tbody tr td div').filter(function() "
143                                 + "{return $(this).text() === \"" + resource.getKey() + "\";})"
144                                 + ".parent().parent().addClass('active');";
145                         target.prependJavaScript(js);
146 
147                     } else {
148                         error("Unable to determine connector object key");
149                         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
150                     }
151                 }
152             }, ActionLink.ActionType.SELECT, IdMEntitlement.RESOURCE_READ);
153             return panel;
154         }
155 
156         @Override
157         protected Collection<ActionLink.ActionType> getBatches() {
158             return List.of();
159         }
160 
161         protected final class ResourcesDataProvider extends DirectoryDataProvider<ResourceTO> {
162 
163             private static final long serialVersionUID = -185944053385660794L;
164 
165             private final SortableDataProviderComparator<ResourceTO> comparator;
166 
167             private ResourcesDataProvider(final int paginatorRows) {
168                 super(paginatorRows);
169                 setSort("key", SortOrder.ASCENDING);
170                 comparator = new SortableDataProviderComparator<>(this);
171             }
172 
173             @Override
174             public Iterator<ResourceTO> iterator(final long first, final long count) {
175                 List<ResourceTO> list = restClient.list();
176                 list.sort(comparator);
177                 return list.subList((int) first, (int) first + (int) count).iterator();
178             }
179 
180             @Override
181             public long size() {
182                 return restClient.list().size();
183             }
184 
185             @Override
186             public IModel<ResourceTO> model(final ResourceTO object) {
187                 return new CompoundPropertyModel<>(object);
188             }
189         }
190     }
191 }