Example usage for org.apache.commons.collections.map ListOrderedMap values

List of usage examples for org.apache.commons.collections.map ListOrderedMap values

Introduction

In this page you can find the example usage for org.apache.commons.collections.map ListOrderedMap values.

Prototype

public Collection values() 

Source Link

Usage

From source file:org.jahia.services.content.ConflictResolver.java

private List<Diff> compare(JCRNodeWrapper sourceNode, JCRNodeWrapper targetNode, String basePath)
        throws RepositoryException {

    List<Diff> diffs = new ArrayList<Diff>();

    boolean remotelyPublished = targetNode.isNodeType("jmix:remotelyPublished");

    if (!remotelyPublished) {

        final ListOrderedMap targetUuids = getChildEntries(targetNode, targetNode.getSession());
        final ListOrderedMap sourceUuids = getChildEntries(sourceNode, sourceNode.getSession());

        if (!targetUuids.values().equals(sourceUuids.values())) {
            for (Iterator<?> iterator = sourceUuids.keySet().iterator(); iterator.hasNext();) {
                String key = (String) iterator.next();
                if (targetUuids.containsKey(key) && !targetUuids.get(key).equals(sourceUuids.get(key))) {
                    diffs.add(new ChildRenamedDiff(key, addPath(basePath, (String) targetUuids.get(key)),
                            addPath(basePath, (String) sourceUuids.get(key))));
                }// w w  w  .ja v a 2 s . co m
            }
        }

        // Child nodes
        if (!targetUuids.keyList().equals(sourceUuids.keyList())) {

            @SuppressWarnings("unchecked")
            List<String> addedUuids = new ArrayList<String>(sourceUuids.keySet());
            addedUuids.removeAll(targetUuids.keySet());
            @SuppressWarnings("unchecked")
            List<String> removedUuids = new ArrayList<String>(targetUuids.keySet());
            removedUuids.removeAll(sourceUuids.keySet());

            // Ordering
            if (targetNode.getPrimaryNodeType().hasOrderableChildNodes()) {
                Map<String, String> newOrdering = getOrdering(sourceUuids, Collections.<String>emptyList());
                @SuppressWarnings("unchecked")
                List<String> oldUuidsList = new ArrayList<String>(targetUuids.keySet());
                oldUuidsList.removeAll(removedUuids);
                @SuppressWarnings("unchecked")
                List<String> newUuidsList = new ArrayList<String>(sourceUuids.keySet());
                newUuidsList.removeAll(addedUuids);
                if (!oldUuidsList.equals(newUuidsList)) {
                    for (int i = 1; i < oldUuidsList.size(); i++) {
                        String x = oldUuidsList.get(i);
                        int j = i;
                        while (j > 0 && sourceUuids.indexOf(oldUuidsList.get(j - 1)) > sourceUuids.indexOf(x)) {
                            oldUuidsList.set(j, oldUuidsList.get(j - 1));
                            j--;
                        }
                        if (j != i) {
                            String orderBeforeUuid = (j + 1 == oldUuidsList.size()) ? null
                                    : oldUuidsList.get(j + 1);
                            diffs.add(new ChildNodeReorderedDiff(x, orderBeforeUuid,
                                    addPath(basePath, (String) sourceUuids.get(x)),
                                    (String) sourceUuids.get(orderBeforeUuid), newOrdering));
                            logger.debug("reorder " + sourceUuids.get(x) + " before "
                                    + sourceUuids.get(orderBeforeUuid));
                            oldUuidsList.set(j, x);
                        }
                    }
                }
            }

            // Removed nodes
            for (String removedUuid : removedUuids) {
                try {
                    this.sourceNode.getSession().getNodeByUUID(removedUuid);
                } catch (ItemNotFoundException e) {
                    // Item has been moved
                    diffs.add(new ChildRemovedDiff(removedUuid,
                            addPath(basePath, (String) targetUuids.get(removedUuid)), removedUuid));
                }
            }

            // Added nodes
            for (String addedUuid : addedUuids) {
                diffs.add(new ChildAddedDiff(addedUuid, addPath(basePath, (String) sourceUuids.get(addedUuid)),
                        addedUuid.equals(sourceUuids.lastKey()) ? null
                                : (String) sourceUuids
                                        .get(sourceUuids.get(sourceUuids.indexOf(addedUuid) + 1))));
            }
        }
    }

    PropertyIterator targetProperties = targetNode.getProperties();

    while (targetProperties.hasNext()) {

        JCRPropertyWrapper targetProperty = (JCRPropertyWrapper) targetProperties.next();

        String propertyName = targetProperty.getName();
        if (IGNORED_PROPRTIES.contains(propertyName)) {
            continue;
        }

        if (!sourceNode.hasProperty(propertyName)) {
            if (targetProperty.isMultiple()) {
                Value[] values = targetProperty.getRealValues();
                for (Value value : values) {
                    diffs.add(new PropertyRemovedDiff(addPath(basePath, propertyName), value));
                }
            } else {
                diffs.add(new PropertyChangedDiff(addPath(basePath, propertyName), null));
            }
        } else {

            JCRPropertyWrapper sourceProperty = sourceNode.getProperty(propertyName);

            if (targetProperty.isMultiple() != sourceProperty.isMultiple()) {
                throw new RepositoryException();
            }

            if (targetProperty.isMultiple()) {

                List<? extends Value> targetValues = Arrays.asList(targetProperty.getRealValues());
                List<? extends Value> sourceValues = Arrays.asList(sourceProperty.getRealValues());

                Map<String, Value> addedValues = new HashMap<String, Value>();
                for (Value value : sourceValues) {
                    addedValues.put(value.getString(), value);
                }
                for (Value value : targetValues) {
                    addedValues.remove(value.getString());
                }
                for (Value value : addedValues.values()) {
                    diffs.add(new PropertyAddedDiff(addPath(basePath, propertyName), value));
                }

                Map<String, Value> removedValues = new HashMap<String, Value>();
                for (Value value : targetValues) {
                    removedValues.put(value.getString(), value);
                }
                for (Value value : sourceValues) {
                    removedValues.remove(value.getString());
                }
                for (Value value : removedValues.values()) {
                    diffs.add(new PropertyRemovedDiff(addPath(basePath, propertyName), value));
                }
            } else {
                if (!equalsValue(targetProperty.getRealValue(), sourceProperty.getRealValue())) {
                    diffs.add(new PropertyChangedDiff(addPath(basePath, propertyName),
                            sourceProperty.getRealValue()));
                }
            }
        }
    }

    PropertyIterator sourceProperties = sourceNode.getProperties();

    while (sourceProperties.hasNext()) {

        JCRPropertyWrapper sourceProperty = (JCRPropertyWrapper) sourceProperties.next();

        String propertyName = sourceProperty.getName();

        if (IGNORED_PROPRTIES.contains(propertyName)) {
            continue;
        }
        if (!targetNode.hasProperty(propertyName)) {
            if (sourceProperty.isMultiple()) {
                Value[] values = sourceProperty.getRealValues();
                for (Value value : values) {
                    diffs.add(new PropertyAddedDiff(addPath(basePath, sourceProperty.getName()), value));
                }
            } else {
                diffs.add(new PropertyChangedDiff(addPath(basePath, sourceProperty.getName()),
                        sourceProperty.getRealValue()));
            }
        }

    }

    for (Diff diff : new ArrayList<Diff>(diffs)) {
        if (diff instanceof PropertyAddedDiff
                && ((PropertyAddedDiff) diff).propertyPath.endsWith(Constants.JCR_MIXINTYPES)) {
            diffs.remove(diff);
            diffs.add(0, diff);
        }
    }

    for (Diff diff : new ArrayList<Diff>(diffs)) {
        if (diff instanceof PropertyRemovedDiff
                && ((PropertyRemovedDiff) diff).propertyPath.endsWith(Constants.JCR_MIXINTYPES)) {
            diffs.remove(diff);
            diffs.add(0, diff);
        }
    }

    if (!remotelyPublished) {
        NodeIterator targetSubNodes = targetNode.getNodes();
        while (targetSubNodes.hasNext()) {
            JCRNodeWrapper targetSubNode = (JCRNodeWrapper) targetSubNodes.next();
            if (sourceNode.hasNode(targetSubNode.getName()) && !targetSubNode.isVersioned()
                    && !sourceNode.getNode(targetSubNode.getName()).isVersioned()
                    && JCRPublicationService.supportsPublication(targetSubNode.getSession(), targetSubNode)) {
                diffs.addAll(compare(sourceNode.getNode(targetSubNode.getName()), targetSubNode,
                        addPath(basePath, targetSubNode.getName())));
            }
        }
    }

    return diffs;
}