Search in sources :

Example 66 with VariablesMap

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

the class ItemProcessingConditionEvaluator method evaluateConditionDefaultTrue.

boolean evaluateConditionDefaultTrue(@Nullable ExpressionType expression, @Nullable AdditionalVariableProvider additionalVariableProvider, OperationResult result) {
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_REQUEST, request, ItemProcessingRequest.class);
    variables.put(ExpressionConstants.VAR_ITEM, request.getItem(), request.getItem().getClass());
    if (additionalVariableProvider != null) {
        additionalVariableProvider.provide(variables);
    }
    try {
        return ExpressionUtil.evaluateConditionDefaultTrue(variables, expression, null, activityRun.getBeans().expressionFactory, "item condition expression", activityRun.getRunningTask(), result);
    } catch (CommonException e) {
        throw new SystemException("Couldn't evaluate 'before item' condition: " + e.getMessage(), e);
    }
}
Also used : SystemException(com.evolveum.midpoint.util.exception.SystemException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) CommonException(com.evolveum.midpoint.util.exception.CommonException)

Example 67 with VariablesMap

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

the class ExpressionValidator method validate.

@Override
public void validate(IValidatable<T> validatable) {
    ExpressionType expressionType = expressionTypeModel.getObject();
    if (expressionType == null) {
        return;
    }
    PrismContext prismContext = serviceLocator.getPrismContext();
    Object valueToValidate = getValueToValidate(validatable);
    String contextDesc = " form component expression validation ";
    PrismPropertyDefinition<OperationResultType> outputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, OperationResultType.COMPLEX_TYPE);
    Task task = serviceLocator.createSimpleTask(OPERATION_EVALUATE_EXPRESSION);
    OperationResult result = new OperationResult(OPERATION_EVALUATE_EXPRESSION);
    ExpressionFactory expressionFactory = serviceLocator.getExpressionFactory();
    Expression<PrismPropertyValue<OperationResultType>, PrismPropertyDefinition<OperationResultType>> expression;
    try {
        expression = expressionFactory.makeExpression(expressionType, outputDefinition, MiscSchemaUtil.getExpressionProfile(), contextDesc, task, result);
    } catch (SchemaException | ObjectNotFoundException | SecurityViolationException e) {
        ValidationError error = new ValidationError();
        error.setMessage("Cannot make expression: " + e.getMessage());
        validatable.error(error);
        return;
    }
    VariablesMap variables = new VariablesMap();
    Class typeClass = (valueToValidate == null ? String.class : valueToValidate.getClass());
    variables.put(ExpressionConstants.VAR_INPUT, valueToValidate, typeClass);
    variables.putObject(ExpressionConstants.VAR_OBJECT, (ObjectType) getObjectType(), ObjectType.class);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, contextDesc, task);
    PrismValueDeltaSetTriple<PrismPropertyValue<OperationResultType>> outputTriple;
    try {
        outputTriple = expression.evaluate(context, result);
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
        ValidationError error = new ValidationError();
        error.setMessage("Cannot evaluate expression: " + e.getMessage());
        validatable.error(error);
        return;
    }
    if (outputTriple == null) {
        return;
    }
    Collection<PrismPropertyValue<OperationResultType>> outputValues = outputTriple.getNonNegativeValues();
    if (outputValues.isEmpty()) {
        return;
    }
    if (outputValues.size() > 1) {
        ValidationError error = new ValidationError();
        error.setMessage("Expression " + contextDesc + " produced more than one value");
        validatable.error(error);
    }
    OperationResultType operationResultType = outputValues.iterator().next().getRealValue();
    if (operationResultType == null) {
        return;
    }
    OperationResult returnResult = OperationResult.createOperationResult(operationResultType);
    if (!returnResult.isSuccess()) {
        ValidationError error = new ValidationError();
        if (returnResult.getUserFriendlyMessage() != null) {
            error.setMessage(WebModelServiceUtils.translateMessage(returnResult, serviceLocator));
        } else {
            error.setMessage(returnResult.getMessage());
        }
        validatable.error(error);
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) PrismContext(com.evolveum.midpoint.prism.PrismContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ValidationError(org.apache.wicket.validation.ValidationError) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 68 with VariablesMap

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

the class AccCertCaseOperationsHelper method computeDelegateTo.

private List<ObjectReferenceType> computeDelegateTo(DelegateWorkItemActionType delegateAction, AccessCertificationWorkItemType workItem, AccessCertificationCaseType aCase, AccessCertificationCampaignType campaign, Task task, OperationResult result) {
    List<ObjectReferenceType> rv = CloneUtil.cloneCollectionMembers(delegateAction.getApproverRef());
    if (!delegateAction.getApproverExpression().isEmpty()) {
        VariablesMap variables = new VariablesMap();
        variables.put(ExpressionConstants.VAR_WORK_ITEM, workItem, AccessCertificationWorkItemType.class);
        variables.put(ExpressionConstants.VAR_CERTIFICATION_CASE, aCase, aCase.asPrismContainerValue().getDefinition());
        variables.putObject(ExpressionConstants.VAR_CAMPAIGN, campaign, AccessCertificationCampaignType.class);
        for (ExpressionType expression : delegateAction.getApproverExpression()) {
            rv.addAll(expressionHelper.evaluateRefExpressionChecked(expression, variables, "computing delegates", task, result));
        }
    }
    return rv;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 69 with VariablesMap

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

the class CaseMiscHelper method getDefaultVariables.

public VariablesMap getDefaultVariables(CaseType aCase, ApprovalContextType wfContext, String requestChannel, OperationResult result) throws SchemaException {
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_REQUESTER, resolveTypedObjectReference(aCase.getRequestorRef(), result));
    variables.put(ExpressionConstants.VAR_OBJECT, resolveTypedObjectReference(aCase.getObjectRef(), result));
    // might be null
    variables.put(ExpressionConstants.VAR_TARGET, resolveTypedObjectReference(aCase.getTargetRef(), result));
    variables.put(ExpressionConstants.VAR_CHANNEL, requestChannel, String.class);
    variables.put(ExpressionConstants.VAR_THE_CASE, aCase, List.class);
    // FIXME these are specific for approvals
    variables.put(ExpressionConstants.VAR_OBJECT_DELTA, getFocusPrimaryDelta(wfContext), ObjectDelta.class);
    variables.put(ExpressionConstants.VAR_APPROVAL_CONTEXT, wfContext, ApprovalContextType.class);
    return variables;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 70 with VariablesMap

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

the class PerformerCommentsFormatterImpl method formatComment.

private String formatComment(ObjectReferenceType performerRef, AbstractWorkItemOutputType output, AbstractWorkItemType workItem, WorkItemCompletionEventType event, Task task, OperationResult result) {
    if (formatting == null || formatting.getCondition() == null && formatting.getValue() == null) {
        return output.getComment();
    }
    ObjectType performer = getPerformer(performerRef, result);
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_PERFORMER, performer, performer.asPrismObject().getDefinition());
    variables.put(ExpressionConstants.VAR_OUTPUT, output, AbstractWorkItemOutputType.class);
    variables.put(ExpressionConstants.VAR_WORK_ITEM, workItem, AbstractWorkItemType.class);
    variables.put(ExpressionConstants.VAR_EVENT, event, WorkItemCompletionEventType.class);
    if (formatting.getCondition() != null) {
        try {
            boolean condition = expressionEvaluationHelper.evaluateBooleanExpression(formatting.getCondition(), variables, "condition in performer comments formatter", task, result);
            if (!condition) {
                return null;
            }
        } catch (CommonException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't evaluate condition expression in comments formatter - continuing as if it were true; " + "performer = {}, output = {}, workItem = {}, event = {}", e, performer, output, workItem, event);
        }
    }
    if (formatting.getValue() != null) {
        try {
            return expressionEvaluationHelper.evaluateStringExpression(formatting.getValue(), variables, "value in performer comments formatter", task, result);
        } catch (CommonException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't evaluate value expression in comments formatter - using plain comment value; " + "performer = {}, output = {}, workItem = {}, event = {}", e, performer, output, workItem, event);
            return output.getComment();
        }
    } else {
        return output.getComment();
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) CommonException(com.evolveum.midpoint.util.exception.CommonException)

Aggregations

VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)166 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)48 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)30 Test (org.testng.annotations.Test)28 Task (com.evolveum.midpoint.task.api.Task)23 NotNull (org.jetbrains.annotations.NotNull)23 QName (javax.xml.namespace.QName)15 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)12 Source (com.evolveum.midpoint.repo.common.expression.Source)12 AbstractModelCommonTest (com.evolveum.midpoint.model.common.AbstractModelCommonTest)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)10 Trace (com.evolveum.midpoint.util.logging.Trace)10 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)10 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)9 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)9 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)8 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)8 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)7