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