Search in sources :

Example 11 with ItemDeltaItem

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

the class TestAbstractAssignmentEvaluator method test300DisableRoleEmployee.

/**
	 * Jack is an Engineer which induces Employee. But role Employee is not valid anymore.
     */
@Test
public void test300DisableRoleEmployee() throws Exception {
    final String TEST_NAME = "test300DisableRoleEmployee";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(TestAssignmentEvaluator.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    // disable role Employee
    ObjectDelta disableEmployeeDelta = DeltaBuilder.deltaFor(RoleType.class, prismContext).item(ACTIVATION_ADMINISTRATIVE_STATUS_PATH).replace(ActivationStatusType.DISABLED).asObjectDelta(ROLE_CORP_EMPLOYEE_OID);
    modelService.executeChanges(Collections.<ObjectDelta<? extends ObjectType>>singletonList(disableEmployeeDelta), null, task, result);
    AssignmentEvaluator<UserType> assignmentEvaluator = createAssignmentEvaluator();
    PrismAsserts.assertParentConsistency(userTypeJack.asPrismObject());
    AssignmentType assignmentType = getAssignmentType(ASSIGNMENT_ROLE_ENGINEER_FILE);
    ObjectDeltaObject<UserType> userOdo = new ObjectDeltaObject<>(userTypeJack.asPrismObject(), null, null);
    userOdo.recompute();
    ItemDeltaItem<PrismContainerValue<AssignmentType>, PrismContainerDefinition<AssignmentType>> assignmentIdi = new ItemDeltaItem<>();
    assignmentIdi.setItemOld(LensUtil.createAssignmentSingleValueContainerClone(assignmentType));
    assignmentIdi.recompute();
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    EvaluatedAssignmentImpl<UserType> evaluatedAssignment = assignmentEvaluator.evaluate(assignmentIdi, PlusMinusZero.ZERO, false, userTypeJack, "testRoleEngineer", task, result);
    evaluatedAssignment.evaluateConstructions(userOdo, task, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    result.computeStatus();
    TestUtil.assertSuccess(result);
    assertNotNull(evaluatedAssignment);
    display("Evaluated assignment", evaluatedAssignment.debugDump());
    assertEquals(2, evaluatedAssignment.getConstructionTriple().size());
    PrismAsserts.assertParentConsistency(userTypeJack.asPrismObject());
    assertConstruction(evaluatedAssignment, ZERO, "title", ZERO, "Engineer");
    assertConstruction(evaluatedAssignment, ZERO, "title", PLUS);
    assertConstruction(evaluatedAssignment, ZERO, "title", MINUS);
    assertNoConstruction(evaluatedAssignment, PLUS, "title");
    assertNoConstruction(evaluatedAssignment, MINUS, "title");
    assertConstruction(evaluatedAssignment, ZERO, "location", ZERO, "Caribbean");
    assertConstruction(evaluatedAssignment, ZERO, "location", PLUS);
    assertConstruction(evaluatedAssignment, ZERO, "location", MINUS);
    assertNoConstruction(evaluatedAssignment, PLUS, "location");
    assertNoConstruction(evaluatedAssignment, MINUS, "location");
    assertEquals("Wrong number of admin GUI configs", 1, evaluatedAssignment.getAdminGuiConfigurations().size());
}
Also used : Task(com.evolveum.midpoint.task.api.Task) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) RoleType(com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) AssignmentType(com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test)

Example 12 with ItemDeltaItem

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

the class PathExpressionEvaluator method evaluate.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java.util.Collection, java.util.Map, boolean, java.lang.String, com.evolveum.midpoint.schema.result.OperationResult)
	 */
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    ItemDeltaItem<?, ?> resolveContext = null;
    if (context.getSources() != null && context.getSources().size() == 1) {
        Source<?, ?> source = context.getSources().iterator().next();
        if (path.isEmpty()) {
            PrismValueDeltaSetTriple<V> outputTriple = (PrismValueDeltaSetTriple<V>) source.toDeltaSetTriple();
            return outputTriple.clone();
        }
        resolveContext = source;
    }
    Map<QName, Object> variablesAndSources = ExpressionUtil.compileVariablesAndSources(context);
    ItemPath resolvePath = path;
    ItemPathSegment first = path.first();
    if (first instanceof NameItemPathSegment && first.isVariable()) {
        QName variableName = ((NameItemPathSegment) first).getName();
        Object variableValue;
        if (variablesAndSources.containsKey(variableName)) {
            variableValue = variablesAndSources.get(variableName);
        } else if (QNameUtil.matchAny(variableName, variablesAndSources.keySet())) {
            QName fullVariableName = QNameUtil.resolveNs(variableName, variablesAndSources.keySet());
            variableValue = variablesAndSources.get(fullVariableName);
        } else {
            throw new ExpressionEvaluationException("No variable with name " + variableName + " in " + context.getContextDescription());
        }
        if (variableValue == null) {
            return null;
        }
        if (variableValue instanceof Item || variableValue instanceof ItemDeltaItem<?, ?>) {
            resolveContext = ExpressionUtil.toItemDeltaItem(variableValue, objectResolver, "path expression in " + context.getContextDescription(), context.getResult());
        } else if (variableValue instanceof PrismPropertyValue<?>) {
            PrismValueDeltaSetTriple<V> outputTriple = new PrismValueDeltaSetTriple<>();
            outputTriple.addToZeroSet((V) variableValue);
            return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
        } else {
            throw new ExpressionEvaluationException("Unexpected variable value " + variableValue + " (" + variableValue.getClass() + ")");
        }
        resolvePath = path.rest();
    }
    if (resolveContext == null) {
        return null;
    }
    while (!resolvePath.isEmpty()) {
        if (resolveContext.isContainer()) {
            resolveContext = resolveContext.findIdi(resolvePath.head());
            resolvePath = resolvePath.tail();
            if (resolveContext == null) {
                throw new ExpressionEvaluationException("Cannot find item using path " + path + " in " + context.getContextDescription());
            }
        } else if (resolveContext.isStructuredProperty()) {
            // The output path does not really matter. The delta will be converted to triple anyway
            // But the path cannot be null, oherwise the code will die
            resolveContext = resolveContext.resolveStructuredProperty(resolvePath, (PrismPropertyDefinition) outputDefinition, new ItemPath());
            break;
        } else if (resolveContext.isNull()) {
            break;
        } else {
            throw new ExpressionEvaluationException("Cannot resolve path " + resolvePath + " on " + resolveContext + " in " + context.getContextDescription());
        }
    }
    PrismValueDeltaSetTriple<V> outputTriple = ItemDelta.toDeltaSetTriple((Item<V, D>) resolveContext.getItemOld(), (ItemDelta<V, D>) resolveContext.getDelta());
    if (outputTriple == null) {
        return null;
    }
    return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) QName(javax.xml.namespace.QName) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) Item(com.evolveum.midpoint.prism.Item) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 13 with ItemDeltaItem

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

