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.commons.lang3.StringUtils;
25  import org.apache.cxf.common.util.CollectionUtils;
26  import org.apache.syncope.client.console.layout.GroupForm;
27  import org.apache.syncope.client.console.layout.GroupFormLayoutInfo;
28  import org.apache.syncope.client.console.rest.GroupRestClient;
29  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
30  import org.apache.syncope.client.ui.commons.wizards.AjaxWizardBuilder;
31  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
32  import org.apache.syncope.common.lib.AnyOperations;
33  import org.apache.syncope.common.lib.EntityTOUtils;
34  import org.apache.syncope.common.lib.request.GroupCR;
35  import org.apache.syncope.common.lib.request.GroupUR;
36  import org.apache.syncope.common.lib.to.GroupTO;
37  import org.apache.syncope.common.lib.to.ProvisioningResult;
38  import org.apache.wicket.PageReference;
39  
40  public class GroupWizardBuilder extends AnyWizardBuilder<GroupTO> implements GroupForm {
41  
42      private static final long serialVersionUID = 5945391813567245081L;
43  
44      protected GroupRestClient groupRestClient;
45  
46      public GroupWizardBuilder(
47              final GroupTO groupTO,
48              final List<String> anyTypeClasses,
49              final GroupFormLayoutInfo formLayoutInfo,
50              final GroupRestClient groupRestClient,
51              final PageReference pageRef) {
52  
53          super(Optional.ofNullable(groupTO).map(GroupWrapper::new).
54                  orElse(null), anyTypeClasses, formLayoutInfo, pageRef);
55          this.groupRestClient = groupRestClient;
56      }
57  
58      /**
59       * Constructor to be used for Remediation details only.
60       *
61       * @param previousGroupTO previous group status.
62       * @param groupTO new group status to be approved.
63       * @param anyTypeClasses any type classes.
64       * @param formLayoutInfo from layout.
65       * @param pageRef reference page.
66       */
67      public GroupWizardBuilder(
68              final GroupTO previousGroupTO,
69              final GroupTO groupTO,
70              final List<String> anyTypeClasses,
71              final GroupFormLayoutInfo formLayoutInfo,
72              final PageReference pageRef) {
73  
74          super(new GroupWrapper(previousGroupTO, groupTO), anyTypeClasses, formLayoutInfo, pageRef);
75      }
76  
77      /**
78       * This method has been overridden to manage asynchronous translation of FIQL string to search classes list and
79       * viceversa.
80       *
81       * @param item wizard backend item.
82       * @return the current builder.
83       */
84      @Override
85      public AjaxWizardBuilder<AnyWrapper<GroupTO>> setItem(final AnyWrapper<GroupTO> item) {
86          return (AjaxWizardBuilder<AnyWrapper<GroupTO>>) (item != null
87                  ? super.setItem(new GroupWrapper(item.getInnerObject()))
88                  : super.setItem(null));
89      }
90  
91      @Override
92      protected Serializable onApplyInternal(final AnyWrapper<GroupTO> modelObject) {
93          GroupTO updated = modelObject instanceof GroupWrapper
94                  ? GroupWrapper.class.cast(modelObject).fillDynamicConditions()
95                  : modelObject.getInnerObject();
96  
97          ProvisioningResult<GroupTO> result;
98          if (updated.getKey() == null) {
99              GroupCR req = new GroupCR();
100             EntityTOUtils.toAnyCR(updated, req);
101             result = groupRestClient.create(req);
102         } else {
103             GroupTO original = getOriginalItem().getInnerObject();
104             fixPlainAndVirAttrs(updated, original);
105 
106             // SYNCOPE-1170
107             boolean othersNotEqualsOrBlanks =
108                     !updated.getADynMembershipConds().equals(original.getADynMembershipConds())
109                     || (StringUtils.isNotBlank(original.getUDynMembershipCond())
110                     && StringUtils.isBlank(updated.getUDynMembershipCond()))
111                     || (StringUtils.isBlank(original.getUDynMembershipCond())
112                     && StringUtils.isNotBlank(updated.getUDynMembershipCond()))
113                     || StringUtils.isAllBlank(original.getUDynMembershipCond(), updated.getUDynMembershipCond())
114                     || !updated.getUDynMembershipCond().equals(original.getUDynMembershipCond())
115                     || !CollectionUtils.diff(updated.getTypeExtensions(), original.getTypeExtensions()).isEmpty();
116 
117             GroupUR groupUR = AnyOperations.diff(updated, original, false);
118 
119             // update just if it is changed
120             if (groupUR.isEmpty() && !othersNotEqualsOrBlanks) {
121                 result = new ProvisioningResult<>();
122                 result.setEntity(updated);
123             } else {
124                 result = groupRestClient.update(original.getETagValue(), groupUR);
125             }
126         }
127 
128         return result;
129     }
130 
131     @Override
132     protected Optional<Details<GroupTO>> addOptionalDetailsPanel(final AnyWrapper<GroupTO> modelObject) {
133         return Optional.of(new GroupDetails(
134                 GroupWrapper.class.cast(modelObject),
135                 mode == AjaxWizard.Mode.TEMPLATE,
136                 modelObject.getInnerObject().getKey() != null,
137                 pageRef));
138     }
139 }