Search in sources :

Example 26 with ExpressionVariables

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

the class Mapping method isInRange.

private boolean isInRange(V value, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    @NotNull ValueSetSpecificationType range = mappingType.getRange();
    if (range.getIsInSetExpression() == null) {
        return false;
    }
    ExpressionVariables variables = new ExpressionVariables();
    // TODO is this ok?
    variables.addVariableDefinitions(this.variables);
    if (value instanceof PrismContainerValue) {
        // artifically create parent for PCV in order to pass it to expression
        PrismContainer.createParentIfNeeded((PrismContainerValue) value, outputDefinition);
    }
    variables.addVariableDefinition(ExpressionConstants.VAR_VALUE, value);
    PrismPropertyDefinition<Boolean> outputDef = new PrismPropertyDefinitionImpl<Boolean>(SchemaConstantsGenerated.C_VALUE, DOMUtil.XSD_BOOLEAN, getPrismContext(), null, false);
    PrismPropertyValue<Boolean> rv = ExpressionUtil.evaluateExpression(variables, outputDef, range.getIsInSetExpression(), expressionFactory, "isInSet expression in " + contextDescription, task, result);
    // but now remove the parent!
    if (value.getParent() != null) {
        value.setParent(null);
    }
    return rv != null && rv.getValue() != null ? rv.getValue() : Boolean.FALSE;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with ExpressionVariables

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

the class PrimaryChangeAspectHelper method evaluateApplicabilityCondition.

//endregion
//region ========================================================================== Expression evaluation
public boolean evaluateApplicabilityCondition(PcpAspectConfigurationType config, ModelContext modelContext, Serializable itemToApprove, ExpressionVariables additionalVariables, PrimaryChangeAspect aspect, Task task, OperationResult result) {
    if (config == null || config.getApplicabilityCondition() == null) {
        return true;
    }
    ExpressionType expressionType = config.getApplicabilityCondition();
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
    ExpressionVariables expressionVariables = new ExpressionVariables();
    expressionVariables.addVariableDefinition(SchemaConstants.C_MODEL_CONTEXT, modelContext);
    expressionVariables.addVariableDefinition(SchemaConstants.C_ITEM_TO_APPROVE, itemToApprove);
    if (additionalVariables != null) {
        expressionVariables.addVariableDefinitions(additionalVariables);
    }
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple;
    try {
        Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, "applicability condition expression", task, result);
        ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, "applicability condition expression", task, result);
        exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | RuntimeException e) {
        // TODO report as a specific exception?
        throw new SystemException("Couldn't evaluate applicability condition in aspect " + aspect.getClass().getSimpleName() + ": " + e.getMessage(), e);
    }
    Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
    if (exprResult.size() == 0) {
        return false;
    } else if (exprResult.size() > 1) {
        throw new IllegalStateException("Applicability condition expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
    }
    Boolean boolResult = exprResult.iterator().next().getValue();
    return boolResult != null ? boolResult : false;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) SystemException(com.evolveum.midpoint.util.exception.SystemException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 28 with ExpressionVariables

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

the class WfExpressionEvaluationHelper method evaluateExpression.

@SuppressWarnings("unchecked")
@NotNull
public <T> List<T> evaluateExpression(ExpressionType expressionType, ExpressionVariables variables, String contextDescription, Class<T> clazz, QName typeName, Function<Object, Object> additionalConvertor, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    ExpressionFactory expressionFactory = getExpressionFactory();
    PrismContext prismContext = expressionFactory.getPrismContext();
    PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl<>(new QName(SchemaConstants.NS_C, "result"), typeName, prismContext);
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, contextDescription, task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, contextDescription, task, result);
    context.setAdditionalConvertor(additionalConvertor);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, context, task, result);
    return exprResultTriple.getZeroSet().stream().map(ppv -> (T) ppv.getRealValue()).collect(Collectors.toList());
}
Also used : SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) SpringApplicationContextHolder.getMiscDataUtil(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getMiscDataUtil) MiscDataUtil(com.evolveum.midpoint.wf.impl.util.MiscDataUtil) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DOMUtil(com.evolveum.midpoint.util.DOMUtil) QNameUtil(com.evolveum.midpoint.util.QNameUtil) PrismContext(com.evolveum.midpoint.prism.PrismContext) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) Expression(com.evolveum.midpoint.repo.common.expression.Expression) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) SpringApplicationContextHolder.getExpressionFactory(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getExpressionFactory) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) ModelExpressionThreadLocalHolder(com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) Task(com.evolveum.midpoint.task.api.Task) ApprovalUtils(com.evolveum.midpoint.wf.util.ApprovalUtils) JAXBException(javax.xml.bind.JAXBException) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Component(org.springframework.stereotype.Component) List(java.util.List) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) DelegateExecution(org.activiti.engine.delegate.DelegateExecution) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) ApprovalLevelOutcomeType(com.evolveum.midpoint.xml.ns._public.common.common_3.ApprovalLevelOutcomeType) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) SpringApplicationContextHolder.getExpressionFactory(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getExpressionFactory) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismContext(com.evolveum.midpoint.prism.PrismContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with ExpressionVariables

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

the class WfExpressionEvaluationHelper method getDefaultVariables.

public ExpressionVariables getDefaultVariables(@Nullable DelegateExecution execution, Task wfTask, OperationResult result) throws SchemaException, ObjectNotFoundException {
    MiscDataUtil miscDataUtil = getMiscDataUtil();
    ExpressionVariables variables = new ExpressionVariables();
    variables.addVariableDefinition(C_REQUESTER, miscDataUtil.resolveObjectReference(wfTask.getWorkflowContext().getRequesterRef(), result));
    variables.addVariableDefinition(C_OBJECT, miscDataUtil.resolveObjectReference(wfTask.getWorkflowContext().getObjectRef(), result));
    // might be null
    variables.addVariableDefinition(C_TARGET, miscDataUtil.resolveObjectReference(wfTask.getWorkflowContext().getTargetRef(), result));
    ObjectDelta objectDelta;
    try {
        objectDelta = miscDataUtil.getFocusPrimaryDelta(wfTask.getWorkflowContext(), true);
    } catch (JAXBException e) {
        throw new SchemaException("Couldn't get object delta: " + e.getMessage(), e);
    }
    variables.addVariableDefinition(SchemaConstants.T_OBJECT_DELTA, objectDelta);
    // Activiti process instance variables (use with care)
    if (execution != null) {
        execution.getVariables().entrySet().forEach(e -> variables.addVariableDefinition(new QName("_" + e.getKey()), e.getValue()));
    }
    return variables;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SpringApplicationContextHolder.getMiscDataUtil(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getMiscDataUtil) MiscDataUtil(com.evolveum.midpoint.wf.impl.util.MiscDataUtil) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta)

Example 30 with ExpressionVariables

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

the class WfTimedActionTriggerHandler method computeDelegateTo.

private List<ObjectReferenceType> computeDelegateTo(DelegateWorkItemActionType delegateAction, WorkItemType workItem, Task wfTask, Task triggerScannerTask, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    List<ObjectReferenceType> rv = new ArrayList<>();
    rv.addAll(CloneUtil.cloneCollectionMembers(delegateAction.getApproverRef()));
    if (!delegateAction.getApproverExpression().isEmpty()) {
        ExpressionVariables variables = evaluationHelper.getDefaultVariables(null, wfTask, result);
        variables.addVariableDefinition(SchemaConstants.C_WORK_ITEM, workItem);
        rv.addAll(evaluationHelper.evaluateRefExpressions(delegateAction.getApproverExpression(), variables, "computing delegates", triggerScannerTask, result));
    }
    //		}
    return rv;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ArrayList(java.util.ArrayList)

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