the class Mapping method parseSource.

private <IV extends PrismValue, ID extends ItemDefinition> Source<IV, ID> parseSource(VariableBindingDefinitionType sourceType, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    ItemPathType itemPathType = sourceType.getPath();
    if (itemPathType == null) {
        throw new SchemaException("No path in source definition in " + getMappingContextDescription());
    }
    ItemPath path = itemPathType.getItemPath();
    if (path.isEmpty()) {
        throw new SchemaException("Empty source path in " + getMappingContextDescription());
    }
    QName name = sourceType.getName();
    if (name == null) {
        name = ItemPath.getName(path.last());
    }
    ItemPath resolvePath = path;
    Object sourceObject = ExpressionUtil.resolvePath(path, variables, sourceContext, objectResolver, "source definition in " + getMappingContextDescription(), task, result);
    Item<IV, ID> itemOld = null;
    ItemDelta<IV, ID> delta = null;
    Item<IV, ID> itemNew = null;
    ItemPath residualPath = null;
    Collection<? extends ItemDelta<?, ?>> subItemDeltas = null;
    if (sourceObject != null) {
        if (sourceObject instanceof ItemDeltaItem<?, ?>) {
            itemOld = ((ItemDeltaItem<IV, ID>) sourceObject).getItemOld();
            delta = ((ItemDeltaItem<IV, ID>) sourceObject).getDelta();
            itemNew = ((ItemDeltaItem<IV, ID>) sourceObject).getItemNew();
            residualPath = ((ItemDeltaItem<IV, ID>) sourceObject).getResidualPath();
            resolvePath = ((ItemDeltaItem<IV, ID>) sourceObject).getResolvePath();
            subItemDeltas = ((ItemDeltaItem<IV, ID>) sourceObject).getSubItemDeltas();
        } else if (sourceObject instanceof Item<?, ?>) {
            itemOld = (Item<IV, ID>) sourceObject;
            itemNew = (Item<IV, ID>) sourceObject;
        } else {
            throw new IllegalStateException("Unknown resolve result " + sourceObject);
        }
    }
    // apply domain
    ValueSetDefinitionType domainSetType = sourceType.getSet();
    if (domainSetType != null) {
        ValueSetDefinition setDef = new ValueSetDefinition(domainSetType, name, "domain of " + name.getLocalPart() + " in " + getMappingContextDescription(), task, result);
        setDef.init(expressionFactory);
        try {
            if (itemOld != null) {
                itemOld = itemOld.clone();
                itemOld.filterValues(val -> setDef.containsTunnel(val));
            }
            if (itemNew != null) {
                itemNew = itemNew.clone();
                itemNew.filterValues(val -> setDef.containsTunnel(val));
            }
            if (delta != null) {
                delta = delta.clone();
                delta.filterValues(val -> setDef.containsTunnel(val));
            }
        } catch (TunnelException te) {
            Throwable cause = te.getCause();
            if (cause instanceof SchemaException) {
                throw (SchemaException) cause;
            } else if (cause instanceof ExpressionEvaluationException) {
                throw (ExpressionEvaluationException) cause;
            } else if (cause instanceof ObjectNotFoundException) {
                throw (ObjectNotFoundException) cause;
            }
        }
    }
    Source<IV, ID> source = new Source<>(itemOld, delta, itemNew, name);
    source.setResidualPath(residualPath);
    source.setResolvePath(resolvePath);
    source.setSubItemDeltas(subItemDeltas);
    return source;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) QName(javax.xml.namespace.QName) Source(com.evolveum.midpoint.repo.common.expression.Source) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) ValueSetDefinition(com.evolveum.midpoint.repo.common.expression.ValueSetDefinition) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 14 with ItemDeltaItem

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

