Search in sources :

Example 1 with MergeStategyType

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

the class ObjectMerger method takeProjections.

private void takeProjections(MergeStategyType strategy, List<ShadowType> mergedProjections, List<ShadowType> matchedProjections, List<ShadowType> candidateProjections, List<ShadowType> projectionsLeft, List<ShadowType> projectionsRight, ProjectionMergeConfigurationType projectionMergeConfig) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("TAKE: Evaluating situation {}, discriminator: {}", projectionMergeConfig.getSituation(), projectionMergeConfig.getProjectionDiscriminator());
    }
    for (ShadowType candidateProjection : candidateProjections) {
        if (projectionMatches(candidateProjection, projectionsLeft, projectionsRight, projectionMergeConfig)) {
            LOGGER.trace("Projection matches {}", candidateProjection);
            matchedProjections.add(candidateProjection);
            if (strategy == MergeStategyType.TAKE) {
                mergedProjections.add(candidateProjection);
            } else if (strategy == null || strategy == MergeStategyType.IGNORE) {
            // Nothing to do here
            } else {
                throw new UnsupportedOperationException("Merge strategy " + strategy + " is not supported");
            }
        } else {
            LOGGER.trace("Discriminator does NOT match {}", candidateProjection);
        }
    }
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)

Example 2 with MergeStategyType

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

the class ObjectMerger method mergeItem.

private <O extends ObjectType, I extends Item> ItemDelta mergeItem(PrismObject<O> objectLeft, PrismObject<O> objectRight, String mergeConfigurationName, ItemMergeConfigurationType itemMergeConfig, ItemPath itemPath, Task task, OperationResult result) throws SchemaException, ConfigurationException, ExpressionEvaluationException, ObjectNotFoundException {
    I itemLeft = (I) objectLeft.findItem(itemPath);
    I itemRight = (I) objectRight.findItem(itemPath);
    if (itemLeft == null && itemRight == null) {
        return null;
    }
    ItemDefinition itemDefinition = null;
    if (itemLeft != null) {
        itemDefinition = itemLeft.getDefinition();
    } else {
        itemDefinition = itemRight.getDefinition();
    }
    if (itemDefinition.isOperational()) {
        // we do not want to modify them explicitly.
        return null;
    }
    Expression<PrismValue, ItemDefinition> valueExpression = null;
    if (itemMergeConfig.getValueExpression() != null) {
        ExpressionType expressionType = itemMergeConfig.getValueExpression();
        valueExpression = expressionFactory.makeExpression(expressionType, itemDefinition, "value expression for item " + itemPath + " in merge configuration " + mergeConfigurationName, task, result);
    }
    ItemDelta itemDelta = itemDefinition.createEmptyDelta(itemPath);
    MergeStategyType leftStrategy = itemMergeConfig.getLeft();
    MergeStategyType rightStrategy = itemMergeConfig.getRight();
    if (leftStrategy == null || leftStrategy == MergeStategyType.IGNORE) {
        if (rightStrategy == null || rightStrategy == MergeStategyType.IGNORE) {
            // IGNORE both
            if (itemLeft == null) {
                return null;
            } else {
                itemDelta.setValueToReplace();
                return itemDelta;
            }
        } else {
            // IGNORE left, TAKE/EXPRESSION right
            if (itemRight == null) {
                itemDelta.setValueToReplace();
            } else {
                Collection<PrismValue> valuesToTake = getValuesToTake(objectLeft, objectRight, SIDE_RIGHT, itemRight, rightStrategy, valueExpression, task, result);
                itemDelta.setValuesToReplace(valuesToTake);
            }
            return itemDelta;
        }
    } else {
        if (rightStrategy == null || rightStrategy == MergeStategyType.IGNORE) {
            if (leftStrategy == MergeStategyType.TAKE) {
                // TAKE left, IGNORE right
                return null;
            } else {
                // EXPRESSION left, IGNORE right
                Collection<PrismValue> valuesToLeave = getValuesToTake(objectLeft, objectRight, SIDE_LEFT, itemLeft, leftStrategy, valueExpression, task, result);
                List<PrismValue> currentLeftValues = itemLeft.getValues();
                Collection<PrismValue> leftValuesToRemove = diffValues(currentLeftValues, valuesToLeave);
                if (leftValuesToRemove != null && !leftValuesToRemove.isEmpty()) {
                    itemDelta.addValuesToDelete(leftValuesToRemove);
                    return itemDelta;
                } else {
                    return null;
                }
            }
        } else {
            // TAKE/EXPRESSION left, TAKE/EXPRESSION right
            if (itemLeft == null) {
                Collection<PrismValue> valuesToTake = getValuesToTake(objectLeft, objectRight, SIDE_RIGHT, itemRight, rightStrategy, valueExpression, task, result);
                itemDelta.addValuesToAdd(valuesToTake);
                return itemDelta;
            } else {
                // We want to add only those values that are not yet there.
                // E.g. adding assignments that are there can cause unnecessary churn
                Collection<PrismValue> leftValuesToLeave = getValuesToTake(objectLeft, objectRight, SIDE_LEFT, itemLeft, leftStrategy, valueExpression, task, result);
                Collection<PrismValue> rightValuesToTake = getValuesToTake(objectLeft, objectRight, SIDE_RIGHT, itemRight, rightStrategy, valueExpression, task, result);
                for (PrismValue rightValueToTake : rightValuesToTake) {
                    if (!PrismValue.collectionContainsEquivalentValue(leftValuesToLeave, rightValueToTake)) {
                        itemDelta.addValueToAdd(rightValueToTake);
                    }
                }
                List<PrismValue> currentLeftValues = itemLeft.getValues();
                Collection<PrismValue> leftValuesToRemove = diffValues(currentLeftValues, leftValuesToLeave);
                if (leftValuesToRemove != null && !leftValuesToRemove.isEmpty()) {
                    itemDelta.addValuesToDelete(leftValuesToRemove);
                }
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Merging item {} T/T case:\n  leftValuesToLeave: {}\n  rightValuesToTake: {}\n  leftValuesToRemove: {}\n itemDelta:\n{}", new Object[] { itemPath, leftValuesToLeave, rightValuesToTake, leftValuesToRemove, itemDelta.debugDump(2) });
                }
                return itemDelta;
            }
        }
    }
}
Also used : ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) MergeStategyType(com.evolveum.midpoint.xml.ns._public.common.common_3.MergeStategyType) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Aggregations

ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)1 PrismValue (com.evolveum.midpoint.prism.PrismValue)1 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)1 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)1 MergeStategyType (com.evolveum.midpoint.xml.ns._public.common.common_3.MergeStategyType)1 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)1