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.List;
23  import org.apache.commons.lang3.tuple.Pair;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.layout.AnyObjectFormLayoutInfo;
26  import org.apache.syncope.client.console.layout.GroupFormLayoutInfo;
27  import org.apache.syncope.client.console.layout.UserFormLayoutInfo;
28  import org.apache.syncope.client.console.pages.BasePage;
29  import org.apache.syncope.client.console.panels.TogglePanel;
30  import org.apache.syncope.client.console.rest.AnyObjectRestClient;
31  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
32  import org.apache.syncope.client.console.rest.GroupRestClient;
33  import org.apache.syncope.client.console.rest.UserRestClient;
34  import org.apache.syncope.client.console.wizards.any.AnyObjectTemplateWizardBuilder;
35  import org.apache.syncope.client.console.wizards.any.AnyWizardBuilder;
36  import org.apache.syncope.client.console.wizards.any.GroupTemplateWizardBuilder;
37  import org.apache.syncope.client.console.wizards.any.TemplateWizardBuilder;
38  import org.apache.syncope.client.console.wizards.any.UserTemplateWizardBuilder;
39  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
40  import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
41  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
42  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
43  import org.apache.syncope.common.lib.SyncopeClientException;
44  import org.apache.syncope.common.lib.to.AnyObjectTO;
45  import org.apache.syncope.common.lib.to.AnyTO;
46  import org.apache.syncope.common.lib.to.GroupTO;
47  import org.apache.syncope.common.lib.to.TemplatableTO;
48  import org.apache.syncope.common.lib.to.UserTO;
49  import org.apache.wicket.MarkupContainer;
50  import org.apache.wicket.PageReference;
51  import org.apache.wicket.ajax.AjaxRequestTarget;
52  import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
53  import org.apache.wicket.event.Broadcast;
54  import org.apache.wicket.markup.html.form.Form;
55  import org.apache.wicket.model.LoadableDetachableModel;
56  import org.apache.wicket.model.Model;
57  import org.apache.wicket.model.StringResourceModel;
58  import org.apache.wicket.spring.injection.annot.SpringBean;
59  
60  public abstract class TemplatesTogglePanel extends TogglePanel<Serializable> {
61  
62      private static final long serialVersionUID = -3195479265440591519L;
63  
64      @SpringBean
65      protected AnyTypeRestClient anyTypeRestClient;
66  
67      @SpringBean
68      protected UserRestClient userRestClient;
69  
70      @SpringBean
71      protected GroupRestClient groupRestClient;
72  
73      @SpringBean
74      protected AnyObjectRestClient anyObjectRestClient;
75  
76      protected TemplatableTO targetObject;
77  
78      protected final Form<?> form;
79  
80      protected final Model<String> typeModel = new Model<>();
81  
82      protected final LoadableDetachableModel<List<String>> anyTypes = new LoadableDetachableModel<>() {
83  
84          private static final long serialVersionUID = 5275935387613157437L;
85  
86          @Override
87          protected List<String> load() {
88              return anyTypeRestClient.list();
89          }
90      };
91  
92      public TemplatesTogglePanel(final String targetId, final MarkupContainer container, final PageReference pageRef) {
93          super("toggleTemplates", pageRef);
94  
95          form = new Form<>("templatesForm");
96          addInnerObject(form);
97  
98          FieldPanel<String> type = new AjaxDropDownChoicePanel<>("type", "type", typeModel, false).
99                  setChoices(anyTypes).
100                 setStyleSheet("form-control").
101                 setRequired(true);
102 
103         type.hideLabel();
104         form.add(type);
105 
106         form.add(new AjaxSubmitLink("changeit", form) {
107 
108             private static final long serialVersionUID = -7978723352517770644L;
109 
110             @Override
111             protected void onSubmit(final AjaxRequestTarget target) {
112                 try {
113                     AjaxWizard.NewItemActionEvent<AnyTO> payload = new AjaxWizard.NewItemActionEvent<>(null, target);
114 
115                     payload.setTitleModel(new StringResourceModel("inner.template.edit", container,
116                             Model.of(Pair.of(typeModel.getObject(), targetObject))).setDefaultValue(
117                             "Edit template"));
118 
119                     List<String> classes = anyTypeRestClient.read(typeModel.getObject()).getClasses();
120 
121                     TemplateWizardBuilder<?> builder;
122                     switch (typeModel.getObject()) {
123                         case "USER":
124                             builder = new UserTemplateWizardBuilder(
125                                     targetObject,
126                                     classes,
127                                     new UserFormLayoutInfo(),
128                                     userRestClient,
129                                     pageRef) {
130 
131                                 private static final long serialVersionUID = -7978723352517770634L;
132 
133                                 @Override
134                                 protected Serializable onApplyInternal(final AnyWrapper<UserTO> modelObject) {
135                                     return TemplatesTogglePanel.this.onApplyInternal(
136                                             targetObject, typeModel.getObject(), modelObject.getInnerObject());
137                                 }
138                             };
139                             break;
140 
141                         case "GROUP":
142                             builder = new GroupTemplateWizardBuilder(
143                                     targetObject,
144                                     classes,
145                                     new GroupFormLayoutInfo(),
146                                     groupRestClient,
147                                     pageRef) {
148 
149                                 private static final long serialVersionUID = -7978723352517770634L;
150 
151                                 @Override
152                                 protected Serializable onApplyInternal(final AnyWrapper<GroupTO> modelObject) {
153                                     return TemplatesTogglePanel.this.onApplyInternal(
154                                             targetObject, typeModel.getObject(), modelObject.getInnerObject());
155                                 }
156                             };
157                             break;
158 
159                         default:
160                             builder = new AnyObjectTemplateWizardBuilder(
161                                     targetObject,
162                                     typeModel.getObject(),
163                                     classes,
164                                     new AnyObjectFormLayoutInfo(),
165                                     anyObjectRestClient,
166                                     pageRef) {
167 
168                                 private static final long serialVersionUID = -7978723352517770634L;
169 
170                                 @Override
171                                 protected Serializable onApplyInternal(final AnyWrapper<AnyObjectTO> modelObject) {
172                                     return TemplatesTogglePanel.this.onApplyInternal(
173                                             targetObject, typeModel.getObject(), modelObject.getInnerObject());
174                                 }
175                             };
176                     }
177                     AnyWizardBuilder.class.cast(builder).setEventSink(container);
178                     payload.forceModalPanel(builder.build(targetId));
179                     send(container, Broadcast.EXACT, payload);
180                     toggle(target, false);
181                 } catch (SyncopeClientException e) {
182                     LOG.error("While editing template for {}", typeModel.getObject(), e);
183                     SyncopeConsoleSession.get().onException(e);
184                 }
185                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
186             }
187 
188             @Override
189             protected void onError(final AjaxRequestTarget target) {
190                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
191             }
192         });
193     }
194 
195     protected abstract Serializable onApplyInternal(TemplatableTO targetObject, String type, AnyTO anyTO);
196 
197     public void setTargetObject(final TemplatableTO targetObject) {
198         this.targetObject = targetObject;
199     }
200 }