Search in sources :

Example 56 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class ReportServiceImpl method evaluateScript.

public Collection<PrismContainerValue<? extends Containerable>> evaluateScript(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    List<PrismContainerValue<? extends Containerable>> results = new ArrayList<>();
    ExpressionVariables variables = new ExpressionVariables();
    variables.addVariableDefinitions(parameters);
    // special variable for audit report
    variables.addVariableDefinition(new QName("auditParams"), getConvertedParams(parameters));
    Task task = taskManager.createTaskInstance(ReportService.class.getName() + ".evaluateScript");
    OperationResult parentResult = task.getResult();
    Collection<FunctionLibrary> functions = createFunctionLibraries();
    Jsr223ScriptEvaluator scripts = new Jsr223ScriptEvaluator("Groovy", prismContext, prismContext.getDefaultProtector());
    ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(task, task.getResult()));
    Object o = null;
    try {
        o = scripts.evaluateReportScript(script, variables, objectResolver, functions, "desc", parentResult);
    } finally {
        ModelExpressionThreadLocalHolder.popExpressionEnvironment();
    }
    if (o != null) {
        if (Collection.class.isAssignableFrom(o.getClass())) {
            Collection resultSet = (Collection) o;
            if (resultSet != null && !resultSet.isEmpty()) {
                for (Object obj : resultSet) {
                    results.add(convertResultingObject(obj));
                }
            }
        } else {
            results.add(convertResultingObject(o));
        }
    }
    return results;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Jsr223ScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) Task(com.evolveum.midpoint.task.api.Task) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Collection(java.util.Collection) Containerable(com.evolveum.midpoint.prism.Containerable) PrismObject(com.evolveum.midpoint.prism.PrismObject)

Example 57 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class GeneralNotifier method getDefaultVariables.

@Override
protected ExpressionVariables getDefaultVariables(Event event, OperationResult result) {
    ExpressionVariables variables = super.getDefaultVariables(event, result);
    variables.addVariableDefinition(SchemaConstants.C_TEXT_FORMATTER, textFormatter);
    return variables;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables)

Example 58 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class AbstractValueTransformationExpressionEvaluator method evaluateRelativeExpression.

