Search in sources :

Example 1 with CompareResult

use of com.evolveum.midpoint.prism.path.ItemPath.CompareResult in project midpoint by Evolveum.

the class ItemDelta method accept.

public static void accept(Collection<? extends ItemDelta> modifications, Visitor visitor, ItemPath path, boolean recursive) {
    for (ItemDelta modification : modifications) {
        ItemPath modPath = modification.getPath();
        CompareResult rel = modPath.compareComplex(path);
        if (rel == CompareResult.EQUIVALENT) {
            modification.accept(visitor, null, recursive);
        } else if (rel == CompareResult.SUBPATH) {
            modification.accept(visitor, path.substract(modPath), recursive);
        }
    }
}
Also used : CompareResult(com.evolveum.midpoint.prism.path.ItemPath.CompareResult) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 2 with CompareResult

use of com.evolveum.midpoint.prism.path.ItemPath.CompareResult in project midpoint by Evolveum.

the class ItemDeltaItem method findIdi.

public <IV extends PrismValue, ID extends ItemDefinition> ItemDeltaItem<IV, ID> findIdi(ItemPath path) {
    if (path.isEmpty()) {
        return (ItemDeltaItem<IV, ID>) this;
    }
    Item<IV, ID> subItemOld = null;
    ItemPath subResidualPath = null;
    ItemPath newResolvePath = resolvePath.subPath(path);
    if (itemOld != null) {
        PartiallyResolvedItem<IV, ID> partialItemOld = itemOld.findPartial(path);
        if (partialItemOld != null) {
            subItemOld = partialItemOld.getItem();
            subResidualPath = partialItemOld.getResidualPath();
        }
    }
    Item<IV, ID> subItemNew = null;
    if (itemNew != null) {
        PartiallyResolvedItem<IV, ID> partialItemNew = itemNew.findPartial(path);
        if (partialItemNew != null) {
            subItemNew = partialItemNew.getItem();
            if (subResidualPath == null) {
                subResidualPath = partialItemNew.getResidualPath();
            }
        }
    }
    ItemDelta<IV, ID> subDelta = null;
    if (delta != null) {
        if (delta instanceof ContainerDelta<?>) {
            subDelta = (ItemDelta<IV, ID>) ((ContainerDelta<?>) delta).getSubDelta(path);
        } else {
            CompareResult compareComplex = delta.getPath().compareComplex(newResolvePath);
            if (compareComplex == CompareResult.EQUIVALENT || compareComplex == CompareResult.SUBPATH) {
                subDelta = (ItemDelta<IV, ID>) delta;
            }
        }
    }
    ItemDeltaItem<IV, ID> subIdi = new ItemDeltaItem<IV, ID>(subItemOld, subDelta, subItemNew);
    subIdi.setResidualPath(subResidualPath);
    subIdi.resolvePath = newResolvePath;
    if (subItemDeltas != null) {
        Item<IV, ID> subAnyItem = subIdi.getAnyItem();
        Collection<ItemDelta<?, ?>> subSubItemDeltas = new ArrayList<>();
        for (ItemDelta<?, ?> subItemDelta : subItemDeltas) {
            CompareResult compareComplex = subItemDelta.getPath().compareComplex(subAnyItem.getPath());
            if (compareComplex == CompareResult.EQUIVALENT || compareComplex == CompareResult.SUBPATH) {
                subSubItemDeltas.add(subItemDelta);
            }
        }
        if (!subSubItemDeltas.isEmpty()) {
            // Niceness optimization
            if (subDelta == null && subSubItemDeltas.size() == 1) {
                ItemDelta<?, ?> subSubItemDelta = subSubItemDeltas.iterator().next();
                if (subSubItemDelta.isApplicableTo(subAnyItem)) {
                    subDelta = (ItemDelta<IV, ID>) subSubItemDelta;
                    subIdi.setDelta(subDelta);
                } else {
                    subIdi.setSubItemDeltas(subSubItemDeltas);
                }
            } else {
                subIdi.setSubItemDeltas(subSubItemDeltas);
            }
        }
    }
    return subIdi;
}
Also used : ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) CompareResult(com.evolveum.midpoint.prism.path.ItemPath.CompareResult) ContainerDelta(com.evolveum.midpoint.prism.delta.ContainerDelta) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 3 with CompareResult

use of com.evolveum.midpoint.prism.path.ItemPath.CompareResult in project midpoint by Evolveum.

the class ObjectDeltaObject method findIdi.

