Search in sources :

Example 1 with ScriptExpressionEvaluationContext

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();
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ArrayList(java.util.ArrayList) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 2 with ScriptExpressionEvaluationContext

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;
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) Task(com.evolveum.midpoint.task.api.Task)

Example 3 with ScriptExpressionEvaluationContext

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();
    }
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext)

Example 4 with ScriptExpressionEvaluationContext

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);
    }
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression)

Example 5 with ScriptExpressionEvaluationContext

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);
    }
}
Also used : com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Trace(com.evolveum.midpoint.util.logging.Trace) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) DOMUtil(com.evolveum.midpoint.util.DOMUtil) SchemaConstantsGenerated(com.evolveum.midpoint.schema.SchemaConstantsGenerated) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) Map(java.util.Map) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) MapUtils(org.apache.commons.collections4.MapUtils) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) Expression(com.evolveum.midpoint.repo.common.expression.Expression) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue) MiscUtil(com.evolveum.midpoint.util.MiscUtil) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) Task(com.evolveum.midpoint.task.api.Task) Collectors(java.util.stream.Collectors) List(java.util.List) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) Validate(org.apache.commons.lang.Validate) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) Task(com.evolveum.midpoint.task.api.Task) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Map(java.util.Map) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

ScriptExpressionEvaluationContext (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext)9 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)2 TypedValue (com.evolveum.midpoint.schema.expression.TypedValue)2 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)2 Task (com.evolveum.midpoint.task.api.Task)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 NotNull (org.jetbrains.annotations.NotNull)2 ExpressionEnvironment (com.evolveum.midpoint.model.common.expression.ExpressionEnvironment)1 FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)1 com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)1 PrismValueDeltaSetTriple (com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 XmlTypeConverter (com.evolveum.midpoint.prism.xml.XmlTypeConverter)1 Expression (com.evolveum.midpoint.repo.common.expression.Expression)1 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)1 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)1 ExpressionUtil (com.evolveum.midpoint.repo.common.expression.ExpressionUtil)1 SchemaConstantsGenerated (com.evolveum.midpoint.schema.SchemaConstantsGenerated)1 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1