Search in sources :

Example 6 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class ExpressionHandlerImplTest method testEvaluateExpression.

@SuppressWarnings("unchecked")
@Test
public void testEvaluateExpression() throws Exception {
    PrismObject<ShadowType> account = PrismTestUtil.parseObject(new File(TEST_FOLDER, "account.xml"));
    ShadowType accountType = account.asObjectable();
    PrismObject<ResourceType> resource = PrismTestUtil.parseObject(new File(TEST_FOLDER_COMMON, "resource-dummy.xml"));
    ResourceType resourceType = resource.asObjectable();
    accountType.setResource(resourceType);
    ObjectSynchronizationType synchronization = resourceType.getSynchronization().getObjectSynchronization().get(0);
    for (ConditionalSearchFilterType filter : synchronization.getCorrelation()) {
        MapXNode clauseXNode = filter.getFilterClauseXNode();
        // key = q:equal, value = map (path + expression)
        RootXNode expressionNode = ((MapXNode) clauseXNode.getSingleSubEntry("filter value").getValue()).getEntryAsRoot(new QName(SchemaConstants.NS_C, "expression"));
        ExpressionType expression = PrismTestUtil.getPrismContext().parserFor(expressionNode).parseRealValue(ExpressionType.class);
        LOGGER.debug("Expression: {}", SchemaDebugUtil.prettyPrint(expression));
        OperationResult result = new OperationResult("testCorrelationRule");
        String name = expressionHandler.evaluateExpression(accountType, expression, "test expression", null, result);
        LOGGER.info(result.debugDump());
        assertEquals("Wrong expression result", "hbarbossa", name);
    }
}
Also used : ObjectSynchronizationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectSynchronizationType) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) QName(javax.xml.namespace.QName) ConditionalSearchFilterType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConditionalSearchFilterType) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) File(java.io.File) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) Test(org.testng.annotations.Test)

Example 7 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class TestParseMappingConst method assertPrismPropertyValueLocal.

@Override
protected void assertPrismPropertyValueLocal(PrismPropertyValue<MappingType> value) throws SchemaException {
    MappingType mappingType = value.getValue();
    ExpressionType expressionType = mappingType.getExpression();
    List<JAXBElement<?>> expressionEvaluatorElements = expressionType.getExpressionEvaluator();
    assertEquals("Wrong number of expression evaluator elemenets", 1, expressionEvaluatorElements.size());
    JAXBElement<?> expressionEvaluatorElement = expressionEvaluatorElements.get(0);
    Object evaluatorElementObject = expressionEvaluatorElement.getValue();
    if (!(evaluatorElementObject instanceof ConstExpressionEvaluatorType)) {
        AssertJUnit.fail("Const expression is of type " + evaluatorElementObject.getClass().getName());
    }
    ConstExpressionEvaluatorType constExpressionEvaluatorType = (ConstExpressionEvaluatorType) evaluatorElementObject;
    System.out.println("ConstExpressionEvaluatorType: " + constExpressionEvaluatorType);
    assertEquals("Wrong value in const evaluator", "foo", constExpressionEvaluatorType.getValue());
}
Also used : MappingType(com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType) ConstExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConstExpressionEvaluatorType) JAXBElement(javax.xml.bind.JAXBElement) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 8 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class Expression method evaluate.

public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    ExpressionVariables processedVariables = null;
    try {
        processedVariables = processInnerVariables(context.getVariables(), context.getContextDescription(), context.getTask(), context.getResult());
        ExpressionEvaluationContext contextWithProcessedVariables = context.shallowClone();
        contextWithProcessedVariables.setVariables(processedVariables);
        PrismValueDeltaSetTriple<V> outputTriple;
        ObjectReferenceType runAsRef = null;
        if (expressionType != null) {
            runAsRef = expressionType.getRunAsRef();
        }
        if (runAsRef == null) {
            outputTriple = evaluateExpressionEvaluators(contextWithProcessedVariables);
        } else {
            UserType userType = objectResolver.resolve(runAsRef, UserType.class, null, "runAs in " + context.getContextDescription(), context.getTask(), context.getResult());
            LOGGER.trace("Running {} as {} ({})", context.getContextDescription(), userType, runAsRef);
            try {
                outputTriple = securityEnforcer.runAs(() -> {
                    try {
                        return evaluateExpressionEvaluators(contextWithProcessedVariables);
                    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException e) {
                        throw new TunnelException(e);
                    }
                }, userType.asPrismObject());
            } catch (TunnelException te) {
                Throwable e = te.getCause();
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                }
                if (e instanceof Error) {
                    throw (Error) e;
                }
                if (e instanceof SchemaException) {
                    throw (SchemaException) e;
                }
                if (e instanceof ExpressionEvaluationException) {
                    throw (ExpressionEvaluationException) e;
                }
                if (e instanceof ObjectNotFoundException) {
                    throw (ObjectNotFoundException) e;
                }
                throw te;
            }
        }
        traceSuccess(context, processedVariables, outputTriple);
        return outputTriple;
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | RuntimeException | Error e) {
        traceFailure(context, processedVariables, e);
        throw e;
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)

Example 9 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.

the class ValueSetDefinition method init.

public void init(ExpressionFactory expressionFactory) throws SchemaException, ObjectNotFoundException {
    ExpressionType conditionType = setDefinitionType.getCondition();
    condition = ExpressionUtil.createCondition(conditionType, expressionFactory, shortDesc, task, result);
}
Also used : ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)

Example 10 with ExpressionType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType 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)

Aggregations

ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)19 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)8 QName (javax.xml.namespace.QName)8 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)7 ArrayList (java.util.ArrayList)7 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)6 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)6 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)6 Test (org.testng.annotations.Test)6 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)5 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)5 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)4 Task (com.evolveum.midpoint.task.api.Task)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)4 MappingType (com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType)4 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)3 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)3