Search in sources :

Example 1 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class DeltaExecution method prepareScripts.

// endregion
// region Provisioning scripts
/**
 * TODO clarify the role of `object` parameter and why it is used only as a second choice (after ctx.objectAny).
 */
private OperationProvisioningScriptsType prepareScripts(PrismObject<E> object, ProvisioningOperationTypeType operation, OperationResult result) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    if (resource == null) {
        LOGGER.warn("Resource does not exist. Skipping processing scripts.");
        return null;
    }
    OperationProvisioningScriptsType resourceScripts = resource.getScripts();
    LensProjectionContext projCtx = (LensProjectionContext) elementContext;
    PrismObject<ShadowType> shadow = getShadow(projCtx, object);
    PrismObject<O> focus = getFocus();
    ResourceShadowDiscriminator rsd = projCtx.getResourceShadowDiscriminator();
    VariablesMap variables = ModelImplUtils.getDefaultVariablesMap(focus, shadow, rsd, resource.asPrismObject(), context.getSystemConfiguration(), elementContext, b.prismContext);
    // Having delta in provisioning scripts may be very useful. E.g. the script can optimize execution of expensive operations.
    variables.put(ExpressionConstants.VAR_DELTA, projCtx.getCurrentDelta(), ObjectDelta.class);
    ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
    ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(context, projCtx, task, result));
    try {
        ScriptExecutor<O> scriptExecutor = new ScriptExecutor<>(context, projCtx, task, b);
        return scriptExecutor.prepareScripts(resourceScripts, rsd, operation, null, variables, expressionProfile, result);
    } finally {
        ModelExpressionThreadLocalHolder.popExpressionEnvironment();
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile)

Example 2 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class ScriptExecutor method executeReconciliationScripts.

void executeReconciliationScripts(BeforeAfterType order, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException, ObjectAlreadyExistsException {
    if (!projCtx.isDoReconciliation()) {
        return;
    }
    if (projCtx.getResource() == null) {
        LOGGER.warn("Resource does not exist. Skipping processing reconciliation scripts.");
        return;
    }
    OperationProvisioningScriptsType resourceScripts = projCtx.getResource().getScripts();
    if (resourceScripts == null) {
        return;
    }
    ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
    executeReconciliationScripts(resourceScripts, order, expressionProfile, result);
}
Also used : ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile)

Example 3 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class SearchEvaluator method evaluate.

public <T extends ObjectType> PipelineData evaluate(SearchExpressionType searchExpression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException, SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, SecurityViolationException, ExpressionEvaluationException {
    Validate.notNull(searchExpression.getType());
    ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
    List<PipelineItem> data = input.getData();
    if (data.isEmpty()) {
        // TODO fix this brutal hack (with dummyValue)
        PrismContainerValue<?> dummyValue = prismContext.itemFactory().createContainerValue();
        PipelineItem dummyItem = new PipelineItem(dummyValue, PipelineData.newOperationResult(), context.getInitialVariables());
        data = Collections.singletonList(dummyItem);
    }
    final PipelineData outputData = PipelineData.createEmpty();
    final MutableBoolean atLeastOne = new MutableBoolean(false);
    for (PipelineItem item : data) {
        // TODO variables from current item
        // TODO operation result handling (global vs local)
        boolean noFetch = expressionHelper.getArgumentAsBoolean(searchExpression.getParameter(), PARAM_NO_FETCH, input, context, false, "search", globalResult);
        Class<T> objectClass = ObjectTypes.getObjectTypeFromTypeQName(searchExpression.getType()).getClassDefinition();
        ObjectQuery unresolvedObjectQuery = null;
        if (searchExpression.getQuery() != null) {
            try {
                unresolvedObjectQuery = context.getQueryConverter().createObjectQuery(objectClass, searchExpression.getQuery());
            } catch (SchemaException e) {
                throw new ScriptExecutionException("Couldn't parse object query. Reason: " + e.getMessage(), e);
            }
        } else if (searchExpression.getSearchFilter() != null) {
            unresolvedObjectQuery = prismContext.queryFactory().createQuery();
            try {
                ObjectFilter filter = prismContext.getQueryConverter().parseFilter(searchExpression.getSearchFilter(), objectClass);
                unresolvedObjectQuery.setFilter(filter);
            } catch (SchemaException e) {
                throw new ScriptExecutionException("Couldn't parse object query. Reason: " + e.getMessage(), e);
            }
        }
        ObjectQuery objectQuery;
        if (unresolvedObjectQuery != null) {
            VariablesMap variables = new VariablesMap();
            // noinspection unchecked
            item.getVariables().forEach((name, value) -> variables.put(name, cloneIfNecessary(name, value)));
            try {
                objectQuery = ExpressionUtil.evaluateQueryExpressions(unresolvedObjectQuery, variables, expressionProfile, expressionFactory, prismContext, "bulk action query", context.getTask(), globalResult);
            } catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException | CommunicationException | ConfigurationException | SecurityViolationException e) {
                // TODO continue on any error?
                throw new ScriptExecutionException("Couldn't evaluate expressions in object query: " + e.getMessage(), e);
            }
        } else {
            objectQuery = null;
        }
        final String variableName = searchExpression.getVariable();
        ResultHandler<T> handler = (object, parentResult) -> {
            context.checkTaskStop();
            atLeastOne.setValue(true);
            if (searchExpression.getScriptingExpression() != null) {
                if (variableName != null) {
                // TODO
                }
                JAXBElement<?> childExpression = searchExpression.getScriptingExpression();
                try {
                    PipelineData expressionResult = scriptingExpressionEvaluator.evaluateExpression((ScriptingExpressionType) childExpression.getValue(), PipelineData.create(object.getValue(), item.getVariables()), context, globalResult);
                    if (!BooleanUtils.isFalse(searchExpression.isAggregateOutput())) {
                        outputData.addAllFrom(expressionResult);
                    }
                    globalResult.setSummarizeSuccesses(true);
                    globalResult.summarize();
                } catch (ScriptExecutionException | SchemaException | ConfigurationException | ObjectNotFoundException | CommunicationException | SecurityViolationException | ExpressionEvaluationException e) {
                    // todo think about this
                    if (context.isContinueOnAnyError()) {
                        LoggingUtils.logUnexpectedException(LOGGER, "Exception when evaluating item from search result list.", e);
                    } else {
                        throw new SystemException(e);
                    }
                }
            } else {
                outputData.addValue(object.getValue(), item.getVariables());
            }
            return true;
        };
        try {
            Collection<SelectorOptions<GetOperationOptions>> options = operationsHelper.createGetOptions(searchExpression.getOptions(), noFetch);
            modelService.searchObjectsIterative(objectClass, objectQuery, handler, options, context.getTask(), globalResult);
        } catch (SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
            // TODO continue on any error?
            throw new ScriptExecutionException("Couldn't execute searchObjects operation: " + e.getMessage(), e);
        }
    }
    if (atLeastOne.isFalse()) {
        // temporary hack, this will be configurable
        context.println("Warning: no matching object found");
    }
    return outputData;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationsHelper(com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Trace(com.evolveum.midpoint.util.logging.Trace) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) ExpressionHelper(com.evolveum.midpoint.model.impl.scripting.helpers.ExpressionHelper) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) Collection(java.util.Collection) JAXBElement(javax.xml.bind.JAXBElement) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) List(java.util.List) Component(org.springframework.stereotype.Component) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) VariablesUtil.cloneIfNecessary(com.evolveum.midpoint.model.impl.scripting.VariablesUtil.cloneIfNecessary) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SearchExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType) Collections(java.util.Collections) Validate(org.apache.commons.lang.Validate) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) JAXBElement(javax.xml.bind.JAXBElement) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions)

