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;
}
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();
}
}
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);
}
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;
}
}
}
Aggregations