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.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          MacroTaskTO taskTO = taskRestClient.readTask(TaskType.MACRO, task);
87          if (modelObject.isNew()) {
88              taskTO.getCommands().add(modelObject.getCommand());
89          } else {
90              taskTO.getCommands().stream().
91                      filter(cmd -> cmd.getKey().equals(modelObject.getCommand().getKey())).
92                      findFirst().
93                      ifPresent(cmd -> cmd.setArgs(modelObject.getCommand().getArgs()));
94          }
95  
96          taskRestClient.update(TaskType.MACRO, taskTO);
97          return modelObject;
98      }
99  
100     @Override
101     protected WizardModel buildModelSteps(final CommandWrapper modelObject, final WizardModel wizardModel) {
102         wizardModel.add(new Profile(modelObject));
103         wizardModel.add(new CommandArgs(modelObject));
104         return wizardModel;
105     }
106 
107     public class Profile extends WizardStep {
108 
109         private static final long serialVersionUID = -3043839139187792810L;
110 
111         private final CommandWrapper command;
112 
113         public Profile(final CommandWrapper command) {
114             this.command = command;
115             MacroTaskTO taskTO = taskRestClient.readTask(TaskType.MACRO, task);
116 
117             AutoCompleteSettings settings = new AutoCompleteSettings();
118             settings.setShowCompleteListOnFocusGain(false);
119             settings.setShowListOnEmptyInput(false);
120 
121             AutoCompleteTextField<String> args = new AutoCompleteTextField<>(
122                     "command", new PropertyModel<>(command, "command.key"), settings) {
123 
124                 private static final long serialVersionUID = -6556576139048844857L;
125 
126                 @Override
127                 protected Iterator<String> getChoices(final String input) {
128                     return commands.getObject().stream().
129                             map(ImplementationTO::getKey).
130                             filter(cmd -> cmd.contains(input)
131                             && taskTO.getCommands().stream().noneMatch(c -> c.getKey().equals(cmd))).
132                             sorted().iterator();
133                 }
134             };
135             args.setRequired(true);
136             args.setEnabled(command.isNew());
137             args.add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
138 
139                 private static final long serialVersionUID = -6139318907146065915L;
140 
141                 @Override
142                 protected void onUpdate(final AjaxRequestTarget target) {
143                     CommandTO cmd = commandRestClient.read(command.getCommand().getKey());
144                     command.getCommand().setArgs(cmd.getArgs());
145                 }
146             });
147             add(args);
148         }
149 
150         @Override
151         public void applyState() {
152             commands.getObject().stream().
153                     filter(cmd -> cmd.getKey().equals(command.getCommand().getKey())
154                     && cmd.getEngine() == ImplementationEngine.GROOVY).
155                     findFirst().ifPresent(cmd -> getWizardModel().finish());
156         }
157     }
158 
159     public class CommandArgs extends WizardStep {
160 
161         private static final long serialVersionUID = -785981096328637758L;
162 
163         public CommandArgs(final CommandWrapper command) {
164             LoadableDetachableModel<Serializable> bean = new LoadableDetachableModel<>() {
165 
166                 private static final long serialVersionUID = 2092144708018739371L;
167 
168                 @Override
169                 protected Serializable load() {
170                     return command.getCommand().getArgs();
171                 }
172             };
173             add(new BeanPanel<>("bean", bean, pageRef).setRenderBodyOnly(true));
174         }
175     }
176 }