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.ui.commons.wizards.any;
20  
21  import org.apache.syncope.client.ui.commons.wizards.AjaxWizardBuilder;
22  import org.apache.syncope.common.lib.to.AnyTO;
23  import org.apache.syncope.common.lib.to.GroupableRelatableTO;
24  import org.apache.wicket.PageReference;
25  
26  public abstract class AbstractAnyWizardBuilder<A extends AnyTO> extends AjaxWizardBuilder<AnyWrapper<A>> {
27  
28      private static final long serialVersionUID = -2480279868319546243L;
29  
30      public AbstractAnyWizardBuilder(final AnyWrapper<A> defaultItem, final PageReference pageRef) {
31          super(defaultItem, pageRef);
32      }
33  
34      protected void fixPlainAndVirAttrs(final AnyTO updated, final AnyTO original) {
35          // re-add to the updated object any missing plain or virtual attribute (compared to original): this to cope with
36          // form layout, which might have not included some plain or virtual attributes
37          original.getPlainAttrs().stream().
38                  filter(attr -> updated.getPlainAttr(attr.getSchema()).isPresent()).
39                  forEach(attr -> updated.getPlainAttrs().add(attr));
40          original.getVirAttrs().stream().
41                  filter(attr -> updated.getVirAttr(attr.getSchema()).isPresent()).
42                  forEach(attr -> updated.getVirAttrs().add(attr));
43          if (updated instanceof GroupableRelatableTO && original instanceof GroupableRelatableTO) {
44              GroupableRelatableTO.class.cast(original).getMemberships()
45                      .forEach(oMemb -> GroupableRelatableTO.class.cast(updated).getMembership(oMemb.getGroupKey())
46                      .ifPresent(uMemb -> {
47                          oMemb.getPlainAttrs().stream().
48                                  filter(attr -> uMemb.getPlainAttr(attr.getSchema()).isPresent()).
49                                  forEach(attr -> uMemb.getPlainAttrs().add(attr));
50                          oMemb.getVirAttrs().stream().
51                                  filter(attr -> uMemb.getVirAttr(attr.getSchema()).isPresent()).
52                                  forEach(attr -> uMemb.getVirAttrs().add(attr));
53                      }));
54          }
55  
56          // remove from the updated object any plain or virtual attribute without values, thus triggering for removal in
57          // the generated patch
58          updated.getPlainAttrs().removeIf(attr -> attr.getValues().isEmpty());
59          updated.getVirAttrs().removeIf(attr -> attr.getValues().isEmpty());
60          if (updated instanceof GroupableRelatableTO) {
61              GroupableRelatableTO.class.cast(updated).getMemberships().forEach(memb -> {
62                  memb.getPlainAttrs().removeIf(attr -> attr.getValues().isEmpty());
63                  memb.getVirAttrs().removeIf(attr -> attr.getValues().isEmpty());
64              });
65          }
66      }
67  }