@Override
public <IV extends PrismValue, ID extends ItemDefinition> ItemDeltaItem<IV, ID> findIdi(ItemPath path) {
    Item<IV, ID> subItemOld = null;
    ItemPath subResidualPath = null;
    if (oldObject != null) {
        PartiallyResolvedItem<IV, ID> partialOld = oldObject.findPartial(path);
        if (partialOld != null) {
            subItemOld = partialOld.getItem();
            subResidualPath = partialOld.getResidualPath();
        }
    }
    Item<IV, ID> subItemNew = null;
    if (newObject != null) {
        PartiallyResolvedItem<IV, ID> partialNew = newObject.findPartial(path);
        if (partialNew != null) {
            subItemNew = partialNew.getItem();
            if (subResidualPath == null) {
                subResidualPath = partialNew.getResidualPath();
            }
        }
    }
    ItemDelta<IV, ID> itemDelta = null;
    Collection<? extends ItemDelta<?, ?>> subSubItemDeltas = null;
    if (delta != null) {
        if (delta.getChangeType() == ChangeType.ADD) {
            PrismObject<O> objectToAdd = delta.getObjectToAdd();
            PartiallyResolvedItem<IV, ID> partialValue = objectToAdd.findPartial(path);
            if (partialValue != null && partialValue.getItem() != null) {
                Item<IV, ID> item = partialValue.getItem();
                itemDelta = item.createDelta();
                itemDelta.addValuesToAdd(item.getClonedValues());
            } else {
            // No item for this path, itemDelta will stay empty.
            }
        } else if (delta.getChangeType() == ChangeType.DELETE) {
            if (subItemOld != null) {
                ItemPath subPath = subItemOld.getPath().remainder(path);
                PartiallyResolvedItem<IV, ID> partialValue = subItemOld.findPartial(subPath);
                if (partialValue != null && partialValue.getItem() != null) {
                    Item<IV, ID> item = partialValue.getItem();
                    itemDelta = item.createDelta();
                    itemDelta.addValuesToDelete(item.getClonedValues());
                } else {
                // No item for this path, itemDelta will stay empty.
                }
            }
        } else if (delta.getChangeType() == ChangeType.MODIFY) {
            for (ItemDelta<?, ?> modification : delta.getModifications()) {
                CompareResult compareComplex = modification.getPath().compareComplex(path);
                if (compareComplex == CompareResult.EQUIVALENT) {
                    if (itemDelta != null) {
                        throw new IllegalStateException("Conflicting modification in delta " + delta + ": " + itemDelta + " and " + modification);
                    }
                    itemDelta = (ItemDelta<IV, ID>) modification;
                } else if (compareComplex == CompareResult.SUPERPATH) {
                    if (subSubItemDeltas == null) {
                        subSubItemDeltas = new ArrayList<>();
                    }
                    ((Collection) subSubItemDeltas).add(modification);
                } else if (compareComplex == CompareResult.SUBPATH) {
                    if (itemDelta != null) {
                        throw new IllegalStateException("Conflicting modification in delta " + delta + ": " + itemDelta + " and " + modification);
                    }
                    itemDelta = (ItemDelta<IV, ID>) modification.getSubDelta(path.substract(modification.getPath()));
                }
            }
        }
    }
    ItemDeltaItem<IV, ID> subIdi = new ItemDeltaItem<IV, ID>(subItemOld, itemDelta, subItemNew);
    subIdi.setSubItemDeltas(subSubItemDeltas);
    subIdi.setResolvePath(path);
    subIdi.setResidualPath(subResidualPath);
    return subIdi;
}
Also used : ArrayList(java.util.ArrayList) CompareResult(com.evolveum.midpoint.prism.path.ItemPath.CompareResult) Item(com.evolveum.midpoint.prism.Item) PartiallyResolvedItem(com.evolveum.midpoint.prism.PartiallyResolvedItem) PartiallyResolvedItem(com.evolveum.midpoint.prism.PartiallyResolvedItem) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 4 with CompareResult

use of com.evolveum.midpoint.prism.path.ItemPath.CompareResult in project midpoint by Evolveum.

the class ItemDelta method applyTo.

