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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations