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.commons;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Optional;
25  import java.util.stream.Collectors;
26  import org.apache.syncope.client.console.PreferenceManager;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
28  import org.apache.syncope.client.console.pages.BasePage;
29  import org.apache.syncope.client.console.panels.AnyDirectoryPanel;
30  import org.apache.syncope.client.console.panels.DisplayAttributesModalPanel;
31  import org.apache.syncope.client.console.rest.BaseRestClient;
32  import org.apache.syncope.client.console.rest.ImplementationRestClient;
33  import org.apache.syncope.client.console.rest.ReconciliationRestClient;
34  import org.apache.syncope.client.console.wicket.ajax.form.AjaxDownloadBehavior;
35  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
36  import org.apache.syncope.client.console.wizards.CSVPullWizardBuilder;
37  import org.apache.syncope.client.console.wizards.CSVPushWizardBuilder;
38  import org.apache.syncope.client.console.wizards.any.ProvisioningReportsPanel;
39  import org.apache.syncope.client.console.wizards.any.ResultPanel;
40  import org.apache.syncope.client.ui.commons.Constants;
41  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
42  import org.apache.syncope.common.lib.to.ProvisioningReport;
43  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
44  import org.apache.syncope.common.rest.api.beans.AnyQuery;
45  import org.apache.syncope.common.rest.api.beans.CSVPullSpec;
46  import org.apache.syncope.common.rest.api.beans.CSVPushSpec;
47  import org.apache.wicket.Component;
48  import org.apache.wicket.PageReference;
49  import org.apache.wicket.ajax.AjaxRequestTarget;
50  import org.apache.wicket.ajax.markup.html.AjaxLink;
51  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
52  import org.apache.wicket.event.IEvent;
53  import org.apache.wicket.markup.html.WebMarkupContainer;
54  import org.apache.wicket.markup.html.panel.Panel;
55  import org.apache.wicket.model.Model;
56  import org.apache.wicket.model.StringResourceModel;
57  
58  public class IdMAnyDirectoryPanelAdditionalActionsProvider implements AnyDirectoryPanelAdditionalActionsProvider {
59  
60      private static final long serialVersionUID = -6768727277642238924L;
61  
62      protected AjaxLink<Void> csvPushLink;
63  
64      protected AjaxLink<Void> csvPullLink;
65  
66      protected final ReconciliationRestClient reconciliationRestClient;
67  
68      protected final ImplementationRestClient implementationRestClient;
69  
70      public IdMAnyDirectoryPanelAdditionalActionsProvider(
71              final ReconciliationRestClient reconciliationRestClient,
72              final ImplementationRestClient implementationRestClient) {
73  
74          this.reconciliationRestClient = reconciliationRestClient;
75          this.implementationRestClient = implementationRestClient;
76      }
77  
78      @Override
79      public void add(
80              final AnyDirectoryPanel<?, ?> panel,
81              final BaseModal<?> modal,
82              final boolean wizardInModal,
83              final WebMarkupContainer container,
84              final String type,
85              final String realm,
86              final String fiql,
87              final int rows,
88              final List<String> pSchemaNames,
89              final List<String> dSchemaNames,
90              final PageReference pageRef) {
91  
92          AjaxDownloadBehavior csvDownloadBehavior = new AjaxDownloadBehavior();
93          WebMarkupContainer csvEventSink = new WebMarkupContainer(Constants.OUTER) {
94  
95              private static final long serialVersionUID = -957948639666058749L;
96  
97              @Override
98              public void onEvent(final IEvent<?> event) {
99                  if (event.getPayload() instanceof AjaxWizard.NewItemCancelEvent) {
100                     ((AjaxWizard.NewItemCancelEvent<?>) event.getPayload()).getTarget().
101                             ifPresent(modal::close);
102                 } else if (event.getPayload() instanceof AjaxWizard.NewItemFinishEvent) {
103                     AjaxWizard.NewItemFinishEvent<?> payload = (AjaxWizard.NewItemFinishEvent) event.getPayload();
104                     Optional<AjaxRequestTarget> target = payload.getTarget();
105 
106                     if (payload.getResult() instanceof ArrayList) {
107                         modal.setContent(new ResultPanel<>(
108                                 null,
109                                 payload.getResult()) {
110 
111                             private static final long serialVersionUID = -2630573849050255233L;
112 
113                             @Override
114                             protected void closeAction(final AjaxRequestTarget target) {
115                                 modal.close(target);
116                             }
117 
118                             @Override
119                             protected Panel customResultBody(
120                                     final String panelId, final Serializable item, final Serializable result) {
121 
122                                 @SuppressWarnings("unchecked")
123                                 ArrayList<ProvisioningReport> reports = (ArrayList<ProvisioningReport>) result;
124                                 return new ProvisioningReportsPanel(panelId, reports, pageRef);
125                             }
126                         });
127                         target.ifPresent(t -> t.add(modal.getForm()));
128 
129                         SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
130                     } else if (Constants.OPERATION_SUCCEEDED.equals(payload.getResult())) {
131                         target.ifPresent(modal::close);
132                         SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
133                     } else if (payload.getResult() instanceof Exception) {
134                         SyncopeConsoleSession.get().onException((Exception) payload.getResult());
135                     } else {
136                         SyncopeConsoleSession.get().error(payload.getResult());
137                     }
138 
139                     target.ifPresent(t -> {
140                         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(t);
141                         t.add(container);
142                     });
143                 }
144             }
145         };
146         csvEventSink.add(csvDownloadBehavior);
147         panel.addOuterObject(csvEventSink);
148         csvPushLink = new AjaxLink<>("csvPush") {
149 
150             private static final long serialVersionUID = -817438685948164787L;
151 
152             @Override
153             public void onClick(final AjaxRequestTarget target) {
154                 CSVPushSpec spec = csvPushSpec(type, pSchemaNames, dSchemaNames);
155                 AnyQuery query = csvAnyQuery(realm, fiql, rows, panel.getDataProvider());
156 
157                 target.add(modal.setContent(new CSVPushWizardBuilder(
158                         spec, query, csvDownloadBehavior, reconciliationRestClient, implementationRestClient, pageRef).
159                         setEventSink(csvEventSink).
160                         build(BaseModal.CONTENT_ID, AjaxWizard.Mode.EDIT)));
161 
162                 modal.header(new StringResourceModel("csvPush", panel, Model.of(spec)));
163                 modal.show(true);
164             }
165         };
166         csvPushLink.setOutputMarkupPlaceholderTag(true).setVisible(wizardInModal).setEnabled(wizardInModal);
167         MetaDataRoleAuthorizationStrategy.authorize(csvPushLink, Component.RENDER,
168                 String.format("%s,%s", IdRepoEntitlement.IMPLEMENTATION_LIST, IdRepoEntitlement.TASK_EXECUTE));
169         panel.addInnerObject(csvPushLink.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true));
170         csvPullLink = new AjaxLink<>("csvPull") {
171 
172             private static final long serialVersionUID = -817438685948164787L;
173 
174             @Override
175             public void onClick(final AjaxRequestTarget target) {
176                 CSVPullSpec spec = csvPullSpec(type, realm);
177 
178                 target.add(modal.setContent(
179                         new CSVPullWizardBuilder(spec, reconciliationRestClient, implementationRestClient, pageRef).
180                                 setEventSink(csvEventSink).
181                                 build(BaseModal.CONTENT_ID, AjaxWizard.Mode.EDIT)));
182 
183                 modal.header(new StringResourceModel("csvPull", panel, Model.of(spec)));
184                 modal.show(true);
185             }
186         };
187         csvPullLink.setOutputMarkupPlaceholderTag(true).setVisible(wizardInModal).setEnabled(wizardInModal);
188         MetaDataRoleAuthorizationStrategy.authorize(csvPullLink, Component.RENDER,
189                 String.format("%s,%s", IdRepoEntitlement.IMPLEMENTATION_LIST, IdRepoEntitlement.TASK_EXECUTE));
190         panel.addInnerObject(csvPullLink.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true));
191     }
192 
193     protected CSVPushSpec csvPushSpec(
194             final String type,
195             final List<String> pSchemaNames,
196             final List<String> dSchemaNames) {
197 
198         CSVPushSpec spec = new CSVPushSpec.Builder(type).build();
199         spec.setFields(PreferenceManager.getList(
200                 DisplayAttributesModalPanel.getPrefDetailView(type)).
201                 stream().filter(name -> !Constants.KEY_FIELD_NAME.equalsIgnoreCase(name)).
202                 collect(Collectors.toList()));
203         spec.setPlainAttrs(PreferenceManager.getList(
204                 DisplayAttributesModalPanel.getPrefPlainAttributeView(type)).
205                 stream().filter(pSchemaNames::contains).collect(Collectors.toList()));
206         spec.setDerAttrs(PreferenceManager.getList(
207                 DisplayAttributesModalPanel.getPrefPlainAttributeView(type)).
208                 stream().filter(dSchemaNames::contains).collect(Collectors.toList()));
209         return spec;
210     }
211 
212     protected CSVPullSpec csvPullSpec(final String type, final String realm) {
213         CSVPullSpec spec = new CSVPullSpec();
214         spec.setAnyTypeKey(type);
215         spec.setDestinationRealm(realm);
216         return spec;
217     }
218 
219     protected AnyQuery csvAnyQuery(
220             final String realm,
221             final String fiql,
222             final int rows,
223             final AnyDataProvider<?> dataProvider) {
224 
225         return new AnyQuery.Builder().realm(realm).
226                 fiql(fiql).page(dataProvider.getCurrentPage() + 1).size(rows).
227                 orderBy(BaseRestClient.toOrderBy(dataProvider.getSort())).
228                 build();
229     }
230 
231     @Override
232     public void hide() {
233         csvPushLink.setEnabled(false);
234         csvPushLink.setVisible(false);
235         csvPullLink.setEnabled(false);
236         csvPullLink.setVisible(false);
237     }
238 }