Search in sources :

Example 6 with Source

use of com.evolveum.midpoint.repo.common.expression.Source in project midpoint by Evolveum.

the class ActivationProcessor method evaluateExistenceMapping.

private <F extends FocusType> boolean evaluateExistenceMapping(final LensContext<F> context, final LensProjectionContext accCtx, final XMLGregorianCalendar now, final boolean current, Task task, final OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    final String accCtxDesc = accCtx.toHumanReadableString();
    final Boolean legal = accCtx.isLegal();
    if (legal == null) {
        throw new IllegalStateException("Null 'legal' for " + accCtxDesc);
    }
    ResourceObjectTypeDefinitionType resourceAccountDefType = accCtx.getResourceObjectTypeDefinitionType();
    if (resourceAccountDefType == null) {
        return legal;
    }
    ResourceActivationDefinitionType activationType = resourceAccountDefType.getActivation();
    if (activationType == null) {
        return legal;
    }
    ResourceBidirectionalMappingType existenceType = activationType.getExistence();
    if (existenceType == null) {
        return legal;
    }
    List<MappingType> outbound = existenceType.getOutbound();
    if (outbound == null || outbound.isEmpty()) {
        // "default mapping"
        return legal;
    }
    MappingEvaluatorParams<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>, ShadowType, F> params = new MappingEvaluatorParams<>();
    params.setMappingTypes(outbound);
    params.setMappingDesc("outbound existence mapping in projection " + accCtxDesc);
    params.setNow(now);
    params.setAPrioriTargetObject(accCtx.getObjectOld());
    params.setEvaluateCurrent(current);
    params.setTargetContext(accCtx);
    params.setFixTarget(true);
    params.setContext(context);
    params.setInitializer(builder -> {
        ItemDeltaItem<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> legalSourceIdi = getLegalIdi(accCtx);
        Source<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> legalSource = new Source<>(legalSourceIdi, ExpressionConstants.VAR_LEGAL);
        builder.defaultSource(legalSource);
        ItemDeltaItem<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> assignedIdi = getAssignedIdi(accCtx);
        Source<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> assignedSource = new Source<>(assignedIdi, ExpressionConstants.VAR_ASSIGNED);
        builder.addSource(assignedSource);
        ItemDeltaItem<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> focusExistsSourceIdi = getFocusExistsIdi(context.getFocusContext());
        Source<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> focusExistsSource = new Source<>(focusExistsSourceIdi, ExpressionConstants.VAR_FOCUS_EXISTS);
        builder.addSource(focusExistsSource);
        builder.addVariableDefinition(ExpressionConstants.VAR_FOCUS, context.getFocusContext().getObjectDeltaObject());
        builder.addVariableDefinition(ExpressionConstants.VAR_USER, context.getFocusContext().getObjectDeltaObject());
        builder.addVariableDefinition(ExpressionConstants.VAR_SHADOW, accCtx.getObjectDeltaObject());
        builder.addVariableDefinition(ExpressionConstants.VAR_RESOURCE, accCtx.getResource());
        builder.originType(OriginType.OUTBOUND);
        builder.originObject(accCtx.getResource());
        return builder;
    });
    final MutableBoolean output = new MutableBoolean(false);
    params.setProcessor((mappingOutputPath, outputStruct) -> {
        PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = outputStruct.getOutputTriple();
        if (outputTriple == null) {
            // The "default existence mapping"
            output.setValue(legal);
            return false;
        }
        Collection<PrismPropertyValue<Boolean>> nonNegativeValues = outputTriple.getNonNegativeValues();
        // (e.g. because the condition is false). This should be fixed.
        if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
            throw new ExpressionEvaluationException("Activation existence expression resulted in null or empty value for projection " + accCtxDesc);
        }
        if (nonNegativeValues.size() > 1) {
            throw new ExpressionEvaluationException("Activation existence expression resulted in too many values (" + nonNegativeValues.size() + ") for projection " + accCtxDesc);
        }
        output.setValue(nonNegativeValues.iterator().next().getValue());
        return false;
    });
    PrismPropertyDefinitionImpl<Boolean> shadowExistsDef = new PrismPropertyDefinitionImpl<>(SHADOW_EXISTS_PROPERTY_NAME, DOMUtil.XSD_BOOLEAN, prismContext);
    shadowExistsDef.setMinOccurs(1);
    shadowExistsDef.setMaxOccurs(1);
    params.setTargetItemDefinition(shadowExistsDef);
    mappingEvaluator.evaluateMappingSetProjection(params, task, result);
    return (boolean) output.getValue();
}
Also used : ResourceBidirectionalMappingType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceBidirectionalMappingType) MappingType(com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) Source(com.evolveum.midpoint.repo.common.expression.Source) ResourceActivationDefinitionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceActivationDefinitionType) ResourceObjectTypeDefinitionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceObjectTypeDefinitionType) ResourceBidirectionalMappingType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceBidirectionalMappingType) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean)