public void applyTo(Item item) throws SchemaException {
    ItemPath itemPath = item.getPath();
    ItemPath deltaPath = getPath();
    CompareResult compareComplex = itemPath.compareComplex(deltaPath);
    if (compareComplex == CompareResult.EQUIVALENT) {
        applyToMatchingPath(item);
        cleanupAllTheWayUp(item);
    } else if (compareComplex == CompareResult.SUBPATH) {
        if (item instanceof PrismContainer<?>) {
            PrismContainer<?> container = (PrismContainer<?>) item;
            ItemPath remainderPath = deltaPath.remainder(itemPath);
            Item subItem = container.findOrCreateItem(remainderPath, getItemClass(), getDefinition());
            applyToMatchingPath(subItem);
        } else {
            throw new SchemaException("Cannot apply delta " + this + " to " + item + " as delta path is below the item path and the item is not a container");
        }
    } else if (compareComplex == CompareResult.SUPERPATH) {
        throw new SchemaException("Cannot apply delta " + this + " to " + item + " as delta path is above the item path");
    } else if (compareComplex == CompareResult.NO_RELATION) {
        throw new SchemaException("Cannot apply delta " + this + " to " + item + " as paths do not match");
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) CompareResult(com.evolveum.midpoint.prism.path.ItemPath.CompareResult) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 5 with CompareResult

use of com.evolveum.midpoint.prism.path.ItemPath.CompareResult in project midpoint by Evolveum.

the class ObjectMerger method computeDefaultDeltas.

private <O extends ObjectType> void computeDefaultDeltas(final ObjectDelta<O> leftObjectDelta, final PrismObject<O> objectLeft, final PrismObject<O> objectRight, final List<ItemPath> processedPaths, MergeConfigurationType mergeConfiguration, final String mergeConfigurationName, final Task task, final OperationResult result) throws SchemaException, ConfigurationException, ExpressionEvaluationException, ObjectNotFoundException {
    final ItemMergeConfigurationType defaultItemMergeConfig = mergeConfiguration.getDefault();
    if (defaultItemMergeConfig != null) {
        try {
            Visitor visitor = new Visitor() {

                @Override
                public void visit(Visitable visitable) {
                    if (!(visitable instanceof Item)) {
                        return;
                    }
                    Item item = (Item) visitable;
                    ItemPath itemPath = item.getPath();
                    if (itemPath == null || itemPath.isEmpty()) {
                        return;
                    }
                    if (SchemaConstants.PATH_LINK_REF.equivalent(itemPath)) {
                        // Skip. There is a special processing for this.
                        return;
                    }
                    boolean found = false;
                    for (ItemPath processedPath : processedPaths) {
                        // Need to check for super-paths here.
                        // E.g. if we have already processed metadata, we do not want to process
                        // metadata/modifyTimestamp
                        CompareResult compareResult = processedPath.compareComplex(itemPath);
                        if (compareResult == CompareResult.EQUIVALENT || compareResult == CompareResult.SUBPATH) {
                            found = true;
                            break;
                        }
                    }
                    if (found) {
                        return;
                    }
                    processedPaths.add(itemPath);
                    if (item instanceof PrismContainer<?>) {
                        if (item.getDefinition().isSingleValue()) {
                            // we will handle every individual property there.
                            return;
                        } else {
                        // TODO: we may need special handling for multi-value containers
                        // such as assignment
                        }
                    }
                    ItemDelta itemDelta;
                    try {
                        itemDelta = mergeItem(objectLeft, objectRight, mergeConfigurationName, defaultItemMergeConfig, itemPath, task, result);
                    } catch (SchemaException | ConfigurationException | ExpressionEvaluationException | ObjectNotFoundException e) {
                        throw new TunnelException(e);
                    }
                    LOGGER.trace("Item {} delta (default): {}", itemPath, itemDelta);
                    if (itemDelta != null && !itemDelta.isEmpty()) {
                        leftObjectDelta.addModification(itemDelta);
                    }
                }
            };
            objectLeft.accept(visitor);
            objectRight.accept(visitor);
        } catch (TunnelException te) {
            if (te.getCause() instanceof SchemaException) {
                throw (SchemaException) te.getCause();
            } else if (te.getCause() instanceof ConfigurationException) {
                throw (ConfigurationException) te.getCause();
            } else if (te.getCause() instanceof ExpressionEvaluationException) {
                throw (ExpressionEvaluationException) te.getCause();
            } else if (te.getCause() instanceof ObjectNotFoundException) {
                throw (ObjectNotFoundException) te.getCause();
            } else {
                throw new SystemException("Unexpected exception: " + te, te);
            }
        }
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) Visitor(com.evolveum.midpoint.prism.Visitor) Visitable(com.evolveum.midpoint.prism.Visitable) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ItemMergeConfigurationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ItemMergeConfigurationType) CompareResult(com.evolveum.midpoint.prism.path.ItemPath.CompareResult) Item(com.evolveum.midpoint.prism.Item) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) SystemException(com.evolveum.midpoint.util.exception.SystemException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 CompareResult (com.evolveum.midpoint.prism.path.ItemPath.CompareResult)5 Item (com.evolveum.midpoint.prism.Item)2 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ArrayList (java.util.ArrayList)2 PartiallyResolvedItem (com.evolveum.midpoint.prism.PartiallyResolvedItem)1 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)1 Visitable (com.evolveum.midpoint.prism.Visitable)1 Visitor (com.evolveum.midpoint.prism.Visitor)1 ContainerDelta (com.evolveum.midpoint.prism.delta.ContainerDelta)1 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)1 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)1 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 TunnelException (com.evolveum.midpoint.util.exception.TunnelException)1 ItemMergeConfigurationType (com.evolveum.midpoint.xml.ns._public.common.common_3.ItemMergeConfigurationType)1