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 de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.List;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.client.console.panels.MultilevelPanel;
27  import org.apache.syncope.client.console.rest.CommandRestClient;
28  import org.apache.syncope.client.console.rest.TaskRestClient;
29  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.BooleanPropertyColumn;
30  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
31  import org.apache.syncope.client.console.wicket.markup.html.form.Action;
32  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
34  import org.apache.syncope.client.ui.commons.Constants;
35  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
36  import org.apache.syncope.common.lib.to.MacroTaskTO;
37  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
38  import org.apache.syncope.common.lib.types.TaskType;
39  import org.apache.wicket.PageReference;
40  import org.apache.wicket.ajax.AjaxRequestTarget;
41  import org.apache.wicket.event.IEvent;
42  import org.apache.wicket.event.IEventSink;
43  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
44  import org.apache.wicket.model.IModel;
45  import org.apache.wicket.model.Model;
46  import org.apache.wicket.model.ResourceModel;
47  import org.apache.wicket.model.StringResourceModel;
48  import org.apache.wicket.spring.injection.annot.SpringBean;
49  
50  public class MacroTaskDirectoryPanel extends SchedTaskDirectoryPanel<MacroTaskTO> {
51  
52      private static final long serialVersionUID = -6247673131495530094L;
53  
54      protected class ExecModalEventSink implements IEventSink, Serializable {
55  
56          private static final long serialVersionUID = -5961049309874978659L;
57  
58          @Override
59          public void onEvent(final IEvent<?> event) {
60              if (event.getPayload() instanceof AjaxWizard.NewItemCancelEvent
61                      || event.getPayload() instanceof AjaxWizard.NewItemFinishEvent) {
62  
63                  AjaxWizard.NewItemEvent<?> nie = AjaxWizard.NewItemEvent.class.cast(event.getPayload());
64                  nie.getTarget().ifPresent(execModal::close);
65              }
66  
67              MacroTaskDirectoryPanel.this.onEvent(event);
68          }
69      }
70  
71      @SpringBean
72      protected CommandRestClient commandRestClient;
73  
74      protected final BaseModal<MacroTaskTO> formPropertyDefModal = new BaseModal<>(Constants.OUTER);
75  
76      protected final BaseModal<MacroTaskTO> execModal;
77  
78      public MacroTaskDirectoryPanel(
79              final TaskRestClient restClient,
80              final MultilevelPanel mlp,
81              final PageReference pageRef) {
82  
83          super(MultilevelPanel.FIRST_LEVEL_ID, restClient, null, mlp, TaskType.MACRO, new MacroTaskTO(), pageRef, true);
84  
85          formPropertyDefModal.size(Modal.Size.Extra_large);
86          formPropertyDefModal.addSubmitButton();
87          setWindowClosedReloadCallback(formPropertyDefModal);
88          addOuterObject(formPropertyDefModal);
89  
90          execModal = new BaseModal<>(Constants.OUTER) {
91  
92              private static final long serialVersionUID = 389935548143327858L;
93  
94              @Override
95              protected void onConfigure() {
96                  super.onConfigure();
97                  setFooterVisible(false);
98              }
99          };
100         execModal.size(Modal.Size.Large);
101         setWindowClosedReloadCallback(execModal);
102         addOuterObject(execModal);
103     }
104 
105     @Override
106     protected List<IColumn<MacroTaskTO, String>> getFieldColumns() {
107         List<IColumn<MacroTaskTO, String>> columns = new ArrayList<>();
108 
109         columns.addAll(getHeadingFieldColumns());
110 
111         columns.add(new BooleanPropertyColumn<>(
112                 new ResourceModel("continueOnError"), "continueOnError", "continueOnError"));
113         columns.add(new BooleanPropertyColumn<>(
114                 new ResourceModel("saveExecs"), "saveExecs", "saveExecs"));
115 
116         columns.addAll(getTrailingFieldColumns());
117 
118         return columns;
119     }
120 
121     @Override
122     protected void viewTaskExecs(final MacroTaskTO taskTO, final AjaxRequestTarget target) {
123         multiLevelPanelRef.next(
124                 new StringResourceModel("task.view", this, new Model<>(Pair.of(null, taskTO))).getObject(),
125                 new TaskExecutionDetails<>(taskTO, pageRef),
126                 target);
127     }
128 
129     @Override
130     public ActionsPanel<MacroTaskTO> getActions(final IModel<MacroTaskTO> model) {
131         ActionsPanel<MacroTaskTO> panel = super.getActions(model);
132 
133         panel.getActions().removeIf(action -> action.getType() == ActionLink.ActionType.EXECUTE);
134 
135         Action<MacroTaskTO> execute = new Action<>(new ActionLink<>() {
136 
137             private static final long serialVersionUID = -3722207913631435501L;
138 
139             @Override
140             public void onClick(final AjaxRequestTarget target, final MacroTaskTO ignore) {
141                 model.setObject(restClient.readTask(TaskType.MACRO, model.getObject().getKey()));
142                 MacroTaskExecWizardBuilder wb = new MacroTaskExecWizardBuilder(model.getObject(), restClient, pageRef);
143                 wb.setEventSink(new ExecModalEventSink());
144 
145                 target.add(execModal.setContent(wb.build(BaseModal.CONTENT_ID, AjaxWizard.Mode.EDIT)));
146 
147                 execModal.header(new StringResourceModel(
148                         "exec", MacroTaskDirectoryPanel.this, Model.of(model.getObject())));
149                 execModal.show(true);
150             }
151         }, ActionLink.ActionType.EXECUTE);
152         execute.setEntitlements(IdRepoEntitlement.TASK_EXECUTE);
153         execute.setOnConfirm(false);
154 
155         panel.add(panel.getActions().size() - 1, execute);
156 
157         return panel;
158     }
159 
160     @Override
161     protected void addFurtherActions(final ActionsPanel<MacroTaskTO> panel, final IModel<MacroTaskTO> model) {
162         panel.add(new ActionLink<>() {
163 
164             private static final long serialVersionUID = -3722207913631435501L;
165 
166             @Override
167             public void onClick(final AjaxRequestTarget target, final MacroTaskTO ignore) {
168                 model.setObject(restClient.readTask(TaskType.MACRO, model.getObject().getKey()));
169                 target.add(modal.setContent(new CommandComposeDirectoryPanel(
170                         model.getObject().getKey(), commandRestClient, modal, pageRef)));
171 
172                 modal.header(new StringResourceModel(
173                         "command.conf", MacroTaskDirectoryPanel.this, Model.of(model.getObject())));
174                 modal.show(true);
175             }
176         }, ActionLink.ActionType.COMPOSE, IdRepoEntitlement.TASK_UPDATE);
177 
178         panel.add(new ActionLink<>() {
179 
180             private static final long serialVersionUID = -3722207913631435501L;
181 
182             @Override
183             public void onClick(final AjaxRequestTarget target, final MacroTaskTO ignore) {
184                 model.setObject(restClient.readTask(TaskType.MACRO, model.getObject().getKey()));
185                 target.add(formPropertyDefModal.setContent(
186                         new FormPropertyDefsPanel(model.getObject(), formPropertyDefModal, pageRef)));
187 
188                 formPropertyDefModal.header(new StringResourceModel(
189                         "form.def", MacroTaskDirectoryPanel.this, Model.of(model.getObject())));
190                 formPropertyDefModal.show(true);
191             }
192         }, ActionLink.ActionType.MAPPING, IdRepoEntitlement.TASK_UPDATE);
193     }
194 }