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.wizards.any;
20  
21  import java.util.List;
22  import java.util.Optional;
23  import org.apache.syncope.client.console.commons.RealmsUtils;
24  import org.apache.syncope.client.console.layout.UserFormLayoutInfo;
25  import org.apache.syncope.client.console.rest.UserRestClient;
26  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
27  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
28  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
29  import org.apache.syncope.common.lib.to.RealmTO;
30  import org.apache.syncope.common.lib.to.TemplatableTO;
31  import org.apache.syncope.common.lib.to.UserTO;
32  import org.apache.syncope.common.lib.types.AnyTypeKind;
33  import org.apache.wicket.PageReference;
34  
35  public class UserTemplateWizardBuilder extends UserWizardBuilder implements TemplateWizardBuilder<UserTO> {
36  
37      private static final long serialVersionUID = 6716803168859873877L;
38  
39      protected final TemplatableTO templatable;
40  
41      public UserTemplateWizardBuilder(
42              final UserTO template,
43              final List<String> anyTypeClasses,
44              final UserFormLayoutInfo formLayoutInfo,
45              final UserRestClient userRestClient,
46              final PageReference pageRef) {
47  
48          super(anyTypeClasses, formLayoutInfo, userRestClient, pageRef);
49          templatable = null;
50  
51          if (template == null) {
52              setItem(new UserWrapper(new UserTO()));
53          } else {
54              setItem(new UserWrapper(template));
55          }
56      }
57  
58      public UserTemplateWizardBuilder(
59              final TemplatableTO templatable,
60              final List<String> anyTypeClasses,
61              final UserFormLayoutInfo formLayoutInfo,
62              final UserRestClient userRestClient,
63              final PageReference pageRef) {
64  
65          super(anyTypeClasses, formLayoutInfo, userRestClient, pageRef);
66          this.templatable = templatable;
67  
68          if (templatable.getTemplates().containsKey(AnyTypeKind.USER.name())) {
69              setItem(new UserWrapper(UserTO.class.cast(templatable.getTemplates().get(AnyTypeKind.USER.name()))));
70          } else {
71              UserTO userTO = new UserTO();
72              if (templatable instanceof RealmTO) {
73                  userTO.setRealm(
74                          String.format("'%s'", RealmsUtils.getFullPath(RealmTO.class.cast(templatable).getFullPath())));
75              }
76              setItem(new UserWrapper(userTO));
77          }
78      }
79  
80      @Override
81      protected Optional<Details<UserTO>> addOptionalDetailsPanel(final AnyWrapper<UserTO> modelObject) {
82          Optional<Details<UserTO>> details = super.addOptionalDetailsPanel(modelObject);
83          if (templatable instanceof RealmTO) {
84              details.ifPresent(Details::disableRealmSpecification);
85          }
86          return details;
87      }
88  
89      @Override
90      public AjaxWizard<AnyWrapper<UserTO>> build(final String id) {
91          return super.build(id, AjaxWizard.Mode.TEMPLATE);
92      }
93  }