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.core.provisioning.java.pushpull;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.common.lib.request.AnyCR;
27  import org.apache.syncope.common.lib.request.AnyUR;
28  import org.apache.syncope.common.lib.request.AttrPatch;
29  import org.apache.syncope.common.lib.request.GroupCR;
30  import org.apache.syncope.common.lib.request.GroupUR;
31  import org.apache.syncope.common.lib.to.AnyTO;
32  import org.apache.syncope.common.lib.to.GroupTO;
33  import org.apache.syncope.common.lib.to.PropagationStatus;
34  import org.apache.syncope.common.lib.to.ProvisioningReport;
35  import org.apache.syncope.common.lib.types.AnyTypeKind;
36  import org.apache.syncope.common.lib.types.PatchOperation;
37  import org.apache.syncope.core.persistence.api.entity.Any;
38  import org.apache.syncope.core.persistence.api.entity.AnyUtils;
39  import org.apache.syncope.core.persistence.api.entity.group.Group;
40  import org.apache.syncope.core.provisioning.api.GroupProvisioningManager;
41  import org.apache.syncope.core.provisioning.api.ProvisioningManager;
42  import org.apache.syncope.core.provisioning.api.WorkflowResult;
43  import org.apache.syncope.core.provisioning.api.pushpull.GroupPullResultHandler;
44  import org.identityconnectors.framework.common.objects.SyncDelta;
45  import org.springframework.beans.factory.annotation.Autowired;
46  
47  public class DefaultGroupPullResultHandler extends AbstractPullResultHandler implements GroupPullResultHandler {
48  
49      @Autowired
50      private GroupProvisioningManager groupProvisioningManager;
51  
52      private final Map<String, String> groupOwnerMap = new HashMap<>();
53  
54      @Override
55      public Map<String, String> getGroupOwnerMap() {
56          return this.groupOwnerMap;
57      }
58  
59      @Override
60      protected AnyUtils getAnyUtils() {
61          return anyUtilsFactory.getInstance(AnyTypeKind.GROUP);
62      }
63  
64      @Override
65      protected String getName(final AnyTO anyTO) {
66          return GroupTO.class.cast(anyTO).getName();
67      }
68  
69      @Override
70      protected String getName(final AnyCR anyCR) {
71          return GroupCR.class.cast(anyCR).getName();
72      }
73  
74      @Override
75      protected ProvisioningManager<?, ?> getProvisioningManager() {
76          return groupProvisioningManager;
77      }
78  
79      @Override
80      protected AnyTO getAnyTO(final Any<?> any) {
81          return groupDataBinder.getGroupTO((Group) any, true);
82      }
83  
84      @Override
85      protected WorkflowResult<? extends AnyUR> update(final AnyUR req) {
86          return gwfAdapter.update((GroupUR) req, profile.getExecutor(), getContext());
87      }
88  
89      @Override
90      protected AnyTO doCreate(final AnyCR anyCR, final SyncDelta delta) {
91          GroupCR groupCR = GroupCR.class.cast(anyCR);
92  
93          Map.Entry<String, List<PropagationStatus>> created = groupProvisioningManager.create(
94                  groupCR,
95                  groupOwnerMap,
96                  Set.of(profile.getTask().getResource().getKey()),
97                  true,
98                  profile.getExecutor(),
99                  getContext());
100 
101         return groupDataBinder.getGroupTO(created.getKey());
102     }
103 
104     @Override
105     protected AnyUR doUpdate(
106             final AnyTO before,
107             final AnyUR req,
108             final SyncDelta delta,
109             final ProvisioningReport result) {
110 
111         GroupUR groupUR = GroupUR.class.cast(req);
112 
113         Pair<GroupUR, List<PropagationStatus>> updated = groupProvisioningManager.update(
114                 groupUR,
115                 Set.of(profile.getTask().getResource().getKey()),
116                 true,
117                 profile.getExecutor(),
118                 getContext());
119 
120         createRemediationIfNeeded(req, delta, result);
121 
122         String groupOwner = null;
123         for (AttrPatch attrPatch : groupUR.getPlainAttrs()) {
124             if (attrPatch.getOperation() == PatchOperation.ADD_REPLACE && attrPatch.getAttr() != null
125                     && attrPatch.getAttr().getSchema().isEmpty() && !attrPatch.getAttr().getValues().isEmpty()) {
126 
127                 groupOwner = attrPatch.getAttr().getValues().get(0);
128             }
129         }
130         if (groupOwner != null) {
131             groupOwnerMap.put(updated.getLeft().getKey(), groupOwner);
132         }
133 
134         return req;
135     }
136 }