Search in sources :

Example 11 with ExpressionEvaluationContext

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

the class GcpExpressionHelper method evaluateBooleanExpression.

private boolean evaluateBooleanExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String opContext, Task taskFromModel, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    PrismContext prismContext = expressionFactory.getPrismContext();
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, opContext, taskFromModel, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, opContext, taskFromModel, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, taskFromModel, result);
    Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
    if (exprResult.size() == 0) {
        return false;
    } else if (exprResult.size() > 1) {
        throw new IllegalStateException("Expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
    }
    Boolean boolResult = exprResult.iterator().next().getValue();
    return boolResult != null ? boolResult : false;
}
Also used : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismContext(com.evolveum.midpoint.prism.PrismContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 12 with ExpressionEvaluationContext

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

the class ResourceManager method evaluateExpression.

private <T> void evaluateExpression(PrismProperty<T> configurationProperty, PrismObject<ResourceType> resource, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    PrismPropertyDefinition<T> propDef = configurationProperty.getDefinition();
    String shortDesc = "connector configuration property " + configurationProperty + " in " + resource;
    List<PrismPropertyValue<T>> extraValues = new ArrayList<>();
    for (PrismPropertyValue<T> configurationPropertyValue : configurationProperty.getValues()) {
        ExpressionWrapper expressionWrapper = configurationPropertyValue.getExpression();
        if (expressionWrapper == null) {
            return;
        }
        Object expressionObject = expressionWrapper.getExpression();
        if (!(expressionObject instanceof ExpressionType)) {
            throw new IllegalStateException("Expected that expression in " + configurationPropertyValue + " will be ExpressionType, but it was " + expressionObject);
        }
        ExpressionType expressionType = (ExpressionType) expressionWrapper.getExpression();
        Expression<PrismPropertyValue<T>, PrismPropertyDefinition<T>> expression = expressionFactory.makeExpression(expressionType, propDef, shortDesc, task, result);
        ExpressionVariables variables = new ExpressionVariables();
        // TODO: populate variables
        ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
        PrismValueDeltaSetTriple<PrismPropertyValue<T>> expressionOutputTriple = expression.evaluate(expressionContext);
        Collection<PrismPropertyValue<T>> expressionOutputValues = expressionOutputTriple.getNonNegativeValues();
        if (expressionOutputValues != null && !expressionOutputValues.isEmpty()) {
            Iterator<PrismPropertyValue<T>> iterator = expressionOutputValues.iterator();
            PrismPropertyValue<T> firstValue = iterator.next();
            configurationPropertyValue.setValue(firstValue.getValue());
            while (iterator.hasNext()) {
                extraValues.add(iterator.next());
            }
        }
    }
    for (PrismPropertyValue<T> extraValue : extraValues) {
        configurationProperty.add(extraValue);
    }
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ArrayList(java.util.ArrayList) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 13 with ExpressionEvaluationContext

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

the class SimpleSmsTransport method evaluateExpression.

private String evaluateExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl(resultName, DOMUtil.XSD_STRING, prismContext);
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResult = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    if (exprResult.getZeroSet().size() != 1) {
        throw new SystemException("Invalid number of return values (" + exprResult.getZeroSet().size() + "), expected 1.");
    }
    return exprResult.getZeroSet().iterator().next().getValue();
}
Also used : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) SystemException(com.evolveum.midpoint.util.exception.SystemException) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 14 with ExpressionEvaluationContext

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

the class BaseHandler method evaluateBooleanExpression.

protected boolean evaluateBooleanExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl<>(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
    if (exprResult.size() == 0) {
        return false;
    } else if (exprResult.size() > 1) {
        throw new IllegalStateException("Filter expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
    }
    Boolean boolResult = exprResult.iterator().next().getValue();
    return boolResult != null ? boolResult : false;
}
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) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 15 with ExpressionEvaluationContext

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

the class ChangeExecutor method evaluateScriptArgument.

private void evaluateScriptArgument(ProvisioningScriptArgumentType argument, ExpressionVariables variables, LensContext<?> context, LensElementContext<?> objectContext, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    QName FAKE_SCRIPT_ARGUMENT_NAME = new QName(SchemaConstants.NS_C, "arg");
    PrismPropertyDefinition<String> scriptArgumentDefinition = new PrismPropertyDefinitionImpl<>(FAKE_SCRIPT_ARGUMENT_NAME, DOMUtil.XSD_STRING, prismContext);
    String shortDesc = "Provisioning script argument expression";
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(argument, scriptArgumentDefinition, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, context, objectContext instanceof LensProjectionContext ? (LensProjectionContext) objectContext : null, task, result);
    Collection<PrismPropertyValue<String>> nonNegativeValues = null;
    if (outputTriple != null) {
        nonNegativeValues = outputTriple.getNonNegativeValues();
    }
    // replace dynamic script with static value..
    argument.getExpressionEvaluator().clear();
    if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
        // We need to create at least one evaluator. Otherwise the
        // expression code will complain
        // Element value = DOMUtil.createElement(SchemaConstants.C_VALUE);
        // DOMUtil.setNill(value);
        JAXBElement<RawType> el = new JAXBElement(SchemaConstants.C_VALUE, RawType.class, new RawType(prismContext));
        argument.getExpressionEvaluator().add(el);
    } else {
        for (PrismPropertyValue<String> val : nonNegativeValues) {
            // Element value =
            // DOMUtil.createElement(SchemaConstants.C_VALUE);
            // value.setTextContent(val.getValue());
            PrimitiveXNode<String> prim = new PrimitiveXNode<>();
            prim.setValue(val.getValue(), DOMUtil.XSD_STRING);
            JAXBElement<RawType> el = new JAXBElement(SchemaConstants.C_VALUE, RawType.class, new RawType(prim, prismContext));
            argument.getExpressionEvaluator().add(el);
        }
    }
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType)

Aggregations

ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)25 QName (javax.xml.namespace.QName)14 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)12 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)11 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)11 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)9 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)6 ArrayList (java.util.ArrayList)6 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)4 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 PrismValue (com.evolveum.midpoint.prism.PrismValue)2 PrismValueDeltaSetTriple (com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)2 ItemDeltaItem (com.evolveum.midpoint.repo.common.expression.ItemDeltaItem)2 Source (com.evolveum.midpoint.repo.common.expression.Source)2