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.tasks;
20  
21  import com.fasterxml.jackson.core.JsonProcessingException;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.List;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
28  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
29  import org.apache.syncope.client.console.commons.IdRepoConstants;
30  import org.apache.syncope.client.console.panels.DirectoryPanel;
31  import org.apache.syncope.client.console.rest.CommandRestClient;
32  import org.apache.syncope.client.console.rest.ImplementationRestClient;
33  import org.apache.syncope.client.console.rest.TaskRestClient;
34  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
35  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
36  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
37  import org.apache.syncope.client.ui.commons.Constants;
38  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
39  import org.apache.syncope.client.ui.commons.panels.ModalPanel;
40  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
41  import org.apache.syncope.common.lib.SyncopeClientException;
42  import org.apache.syncope.common.lib.command.CommandTO;
43  import org.apache.syncope.common.lib.to.MacroTaskTO;
44  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
45  import org.apache.syncope.common.lib.types.TaskType;
46  import org.apache.wicket.PageReference;
47  import org.apache.wicket.ajax.AjaxRequestTarget;
48  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
49  import org.apache.wicket.event.Broadcast;
50  import org.apache.wicket.event.IEvent;
51  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
52  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
53  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
54  import org.apache.wicket.markup.html.basic.Label;
55  import org.apache.wicket.markup.repeater.Item;
56  import org.apache.wicket.model.CompoundPropertyModel;
57  import org.apache.wicket.model.IModel;
58  import org.apache.wicket.model.ResourceModel;
59  import org.apache.wicket.spring.injection.annot.SpringBean;
60  
61  public class CommandComposeDirectoryPanel extends DirectoryPanel<
62          CommandWrapper, CommandWrapper, DirectoryDataProvider<CommandWrapper>, CommandRestClient>
63          implements ModalPanel {
64  
65      private static final long serialVersionUID = 8899580817658145305L;
66  
67      @SpringBean
68      protected ImplementationRestClient implementationRestClient;
69  
70      @SpringBean
71      protected TaskRestClient taskRestClient;
72  
73      @SpringBean
74      protected CommandRestClient commandRestClient;
75  
76      protected final String task;
77  
78      protected final BaseModal<MacroTaskTO> baseModal;
79  
80      public CommandComposeDirectoryPanel(
81              final String task,
82              final CommandRestClient restClient,
83              final BaseModal<MacroTaskTO> baseModal,
84              final PageReference pageRef) {
85  
86          super(BaseModal.CONTENT_ID, restClient, pageRef, false);
87  
88          disableCheckBoxes();
89  
90          this.task = task;
91          this.baseModal = baseModal;
92  
93          enableUtilityButton();
94  
95          addNewItemPanelBuilder(new CommandComposeWizardBuilder(
96                  task,
97                  new CommandWrapper(true),
98                  implementationRestClient,
99                  taskRestClient,
100                 commandRestClient,
101                 pageRef), true);
102 
103         MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.TASK_UPDATE);
104         initResultTable();
105     }
106 
107     @Override
108     protected List<IColumn<CommandWrapper, String>> getColumns() {
109         List<IColumn<CommandWrapper, String>> columns = new ArrayList<>();
110 
111         columns.add(new AbstractColumn<>(new ResourceModel(Constants.KEY_FIELD_NAME), Constants.KEY_FIELD_NAME) {
112 
113             private static final long serialVersionUID = -4008579357070833846L;
114 
115             @Override
116             public void populateItem(
117                     final Item<ICellPopulator<CommandWrapper>> cellItem,
118                     final String componentId,
119                     final IModel<CommandWrapper> rowModel) {
120 
121                 cellItem.add(new Label(componentId, rowModel.getObject().getCommand().getKey()));
122             }
123         });
124 
125         columns.add(new AbstractColumn<>(new ResourceModel("arguments"), "arguments") {
126 
127             private static final long serialVersionUID = -4008579357070833846L;
128 
129             @Override
130             public void populateItem(
131                     final Item<ICellPopulator<CommandWrapper>> cellItem,
132                     final String componentId,
133                     final IModel<CommandWrapper> rowModel) {
134 
135                 cellItem.add(new Label(componentId, rowModel.getObject().getCommand().getArgs().getClass().getName()));
136             }
137         });
138 
139         return columns;
140     }
141 
142     @Override
143     public ActionsPanel<CommandWrapper> getActions(final IModel<CommandWrapper> model) {
144         ActionsPanel<CommandWrapper> panel = super.getActions(model);
145 
146         panel.add(new ActionLink<>() {
147 
148             private static final long serialVersionUID = -3722207913631435501L;
149 
150             @Override
151             public void onClick(final AjaxRequestTarget target, final CommandWrapper ignore) {
152                 CommandComposeDirectoryPanel.this.getTogglePanel().close(target);
153                 send(CommandComposeDirectoryPanel.this, Broadcast.EXACT,
154                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
155             }
156         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.TASK_UPDATE);
157 
158         panel.add(new ActionLink<>() {
159 
160             private static final long serialVersionUID = -3722207913631435501L;
161 
162             @Override
163             public void onClick(final AjaxRequestTarget target, final CommandWrapper ignore) {
164                 try {
165                     MacroTaskTO actual = taskRestClient.readTask(TaskType.MACRO, task);
166 
167                     // cannot rely on actual.getCommands().remove(model.getObject().getCommand())
168                     // since CommandArgs instances could not be implementing equals() / hashCode()
169                     Integer idx = null;
170                     for (int i = 0; i < actual.getCommands().size() && idx == null; i++) {
171                         CommandTO actualCmd = actual.getCommands().get(i);
172                         try {
173                             if (actualCmd.getKey().equals(model.getObject().getCommand().getKey())
174                                     && MAPPER.writeValueAsString(actualCmd.getArgs()).equals(
175                                             MAPPER.writeValueAsString(model.getObject().getCommand().getArgs()))) {
176 
177                                 idx = i;
178                             }
179                         } catch (JsonProcessingException e) {
180                             LOG.error("While comparing command arguments", e);
181                         }
182                     }
183                     if (idx == null) {
184                         SyncopeConsoleSession.get().info(getString(Constants.OPERATION_NO_OP));
185                     } else {
186                         actual.getCommands().remove(idx.intValue());
187                         taskRestClient.update(TaskType.MACRO, actual);
188                         SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
189                     }
190 
191                     customActionOnFinishCallback(target);
192                 } catch (SyncopeClientException e) {
193                     LOG.error("While deleting {}", model.getObject(), e);
194                     SyncopeConsoleSession.get().onException(e);
195                 }
196                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
197             }
198         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.TASK_UPDATE, true);
199 
200         return panel;
201     }
202 
203     @Override
204     public ActionsPanel<Serializable> getHeader(final String componentId) {
205         ActionsPanel<Serializable> panel = new ActionsPanel<>(componentId, null);
206 
207         panel.add(new ActionLink<>() {
208 
209             private static final long serialVersionUID = -7978723352517770644L;
210 
211             @Override
212             public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
213                 if (target != null) {
214                     customActionOnFinishCallback(target);
215                 }
216             }
217         }, ActionLink.ActionType.RELOAD, IdRepoEntitlement.TASK_READ).hideLabel();
218         return panel;
219     }
220 
221     @Override
222     protected Collection<ActionLink.ActionType> getBatches() {
223         return List.of();
224     }
225 
226     @Override
227     protected CommandComposeDataProvider dataProvider() {
228         return new CommandComposeDataProvider(rows);
229     }
230 
231     @Override
232     protected String paginatorRowsKey() {
233         return IdRepoConstants.PREF_COMMAND_PAGINATOR_ROWS;
234     }
235 
236     @Override
237     public void onEvent(final IEvent<?> event) {
238         super.onEvent(event);
239         if (event.getPayload() instanceof ExitEvent) {
240             AjaxRequestTarget target = ExitEvent.class.cast(event.getPayload()).getTarget();
241             baseModal.show(false);
242             baseModal.close(target);
243         }
244     }
245 
246     protected class CommandComposeDataProvider extends DirectoryDataProvider<CommandWrapper> {
247 
248         private static final long serialVersionUID = 4725679400450513556L;
249 
250         public CommandComposeDataProvider(final int paginatorRows) {
251             super(paginatorRows);
252         }
253 
254         @Override
255         public Iterator<CommandWrapper> iterator(final long first, final long count) {
256             MacroTaskTO actual = taskRestClient.readTask(TaskType.MACRO, task);
257             return actual.getCommands().subList((int) first, (int) (first + count)).stream().
258                     map(command -> new CommandWrapper(false).setCommand(command)).
259                     iterator();
260         }
261 
262         @Override
263         public long size() {
264             MacroTaskTO actual = taskRestClient.readTask(TaskType.MACRO, task);
265             return actual.getCommands().size();
266         }
267 
268         @Override
269         public IModel<CommandWrapper> model(final CommandWrapper object) {
270             return new CompoundPropertyModel<>(object);
271         }
272     }
273 }