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.io.Serializable;
22  import java.util.HashSet;
23  import java.util.Set;
24  import java.util.stream.Collectors;
25  import org.apache.syncope.client.console.layout.LinkedAccountForm;
26  import org.apache.syncope.client.console.layout.LinkedAccountFormLayoutInfo;
27  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
28  import org.apache.syncope.client.console.rest.UserRestClient;
29  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal.ModalEvent;
30  import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
31  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
32  import org.apache.syncope.client.ui.commons.wizards.any.EntityWrapper;
33  import org.apache.syncope.common.lib.Attr;
34  import org.apache.syncope.common.lib.request.LinkedAccountUR;
35  import org.apache.syncope.common.lib.request.UserUR;
36  import org.apache.syncope.common.lib.to.LinkedAccountTO;
37  import org.apache.syncope.common.lib.to.UserTO;
38  import org.apache.wicket.PageReference;
39  import org.apache.wicket.ajax.AjaxRequestTarget;
40  import org.apache.wicket.extensions.wizard.WizardModel;
41  import org.apache.wicket.model.IModel;
42  
43  /**
44   * Accounts wizard builder.
45   */
46  public class LinkedAccountWizardBuilder extends BaseAjaxWizardBuilder<LinkedAccountTO> implements LinkedAccountForm {
47  
48      private static final long serialVersionUID = -9142332740863374891L;
49  
50      protected final IModel<UserTO> model;
51  
52      protected LinkedAccountFormLayoutInfo formLayoutInfo;
53  
54      protected final UserRestClient userRestClient;
55  
56      protected final AnyTypeRestClient anyTypeRestClient;
57  
58      public LinkedAccountWizardBuilder(
59              final IModel<UserTO> model,
60              final LinkedAccountFormLayoutInfo formLayoutInfo,
61              final UserRestClient userRestClient,
62              final AnyTypeRestClient anyTypeRestClient,
63              final PageReference pageRef) {
64  
65          super(new LinkedAccountTO(), pageRef);
66  
67          this.model = model;
68          this.formLayoutInfo = formLayoutInfo;
69          this.userRestClient = userRestClient;
70          this.anyTypeRestClient = anyTypeRestClient;
71      }
72  
73      @Override
74      public AjaxWizard<LinkedAccountTO> build(final String id, final AjaxWizard.Mode mode) {
75          return super.build(id, mode);
76      }
77  
78      @Override
79      protected WizardModel buildModelSteps(final LinkedAccountTO modelObject, final WizardModel wizardModel) {
80          wizardModel.add(new LinkedAccountDetailsPanel(modelObject));
81          if (formLayoutInfo.isCredentials()) {
82              wizardModel.add(new LinkedAccountCredentialsPanel(
83                      new EntityWrapper<>(modelObject), formLayoutInfo.getWhichCredentials()));
84          }
85  
86          if (formLayoutInfo.isPlainAttrs()) {
87              wizardModel.add(new LinkedAccountPlainAttrsPanel(
88                      new EntityWrapper<>(modelObject),
89                      model.getObject(),
90                      anyTypeRestClient.read(model.getObject().getType()).getClasses(),
91                      formLayoutInfo.getWhichPlainAttrs()));
92          }
93  
94          if (formLayoutInfo.isPrivileges()) {
95              wizardModel.add(new LinkedAccountPrivilegesPanel(modelObject));
96          }
97  
98          return wizardModel;
99      }
100 
101     @Override
102     protected Serializable onApplyInternal(final LinkedAccountTO modelObject) {
103         fixPlainAttrs(modelObject);
104 
105         LinkedAccountUR linkedAccountPatch = new LinkedAccountUR.Builder().linkedAccountTO(modelObject).build();
106         linkedAccountPatch.setLinkedAccountTO(modelObject);
107         UserUR req = new UserUR();
108         req.setKey(model.getObject().getKey());
109         req.getLinkedAccounts().add(linkedAccountPatch);
110         model.setObject(userRestClient.update(model.getObject().getETagValue(), req).getEntity());
111 
112         return modelObject;
113     }
114 
115     private void fixPlainAttrs(final LinkedAccountTO linkedAccountTO) {
116         Set<Attr> validAttrs = new HashSet<>(linkedAccountTO.getPlainAttrs().stream().
117                 filter(attr -> !attr.getValues().isEmpty()).
118                 collect(Collectors.toSet()));
119         linkedAccountTO.getPlainAttrs().clear();
120         linkedAccountTO.getPlainAttrs().addAll(validAttrs);
121     }
122 
123     @Override
124     protected Serializable getCreateCustomPayloadEvent(final Serializable afterObject, final AjaxRequestTarget target) {
125         LinkedAccountTO linkedAccountTO = LinkedAccountTO.class.cast(afterObject);
126         return new CreateEvent(
127                 linkedAccountTO.getConnObjectKeyValue(),
128                 model.getObject(),
129                 target);
130     }
131 
132     private static class CreateEvent extends ModalEvent {
133 
134         private static final long serialVersionUID = 6416834092156281986L;
135 
136         private final String key;
137 
138         private final UserTO userTO;
139 
140         CreateEvent(
141                 final String key,
142                 final UserTO userTO,
143                 final AjaxRequestTarget target) {
144 
145             super(target);
146             this.key = key;
147             this.userTO = userTO;
148         }
149 
150         public String getKey() {
151             return key;
152         }
153 
154         public UserTO getUserTO() {
155             return userTO;
156         }
157     }
158 }