Search in sources :

Example 1 with ExpressionEvaluationContext

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

the class AccCertExpressionHelper method evaluateExpression.

private <T> List<T> evaluateExpression(Class<T> resultClass, ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    QName xsdType = XsdTypeMapper.toXsdType(resultClass);
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<T> resultDef = new PrismPropertyDefinitionImpl<>(resultName, xsdType, prismContext);
    Expression<PrismPropertyValue<T>, PrismPropertyDefinition<T>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<T>> exprResult = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    List<T> retval = new ArrayList<>();
    for (PrismPropertyValue<T> item : exprResult.getZeroSet()) {
        retval.add(item.getValue());
    }
    return retval;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList)

Example 2 with ExpressionEvaluationContext

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

the class CorrelationConfirmationEvaluator method evaluateConfirmationExpression.

public <F extends FocusType> boolean evaluateConfirmationExpression(Class<F> focusType, F user, ShadowType shadow, ResourceType resource, SystemConfigurationType configuration, ExpressionType expressionType, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    Validate.notNull(user, "User must not be null.");
    Validate.notNull(shadow, "Resource object shadow must not be null.");
    Validate.notNull(expressionType, "Expression must not be null.");
    Validate.notNull(result, "Operation result must not be null.");
    ExpressionVariables variables = Utils.getDefaultExpressionVariables(user, shadow, resource, configuration);
    String shortDesc = "confirmation expression for " + resource.asPrismObject();
    PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    Collection<PrismPropertyValue<Boolean>> nonNegativeValues = outputTriple.getNonNegativeValues();
    if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
        throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
    }
    if (nonNegativeValues.size() > 1) {
        throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
    }
    PrismPropertyValue<Boolean> resultpval = nonNegativeValues.iterator().next();
    if (resultpval == null) {
        throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
    }
    Boolean resultVal = resultpval.getValue();
    if (resultVal == null) {
        throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
    }
    return resultVal;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)

Example 3 with ExpressionEvaluationContext

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

the class TestExpression method testIterationCondition.

@Test
public void testIterationCondition() throws Exception {
    final String TEST_NAME = "testIterationCondition";
    TestUtil.displayTestTile(TEST_NAME);
    // GIVEN
    OperationResult result = new OperationResult(TestExpression.class.getName() + "." + TEST_NAME);
    rememberScriptExecutionCount();
    ExpressionType expressionType = PrismTestUtil.parseAtomicValue(EXPRESSION_ITERATION_CONDITION_FILE, ExpressionType.COMPLEX_TYPE);
    PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, TEST_NAME, null, result);
    ExpressionVariables variables = new ExpressionVariables();
    PrismObject<UserType> user = PrismTestUtil.parseObject(USER_JACK_FILE);
    variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, user);
    variables.addVariableDefinition(ExpressionConstants.VAR_USER, user);
    PrismObject<ShadowType> account = PrismTestUtil.parseObject(ACCOUNT_JACK_DUMMYFILE);
    variables.addVariableDefinition(ExpressionConstants.VAR_SHADOW, account);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION, 1);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION_TOKEN, "001");
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, TEST_NAME, null, result);
    // WHEN
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = expression.evaluate(expressionContext);
    // THEN
    assertNotNull(outputTriple);
    outputTriple.checkConsistence();
    // Make sure that the script is executed only once. There is no delta in the variables, no need to do it twice.
    assertScriptExecutionIncrement(1);
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test)

Example 4 with ExpressionEvaluationContext

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

the class LensUtil method formatIterationToken.

public static <F extends ObjectType> String formatIterationToken(LensContext<F> context, LensElementContext<?> accountContext, IterationSpecificationType iterationType, int iteration, ExpressionFactory expressionFactory, ExpressionVariables variables, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    if (iterationType == null) {
        return formatIterationTokenDefault(iteration);
    }
    ExpressionType tokenExpressionType = iterationType.getTokenExpression();
    if (tokenExpressionType == null) {
        return formatIterationTokenDefault(iteration);
    }
    PrismPropertyDefinition<String> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.VAR_ITERATION_TOKEN, DOMUtil.XSD_STRING, context.getPrismContext());
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(tokenExpressionType, outputDefinition, "iteration token expression in " + accountContext.getHumanReadableName(), task, result);
    Collection<Source<?, ?>> sources = new ArrayList<>();
    PrismPropertyDefinitionImpl<Integer> inputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.VAR_ITERATION, DOMUtil.XSD_INT, context.getPrismContext());
    inputDefinition.setMaxOccurs(1);
    PrismProperty<Integer> input = inputDefinition.instantiate();
    input.add(new PrismPropertyValue<Integer>(iteration));
    ItemDeltaItem<PrismPropertyValue<Integer>, PrismPropertyDefinition<Integer>> idi = new ItemDeltaItem<>(input);
    Source<PrismPropertyValue<Integer>, PrismPropertyDefinition<Integer>> iterationSource = new Source<>(idi, ExpressionConstants.VAR_ITERATION);
    sources.add(iterationSource);
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(sources, variables, "iteration token expression in " + accountContext.getHumanReadableName(), task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, expressionContext, task, result);
    Collection<PrismPropertyValue<String>> outputValues = outputTriple.getNonNegativeValues();
    if (outputValues.isEmpty()) {
        return "";
    }
    if (outputValues.size() > 1) {
        throw new ExpressionEvaluationException("Iteration token expression in " + accountContext.getHumanReadableName() + " returned more than one value (" + outputValues.size() + " values)");
    }
    String realValue = outputValues.iterator().next().getValue();
    if (realValue == null) {
        return "";
    }
    return realValue;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ArrayList(java.util.ArrayList) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Source(com.evolveum.midpoint.repo.common.expression.Source) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem)

Example 5 with ExpressionEvaluationContext

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

the class LensUtil method evaluateIterationCondition.

public static <F extends ObjectType> boolean evaluateIterationCondition(LensContext<F> context, LensElementContext<?> accountContext, IterationSpecificationType iterationType, int iteration, String iterationToken, boolean beforeIteration, ExpressionFactory expressionFactory, ExpressionVariables variables, Task task, OperationResult result) throws ExpressionEvaluationException, SchemaException, ObjectNotFoundException {
    if (iterationType == null) {
        return true;
    }
    ExpressionType expressionType;
    String desc;
    if (beforeIteration) {
        expressionType = iterationType.getPreIterationCondition();
        desc = "pre-iteration expression in " + accountContext.getHumanReadableName();
    } else {
        expressionType = iterationType.getPostIterationCondition();
        desc = "post-iteration expression in " + accountContext.getHumanReadableName();
    }
    if (expressionType == null) {
        return true;
    }
    PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN, context.getPrismContext());
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, desc, task, result);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION, iteration);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION_TOKEN, iterationToken);
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, desc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, expressionContext, task, result);
    Collection<PrismPropertyValue<Boolean>> outputValues = outputTriple.getNonNegativeValues();
    if (outputValues.isEmpty()) {
        return false;
    }
    if (outputValues.size() > 1) {
        throw new ExpressionEvaluationException(desc + " returned more than one value (" + outputValues.size() + " values)");
    }
    Boolean realValue = outputValues.iterator().next().getValue();
    if (realValue == null) {
        return false;
    }
    return realValue;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean)

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