use of com.evolveum.midpoint.model.impl.lens.LensFocusContext in project midpoint by Evolveum.
the class AssignmentProcessor method setReferences.
private <F extends ObjectType> void setReferences(LensFocusContext<F> focusContext, QName name, Collection<PrismReferenceValue> targetState) throws SchemaException {
ItemName itemName = ItemName.fromQName(name);
PrismObject<F> focusOld = focusContext.getObjectOld();
if (focusOld == null) {
if (targetState.isEmpty()) {
return;
}
} else {
PrismReference existingState = focusOld.findReference(itemName);
if (existingState == null || existingState.isEmpty()) {
if (targetState.isEmpty()) {
return;
}
} else {
// we don't use QNameUtil.match here, because we want to ensure we store qualified values there
// (and newValues are all qualified)
Comparator<PrismReferenceValue> comparator = (a, b) -> 2 * a.getOid().compareTo(b.getOid()) + (Objects.equals(a.getRelation(), b.getRelation()) ? 0 : 1);
if (MiscUtil.unorderedCollectionCompare(targetState, existingState.getValues(), comparator)) {
return;
}
}
}
PrismReferenceDefinition itemDef = focusContext.getObjectDefinition().findItemDefinition(itemName, PrismReferenceDefinition.class);
ReferenceDelta itemDelta = prismContext.deltaFactory().reference().create(itemName, itemDef);
itemDelta.setValuesToReplace(targetState);
focusContext.swallowToSecondaryDelta(itemDelta);
}
use of com.evolveum.midpoint.model.impl.lens.LensFocusContext in project midpoint by Evolveum.
the class AssignmentProcessor method evaluateFocusMappings.
private <AH extends AssignmentHolderType> void evaluateFocusMappings(LensContext<AH> context, XMLGregorianCalendar now, LensFocusContext<AH> focusContext, DeltaSetTriple<EvaluatedAssignmentImpl<AH>> evaluatedAssignmentTriple, Task task, OperationResult parentResult) throws SchemaException, ExpressionEvaluationException, PolicyViolationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException, CommunicationException {
OperationResult result = parentResult.subresult(OP_EVALUATE_FOCUS_MAPPINGS).setMinor().build();
try {
LOGGER.trace("Starting evaluation of assignment-held mappings");
ObjectDeltaObject<AH> focusOdoRelative = focusContext.getObjectDeltaObjectRelative();
List<AssignedFocusMappingEvaluationRequest> allRequests = new ArrayList<>();
for (EvaluatedAssignmentImpl<AH> evaluatedAssignment : evaluatedAssignmentTriple.getAllValues()) {
allRequests.addAll(evaluatedAssignment.getFocusMappingEvaluationRequests());
}
FocalMappingSetEvaluation.TripleCustomizer<?, ?> customizer = (triple, abstractRequest) -> {
if (triple == null) {
return null;
}
DeltaSetTriple<ItemValueWithOrigin<PrismValue, ItemDefinition<?>>> rv = prismContext.deltaFactory().createDeltaSetTriple();
AssignedFocusMappingEvaluationRequest request = (AssignedFocusMappingEvaluationRequest) abstractRequest;
// noinspection unchecked
EvaluatedAssignmentImpl<AH> evaluatedAssignment = (EvaluatedAssignmentImpl<AH>) request.getEvaluatedAssignment();
PlusMinusZero relativeMode = request.getRelativeMode();
Set<PlusMinusZero> presence = new HashSet<>();
PlusMinusZero resultingMode = null;
if (evaluatedAssignmentTriple.presentInPlusSet(evaluatedAssignment)) {
resultingMode = PlusMinusZero.compute(PlusMinusZero.PLUS, relativeMode);
presence.add(PlusMinusZero.PLUS);
}
if (evaluatedAssignmentTriple.presentInMinusSet(evaluatedAssignment)) {
resultingMode = PlusMinusZero.compute(PlusMinusZero.MINUS, relativeMode);
presence.add(PlusMinusZero.MINUS);
}
if (evaluatedAssignmentTriple.presentInZeroSet(evaluatedAssignment)) {
resultingMode = PlusMinusZero.compute(PlusMinusZero.ZERO, relativeMode);
presence.add(PlusMinusZero.ZERO);
}
LOGGER.trace("triple customizer: presence = {}, relativeMode = {}, resultingMode = {}", presence, relativeMode, resultingMode);
if (presence.isEmpty()) {
throw new IllegalStateException("Evaluated assignment is not present in any of plus/minus/zero sets " + "of the triple. Assignment = " + evaluatedAssignment + ", triple = " + triple);
} else if (presence.size() > 1) {
// TODO think about this
throw new IllegalStateException("Evaluated assignment is present in more than one plus/minus/zero sets " + "of the triple: " + presence + ". Assignment = " + evaluatedAssignment + ", triple = " + triple);
}
if (resultingMode != null) {
switch(resultingMode) {
case PLUS:
// MID-6403
rv.addAllToPlusSet(triple.getNonNegativeValues());
break;
case MINUS:
// MID-6403
rv.addAllToMinusSet(triple.getNonPositiveValues());
break;
case ZERO:
rv = triple;
break;
}
}
return rv;
};
FocalMappingSetEvaluation.EvaluatedMappingConsumer mappingConsumer = (mapping, abstractRequest) -> {
AssignedFocusMappingEvaluationRequest request = (AssignedFocusMappingEvaluationRequest) abstractRequest;
request.getEvaluatedAssignment().addFocusMapping(mapping);
};
TargetObjectSpecification<AH> targetSpecification = new FixedTargetSpecification<>(focusOdoRelative.getNewObject(), true);
MappingEvaluationEnvironment env = new MappingEvaluationEnvironment("focus mappings in assignments of " + focusContext.getHumanReadableName(), now, task);
FocalMappingSetEvaluation<AH, AH> mappingSetEvaluation = new FocalMappingSetEvaluationBuilder<AH, AH>().context(context).evaluationRequests(allRequests).phase(null).focusOdo(focusOdoRelative).targetSpecification(targetSpecification).tripleCustomizer(customizer).mappingConsumer(mappingConsumer).iteration(focusContext.getIteration()).iterationToken(focusContext.getIterationToken()).beans(beans).env(env).result(result).build();
mappingSetEvaluation.evaluateMappingsToTriples();
PathKeyedMap<DeltaSetTriple<ItemValueWithOrigin<?, ?>>> focusOutputTripleMap = mappingSetEvaluation.getOutputTripleMap();
logOutputTripleMap(focusOutputTripleMap);
DeltaSetTripleMapConsolidation<AH> consolidation = new DeltaSetTripleMapConsolidation<>(focusOutputTripleMap, focusOdoRelative.getNewObject(), focusOdoRelative.getObjectDelta(), context::primaryFocusItemDeltaExists, null, null, focusContext.getObjectDefinition(), env, beans, context, result);
consolidation.computeItemDeltas();
Collection<ItemDelta<?, ?>> focusDeltas = consolidation.getItemDeltas();
LOGGER.trace("Computed focus deltas: {}", focusDeltas);
focusContext.swallowToSecondaryDelta(focusDeltas);
focusContext.recompute();
} catch (Throwable t) {
result.recordFatalError(t.getMessage(), t);
throw t;
} finally {
result.computeStatusIfUnknown();
}
}
use of com.evolveum.midpoint.model.impl.lens.LensFocusContext in project midpoint by Evolveum.
the class ObjectPolicyAspectPart method extractObjectBasedInstructions.
<T extends ObjectType> void extractObjectBasedInstructions(@NotNull ObjectTreeDeltas<T> objectTreeDeltas, @Nullable PrismObject<? extends FocusType> requester, @NotNull List<PcpStartInstruction> instructions, @NotNull ModelInvocationContext<T> ctx, @NotNull OperationResult parentResult) throws SchemaException, ObjectNotFoundException {
OperationResult result = parentResult.subresult(OP_EXTRACT_OBJECT_BASED_INSTRUCTIONS).setMinor().build();
ApprovalProcessStartInstructionCreationTraceType trace;
if (result.isTracingNormal(ApprovalProcessStartInstructionCreationTraceType.class)) {
trace = new ApprovalProcessStartInstructionCreationTraceType(prismContext);
result.addTrace(trace);
} else {
trace = null;
}
try {
ObjectDelta<T> focusDelta = objectTreeDeltas.getFocusChange();
LensFocusContext<T> focusContext = (LensFocusContext<T>) ctx.modelContext.getFocusContext();
PrismObject<T> object = focusContext.getObjectOld() != null ? focusContext.getObjectOld() : focusContext.getObjectNew();
List<? extends EvaluatedPolicyRule> triggeredApprovalActionRules = main.selectTriggeredApprovalActionRules(focusContext.getObjectPolicyRules());
LOGGER.trace("extractObjectBasedInstructions: triggeredApprovalActionRules:\n{}", debugDumpLazily(triggeredApprovalActionRules));
List<PcpStartInstruction> newInstructions = new ArrayList<>();
if (!triggeredApprovalActionRules.isEmpty()) {
generateObjectOidIfNeeded(focusDelta, ctx.modelContext);
ProcessSpecifications processSpecifications = ProcessSpecifications.createFromRules(triggeredApprovalActionRules, prismContext);
LOGGER.trace("Process specifications:\n{}", debugDumpLazily(processSpecifications));
for (ProcessSpecification processSpecificationEntry : processSpecifications.getSpecifications()) {
if (focusDelta.isEmpty()) {
// we're done
break;
}
WfProcessSpecificationType processSpecification = processSpecificationEntry.basicSpec;
List<ObjectDelta<T>> deltasToApprove = extractDeltasToApprove(focusDelta, processSpecification);
LOGGER.trace("Deltas to approve:\n{}", debugDumpLazily(deltasToApprove));
if (deltasToApprove.isEmpty()) {
continue;
}
LOGGER.trace("Remaining delta:\n{}", debugDumpLazily(focusDelta));
ApprovalSchemaBuilder builder = new ApprovalSchemaBuilder(main, approvalSchemaHelper);
builder.setProcessSpecification(processSpecificationEntry);
for (Pair<ApprovalPolicyActionType, EvaluatedPolicyRule> actionWithRule : processSpecificationEntry.actionsWithRules) {
ApprovalPolicyActionType approvalAction = actionWithRule.getLeft();
builder.add(main.getSchemaFromAction(approvalAction), approvalAction, object, actionWithRule.getRight());
}
buildSchemaForObject(requester, newInstructions, ctx, result, deltasToApprove, builder);
}
} else if (configurationHelper.getUseDefaultApprovalPolicyRules(ctx.wfConfiguration) != DefaultApprovalPolicyRulesUsageType.NEVER) {
// default rule
ApprovalSchemaBuilder builder = new ApprovalSchemaBuilder(main, approvalSchemaHelper);
if (builder.addPredefined(object, RelationKindType.OWNER, result)) {
LOGGER.trace("Added default approval action, as no explicit one was found");
generateObjectOidIfNeeded(focusDelta, ctx.modelContext);
List<ObjectDelta<T>> deltasToApprove = singletonList(focusDelta.clone());
focusDelta.clear();
buildSchemaForObject(requester, newInstructions, ctx, result, deltasToApprove, builder);
}
}
if (trace != null) {
for (PcpStartInstruction newInstruction : newInstructions) {
trace.getCaseRef().add(ObjectTypeUtil.createObjectRefWithFullObject(newInstruction.getCase(), prismContext));
}
}
instructions.addAll(newInstructions);
result.addReturn("instructionsCreated", newInstructions.size());
} catch (Throwable t) {
result.recordFatalError(t);
throw t;
} finally {
result.computeStatusIfUnknown();
}
}
Aggregations