Search in sources :

Example 21 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class CustomNotifier method evaluateExpression.

private List<NotificationMessageType> evaluateExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<NotificationMessageType> resultDef = new PrismPropertyDefinitionImpl<>(resultName, NotificationMessageType.COMPLEX_TYPE, prismContext);
    Expression<PrismPropertyValue<NotificationMessageType>, PrismPropertyDefinition<NotificationMessageType>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<NotificationMessageType>> exprResult = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    return exprResult.getZeroSet().stream().map(PrismPropertyValue::getValue).collect(Collectors.toList());
}
Also used : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) NotificationMessageType(com.evolveum.midpoint.xml.ns._public.common.common_3.NotificationMessageType) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 22 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class ExpressionFilterHelper method processEvent.

@Override
public boolean processEvent(Event event, EventHandlerType eventHandlerType, NotificationManager notificationManager, Task task, OperationResult result) {
    if (eventHandlerType.getExpressionFilter().isEmpty()) {
        return true;
    }
    logStart(LOGGER, event, eventHandlerType, eventHandlerType.getExpressionFilter());
    boolean retval = true;
    for (ExpressionType expressionType : eventHandlerType.getExpressionFilter()) {
        if (!evaluateBooleanExpressionChecked(expressionType, getDefaultVariables(event, result), "event filter expression", task, result)) {
            retval = false;
            break;
        }
    }
    logEnd(LOGGER, event, eventHandlerType, retval);
    return retval;
}
Also used : ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 23 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class AbstractSearchExpressionEvaluator method evaluatePopulateExpression.

private <IV extends PrismValue, ID extends ItemDefinition, C extends Containerable> ItemDelta<IV, ID> evaluatePopulateExpression(PopulateItemType populateItem, ExpressionVariables variables, ExpressionEvaluationContext params, PrismContainerDefinition<C> objectDefinition, String contextDescription, boolean evaluateMinus, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    ExpressionType expressionType = populateItem.getExpression();
    if (expressionType == null) {
        LOGGER.warn("No expression in populateObject in assignment expression in {}, " + "skipping. Subsequent operations will most likely fail", contextDescription);
        return null;
    }
    VariableBindingDefinitionType targetType = populateItem.getTarget();
    if (targetType == null) {
        LOGGER.warn("No target in populateObject in assignment expression in {}, " + "skipping. Subsequent operations will most likely fail", contextDescription);
        return null;
    }
    ItemPathType itemPathType = targetType.getPath();
    if (itemPathType == null) {
        throw new SchemaException("No path in target definition in " + contextDescription);
    }
    ItemPath targetPath = itemPathType.getItemPath();
    ID propOutputDefinition = ExpressionUtil.resolveDefinitionPath(targetPath, variables, objectDefinition, "target definition in " + contextDescription);
    if (propOutputDefinition == null) {
        throw new SchemaException("No target item that would conform to the path " + targetPath + " in " + contextDescription);
    }
    String expressionDesc = "expression in assignment expression in " + contextDescription;
    ExpressionFactory expressionFactory = params.getExpressionFactory();
    Expression<IV, ID> expression = expressionFactory.makeExpression(expressionType, propOutputDefinition, expressionDesc, task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, expressionDesc, task, result);
    context.setExpressionFactory(expressionFactory);
    context.setStringPolicyResolver(params.getStringPolicyResolver());
    context.setDefaultTargetContext(params.getDefaultTargetContext());
    context.setSkipEvaluationMinus(true);
    context.setSkipEvaluationPlus(false);
    PrismValueDeltaSetTriple<IV> outputTriple = expression.evaluate(context);
    LOGGER.trace("output triple: {}", outputTriple.debugDump());
    Collection<IV> pvalues = outputTriple.getNonNegativeValues();
    // Maybe not really clean but it works. TODO: refactor later
    NameItemPathSegment first = (NameItemPathSegment) targetPath.first();
    if (first.isVariable()) {
        targetPath = targetPath.rest();
    }
    ItemDelta<IV, ID> itemDelta = propOutputDefinition.createEmptyDelta(targetPath);
    itemDelta.addValuesToAdd(PrismValue.cloneCollection(pvalues));
    LOGGER.trace("Item delta:\n{}", itemDelta.debugDump());
    return itemDelta;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) VariableBindingDefinitionType(com.evolveum.midpoint.xml.ns._public.common.common_3.VariableBindingDefinitionType) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 24 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class ValuePolicyProcessor method testCheckExpression.

private <O extends ObjectType> void testCheckExpression(String newPassword, LimitationsType lims, PrismObject<O> object, String shortDesc, Task task, OperationResult result, StringBuilder message) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    List<CheckExpressionType> checkExpressions = lims.getCheckExpression();
    if (checkExpressions.isEmpty()) {
        return;
    }
    for (CheckExpressionType checkExpression : checkExpressions) {
        ExpressionType expressionType = checkExpression.getExpression();
        if (expressionType == null) {
            return;
        }
        if (!checkExpression(newPassword, expressionType, object, shortDesc, task, result)) {
            String msg = checkExpression.getFailureMessage();
            if (msg == null) {
                msg = "Check expression failed";
            }
            result.addSubresult(new OperationResult("Check expression", OperationResultStatus.FATAL_ERROR, msg));
            message.append(msg);
            message.append("\n");
        }
    }
}
Also used : CheckExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.CheckExpressionType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) CheckExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.CheckExpressionType) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 25 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class ObjectMerger method mergeItem.

