use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.
the class BasicExpressionFunctions method getPropertyValues.
public <T> Collection<T> getPropertyValues(ObjectType object, String path) {
if (object == null) {
return null;
}
ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
ScriptExpression scriptExpression = scriptContext.getScriptExpression();
ItemPath itemPath = scriptExpression.parsePath(path);
PrismProperty property = object.asPrismObject().findProperty(itemPath);
if (property == null) {
return new ArrayList<T>(0);
}
return property.getRealValues();
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.
the class MidpointFunctionsImpl method getCurrentTask.
@Override
public Task getCurrentTask() {
Task fromModelHolder = ModelExpressionThreadLocalHolder.getCurrentTask();
if (fromModelHolder != null) {
return fromModelHolder;
}
// fallback (MID-4130): but maybe we should instead make sure ModelExpressionThreadLocalHolder is set up correctly
ScriptExpressionEvaluationContext ctx = ScriptExpressionEvaluationContext.getThreadLocal();
if (ctx != null) {
return ctx.getTask();
}
return null;
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.
the class TestModelExpressions method evaluate.
@SuppressWarnings("SameParameterValue")
private <T> List<PrismPropertyValue<T>> evaluate(ScriptExpression scriptExpression, VariablesMap variables, boolean useNew, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
if (task == null) {
task = taskManager.createTaskInstance();
}
try {
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(task, result));
ScriptExpressionEvaluationContext context = new ScriptExpressionEvaluationContext();
context.setVariables(variables);
context.setEvaluateNew(useNew);
context.setScriptExpression(scriptExpression);
context.setContextDescription(contextDescription);
context.setTask(task);
context.setResult(result);
return scriptExpression.evaluate(context);
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.
the class ReportServiceImpl method evaluateScript.
@Override
public Collection<? extends PrismValue> evaluateScript(PrismObject<ReportType> report, @NotNull ExpressionType expression, VariablesMap variables, String shortDesc, Task task, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
if (expression.getExpressionEvaluator().size() == 1 && expression.getExpressionEvaluator().get(0).getValue() instanceof ScriptExpressionEvaluatorType) {
ScriptExpressionEvaluationContext context = new ScriptExpressionEvaluationContext();
context.setVariables(variables);
context.setContextDescription(shortDesc);
context.setTask(task);
context.setResult(result);
setupExpressionProfiles(context, report);
ScriptExpressionEvaluatorType expressionType = (ScriptExpressionEvaluatorType) expression.getExpressionEvaluator().get(0).getValue();
if (expressionType.getObjectVariableMode() == null) {
ScriptExpressionEvaluatorConfigurationType defaultScriptConfiguration = report.asObjectable().getDefaultScriptConfiguration();
expressionType.setObjectVariableMode(defaultScriptConfiguration == null ? ObjectVariableModeType.OBJECT : defaultScriptConfiguration.getObjectVariableMode());
}
context.setExpressionType(expressionType);
context.setFunctions(createFunctionLibraries());
context.setObjectResolver(objectResolver);
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(expressionType, context.getOutputDefinition(), context.getExpressionProfile(), expressionFactory, context.getContextDescription(), context.getResult());
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(context.getTask(), context.getResult()));
try {
return scriptExpression.evaluate(context);
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
} else {
return ExpressionUtil.evaluateExpressionNative(null, variables, null, expression, determineExpressionProfile(report, result), expressionFactory, shortDesc, task, result);
}
}
use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.
the class CustomFunctions method execute.
/**
* This method is invoked by the scripts. It is supposed to be only public method exposed
* by this class.
*/
public <V extends PrismValue, D extends ItemDefinition> Object execute(String functionName, Map<String, Object> params) throws ExpressionEvaluationException {
Validate.notNull(functionName, "Function name must be specified");
ScriptExpressionEvaluationContext ctx = ScriptExpressionEvaluationContext.getThreadLocal();
Task task;
OperationResult result;
if (ctx != null) {
if (ctx.getTask() != null) {
task = ctx.getTask();
} else {
// We shouldn't use task of unknown provenience.
throw new IllegalStateException("No task in ScriptExpressionEvaluationContext for the current thread found");
}
if (ctx.getResult() != null) {
result = ctx.getResult();
} else {
// This situation should never occur anyway.
throw new IllegalStateException("No operation result in ScriptExpressionEvaluationContext for the current thread found");
}
} else {
// This situation should never occur anyway.
throw new IllegalStateException("No ScriptExpressionEvaluationContext for current thread found");
}
List<ExpressionType> functions = library.getFunction().stream().filter(expression -> functionName.equals(expression.getName())).collect(Collectors.toList());
LOGGER.trace("functions {}", functions);
ExpressionType expressionType = MiscUtil.extractSingletonRequired(functions, () -> new ExpressionEvaluationException(functions.size() + " functions named '" + functionName + "' present"), () -> new ExpressionEvaluationException("No function named '" + functionName + "' present"));
LOGGER.trace("function to execute {}", expressionType);
try {
VariablesMap variables = new VariablesMap();
if (MapUtils.isNotEmpty(params)) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
variables.put(entry.getKey(), convertInput(entry, expressionType));
}
}
QName returnType = defaultIfNull(expressionType.getReturnType(), DOMUtil.XSD_STRING);
D outputDefinition = prepareOutputDefinition(returnType, expressionType.getReturnMultiplicity());
String shortDesc = "custom function execute";
Expression<V, D> expression = expressionFactory.makeExpression(expressionType, outputDefinition, expressionProfile, shortDesc, task, result);
ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, shortDesc, task);
PrismValueDeltaSetTriple<V> outputTriple = expression.evaluate(context, result);
LOGGER.trace("Result of the expression evaluation: {}", outputTriple);
if (outputTriple == null) {
return null;
}
Collection<V> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues.isEmpty()) {
return null;
}
if (outputDefinition.isMultiValue()) {
return PrismValueCollectionsUtil.getRealValuesOfCollection(nonNegativeValues);
}
if (nonNegativeValues.size() > 1) {
throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
return nonNegativeValues.iterator().next().getRealValue();
} catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
throw new ExpressionEvaluationException(e.getMessage(), e);
}
}
Aggregations