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.List;
23  import java.util.Optional;
24  import org.apache.syncope.client.console.layout.AnyObjectForm;
25  import org.apache.syncope.client.console.layout.AnyObjectFormLayoutInfo;
26  import org.apache.syncope.client.console.rest.AnyObjectRestClient;
27  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
28  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
29  import org.apache.syncope.common.lib.AnyOperations;
30  import org.apache.syncope.common.lib.EntityTOUtils;
31  import org.apache.syncope.common.lib.request.AnyObjectCR;
32  import org.apache.syncope.common.lib.request.AnyObjectUR;
33  import org.apache.syncope.common.lib.to.AnyObjectTO;
34  import org.apache.syncope.common.lib.to.ProvisioningResult;
35  import org.apache.wicket.PageReference;
36  
37  public class AnyObjectWizardBuilder extends AnyWizardBuilder<AnyObjectTO> implements AnyObjectForm {
38  
39      private static final long serialVersionUID = -2480279868319546243L;
40  
41      protected AnyObjectRestClient anyObjectRestClient;
42  
43      public AnyObjectWizardBuilder(
44              final AnyObjectTO anyObjectTO,
45              final List<String> anyTypeClasses,
46              final AnyObjectFormLayoutInfo formLayoutInfo,
47              final AnyObjectRestClient anyObjectRestClient,
48              final PageReference pageRef) {
49  
50          super(Optional.ofNullable(anyObjectTO).map(AnyObjectWrapper::new).
51                  orElse(null), anyTypeClasses, formLayoutInfo, pageRef);
52          this.anyObjectRestClient = anyObjectRestClient;
53      }
54  
55      /**
56       * Constructor to be used for Remediation details only.
57       *
58       * @param previousAnyObjectTO previous anyObject status.
59       * @param anyObjectTO new anyObject status to be approved.
60       * @param anyTypeClasses any type classes.
61       * @param formLayoutInfo from layout.
62       * @param pageRef reference page.
63       */
64      public AnyObjectWizardBuilder(
65              final AnyObjectTO previousAnyObjectTO,
66              final AnyObjectTO anyObjectTO,
67              final List<String> anyTypeClasses,
68              final AnyObjectFormLayoutInfo formLayoutInfo,
69              final PageReference pageRef) {
70  
71          super(new AnyObjectWrapper(previousAnyObjectTO, anyObjectTO), anyTypeClasses, formLayoutInfo, pageRef);
72      }
73  
74      @Override
75      protected Serializable onApplyInternal(final AnyWrapper<AnyObjectTO> modelObject) {
76          final AnyObjectTO inner = modelObject.getInnerObject();
77  
78          ProvisioningResult<AnyObjectTO> result;
79          if (inner.getKey() == null) {
80              AnyObjectCR req = new AnyObjectCR();
81              EntityTOUtils.toAnyCR(inner, req);
82  
83              result = anyObjectRestClient.create(req);
84          } else {
85              fixPlainAndVirAttrs(inner, getOriginalItem().getInnerObject());
86              AnyObjectUR req = AnyOperations.diff(inner, getOriginalItem().getInnerObject(), false);
87  
88              // update just if it is changed
89              if (req.isEmpty()) {
90                  result = new ProvisioningResult<>();
91                  result.setEntity(inner);
92              } else {
93                  result = anyObjectRestClient.update(getOriginalItem().getInnerObject().getETagValue(), req);
94              }
95          }
96  
97          return result;
98      }
99  
100     @Override
101     protected Optional<Details<AnyObjectTO>> addOptionalDetailsPanel(final AnyWrapper<AnyObjectTO> modelObject) {
102         return Optional.of(new AnyObjectDetails(
103                 modelObject,
104                 mode == AjaxWizard.Mode.TEMPLATE,
105                 modelObject.getInnerObject().getKey() != null,
106                 pageRef));
107     }
108 }