1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }