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.ui.commons.wizards;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.concurrent.Callable;
25  import java.util.concurrent.Future;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.syncope.client.ui.commons.BaseSession;
28  import org.apache.wicket.Component;
29  import org.apache.wicket.PageReference;
30  import org.apache.wicket.Session;
31  import org.apache.wicket.ajax.AjaxRequestTarget;
32  import org.apache.wicket.extensions.wizard.WizardModel;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  public abstract class AjaxWizardBuilder<T extends Serializable> extends AbstractModalPanelBuilder<T> {
37  
38      private static final long serialVersionUID = 5241745929825564456L;
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(AjaxWizardBuilder.class);
41  
42      protected AjaxWizard.Mode mode = AjaxWizard.Mode.CREATE;
43  
44      protected final List<Component> outerObjects = new ArrayList<>();
45  
46      /**
47       * Construct.
48       *
49       * @param defaultItem default item.
50       * @param pageRef Caller page reference.
51       */
52      public AjaxWizardBuilder(final T defaultItem, final PageReference pageRef) {
53          super(defaultItem, pageRef);
54      }
55  
56      public AjaxWizardBuilder<T> addOuterObject(final Component... childs) {
57          outerObjects.addAll(List.of(childs));
58          return this;
59      }
60  
61      @Override
62      public AjaxWizard<T> build(final String id, final int index, final AjaxWizard.Mode mode) {
63          AjaxWizard<T> wizard = build(id, mode);
64          for (int i = 1; i < index; i++) {
65              wizard.getWizardModel().next();
66          }
67          return wizard;
68      }
69  
70      /**
71       * Build the wizard with a default wizard id.
72       *
73       * @param mode wizard mode.
74       * @return wizard.
75       */
76      public AjaxWizard<T> build(final AjaxWizard.Mode mode) {
77          return build(AbstractWizardMgtPanel.WIZARD_ID, mode);
78      }
79  
80      /**
81       * Build the wizard.
82       *
83       * @param id component id.
84       * @param mode wizard mode.
85       * @return wizard.
86       */
87      public AjaxWizard<T> build(final String id, final AjaxWizard.Mode mode) {
88          this.mode = mode;
89  
90          // get the specified item if available
91          T modelObj = newModelObject();
92  
93          return new AjaxWizard<>(id, modelObj, buildModelSteps(modelObj, new WizardModel()), mode, pageRef) {
94  
95              private static final long serialVersionUID = 7770507663760640735L;
96  
97              @Override
98              protected void onCancelInternal() {
99                  AjaxWizardBuilder.this.onCancelInternal(modelObj);
100             }
101 
102             @Override
103             protected Pair<Serializable, Serializable> onApplyInternal(final AjaxRequestTarget target) {
104                 Serializable res = AjaxWizardBuilder.this.onApplyInternal(modelObj);
105 
106                 Serializable payload;
107                 switch (mode) {
108                     case CREATE:
109                         payload = getCreateCustomPayloadEvent(res, target);
110                         break;
111                     case EDIT:
112                     case TEMPLATE:
113                         payload = getEditCustomPayloadEvent(res, target);
114                         break;
115                     default:
116                         payload = null;
117                 }
118 
119                 return Pair.of(payload, res);
120             }
121 
122             @Override
123             protected long getMaxWaitTimeInSeconds() {
124                 return AjaxWizardBuilder.this.getMaxWaitTimeInSeconds();
125             }
126 
127             @Override
128             protected void sendError(final Exception exception) {
129                 BaseSession.class.cast(Session.get()).onException(exception);
130             }
131 
132             @Override
133             protected void sendWarning(final String message) {
134                 AjaxWizardBuilder.this.sendWarning(message);
135             }
136 
137             @Override
138             protected Future<Pair<Serializable, Serializable>> execute(
139                 final Callable<Pair<Serializable, Serializable>> future) {
140                 return AjaxWizardBuilder.this.execute(future);
141             }
142         }.setEventSink(eventSink).addOuterObject(outerObjects);
143     }
144 
145     protected abstract WizardModel buildModelSteps(T modelObject, WizardModel wizardModel);
146 
147     /**
148      * Override to send custom events after create.
149      *
150      * @param afterObject after applied changes object.
151      * @param target ajax request target
152      * @return payload to be sent.
153      */
154     protected Serializable getCreateCustomPayloadEvent(final Serializable afterObject, final AjaxRequestTarget target) {
155         return null;
156     }
157 
158     protected abstract long getMaxWaitTimeInSeconds();
159 
160     protected abstract void sendError(Exception exception);
161 
162     protected abstract void sendWarning(String message);
163 
164     protected abstract Future<Pair<Serializable, Serializable>> execute(
165             Callable<Pair<Serializable, Serializable>> future);
166 
167     /**
168      * Override to send custom events after edit.
169      *
170      * @param afterObject after applied changes object.
171      * @param target ajax request target
172      * @return payload to be sent.
173      */
174     protected Serializable getEditCustomPayloadEvent(final Serializable afterObject, final AjaxRequestTarget target) {
175         return null;
176     }
177 }