Search in sources :

Example 1 with VariablesMap

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

the class ContainerableListPanel method evaluateExpression.

protected Collection<String> evaluateExpression(C rowValue, Item<?, ?> columnItem, ExpressionType expression, GuiObjectColumnType customColumn) {
    Task task = getPageBase().createSimpleTask(OPERATION_EVALUATE_EXPRESSION);
    OperationResult result = task.getResult();
    try {
        VariablesMap variablesMap = new VariablesMap();
        variablesMap.put(ExpressionConstants.VAR_OBJECT, rowValue, rowValue.getClass());
        if (columnItem != null) {
            variablesMap.put(ExpressionConstants.VAR_INPUT, columnItem, columnItem.getDefinition());
        }
        return ExpressionUtil.evaluateStringExpression(variablesMap, getPageBase().getPrismContext(), expression, MiscSchemaUtil.getExpressionProfile(), getPageBase().getExpressionFactory(), "evaluate column expression", task, result);
    } catch (Exception e) {
        LOGGER.error("Couldn't execute expression for {} column. Reason: {}", customColumn, e.getMessage(), e);
        result.recomputeStatus();
        OperationResultStatusPresentationProperties props = OperationResultStatusPresentationProperties.parseOperationalResultStatus(result.getStatus());
        // TODO: this is not entirely correct
        return Collections.singletonList(getPageBase().createStringResource(props.getStatusLabelKey()).getString());
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) OperationResultStatusPresentationProperties(com.evolveum.midpoint.web.page.admin.server.dto.OperationResultStatusPresentationProperties)

Example 2 with VariablesMap

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

the class ObjectNameColumn method evaluateExpression.

private String evaluateExpression(Item<ICellPopulator<SelectableBean<O>>> cellItem, ItemPath itemPath, O value) {
    Task task = pageBase.createSimpleTask("evaluate column expression");
    try {
        com.evolveum.midpoint.prism.Item<?, ?> item = value.asPrismObject().findItem(itemPath);
        VariablesMap variablesMap = new VariablesMap();
        variablesMap.put(ExpressionConstants.VAR_OBJECT, value, value.getClass());
        if (item != null) {
            variablesMap.put(ExpressionConstants.VAR_INPUT, item, item.getDefinition().getTypeClass());
        }
        Collection<String> evaluatedValues = ExpressionUtil.evaluateStringExpression(variablesMap, pageBase.getPrismContext(), expression, MiscSchemaUtil.getExpressionProfile(), pageBase.getExpressionFactory(), "evaluate column expression", task, task.getResult());
        if (evaluatedValues != null) {
            return evaluatedValues.iterator().next();
        }
        return null;
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
        LOGGER.error("Couldn't execute expression for name column");
        OperationResult result = task.getResult();
        OperationResultStatusPresentationProperties props = OperationResultStatusPresentationProperties.parseOperationalResultStatus(result.getStatus());
        return cellItem.getString(props.getStatusLabelKey());
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PageOperationResult(com.evolveum.midpoint.web.page.error.PageOperationResult) OperationResultStatusPresentationProperties(com.evolveum.midpoint.web.page.admin.server.dto.OperationResultStatusPresentationProperties) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 3 with VariablesMap

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

the class DirectAssignmentCertificationHandler method itemSelectionExpressionAccepts.

@SuppressWarnings("unused")
private boolean itemSelectionExpressionAccepts(AssignmentType assignment, boolean isInducement, ObjectType object, AccessCertificationCampaignType campaign, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    AccessCertificationObjectBasedScopeType scope = null;
    if (campaign.getScopeDefinition() instanceof AccessCertificationObjectBasedScopeType) {
        scope = (AccessCertificationObjectBasedScopeType) (campaign.getScopeDefinition());
    }
    if (scope == null || scope.getItemSelectionExpression() == null) {
        // no expression, no rejections
        return true;
    }
    ExpressionType selectionExpression = scope.getItemSelectionExpression();
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_ASSIGNMENT, assignment, AssignmentType.class);
    if (object instanceof FocusType) {
        variables.putObject(ExpressionConstants.VAR_FOCUS, (FocusType) object, FocusType.class);
    }
    if (object instanceof UserType) {
        variables.putObject(ExpressionConstants.VAR_USER, (UserType) object, UserType.class);
    }
    return expressionHelper.evaluateBooleanExpression(selectionExpression, variables, "item selection for assignment " + ObjectTypeUtil.toShortString(assignment), task, result);
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 4 with VariablesMap

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

the class PrimaryChangeAspectHelper method evaluateApplicabilityCondition.

// endregion
// region ========================================================================== Expression evaluation
public boolean evaluateApplicabilityCondition(PcpAspectConfigurationType config, ModelContext modelContext, Serializable itemToApprove, VariablesMap 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 = prismContext.definitionFactory().createPropertyDefinition(resultName, DOMUtil.XSD_BOOLEAN);
    VariablesMap variablesMap = new VariablesMap();
    variablesMap.put(ExpressionConstants.VAR_MODEL_CONTEXT, modelContext, ModelContext.class);
    variablesMap.put(ExpressionConstants.VAR_ITEM_TO_APPROVE, itemToApprove, itemToApprove.getClass());
    if (additionalVariables != null) {
        variablesMap.addVariableDefinitions(additionalVariables);
    }
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple;
    try {
        Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, MiscSchemaUtil.getExpressionProfile(), "applicability condition expression", task, result);
        ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variablesMap, "applicability condition expression", task);
        exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | RuntimeException | CommunicationException | ConfigurationException | SecurityViolationException 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 : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 5 with VariablesMap

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

the class CaseStageOpening method computeAdditionalInformation.

private void computeAdditionalInformation(CaseWorkItemType workItem, OperationResult result) {
    if (stageDef.getAdditionalInformation() != null) {
        try {
            VariablesMap variables = beans.caseMiscHelper.getDefaultVariables(currentCase, approvalContext, task.getChannel(), result);
            workItem.getAdditionalInformation().addAll(beans.expressionEvaluationHelper.evaluateExpression(stageDef.getAdditionalInformation(), variables, "additional information expression", InformationType.class, InformationType.COMPLEX_TYPE, true, this::createInformationType, operation.getTask(), result));
        } catch (Throwable t) {
            throw new SystemException("Couldn't evaluate additional information expression in " + operation, t);
        }
    }
}
Also used : SystemException(com.evolveum.midpoint.util.exception.SystemException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

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