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.Date;
23  import org.apache.commons.lang3.time.FastDateFormat;
24  import org.apache.syncope.client.console.rest.TaskRestClient;
25  import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
26  import org.apache.syncope.client.ui.commons.Constants;
27  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDateTimeFieldPanel;
30  import org.apache.syncope.client.ui.commons.panels.SyncopeFormPanel;
31  import org.apache.syncope.common.lib.SyncopeConstants;
32  import org.apache.syncope.common.lib.form.SyncopeForm;
33  import org.apache.syncope.common.lib.to.MacroTaskTO;
34  import org.apache.wicket.PageReference;
35  import org.apache.wicket.ajax.AjaxRequestTarget;
36  import org.apache.wicket.extensions.wizard.WizardModel;
37  import org.apache.wicket.extensions.wizard.WizardStep;
38  import org.apache.wicket.model.IModel;
39  import org.apache.wicket.model.Model;
40  
41  public class MacroTaskExecWizardBuilder extends BaseAjaxWizardBuilder<MacroTaskTO> {
42  
43      private static final long serialVersionUID = 3318576575286024205L;
44  
45      protected final TaskRestClient taskRestClient;
46  
47      protected final IModel<SyncopeForm> formModel = Model.of();
48  
49      protected final Model<Date> startAtDateModel = new Model<>();
50  
51      protected final Model<Boolean> dryRunModel = new Model<>(false);
52  
53      public MacroTaskExecWizardBuilder(
54              final MacroTaskTO defaultItem,
55              final TaskRestClient taskRestClient,
56              final PageReference pageRef) {
57  
58          super(defaultItem, pageRef);
59          this.taskRestClient = taskRestClient;
60      }
61  
62      @Override
63      protected Serializable onApplyInternal(final MacroTaskTO modelObject) {
64          if (formModel.getObject() == null) {
65              taskRestClient.startExecution(modelObject.getKey(),
66                      startAtDateModel.getObject(),
67                      dryRunModel.getObject());
68          } else {
69              taskRestClient.startExecution(modelObject.getKey(),
70                      startAtDateModel.getObject(),
71                      dryRunModel.getObject(),
72                      formModel.getObject());
73          }
74  
75          return null;
76      }
77  
78      @Override
79      protected WizardModel buildModelSteps(final MacroTaskTO modelObject, final WizardModel wizardModel) {
80          if (!modelObject.getFormPropertyDefs().isEmpty()) {
81              formModel.setObject(taskRestClient.getMacroTaskForm(modelObject.getKey()));
82              wizardModel.add(new Form());
83          }
84          wizardModel.add(new StartAt());
85          return wizardModel;
86      }
87  
88      protected class Form extends WizardStep {
89  
90          private static final long serialVersionUID = 7352192594863229013L;
91  
92          protected Form() {
93              add(new SyncopeFormPanel<>("form", formModel.getObject()));
94          }
95      }
96  
97      protected class StartAt extends WizardStep {
98  
99          private static final long serialVersionUID = -961082324376783538L;
100 
101         protected StartAt() {
102             AjaxDateTimeFieldPanel startAtDate = new AjaxDateTimeFieldPanel(
103                     "startAtDate", "startAtDate", startAtDateModel,
104                     FastDateFormat.getInstance(SyncopeConstants.DATE_PATTERNS[3]));
105             add(startAtDate.setReadOnly(true).hideLabel());
106 
107             AjaxCheckBoxPanel startAtCheck = new AjaxCheckBoxPanel(
108                     "startAtCheck", "startAtCheck", new Model<>(false), false);
109             startAtCheck.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
110 
111                 private static final long serialVersionUID = -1107858522700306810L;
112 
113                 @Override
114                 protected void onUpdate(final AjaxRequestTarget target) {
115                     target.add(startAtDate.setModelObject(null).setReadOnly(!startAtCheck.getModelObject()));
116                 }
117             });
118             add(startAtCheck);
119 
120             add(new AjaxCheckBoxPanel("dryRun", "dryRun", dryRunModel, false));
121         }
122     }
123 }