Search in sources :

Example 6 with ScriptExpressionEvaluationContext

use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.

the class SandboxTypeCheckingExtension method handleUnresolvedVariableExpression.

@Override
public boolean handleUnresolvedVariableExpression(VariableExpression vexp) {
    String variableName = vexp.getName();
    ScriptExpressionEvaluationContext context = getContext();
    String contextDescription = context.getContextDescription();
    if (!isDynamic(vexp)) {
        LOGGER.error("Unresolved script variable {} because it is not dynamic, in {}", variableName, contextDescription);
        return false;
    }
    VariablesMap variables = context.getVariables();
    if (variables != null) {
        TypedValue variableTypedValue = variables.get(variableName);
        if (variableTypedValue != null) {
            Class variableClass;
            try {
                variableClass = variableTypedValue.determineClass();
            } catch (SchemaException e) {
                String msg = "Cannot determine type of variable '" + variableName + "' (" + variableTypedValue + ") in " + contextDescription + ": " + e.getMessage();
                LOGGER.error("{}", msg);
                throw new IllegalStateException(msg, e);
            }
            LOGGER.trace("Determine script variable {} as expression variable, class {} in {}", variableName, variableClass, contextDescription);
            storeType(vexp, ClassHelper.make(variableClass));
            setHandled(true);
            return true;
        }
    }
    Collection<FunctionLibrary> functions = context.getFunctions();
    if (functions != null) {
        for (FunctionLibrary function : functions) {
            if (function.getVariableName().equals(variableName)) {
                Class functionClass = function.getGenericFunctions().getClass();
                LOGGER.trace("Determine script variable {} as function library, class {} in {}", variableName, functionClass, contextDescription);
                storeType(vexp, ClassHelper.make(functionClass));
                setHandled(true);
                return true;
            }
        }
    }
    LOGGER.error("Unresolved script variable {} because no declaration for it cannot be found in {}", variableName, contextDescription);
    return false;
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 7 with ScriptExpressionEvaluationContext

use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.

the class ModelImplUtils method evaluateScript.

@NotNull
public static <V extends PrismValue, F extends ObjectType> List<V> evaluateScript(ScriptExpression scriptExpression, LensContext<F> lensContext, VariablesMap variables, boolean useNew, String shortDesc, Task task, OperationResult parentResult) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment.ExpressionEnvironmentBuilder<F, PrismValue, ItemDefinition<?>>().lensContext(lensContext).currentResult(parentResult).currentTask(task).build());
    try {
        ScriptExpressionEvaluationContext context = new ScriptExpressionEvaluationContext();
        context.setVariables(variables);
        context.setSuggestedReturnType(ScriptExpressionReturnTypeType.SCALAR);
        context.setEvaluateNew(useNew);
        context.setScriptExpression(scriptExpression);
        context.setContextDescription(shortDesc);
        context.setTask(task);
        context.setResult(parentResult);
        return scriptExpression.evaluate(context);
    } finally {
        ModelExpressionThreadLocalHolder.popExpressionEnvironment();
    }
}
Also used : ExpressionEnvironment(com.evolveum.midpoint.model.common.expression.ExpressionEnvironment) ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ScriptExpressionEvaluationContext

use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.

the class MidpointFunctionsImpl method isDirectlyAssigned.

@Override
public boolean isDirectlyAssigned(String targetOid) {
    ModelContext<? extends FocusType> ctx = ModelExpressionThreadLocalHolder.getLensContextRequired();
    ModelElementContext<? extends FocusType> focusContext = ctx.getFocusContextRequired();
    PrismObject<? extends FocusType> focus;
    ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
    if (scriptContext == null) {
        focus = focusContext.getObjectAny();
    } else if (scriptContext.isEvaluateNew()) {
        // Evaluating new state
        if (focusContext.isDelete()) {
            return false;
        }
        focus = focusContext.getObjectNew();
    } else {
        // Evaluating old state
        if (focusContext.isAdd()) {
            return false;
        }
        focus = focusContext.getObjectOld();
    }
    if (focus == null) {
        return false;
    }
    return isDirectlyAssigned(focus.asObjectable(), targetOid);
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext)

Example 9 with ScriptExpressionEvaluationContext

use of com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext in project midpoint by Evolveum.

the class MidpointFunctionsImpl method hasLinkedAccount.

@Override
public <F extends ObjectType> boolean hasLinkedAccount(String resourceOid) {
    ModelContext<F> ctx = ModelExpressionThreadLocalHolder.getLensContextRequired();
    ModelElementContext<F> focusContext = ctx.getFocusContextRequired();
    ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
    ResourceShadowDiscriminator rat = new ResourceShadowDiscriminator(resourceOid, ShadowKindType.ACCOUNT, null, null, false);
    ModelProjectionContext projectionContext = ctx.findProjectionContext(rat);
    LOGGER.trace("hasLinkedAccount: found projection context {} for {}", projectionContext, rat);
    if (projectionContext == null) {
        // but check if it is not among list of deleted contexts
        if (scriptContext == null || scriptContext.isEvaluateNew()) {
            return false;
        }
        // evaluating old state
        for (ResourceShadowDiscriminator deletedOne : ctx.getHistoricResourceObjects()) {
            if (resourceOid.equals(deletedOne.getResourceOid()) && deletedOne.getKind() == ShadowKindType.ACCOUNT && deletedOne.getIntent() == null || "default".equals(deletedOne.getIntent())) {
                // TODO implement this seriously
                // TODO remove
                LOGGER.trace("Found deleted one: {}", deletedOne);
                return true;
            }
        }
        return false;
    }
    if (projectionContext.isGone()) {
        return false;
    }
    SynchronizationPolicyDecision synchronizationPolicyDecision = projectionContext.getSynchronizationPolicyDecision();
    SynchronizationIntent synchronizationIntent = projectionContext.getSynchronizationIntent();
    if (scriptContext == null) {
        if (synchronizationPolicyDecision == null) {
            return synchronizationIntent != SynchronizationIntent.DELETE && synchronizationIntent != SynchronizationIntent.UNLINK;
        } else {
            return synchronizationPolicyDecision != SynchronizationPolicyDecision.DELETE && synchronizationPolicyDecision != SynchronizationPolicyDecision.UNLINK;
        }
    } else if (scriptContext.isEvaluateNew()) {
        // Evaluating new state
        if (focusContext.isDelete()) {
            return false;
        }
        if (synchronizationPolicyDecision == null) {
            return synchronizationIntent != SynchronizationIntent.DELETE && synchronizationIntent != SynchronizationIntent.UNLINK;
        } else {
            return synchronizationPolicyDecision != SynchronizationPolicyDecision.DELETE && synchronizationPolicyDecision != SynchronizationPolicyDecision.UNLINK;
        }
    } else {
        // Evaluating old state
        if (focusContext.isAdd()) {
            return false;
        }
        if (synchronizationPolicyDecision == null) {
            return synchronizationIntent != SynchronizationIntent.ADD;
        } else {
            return synchronizationPolicyDecision != SynchronizationPolicyDecision.ADD;
        }
    }
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext)

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