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.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Optional;
25  import java.util.function.Function;
26  import java.util.stream.Collectors;
27  import org.apache.syncope.common.lib.policy.DefaultPullCorrelationRuleConf;
28  import org.apache.syncope.common.lib.policy.PullCorrelationRuleConf;
29  import org.apache.syncope.common.lib.to.Item;
30  import org.apache.syncope.common.lib.to.Provision;
31  import org.apache.syncope.core.persistence.api.dao.search.AnyCond;
32  import org.apache.syncope.core.persistence.api.dao.search.AttrCond;
33  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
34  import org.apache.syncope.core.provisioning.api.rules.PullCorrelationRule;
35  import org.apache.syncope.core.provisioning.api.rules.PullCorrelationRuleConfClass;
36  import org.identityconnectors.framework.common.objects.Attribute;
37  import org.identityconnectors.framework.common.objects.SyncDelta;
38  
39  @PullCorrelationRuleConfClass(DefaultPullCorrelationRuleConf.class)
40  public class DefaultPullCorrelationRule implements PullCorrelationRule {
41  
42      private DefaultPullCorrelationRuleConf conf;
43  
44      @Override
45      public void setConf(final PullCorrelationRuleConf conf) {
46          if (conf instanceof DefaultPullCorrelationRuleConf) {
47              this.conf = DefaultPullCorrelationRuleConf.class.cast(conf);
48          } else {
49              throw new IllegalArgumentException(
50                      DefaultPullCorrelationRuleConf.class.getName() + " expected, got " + conf.getClass().getName());
51          }
52      }
53  
54      @Override
55      public SearchCond getSearchCond(final SyncDelta syncDelta, final Provision provision) {
56          Map<String, Item> mappingItems = provision.getMapping().getItems().stream().
57                  collect(Collectors.toMap(Item::getIntAttrName, Function.identity()));
58  
59          // search for anys by attribute(s) specified in the policy
60          List<SearchCond> searchConds = new ArrayList<>();
61  
62          conf.getSchemas().forEach(schema -> {
63              Attribute attr = Optional.ofNullable(mappingItems.get(schema)).
64                      map(item -> syncDelta.getObject().getAttributeByName(item.getExtAttrName())).orElse(null);
65              if (attr == null) {
66                  throw new IllegalArgumentException(
67                          "Connector object does not contains the attributes to perform the search: " + schema);
68              }
69  
70              AttrCond.Type type;
71              String expression = null;
72  
73              if (attr.getValue() == null || attr.getValue().isEmpty()
74                      || (attr.getValue().size() == 1 && attr.getValue().get(0) == null)) {
75  
76                  type = AttrCond.Type.ISNULL;
77              } else {
78                  type = AttrCond.Type.EQ;
79                  expression = attr.getValue().size() > 1
80                          ? attr.getValue().toString()
81                          : attr.getValue().get(0).toString();
82              }
83  
84              AttrCond cond = "key".equalsIgnoreCase(schema)
85                      || "username".equalsIgnoreCase(schema) || "name".equalsIgnoreCase(schema)
86                      ? new AnyCond()
87                      : new AttrCond();
88              cond.setSchema(schema);
89              cond.setType(type);
90              cond.setExpression(expression);
91  
92              searchConds.add(SearchCond.getLeaf(cond));
93          });
94  
95          return conf.isOrSchemas()
96                  ? SearchCond.getOr(searchConds)
97                  : SearchCond.getAnd(searchConds);
98      }
99  }