1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.console.tasks;
20
21 import java.io.Serializable;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.apache.syncope.client.console.panels.BeanPanel;
25 import org.apache.syncope.client.console.rest.CommandRestClient;
26 import org.apache.syncope.client.console.rest.ImplementationRestClient;
27 import org.apache.syncope.client.console.rest.TaskRestClient;
28 import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
29 import org.apache.syncope.client.ui.commons.Constants;
30 import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
31 import org.apache.syncope.common.lib.command.CommandTO;
32 import org.apache.syncope.common.lib.to.ImplementationTO;
33 import org.apache.syncope.common.lib.to.MacroTaskTO;
34 import org.apache.syncope.common.lib.types.IdRepoImplementationType;
35 import org.apache.syncope.common.lib.types.ImplementationEngine;
36 import org.apache.syncope.common.lib.types.TaskType;
37 import org.apache.wicket.PageReference;
38 import org.apache.wicket.ajax.AjaxRequestTarget;
39 import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
40 import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
41 import org.apache.wicket.extensions.wizard.WizardModel;
42 import org.apache.wicket.extensions.wizard.WizardStep;
43 import org.apache.wicket.model.LoadableDetachableModel;
44 import org.apache.wicket.model.PropertyModel;
45
46 public class CommandComposeWizardBuilder extends BaseAjaxWizardBuilder<CommandWrapper> {
47
48 private static final long serialVersionUID = -2300926041782845851L;
49
50 protected final LoadableDetachableModel<List<ImplementationTO>> commands = new LoadableDetachableModel<>() {
51
52 private static final long serialVersionUID = 4659376149825914247L;
53
54 @Override
55 protected List<ImplementationTO> load() {
56 return implementationRestClient.list(IdRepoImplementationType.COMMAND);
57 }
58 };
59
60 protected final String task;
61
62 protected final ImplementationRestClient implementationRestClient;
63
64 protected final TaskRestClient taskRestClient;
65
66 protected final CommandRestClient commandRestClient;
67
68 public CommandComposeWizardBuilder(
69 final String task,
70 final CommandWrapper defaultItem,
71 final ImplementationRestClient implementationRestClient,
72 final TaskRestClient taskRestClient,
73 final CommandRestClient commandRestClient,
74 final PageReference pageRef) {
75
76 super(defaultItem, pageRef);
77
78 this.task = task;
79 this.implementationRestClient = implementationRestClient;
80 this.taskRestClient = taskRestClient;
81 this.commandRestClient = commandRestClient;
82 }
83
84 @Override
85 protected Serializable onApplyInternal(final CommandWrapper modelObject) {
86 if (modelObject.getCommand().getArgs() == null) {
87 throw new IllegalArgumentException("Incorrect Command definition");
88 }
89
90 MacroTaskTO taskTO = taskRestClient.readTask(TaskType.MACRO, task);
91 if (modelObject.isNew()) {
92 taskTO.getCommands().add(modelObject.getCommand());
93 } else {
94 taskTO.getCommands().stream().
95 filter(cmd -> cmd.getKey().equals(modelObject.getCommand().getKey())).
96 findFirst().
97 ifPresent(cmd -> cmd.setArgs(modelObject.getCommand().getArgs()));
98 }
99
100 taskRestClient.update(TaskType.MACRO, taskTO);
101 return modelObject;
102 }
103
104 @Override
105 protected WizardModel buildModelSteps(final CommandWrapper modelObject, final WizardModel wizardModel) {
106 wizardModel.add(new Profile(modelObject));
107 wizardModel.add(new CommandArgs(modelObject));
108 return wizardModel;
109 }
110
111 public class Profile extends WizardStep {
112
113 private static final long serialVersionUID = -3043839139187792810L;
114
115 private final CommandWrapper command;
116
117 public Profile(final CommandWrapper command) {
118 this.command = command;
119
120 AutoCompleteSettings settings = new AutoCompleteSettings();
121 settings.setShowCompleteListOnFocusGain(false);
122 settings.setShowListOnEmptyInput(false);
123
124 AutoCompleteTextField<String> args = new AutoCompleteTextField<>(
125 "command", new PropertyModel<>(command, "command.key"), settings) {
126
127 private static final long serialVersionUID = -6556576139048844857L;
128
129 @Override
130 protected Iterator<String> getChoices(final String input) {
131 return commands.getObject().stream().
132 map(ImplementationTO::getKey).
133 filter(cmd -> cmd.contains(input)).
134 sorted().iterator();
135 }
136 };
137 args.setRequired(true);
138 args.setEnabled(command.isNew());
139 args.add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
140
141 private static final long serialVersionUID = -6139318907146065915L;
142
143 @Override
144 protected void onUpdate(final AjaxRequestTarget target) {
145 try {
146 CommandTO cmd = commandRestClient.read(command.getCommand().getKey());
147 command.getCommand().setArgs(cmd.getArgs());
148 } catch (Exception e) {
149 LOG.error("While attempting to read Command {}", command.getCommand().getKey(), e);
150 }
151 }
152 });
153 add(args);
154 }
155
156 @Override
157 public void applyState() {
158 commands.getObject().stream().
159 filter(cmd -> cmd.getKey().equals(command.getCommand().getKey())
160 && cmd.getEngine() == ImplementationEngine.GROOVY).
161 findFirst().ifPresent(cmd -> getWizardModel().finish());
162 }
163 }
164
165 public class CommandArgs extends WizardStep {
166
167 private static final long serialVersionUID = -785981096328637758L;
168
169 public CommandArgs(final CommandWrapper command) {
170 LoadableDetachableModel<Serializable> bean = new LoadableDetachableModel<>() {
171
172 private static final long serialVersionUID = 2092144708018739371L;
173
174 @Override
175 protected Serializable load() {
176 return command.getCommand().getArgs();
177 }
178 };
179 add(new BeanPanel<>("bean", bean, pageRef).setRenderBodyOnly(true));
180 }
181 }
182 }