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 java.util.List;
23  import org.apache.commons.lang3.BooleanUtils;
24  import org.apache.syncope.client.console.wicket.markup.html.form.JsonEditorPanel;
25  import org.apache.syncope.client.console.wicket.markup.html.form.XMLEditorPanel;
26  import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxSpinnerFieldPanel;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
32  import org.apache.syncope.client.ui.commons.markup.html.form.EncryptedFieldPanel;
33  import org.apache.syncope.common.keymaster.client.api.DomainOps;
34  import org.apache.syncope.common.keymaster.client.api.model.Domain;
35  import org.apache.syncope.common.lib.types.CipherAlgorithm;
36  import org.apache.wicket.PageReference;
37  import org.apache.wicket.ajax.AjaxRequestTarget;
38  import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
39  import org.apache.wicket.extensions.wizard.WizardModel;
40  import org.apache.wicket.extensions.wizard.WizardStep;
41  import org.apache.wicket.model.Model;
42  import org.apache.wicket.model.PropertyModel;
43  
44  public class DomainWizardBuilder extends BaseAjaxWizardBuilder<Domain> {
45  
46      private static final long serialVersionUID = -6731030158762705250L;
47  
48      private static final List<String> JDBC_DRIVERS = List.of(
49              "org.postgresql.Driver",
50              "com.mysql.cj.jdbc.Driver",
51              "org.mariadb.jdbc.Driver",
52              "com.microsoft.sqlserver.jdbc.SQLServerDriver",
53              "oracle.jdbc.OracleDriver",
54              "org.h2.Driver");
55  
56      private static final List<String> DATABASE_PLATFORMS = List.of(
57              "org.apache.openjpa.jdbc.sql.PostgresDictionary",
58              "org.apache.openjpa.jdbc.sql.MySQLDictionary"
59              + "(blobTypeName=LONGBLOB,dateFractionDigits=3,useSetStringForClobs=true)",
60              "org.apache.openjpa.jdbc.sql.MariaDBDictionary"
61              + "(blobTypeName=LONGBLOB,dateFractionDigits=3)",
62              "org.apache.openjpa.jdbc.sql.SQLServerDictionary",
63              "org.apache.openjpa.jdbc.sql.OracleDictionary",
64              "org.apache.openjpa.jdbc.sql.H2Dictionary");
65  
66      private final DomainOps domainOps;
67  
68      public DomainWizardBuilder(final DomainOps domainOps, final Domain domain, final PageReference pageRef) {
69          super(domain, pageRef);
70          this.domainOps = domainOps;
71      }
72  
73      @Override
74      protected Serializable onApplyInternal(final Domain domain) {
75          domainOps.create(domain);
76          return domain;
77      }
78  
79      @Override
80      protected WizardModel buildModelSteps(final Domain domain, final WizardModel wizardModel) {
81          wizardModel.add(new Storage(domain));
82          wizardModel.add(new AdminCredentials(domain));
83          wizardModel.add(new Content(domain));
84          wizardModel.add(new KeymasterConfParams(domain, pageRef));
85          return wizardModel;
86      }
87  
88      public static class Storage extends WizardStep {
89  
90          private static final long serialVersionUID = 3671044119870133102L;
91  
92          public Storage(final Domain domain) {
93              add(new AjaxTextFieldPanel(
94                      "key",
95                      "key",
96                      new PropertyModel<>(domain, "key")).addRequiredLabel());
97  
98              AjaxDropDownChoicePanel<String> jdbcDriver = new AjaxDropDownChoicePanel<>(
99                      "jdbcDriver", "jdbcDriver", new PropertyModel<>(domain, "jdbcDriver"), false);
100             jdbcDriver.setChoices(JDBC_DRIVERS);
101             jdbcDriver.addRequiredLabel();
102             jdbcDriver.setNullValid(false);
103             add(jdbcDriver);
104 
105             add(new AjaxTextFieldPanel(
106                     "jdbcURL",
107                     "jdbcURL",
108                     new PropertyModel<>(domain, "jdbcURL")).addRequiredLabel());
109 
110             add(new AjaxTextFieldPanel(
111                     "dbSchema",
112                     "dbSchema",
113                     new PropertyModel<>(domain, "dbSchema")).setRequired(false));
114 
115             add(new AjaxTextFieldPanel(
116                     "dbUsername",
117                     "dbUsername",
118                     new PropertyModel<>(domain, "dbUsername")).addRequiredLabel());
119             add(new EncryptedFieldPanel(
120                     "dbPassword", "dbPassword", new PropertyModel<>(domain, "dbPassword"), false));
121 
122             AjaxDropDownChoicePanel<Domain.TransactionIsolation> transactionIsolation = new AjaxDropDownChoicePanel<>(
123                     "transactionIsolation", "transactionIsolation",
124                     new PropertyModel<>(domain, "transactionIsolation"), false);
125             transactionIsolation.setChoices(List.of(Domain.TransactionIsolation.values()));
126             transactionIsolation.addRequiredLabel();
127             transactionIsolation.setNullValid(false);
128             add(transactionIsolation);
129 
130             add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).build(
131                     "poolMaxActive",
132                     "poolMaxActive",
133                     Integer.class,
134                     new PropertyModel<>(domain, "poolMaxActive")).addRequiredLabel());
135             add(new AjaxSpinnerFieldPanel.Builder<Integer>().min(0).build(
136                     "poolMinIdle",
137                     "poolMinIdle",
138                     Integer.class,
139                     new PropertyModel<>(domain, "poolMinIdle")).addRequiredLabel());
140 
141             add(new AjaxTextFieldPanel(
142                     "auditSql",
143                     "auditSql",
144                     new PropertyModel<>(domain, "auditSql")).addRequiredLabel());
145 
146             add(new AjaxTextFieldPanel(
147                     "orm",
148                     "orm",
149                     new PropertyModel<>(domain, "orm")).addRequiredLabel());
150 
151             AjaxDropDownChoicePanel<String> databasePlatform = new AjaxDropDownChoicePanel<>(
152                     "databasePlatform", "databasePlatform", new PropertyModel<>(domain, "databasePlatform"), false);
153             databasePlatform.setChoices(DATABASE_PLATFORMS);
154             databasePlatform.addRequiredLabel();
155             databasePlatform.setNullValid(false);
156             add(databasePlatform);
157         }
158     }
159 
160     public static class AdminCredentials extends WizardStep {
161 
162         private static final long serialVersionUID = -7472243942630790243L;
163 
164         public AdminCredentials(final Domain domain) {
165             AjaxDropDownChoicePanel<CipherAlgorithm> adminCipherAlgorithm = new AjaxDropDownChoicePanel<>(
166                     "adminCipherAlgorithm", "adminCipherAlgorithm",
167                     new PropertyModel<>(domain, "adminCipherAlgorithm"), false);
168             adminCipherAlgorithm.setChoices(List.of(CipherAlgorithm.values()));
169             adminCipherAlgorithm.addRequiredLabel();
170             adminCipherAlgorithm.setNullValid(false);
171             add(adminCipherAlgorithm);
172 
173             EncryptedFieldPanel adminPassword = new EncryptedFieldPanel(
174                     "adminPassword", "adminPassword", new PropertyModel<>(domain, "adminPassword"), false);
175             adminPassword.addRequiredLabel();
176             add(adminPassword);
177         }
178     }
179 
180     public class Content extends WizardStep {
181 
182         private static final long serialVersionUID = -4214163958296844853L;
183 
184         public Content(final Domain domain) {
185             XMLEditorPanel content = new XMLEditorPanel(
186                     null, new PropertyModel<>(domain, "content"), false, pageRef);
187             content.setOutputMarkupPlaceholderTag(true);
188             content.setVisible(false);
189             add(content);
190 
191             AjaxCheckBoxPanel defaultContent = new AjaxCheckBoxPanel(
192                     "defaultContent", "defaultContent", Model.of(true), true);
193             defaultContent.getField().add(new AjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
194 
195                 private static final long serialVersionUID = -1107858522700306810L;
196 
197                 @Override
198                 protected void onUpdate(final AjaxRequestTarget target) {
199                     content.setVisible(!BooleanUtils.toBoolean(defaultContent.getField().getConvertedInput()));
200                     target.add(content);
201                 }
202             });
203             add(defaultContent);
204         }
205     }
206 
207     public static class KeymasterConfParams extends WizardStep {
208 
209         private static final long serialVersionUID = -8448363577805933925L;
210 
211         public KeymasterConfParams(final Domain domain, final PageReference pageRef) {
212             JsonEditorPanel keymasterConfParams = new JsonEditorPanel(
213                     null, new PropertyModel<>(domain, "keymasterConfParams"), false, pageRef);
214             keymasterConfParams.setOutputMarkupPlaceholderTag(true);
215             keymasterConfParams.setVisible(false);
216             add(keymasterConfParams);
217 
218             AjaxCheckBoxPanel defaultKeymasterConfParams = new AjaxCheckBoxPanel(
219                     "defaultKeymasterConfParams", "defaultKeymasterConfParams", Model.of(true), true);
220             defaultKeymasterConfParams.getField().add(new AjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
221 
222                 private static final long serialVersionUID = -6139318907146065915L;
223 
224                 @Override
225                 protected void onUpdate(final AjaxRequestTarget target) {
226                     keymasterConfParams.setVisible(
227                             !BooleanUtils.toBoolean(defaultKeymasterConfParams.getField().getConvertedInput()));
228                     target.add(keymasterConfParams);
229                 }
230             });
231             add(defaultKeymasterConfParams);
232         }
233     }
234 }