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.panels;
20  
21  import java.io.Serializable;
22  import org.apache.syncope.client.console.rest.ApplicationRestClient;
23  import org.apache.syncope.client.console.wicket.markup.html.form.JsonEditorPanel;
24  import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
25  import org.apache.syncope.client.ui.commons.Constants;
26  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
27  import org.apache.syncope.common.lib.to.ApplicationTO;
28  import org.apache.syncope.common.lib.to.PrivilegeTO;
29  import org.apache.wicket.PageReference;
30  import org.apache.wicket.extensions.wizard.WizardModel;
31  import org.apache.wicket.extensions.wizard.WizardStep;
32  import org.apache.wicket.model.Model;
33  import org.apache.wicket.model.PropertyModel;
34  import org.apache.wicket.model.StringResourceModel;
35  
36  public class PrivilegeWizardBuilder extends BaseAjaxWizardBuilder<PrivilegeTO> {
37  
38      private static final long serialVersionUID = -1817419622749405208L;
39  
40      protected final ApplicationTO application;
41  
42      protected final ApplicationRestClient applicationRestClient;
43  
44      public PrivilegeWizardBuilder(
45              final ApplicationTO application,
46              final PrivilegeTO privilege,
47              final ApplicationRestClient applicationRestClient,
48              final PageReference pageRef) {
49  
50          super(privilege, pageRef);
51  
52          this.application = application;
53          this.applicationRestClient = applicationRestClient;
54      }
55  
56      @Override
57      protected WizardModel buildModelSteps(final PrivilegeTO modelObject, final WizardModel wizardModel) {
58          wizardModel.add(new Profile(modelObject));
59          wizardModel.add(new Spec(modelObject));
60          return wizardModel;
61      }
62  
63      @Override
64      protected Serializable onApplyInternal(final PrivilegeTO modelObject) {
65          application.getPrivileges().removeIf(privilege -> privilege.getKey().equals(modelObject.getKey()));
66          application.getPrivileges().add(modelObject);
67  
68          applicationRestClient.update(application);
69  
70          return modelObject;
71      }
72  
73      public static class Profile extends WizardStep {
74  
75          private static final long serialVersionUID = 11881843064077955L;
76  
77          public Profile(final PrivilegeTO privilege) {
78              setTitleModel(privilege.getKey() == null
79                      ? new StringResourceModel("privilege.new")
80                      : new StringResourceModel("privilege.edit", Model.of(privilege)));
81  
82              AjaxTextFieldPanel key = new AjaxTextFieldPanel(
83                      Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME,
84                      new PropertyModel<>(privilege, Constants.KEY_FIELD_NAME), false);
85              key.setReadOnly(privilege.getKey() != null);
86              key.setRequired(true);
87              add(key);
88  
89              AjaxTextFieldPanel description = new AjaxTextFieldPanel(
90                      Constants.DESCRIPTION_FIELD_NAME, Constants.DESCRIPTION_FIELD_NAME,
91                      new PropertyModel<>(privilege, Constants.DESCRIPTION_FIELD_NAME), false);
92              description.setRequired(false);
93              add(description);
94          }
95      }
96  
97      public static class Spec extends WizardStep {
98  
99          private static final long serialVersionUID = -3237113253888332643L;
100 
101         public Spec(final PrivilegeTO privilege) {
102             setTitleModel(privilege.getKey() == null
103                     ? new StringResourceModel("privilege.new")
104                     : new StringResourceModel("privilege.edit", Model.of(privilege)));
105 
106             add(new JsonEditorPanel(new PropertyModel<>(privilege, "spec")));
107         }
108     }
109 }