private PrismValueDeltaSetTriple<V> evaluateRelativeExpression(final List<SourceTriple<?, ?>> sourceTriples, final ExpressionVariables variables, final boolean skipEvaluationMinus, final boolean skipEvaluationPlus, final Boolean includeNulls, final ExpressionEvaluationContext evaluationContext, final String contextDescription, final Task task, final OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    List<Collection<? extends PrismValue>> valueCollections = new ArrayList<>(sourceTriples.size());
    for (SourceTriple<?, ?> sourceTriple : sourceTriples) {
        Collection<? extends PrismValue> values = sourceTriple.union();
        if (values.isEmpty()) {
            // No values for this source. Add null instead. It will make sure that the expression will
            // be evaluate at least once.
            values.add(null);
        }
        valueCollections.add(values);
    }
    final PrismValueDeltaSetTriple<V> outputTriple = new PrismValueDeltaSetTriple<>();
    Processor<Collection<? extends PrismValue>> processor = pvalues -> {
        if (includeNulls != null && !includeNulls && MiscUtil.isAllNull(pvalues)) {
            return;
        }
        Map<QName, Object> sourceVariables = new HashMap<>();
        Iterator<SourceTriple<PrismValue, ?>> sourceTriplesIterator = (Iterator) sourceTriples.iterator();
        boolean hasMinus = false;
        boolean hasZero = false;
        boolean hasPlus = false;
        for (PrismValue pval : pvalues) {
            SourceTriple<PrismValue, ?> sourceTriple = sourceTriplesIterator.next();
            QName name = sourceTriple.getName();
            sourceVariables.put(name, getRealContent(pval, sourceTriple.getResidualPath()));
            if (sourceTriple.presentInPlusSet(pval)) {
                hasPlus = true;
            } else if (sourceTriple.presentInZeroSet(pval)) {
                hasZero = true;
            } else if (sourceTriple.presentInMinusSet(pval)) {
                hasMinus = true;
            }
        }
        if (!hasPlus && !hasMinus && !hasZero && !MiscUtil.isAllNull(pvalues)) {
            throw new IllegalStateException("Internal error! The impossible has happened! pvalues=" + pvalues + "; source triples: " + sourceTriples + "; in " + contextDescription);
        }
        if (hasPlus && hasMinus) {
            return;
        }
        if (hasPlus && skipEvaluationPlus) {
            return;
        } else if (hasMinus && skipEvaluationMinus) {
            return;
        }
        ExpressionVariables scriptVariables = new ExpressionVariables();
        scriptVariables.addVariableDefinitions(sourceVariables);
        PlusMinusZero valueDestination = null;
        boolean useNew = false;
        if (hasPlus) {
            scriptVariables.addVariableDefinitionsNew(variables);
            valueDestination = PlusMinusZero.PLUS;
            useNew = true;
        } else if (hasMinus) {
            scriptVariables.addVariableDefinitionsOld(variables);
            valueDestination = PlusMinusZero.MINUS;
        } else {
            scriptVariables.addVariableDefinitionsNew(variables);
            valueDestination = PlusMinusZero.ZERO;
            useNew = true;
        }
        List<V> scriptResults;
        try {
            scriptResults = transformSingleValue(scriptVariables, valueDestination, useNew, evaluationContext, contextDescription, task, result);
        } catch (ExpressionEvaluationException e) {
            throw new TunnelException(new ExpressionEvaluationException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        } catch (ObjectNotFoundException e) {
            throw new TunnelException(new ObjectNotFoundException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        } catch (SchemaException e) {
            throw new TunnelException(new SchemaException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        } catch (RuntimeException e) {
            throw new TunnelException(new RuntimeException(e.getMessage() + "(" + dumpSourceValues(sourceVariables) + ") in " + contextDescription, e));
        }
        outputTriple.addAllToSet(valueDestination, scriptResults);
    };
    try {
        MiscUtil.carthesian((Collection) valueCollections, (Processor) processor);
    } catch (TunnelException e) {
        Throwable originalException = e.getCause();
        if (originalException instanceof ExpressionEvaluationException) {
            throw (ExpressionEvaluationException) originalException;
        } else if (originalException instanceof ObjectNotFoundException) {
            throw (ObjectNotFoundException) originalException;
        } else if (originalException instanceof SchemaException) {
            throw (SchemaException) originalException;
        } else if (originalException instanceof RuntimeException) {
            throw (RuntimeException) originalException;
        } else {
            throw new IllegalStateException("Unexpected exception: " + e + ": " + e.getMessage(), e);
        }
    }
    cleanupTriple(outputTriple);
    return outputTriple;
}
Also used : PrismValue(com.evolveum.midpoint.prism.PrismValue) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) SourceTriple(com.evolveum.midpoint.repo.common.expression.SourceTriple) java.util(java.util) Item(com.evolveum.midpoint.prism.Item) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) SecurityEnforcer(com.evolveum.midpoint.security.api.SecurityEnforcer) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TransformExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.TransformExpressionEvaluatorType) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) Trace(com.evolveum.midpoint.util.logging.Trace) PrettyPrinter(com.evolveum.midpoint.util.PrettyPrinter) ExpressionConstants(com.evolveum.midpoint.schema.constants.ExpressionConstants) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) TransformExpressionRelativityModeType(com.evolveum.midpoint.xml.ns._public.common.common_3.TransformExpressionRelativityModeType) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ExpressionEvaluator(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluator) DeltaSetTriple(com.evolveum.midpoint.prism.delta.DeltaSetTriple) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) MiscUtil(com.evolveum.midpoint.util.MiscUtil) Task(com.evolveum.midpoint.task.api.Task) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) PlusMinusZero(com.evolveum.midpoint.prism.delta.PlusMinusZero) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Processor(com.evolveum.midpoint.util.Processor) Entry(java.util.Map.Entry) Source(com.evolveum.midpoint.repo.common.expression.Source) QName(javax.xml.namespace.QName) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismValue(com.evolveum.midpoint.prism.PrismValue) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) QName(javax.xml.namespace.QName) PlusMinusZero(com.evolveum.midpoint.prism.delta.PlusMinusZero) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SourceTriple(com.evolveum.midpoint.repo.common.expression.SourceTriple)

Example 59 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class ValuePolicyProcessor method checkExpression.

public <O extends ObjectType> boolean checkExpression(String generatedValue, ExpressionType checkExpression, PrismObject<O> object, String shortDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    ExpressionVariables variables = new ExpressionVariables();
    variables.addVariableDefinition(ExpressionConstants.VAR_INPUT, generatedValue);
    variables.addVariableDefinition(ExpressionConstants.VAR_OBJECT, object);
    PrismPropertyValue<Boolean> output = ExpressionUtil.evaluateCondition(variables, checkExpression, expressionFactory, shortDesc, task, result);
    return ExpressionUtil.getBooleanConditionOutput(output);
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables)

Example 60 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class ExpressionHandler method evaluateConfirmationExpression.

public boolean evaluateConfirmationExpression(UserType user, ShadowType shadow, 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.");
    ResourceType resource = resolveResource(shadow, result);
    ExpressionVariables variables = getDefaultXPathVariables(user, shadow, resource);
    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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Aggregations

ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)65 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)17 QName (javax.xml.namespace.QName)15 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)10 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)10 Test (org.testng.annotations.Test)10 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)9 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)9 Task (com.evolveum.midpoint.task.api.Task)8 ArrayList (java.util.ArrayList)8 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)7 PrismObject (com.evolveum.midpoint.prism.PrismObject)6 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)6 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)6 PrismContext (com.evolveum.midpoint.prism.PrismContext)5 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)5 SystemException (com.evolveum.midpoint.util.exception.SystemException)5 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)5 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)4