Example 4 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.

the class ExecuteScriptExecutor method getParameters.

private Parameters getParameters(ActionExpressionType action, PipelineData input, ExecutionContext context, OperationResult globalResult) throws CommunicationException, ObjectNotFoundException, SchemaException, ScriptExecutionException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
    ScriptExpressionEvaluatorType script = expressionHelper.getActionArgument(ScriptExpressionEvaluatorType.class, action, ExecuteScriptActionExpressionType.F_SCRIPT, PARAM_SCRIPT, input, context, null, PARAM_SCRIPT, globalResult);
    ItemDefinition<?> outputDefinition;
    String outputItemUri = expressionHelper.getSingleArgumentValue(action.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
    if (StringUtils.isNotBlank(outputItemUri)) {
        outputDefinition = getItemDefinition(outputItemUri);
    } else if (action instanceof ExecuteScriptActionExpressionType) {
        ExecuteScriptActionExpressionType execute = (ExecuteScriptActionExpressionType) action;
        if (execute.getOutputItemName() != null) {
            outputDefinition = getItemDefinitionFromItemName(execute.getOutputItemName());
        } else if (execute.getOutputTypeName() != null) {
            outputDefinition = getItemDefinitionFromTypeName(execute.getOutputTypeName());
        } else {
            outputDefinition = null;
        }
    } else {
        outputDefinition = null;
    }
    boolean forWholeInput = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_FOR_WHOLE_INPUT, PARAM_FOR_WHOLE_INPUT, input, context, false, PARAM_FOR_WHOLE_INPUT, globalResult);
    boolean quiet = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_QUIET, PARAM_QUIET, input, context, false, PARAM_QUIET, globalResult);
    if (script == null) {
        throw new IllegalArgumentException("No script provided");
    }
    // TODO
    ExpressionProfile expressionProfile = null;
    ScriptExpression scriptExpression;
    try {
        scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, expressionProfile, expressionFactory, "script", globalResult);
    } catch (ExpressionSyntaxException | SecurityViolationException e) {
        throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
    }
    return new Parameters(scriptExpression, outputDefinition, forWholeInput, quiet);
}
Also used : ExecuteScriptActionExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptActionExpressionType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)

Example 5 with ExpressionProfile

use of com.evolveum.midpoint.schema.expression.ExpressionProfile 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

ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)16 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)7 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)3 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)3 ScriptExpressionProfile (com.evolveum.midpoint.schema.expression.ScriptExpressionProfile)3 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 Trace (com.evolveum.midpoint.util.logging.Trace)3 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)3 NotNull (org.jetbrains.annotations.NotNull)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)2 ExpressionUtil (com.evolveum.midpoint.repo.common.expression.ExpressionUtil)2 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)2 ExpressionEvaluatorProfile (com.evolveum.midpoint.schema.expression.ExpressionEvaluatorProfile)2 MiscSchemaUtil (com.evolveum.midpoint.schema.util.MiscSchemaUtil)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)2 Collection (java.util.Collection)2 List (java.util.List)2