the class MappingEvaluator method computeTargetValues.

private <V extends PrismValue, F extends FocusType> Collection<V> computeTargetValues(VariableBindingDefinitionType target, Object defaultTargetContext, ExpressionVariables variables, ObjectResolver objectResolver, String contextDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException {
    if (target == null) {
        // Is this correct? What about default targets?
        return null;
    }
    ItemPathType itemPathType = target.getPath();
    if (itemPathType == null) {
        // Is this correct? What about default targets?
        return null;
    }
    ItemPath path = itemPathType.getItemPath();
    Object object = ExpressionUtil.resolvePath(path, variables, defaultTargetContext, objectResolver, contextDesc, task, result);
    if (object == null) {
        return new ArrayList<>();
    } else if (object instanceof Item) {
        return ((Item) object).getValues();
    } else if (object instanceof PrismValue) {
        return (List<V>) Collections.singletonList((PrismValue) object);
    } else if (object instanceof ItemDeltaItem) {
        ItemDeltaItem<V, ?> idi = (ItemDeltaItem<V, ?>) object;
        PrismValueDeltaSetTriple<V> triple = idi.toDeltaSetTriple();
        return triple != null ? triple.getNonNegativeValues() : new ArrayList<V>();
    } else {
        throw new IllegalStateException("Unsupported target value(s): " + object.getClass() + " (" + object + ")");
    }
}
Also used : Item(com.evolveum.midpoint.prism.Item) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) PrismObject(com.evolveum.midpoint.prism.PrismObject) PrismValue(com.evolveum.midpoint.prism.PrismValue) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 15 with ItemDeltaItem

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

the class LensUtil method getIterationVariableValue.

public static Object getIterationVariableValue(LensProjectionContext accCtx) {
    Integer iterationOld = null;
    PrismObject<ShadowType> shadowCurrent = accCtx.getObjectCurrent();
    if (shadowCurrent != null) {
        iterationOld = shadowCurrent.asObjectable().getIteration();
    }
    if (iterationOld == null) {
        return accCtx.getIteration();
    }
    PrismPropertyDefinition<Integer> propDef = new PrismPropertyDefinitionImpl<>(ExpressionConstants.VAR_ITERATION, DOMUtil.XSD_INT, accCtx.getPrismContext());
    PrismProperty<Integer> propOld = propDef.instantiate();
    propOld.setRealValue(iterationOld);
    PropertyDelta<Integer> propDelta = propDef.createEmptyDelta(new ItemPath(ExpressionConstants.VAR_ITERATION));
    propDelta.setValueToReplace(new PrismPropertyValue<Integer>(accCtx.getIteration()));
    PrismProperty<Integer> propNew = propDef.instantiate();
    propNew.setRealValue(accCtx.getIteration());
    ItemDeltaItem<PrismPropertyValue<Integer>, PrismPropertyDefinition<Integer>> idi = new ItemDeltaItem<>(propOld, propDelta, propNew);
    return idi;
}
Also used : ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

ItemDeltaItem (com.evolveum.midpoint.repo.common.expression.ItemDeltaItem)25 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)16 ObjectDeltaObject (com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject)16 PrismContainerDefinition (com.evolveum.midpoint.prism.PrismContainerDefinition)15 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)15 Task (com.evolveum.midpoint.task.api.Task)15 AssignmentType (com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType)14 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)13 Test (org.testng.annotations.Test)13 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)9 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)4 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)4 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)3 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)3 PrismValueDeltaSetTriple (com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple)3 NameItemPathSegment (com.evolveum.midpoint.prism.path.NameItemPathSegment)3 Source (com.evolveum.midpoint.repo.common.expression.Source)3