/
AssociationFromLinkExpressionEvaluator.java
265 lines (225 loc) · 12.9 KB
/
AssociationFromLinkExpressionEvaluator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright (c) 2014-2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.model.common.expression.evaluator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition;
import com.evolveum.midpoint.model.api.context.AssignmentPath;
import com.evolveum.midpoint.model.api.context.AssignmentPathSegment;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ItemDeltaUtil;
import com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.builder.S_AtomicFilterExit;
import com.evolveum.midpoint.repo.common.ObjectResolver;
import com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext;
import com.evolveum.midpoint.repo.common.expression.ExpressionEvaluator;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.ResultHandler;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.ExpressionConstants;
import com.evolveum.midpoint.schema.expression.TypedValue;
import com.evolveum.midpoint.schema.processor.ObjectFactory;
import com.evolveum.midpoint.schema.processor.ResourceAttribute;
import com.evolveum.midpoint.schema.processor.ResourceAttributeContainer;
import com.evolveum.midpoint.schema.util.FocusTypeUtil;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractRoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssociationFromLinkExpressionEvaluatorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAssociationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowDiscriminatorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import org.apache.commons.collections4.CollectionUtils;
import static com.evolveum.midpoint.schema.GetOperationOptions.createNoFetchCollection;
/**
* @author Radovan Semancik
*
*/
public class AssociationFromLinkExpressionEvaluator
implements ExpressionEvaluator<PrismContainerValue<ShadowAssociationType>,
PrismContainerDefinition<ShadowAssociationType>> {
private static final Trace LOGGER = TraceManager.getTrace(AssociationFromLinkExpressionEvaluator.class);
private AssociationFromLinkExpressionEvaluatorType evaluatorType;
private PrismContainerDefinition<ShadowAssociationType> outputDefinition;
private ObjectResolver objectResolver;
private PrismContext prismContext;
AssociationFromLinkExpressionEvaluator(AssociationFromLinkExpressionEvaluatorType evaluatorType,
PrismContainerDefinition<ShadowAssociationType> outputDefinition, ObjectResolver objectResolver, PrismContext prismContext) {
this.evaluatorType = evaluatorType;
this.outputDefinition = outputDefinition;
this.objectResolver = objectResolver;
this.prismContext = prismContext;
}
/* (non-Javadoc)
* @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java.util.Collection, java.util.Map, boolean, java.lang.String, com.evolveum.midpoint.schema.result.OperationResult)
*/
@Override
public PrismValueDeltaSetTriple<PrismContainerValue<ShadowAssociationType>> evaluate(ExpressionEvaluationContext context) throws SchemaException,
ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
String desc = context.getContextDescription();
AbstractRoleType thisRole;
Integer assignmentPathIndex = evaluatorType.getAssignmentPathIndex();
if (assignmentPathIndex == null) {
// Legacy ... or default in simple cases
@SuppressWarnings("unchecked")
TypedValue<AbstractRoleType> orderOneObjectTypedValue = context.getVariables().get(ExpressionConstants.VAR_ORDER_ONE_OBJECT);
if (orderOneObjectTypedValue == null || orderOneObjectTypedValue.getValue() == null) {
throw new ExpressionEvaluationException("No order one object variable in "+desc+"; the expression may be used in a wrong place. It is only supposed to work in a role.");
}
Object orderOneObject = orderOneObjectTypedValue.getValue();
if (!(orderOneObject instanceof AbstractRoleType)) {
throw new ExpressionEvaluationException("Order one object variable in "+desc+" is not a role, it is "+orderOneObject.getClass().getName()
+"; the expression may be used in a wrong place. It is only supposed to work in a role.");
}
thisRole = (AbstractRoleType)orderOneObject;
} else {
@SuppressWarnings("unchecked")
TypedValue<AssignmentPath> assignmentPathTypedValue = context.getVariables().get(ExpressionConstants.VAR_ASSIGNMENT_PATH);
if (assignmentPathTypedValue == null || assignmentPathTypedValue.getValue() == null) {
throw new ExpressionEvaluationException("No assignment path variable in "+desc+"; the expression may be used in a wrong place. It is only supposed to work in a role.");
}
AssignmentPath assignmentPath = (AssignmentPath) assignmentPathTypedValue.getValue();
if (assignmentPath.isEmpty()) {
throw new ExpressionEvaluationException("Empty assignment path variable in "+desc+"; the expression may be used in a wrong place. It is only supposed to work in a role.");
}
LOGGER.trace("ASSPATH {}:\n{}", evaluatorType.getDescription(), assignmentPath.debugDumpLazily(1));
AssignmentPathSegment segment;
try {
segment = assignmentPath.getSegment(assignmentPathIndex);
} catch (IndexOutOfBoundsException e) {
throw new ExpressionEvaluationException("Wrong assignment path index in "+desc+"; Index "+assignmentPathIndex+" cannot be applied to a path of legth "+assignmentPath.size(), e);
}
thisRole = (AbstractRoleType) segment.getSource();
}
LOGGER.trace("thisRole {}: {}", evaluatorType.getDescription(), thisRole);
LOGGER.trace("Evaluating association from link on: {}", thisRole);
TypedValue<RefinedObjectClassDefinition> rAssocTargetDefTypedValue = context.getVariables().get(ExpressionConstants.VAR_ASSOCIATION_TARGET_OBJECT_CLASS_DEFINITION);
if (rAssocTargetDefTypedValue == null || rAssocTargetDefTypedValue.getValue() == null) {
throw new ExpressionEvaluationException("No association target object class definition variable in "+desc+"; the expression may be used in a wrong place. It is only supposed to create an association.");
}
RefinedObjectClassDefinition rAssocTargetDef = (RefinedObjectClassDefinition) rAssocTargetDefTypedValue.getValue();
ShadowDiscriminatorType projectionDiscriminator = evaluatorType.getProjectionDiscriminator();
if (projectionDiscriminator == null) {
throw new ExpressionEvaluationException("No projectionDiscriminator in "+desc);
}
ShadowKindType kind = projectionDiscriminator.getKind();
if (kind == null) {
throw new ExpressionEvaluationException("No kind in projectionDiscriminator in "+desc);
}
String intent = projectionDiscriminator.getIntent();
PrismContainer<ShadowAssociationType> output = outputDefinition.instantiate();
QName assocName = context.getMappingQName();
String resourceOid = rAssocTargetDef.getResourceOid();
Collection<SelectorOptions<GetOperationOptions>> options = null;
List<String> candidateShadowOidList = new ArrayList<>();
// Always process the first role (myself) regardless of recursion setting
gatherCandidateShadowsFromAbstractRole(thisRole, candidateShadowOidList);
if (thisRole instanceof OrgType && matchesForRecursion((OrgType)thisRole)) {
gatherCandidateShadowsFromAbstractRoleRecurse((OrgType)thisRole, candidateShadowOidList, options, desc, context);
}
LOGGER.trace("Candidate shadow OIDs: {}", candidateShadowOidList);
selectMatchingShadows(candidateShadowOidList, output, resourceOid, kind, intent, assocName, context);
return ItemDeltaUtil.toDeltaSetTriple(output, null, prismContext);
}
private void selectMatchingShadows(List<String> candidateShadowsOidList,
PrismContainer<ShadowAssociationType> output, String resourceOid, ShadowKindType kind,
String intent, QName assocName, ExpressionEvaluationContext params)
throws SchemaException {
S_AtomicFilterExit filter = prismContext.queryFor(ShadowType.class)
.id(candidateShadowsOidList.toArray(new String[0]))
.and().item(ShadowType.F_RESOURCE_REF).ref(resourceOid)
.and().item(ShadowType.F_KIND).eq(kind);
if (intent != null) {
filter = filter.and().item(ShadowType.F_INTENT).eq(intent);
}
ObjectQuery query = filter.build();
ResultHandler<ShadowType> handler = (object, parentResult) -> {
PrismContainerValue<ShadowAssociationType> newValue = output.createNewValue();
ShadowAssociationType shadowAssociationType = newValue.asContainerable();
shadowAssociationType.setName(assocName);
toAssociation(object, shadowAssociationType);
return true;
};
try {
objectResolver.searchIterative(ShadowType.class, query, createNoFetchCollection(), handler, params.getTask(), params.getResult());
} catch (CommonException e) {
throw new SystemException("Couldn't search for relevant shadows: " + e.getMessage(), e);
}
}
private void toAssociation(PrismObject<ShadowType> shadow, ShadowAssociationType shadowAssociationType) {
shadowAssociationType.setShadowRef(new ObjectReferenceType().oid(shadow.getOid()).type(ShadowType.COMPLEX_TYPE));
// We also need to add identifiers here. Otherwise the delta won't match the shadow association.
// And therefore new values won't be computed correctly (MID-4948)
// This is not a clean systemic solution. But there was no time for a better solution before 3.9 release.
try {
ResourceAttributeContainer shadowAttributesContainer = ShadowUtil.getAttributesContainer(shadow);
ResourceAttributeContainer identifiersContainer = ObjectFactory.createResourceAttributeContainer(
ShadowAssociationType.F_IDENTIFIERS, shadowAttributesContainer.getDefinition(), prismContext);
shadowAssociationType.asPrismContainerValue().add(identifiersContainer);
Collection<ResourceAttribute<?>> shadowIdentifiers = ShadowUtil.getAllIdentifiers(shadow);
for (ResourceAttribute<?> shadowIdentifier : shadowIdentifiers) {
identifiersContainer.add(shadowIdentifier.clone());
}
} catch (SchemaException e) {
// Should not happen
throw new SystemException(e.getMessage(), e);
}
}
private void gatherCandidateShadowsFromAbstractRole(AbstractRoleType thisRole, List<String> candidateShadowsOidList) {
for (ObjectReferenceType linkRef: thisRole.getLinkRef()) {
CollectionUtils.addIgnoreNull(candidateShadowsOidList, linkRef.getOid());
}
}
private void gatherCandidateShadowsFromAbstractRoleRecurse(OrgType thisOrg,
List< String > candidateShadowsOidList,
Collection<SelectorOptions<GetOperationOptions>> options,
String desc, ExpressionEvaluationContext params) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
for (ObjectReferenceType parentOrgRef: thisOrg.getParentOrgRef()) {
OrgType parent = objectResolver.resolve(parentOrgRef, OrgType.class, options, desc, params.getTask(), params.getResult());
if (matchesForRecursion(parent)) {
gatherCandidateShadowsFromAbstractRole(parent, candidateShadowsOidList);
gatherCandidateShadowsFromAbstractRoleRecurse(parent, candidateShadowsOidList, options, desc, params);
}
}
}
private boolean matchesForRecursion(OrgType thisOrg) {
for (String recurseUpOrgType: evaluatorType.getRecurseUpOrgType()) {
if (FocusTypeUtil.determineSubTypes(thisOrg).contains(recurseUpOrgType)) {
return true;
}
}
return false;
}
/* (non-Javadoc)
* @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#shortDebugDump()
*/
@Override
public String shortDebugDump() {
return "associationFromLink";
}
}