Example 7 with Source

use of com.evolveum.midpoint.repo.common.expression.Source in project midpoint by Evolveum.

the class AbstractValueTransformationExpressionEvaluator method evaluateRelativeExpression.

private PrismValueDeltaSetTriple<V> evaluateRelativeExpression(final List<SourceTriple<?, ?>> sourceTriples, final ExpressionVariables variables, final boolean skipEvaluationMinus, final boolean skipEvaluationPlus, final Boolean includeNulls, final ExpressionEvaluationContext evaluationContext, final String contextDescription, final Task task, final OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    List<Collection<? extends PrismValue>> valueCollections = new ArrayList<>(sourceTriples.size());
    for (SourceTriple<?, ?> sourceTriple : sourceTriples) {
        Collection<? extends PrismValue> values = sourceTriple.union();
        if (values.isEmpty()) {
            // No values for this source. Add null instead. It will make sure that the expression will
            // be evaluate at least once.
            values.add(null);
        }
        valueCollections.add(values);
    }
    final PrismValueDeltaSetTriple<V> outputTriple = new PrismValueDeltaSetTriple<>();
    Processor<Collection<? extends PrismValue>> processor = pvalues -> {
        if (includeNulls != null && !includeNulls && MiscUtil.isAllNull(pvalues)) {
            return;
        }
        Map<QName, Object> sourceVariables = new HashMap<>();
        Iterator<SourceTriple<PrismValue, ?>> sourceTriplesIterator = (Iterator) sourceTriples.iterator();
        boolean hasMinus = false;
        boolean hasZero = false;
        boolean hasPlus = false;
        for (PrismValue pval : pvalues) {
            SourceTriple<PrismValue, ?> sourceTriple = sourceTriplesIterator.next();
            QName name = sourceTriple.getName();
            sourceVariables.put(name, getRealContent(pval, sourceTriple.getResidualPath()));
            if (sourceTriple.presentInPlusSet(pval)) {
                hasPlus = true;
            } else if (sourceTriple.presentInZeroSet(pval)) {
                hasZero = true;
            } else if (sourceTriple.presentInMinusSet(pval)) {
                hasMinus = true;
            }
        }
        if (!hasPlus && !hasMinus && !hasZero && !MiscUtil.isAllNull(pvalues)) {
            throw new IllegalStateException("Internal error! The impossible has happened! pvalues=" + pvalues + "; source triples: " + sourceTriples + "; in " + contextDescription);
        }
        if (hasPlus && hasMinus) {
            return;
        }
        if (hasPlus && skipEvaluationPlus) {
            return;
        } else if (hasMinus && skipEvaluationMinus) {
            return;
        }
        ExpressionVariables scriptVariables = new ExpressionVariables();
        scriptVariables.addVariableDefinitions(sourceVariables);
        PlusMinusZero valueDestination = null;
        boolean useNew = false;
        if (hasPlus) {
            scriptVariables.addVariableDefinitionsNew(variables);
            valueDestination = PlusMinusZero.PLUS;
            useNew = true;
        } else if (hasMinus) {
            scriptVariables.addVariableDefinitionsOld(variables);
            valueDestination = PlusMinusZero.MINUS;
        } else {
            scriptVariables.addVariableDefinitionsNew(variables);
            valueDestination = PlusMinusZero.ZERO;
            useNew = true;
        }
        List<V> scriptResults;
        try {
            scriptResults = transformSingleValue(scriptVariables, valueDestination, useNew, evaluationContext, contextDescription, task, result);
        } catch (ExpressionEvaluationException e) {
            throw new TunnelException(new ExpressionEvaluationException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        } catch (ObjectNotFoundException e) {
            throw new TunnelException(new ObjectNotFoundException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        } catch (SchemaException e) {
            throw new TunnelException(new SchemaException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        } catch (RuntimeException e) {
            throw new TunnelException(new RuntimeException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        }
        outputTriple.addAllToSet(valueDestination, scriptResults);
    };
    try {
        MiscUtil.carthesian((Collection) valueCollections, (Processor) processor);
    } catch (TunnelException e) {
        Throwable originalException = e.getCause();
        if (originalException instanceof ExpressionEvaluationException) {
            throw (ExpressionEvaluationException) originalException;
        } else if (originalException instanceof ObjectNotFoundException) {
            throw (ObjectNotFoundException) originalException;
        } else if (originalException instanceof SchemaException) {
            throw (SchemaException) originalException;
        } else if (originalException instanceof RuntimeException) {
            throw (RuntimeException) originalException;
        } else {
            throw new IllegalStateException("Unexpected exception: " + e + ": " + e.getMessage(), e);
        }
    }
    cleanupTriple(outputTriple);
    return outputTriple;
}
Also used : PrismValue(com.evolveum.midpoint.prism.PrismValue) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) SourceTriple(com.evolveum.midpoint.repo.common.expression.SourceTriple) java.util(java.util) Item(com.evolveum.midpoint.prism.Item) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) SecurityEnforcer(com.evolveum.midpoint.security.api.SecurityEnforcer) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TransformExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.TransformExpressionEvaluatorType) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) Trace(com.evolveum.midpoint.util.logging.Trace) PrettyPrinter(com.evolveum.midpoint.util.PrettyPrinter) ExpressionConstants(com.evolveum.midpoint.schema.constants.ExpressionConstants) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) TransformExpressionRelativityModeType(com.evolveum.midpoint.xml.ns._public.common.common_3.TransformExpressionRelativityModeType) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ExpressionEvaluator(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluator) DeltaSetTriple(com.evolveum.midpoint.prism.delta.DeltaSetTriple) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) MiscUtil(com.evolveum.midpoint.util.MiscUtil) Task(com.evolveum.midpoint.task.api.Task) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) PlusMinusZero(com.evolveum.midpoint.prism.delta.PlusMinusZero) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Processor(com.evolveum.midpoint.util.Processor) Entry(java.util.Map.Entry) Source(com.evolveum.midpoint.repo.common.expression.Source) QName(javax.xml.namespace.QName) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismValue(com.evolveum.midpoint.prism.PrismValue) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) QName(javax.xml.namespace.QName) PlusMinusZero(com.evolveum.midpoint.prism.delta.PlusMinusZero) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SourceTriple(com.evolveum.midpoint.repo.common.expression.SourceTriple)

Aggregations

Source (com.evolveum.midpoint.repo.common.expression.Source)7 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 ItemDeltaItem (com.evolveum.midpoint.repo.common.expression.ItemDeltaItem)5 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)4 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)3 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)2 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)2 PrismValueDeltaSetTriple (com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 MappingType (com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType)2 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)2 ArrayList (java.util.ArrayList)2 RefinedObjectClassDefinition (com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition)1 SynchronizationPolicyDecision (com.evolveum.midpoint.model.api.context.SynchronizationPolicyDecision)1 Mapping (com.evolveum.midpoint.model.common.mapping.Mapping)1 MappingFactory (com.evolveum.midpoint.model.common.mapping.MappingFactory)1 ValuePolicyProcessor (com.evolveum.midpoint.model.common.stringpolicy.ValuePolicyProcessor)1