private <O extends ObjectType, I extends Item> ItemDelta mergeItem(PrismObject<O> objectLeft, PrismObject<O> objectRight, String mergeConfigurationName, ItemMergeConfigurationType itemMergeConfig, ItemPath itemPath, Task task, OperationResult result) throws SchemaException, ConfigurationException, ExpressionEvaluationException, ObjectNotFoundException {
    I itemLeft = (I) objectLeft.findItem(itemPath);
    I itemRight = (I) objectRight.findItem(itemPath);
    if (itemLeft == null && itemRight == null) {
        return null;
    }
    ItemDefinition itemDefinition = null;
    if (itemLeft != null) {
        itemDefinition = itemLeft.getDefinition();
    } else {
        itemDefinition = itemRight.getDefinition();
    }
    if (itemDefinition.isOperational()) {
        // we do not want to modify them explicitly.
        return null;
    }
    Expression<PrismValue, ItemDefinition> valueExpression = null;
    if (itemMergeConfig.getValueExpression() != null) {
        ExpressionType expressionType = itemMergeConfig.getValueExpression();
        valueExpression = expressionFactory.makeExpression(expressionType, itemDefinition, "value expression for item " + itemPath + " in merge configuration " + mergeConfigurationName, task, result);
    }
    ItemDelta itemDelta = itemDefinition.createEmptyDelta(itemPath);
    MergeStategyType leftStrategy = itemMergeConfig.getLeft();
    MergeStategyType rightStrategy = itemMergeConfig.getRight();
    if (leftStrategy == null || leftStrategy == MergeStategyType.IGNORE) {
        if (rightStrategy == null || rightStrategy == MergeStategyType.IGNORE) {
            // IGNORE both
            if (itemLeft == null) {
                return null;
            } else {
                itemDelta.setValueToReplace();
                return itemDelta;
            }
        } else {
            // IGNORE left, TAKE/EXPRESSION right
            if (itemRight == null) {
                itemDelta.setValueToReplace();
            } else {
                Collection<PrismValue> valuesToTake = getValuesToTake(objectLeft, objectRight, SIDE_RIGHT, itemRight, rightStrategy, valueExpression, task, result);
                itemDelta.setValuesToReplace(valuesToTake);
            }
            return itemDelta;
        }
    } else {
        if (rightStrategy == null || rightStrategy == MergeStategyType.IGNORE) {
            if (leftStrategy == MergeStategyType.TAKE) {
                // TAKE left, IGNORE right
                return null;
            } else {
                // EXPRESSION left, IGNORE right
                Collection<PrismValue> valuesToLeave = getValuesToTake(objectLeft, objectRight, SIDE_LEFT, itemLeft, leftStrategy, valueExpression, task, result);
                List<PrismValue> currentLeftValues = itemLeft.getValues();
                Collection<PrismValue> leftValuesToRemove = diffValues(currentLeftValues, valuesToLeave);
                if (leftValuesToRemove != null && !leftValuesToRemove.isEmpty()) {
                    itemDelta.addValuesToDelete(leftValuesToRemove);
                    return itemDelta;
                } else {
                    return null;
                }
            }
        } else {
            // TAKE/EXPRESSION left, TAKE/EXPRESSION right
            if (itemLeft == null) {
                Collection<PrismValue> valuesToTake = getValuesToTake(objectLeft, objectRight, SIDE_RIGHT, itemRight, rightStrategy, valueExpression, task, result);
                itemDelta.addValuesToAdd(valuesToTake);
                return itemDelta;
            } else {
                // We want to add only those values that are not yet there.
                // E.g. adding assignments that are there can cause unnecessary churn
                Collection<PrismValue> leftValuesToLeave = getValuesToTake(objectLeft, objectRight, SIDE_LEFT, itemLeft, leftStrategy, valueExpression, task, result);
                Collection<PrismValue> rightValuesToTake = getValuesToTake(objectLeft, objectRight, SIDE_RIGHT, itemRight, rightStrategy, valueExpression, task, result);
                for (PrismValue rightValueToTake : rightValuesToTake) {
                    if (!PrismValue.collectionContainsEquivalentValue(leftValuesToLeave, rightValueToTake)) {
                        itemDelta.addValueToAdd(rightValueToTake);
                    }
                }
                List<PrismValue> currentLeftValues = itemLeft.getValues();
                Collection<PrismValue> leftValuesToRemove = diffValues(currentLeftValues, leftValuesToLeave);
                if (leftValuesToRemove != null && !leftValuesToRemove.isEmpty()) {
                    itemDelta.addValuesToDelete(leftValuesToRemove);
                }
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Merging item {} T/T case:\n  leftValuesToLeave: {}\n  rightValuesToTake: {}\n  leftValuesToRemove: {}\n itemDelta:\n{}", new Object[] { itemPath, leftValuesToLeave, rightValuesToTake, leftValuesToRemove, itemDelta.debugDump(2) });
                }
                return itemDelta;
            }
        }
    }
}
Also used : ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) MergeStategyType(com.evolveum.midpoint.xml.ns._public.common.common_3.MergeStategyType) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Aggregations

ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)19 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)8 QName (javax.xml.namespace.QName)8 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)7 ArrayList (java.util.ArrayList)7 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)6 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)6 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)6 Test (org.testng.annotations.Test)6 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)5 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)5 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)4 Task (com.evolveum.midpoint.task.api.Task)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)4 MappingType (com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType)4 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)3 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)3