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