Package org.apache.jackrabbit.core.state

Examples of org.apache.jackrabbit.core.state.ItemState


     * @throws RepositoryException if the property is not valid
     */
    private PropertyState getPropertyState() throws RepositoryException {
        // JCR-1272: Need to get the state reference now so it
        // doesn't get invalidated after the sanity check
        ItemState state = getItemState();
        sanityCheck();
        return (PropertyState) state;
    }
View Full Code Here


         * @throws ItemStateException if any other error occurs.
         */
        public ItemState getItemState(ItemId id) throws NoSuchItemStateException, ItemStateException {
            // check ChangeLog first
            try {
                ItemState state = changes.get(id);
                if (state != null) {
                    return state;
                }
            } catch (NoSuchItemStateException e) {
                // item has been deleted, but we still return it by asking base
View Full Code Here

         * otherwise <code>false</code>.
         */
        public boolean hasItemState(ItemId id) {
            // check ChangeLog first
            try {
                ItemState state = changes.get(id);
                if (state != null) {
                    return true;
                }
            } catch (NoSuchItemStateException e) {
                // item has been deleted, but we still might return true by asking base
View Full Code Here

         * @param changes deleted {@link ItemState} are retrieved from this
         *  <code>ChangeLog</code>.
         */
        private AtticItemStateManager(ChangeLog changes) {
            for (Iterator it = changes.deletedStates(); it.hasNext();) {
                ItemState state = (ItemState) it.next();
                deleted.put(state.getId(), state);
            }
        }
View Full Code Here

         * @throws NoSuchItemStateException if the {@link ItemState} cannot
         * be found in the deleted map.
         * @throws ItemStateException if any other error occurs.
         */
        public ItemState getItemState(ItemId id) throws NoSuchItemStateException, ItemStateException {
            ItemState state = (ItemState) deleted.get(id);
            if (state != null) {
                return state;
            } else {
                throw new NoSuchItemStateException("Item not in the attic: " + id);
            }
View Full Code Here

         */

        // 1. modified items

        for (Iterator it = changes.modifiedStates(); it.hasNext();) {
            ItemState state = (ItemState) it.next();
            if (state.isNode()) {
                // node changed
                // covers the following cases:
                // 1) property added
                // 2) property removed
                // 3) child node added
                // 4) child node removed
                // 5) node moved
                // 6) node reordered
                // cases 1) and 2) are detected with added and deleted states
                // on the PropertyState itself.
                // cases 3) and 4) are detected with added and deleted states
                // on the NodeState itself.
                // in case 5) two or three nodes change. two nodes are changed
                // when a child node is renamed. three nodes are changed when
                // a node is really moved. In any case we are only interested in
                // the node that actually got moved.
                // in case 6) only one node state changes. the state of the
                // parent node.
                NodeState n = (NodeState) state;

                if (n.hasOverlayedState()) {
                    NodeId oldParentId = n.getOverlayedState().getParentId();
                    NodeId newParentId = n.getParentId();
                    if (newParentId != null && !oldParentId.equals(newParentId)) {
                        // node moved
                        // generate node removed & node added event
                        NodeState oldParent;
                        try {
                            oldParent = (NodeState) changes.get(oldParentId);
                        } catch (NoSuchItemStateException e) {
                            // old parent has been deleted, retrieve from
                            // shared item state manager
                            oldParent = (NodeState) stateMgr.getItemState(oldParentId);
                        }

                        NodeTypeImpl oldParentNodeType = getNodeType(oldParent, session);
                        Set mixins = oldParent.getMixinTypeNames();
                        Path newPath = getPath(n.getNodeId(), hmgr);
                        Path oldPath = getZombiePath(n.getNodeId(), hmgr);
                        if (!oldPath.equals(newPath)) {
                            events.add(EventState.childNodeRemoved(oldParentId,
                                    getParent(oldPath),
                                    n.getNodeId(),
                                    oldPath.getNameElement(),
                                    oldParentNodeType,
                                    mixins,
                                    session));

                            NodeState newParent = (NodeState) changes.get(newParentId);
                            NodeTypeImpl newParentNodeType = getNodeType(newParent, session);
                            mixins = newParent.getMixinTypeNames();
                            events.add(EventState.childNodeAdded(newParentId,
                                    getParent(newPath),
                                    n.getNodeId(),
                                    newPath.getNameElement(),
                                    newParentNodeType,
                                    mixins,
                                    session));
                        } else {
                            log.error("Unable to calculate old path of moved node");
                        }
                    } else {
                        // a moved node always has a modified parent node
                        NodeState parent = null;
                        try {
                            // root node does not have a parent UUID
                            if (state.getParentId() != null) {
                                parent = (NodeState) changes.get(state.getParentId());
                            }
                        } catch (NoSuchItemStateException e) {
                            // should never happen actually. this would mean
                            // the parent of this modified node is deleted
                            String msg = "Parent of node " + state.getId() + " is deleted.";
                            log.error(msg);
                            throw new ItemStateException(msg, e);
                        }
                        if (parent != null) {
                            // check if node has been renamed
                            NodeState.ChildNodeEntry moved = null;
                            for (Iterator removedNodes = parent.getRemovedChildNodeEntries().iterator(); removedNodes.hasNext();) {
                                NodeState.ChildNodeEntry child = (NodeState.ChildNodeEntry) removedNodes.next();
                                if (child.getId().equals(n.getNodeId())) {
                                    // found node re-added with different name
                                    moved = child;
                                }
                            }
                            if (moved != null) {
                                NodeTypeImpl nodeType = getNodeType(parent, session);
                                Set mixins = parent.getMixinTypeNames();
                                Path newPath = getPath(state.getId(), hmgr);
                                Path parentPath = getParent(newPath);
                                Path oldPath;
                                try {
                                    if (moved.getIndex() == 0) {
                                        oldPath = Path.create(parentPath, moved.getName(), false);
                                    } else {
                                        oldPath = Path.create(parentPath, moved.getName(), moved.getIndex(), false);
                                    }
                                } catch (MalformedPathException e) {
                                    // should never happen actually
                                    String msg = "Malformed path for item: " + state.getId();
                                    log.error(msg);
                                    throw new ItemStateException(msg, e);
                                }
                                events.add(EventState.childNodeRemoved(parent.getNodeId(),
                                        parentPath,
                                        n.getNodeId(),
                                        oldPath.getNameElement(),
                                        nodeType,
                                        mixins,
                                        session));
                                events.add(EventState.childNodeAdded(parent.getNodeId(),
                                        parentPath,
                                        n.getNodeId(),
                                        newPath.getNameElement(),
                                        nodeType,
                                        mixins,
                                        session));
                            }
                        }
                    }
                }

                // check if child nodes of modified node state have been reordered
                List reordered = n.getReorderedChildNodeEntries();
                NodeTypeImpl nodeType = getNodeType(n, session);
                Set mixins = n.getMixinTypeNames();
                if (reordered.size() > 0) {
                    // create a node removed and a node added event for every
                    // reorder
                    for (Iterator ro = reordered.iterator(); ro.hasNext();) {
                        NodeState.ChildNodeEntry child = (NodeState.ChildNodeEntry) ro.next();
                        QName name = child.getName();
                        int index = (child.getIndex() != 1) ? child.getIndex() : 0;
                        Path parentPath = getPath(n.getNodeId(), hmgr);
                        Path.PathElement addedElem = Path.create(name, index).getNameElement();
                        // get removed index
                        NodeState overlayed = (NodeState) n.getOverlayedState();
                        NodeState.ChildNodeEntry entry = overlayed.getChildNodeEntry(child.getId());
                        if (entry == null) {
                            throw new ItemStateException("Unable to retrieve old child index for item: " + child.getId());
                        }
                        int oldIndex = (entry.getIndex() != 1) ? entry.getIndex() : 0;
                        Path.PathElement removedElem = Path.create(name, oldIndex).getNameElement();

                        events.add(EventState.childNodeRemoved(n.getNodeId(),
                                parentPath,
                                child.getId(),
                                removedElem,
                                nodeType,
                                mixins,
                                session));

                        events.add(EventState.childNodeAdded(n.getNodeId(),
                                parentPath,
                                child.getId(),
                                addedElem,
                                nodeType,
                                mixins,
                                session));
                    }
                }
            } else {
                // property changed
                Path path = getPath(state.getId(), hmgr);
                NodeState parent = (NodeState) stateMgr.getItemState(state.getParentId());
                NodeTypeImpl nodeType = getNodeType(parent, session);
                Set mixins = parent.getMixinTypeNames();
                events.add(EventState.propertyChanged(state.getParentId(),
                        getParent(path),
                        path.getNameElement(),
                        nodeType,
                        mixins,
                        session));
            }
        }

        // 2. removed items

        for (Iterator it = changes.deletedStates(); it.hasNext();) {
            ItemState state = (ItemState) it.next();
            if (state.isNode()) {
                // node deleted
                NodeState n = (NodeState) state;
                NodeState parent = (NodeState) stateMgr.getItemState(n.getParentId());
                NodeTypeImpl nodeType = getNodeType(parent, session);
                Set mixins = parent.getMixinTypeNames();
                Path path = getZombiePath(state.getId(), hmgr);
                events.add(EventState.childNodeRemoved(n.getParentId(),
                        getParent(path),
                        n.getNodeId(),
                        path.getNameElement(),
                        nodeType,
                        mixins,
                        session));
            } else {
                // property removed
                // only create an event if node still exists
                try {
                    NodeState n = (NodeState) changes.get(state.getParentId());
                    // node state exists -> only property removed
                    NodeTypeImpl nodeType = getNodeType(n, session);
                    Set mixins = n.getMixinTypeNames();
                    Path path = getZombiePath(state.getId(), hmgr);
                    events.add(EventState.propertyRemoved(state.getParentId(),
                            getParent(path),
                            path.getNameElement(),
                            nodeType,
                            mixins,
                            session));
                } catch (NoSuchItemStateException e) {
                    // node removed as well -> do not create an event
                }
            }
        }

        // 3. added items

        for (Iterator it = changes.addedStates(); it.hasNext();) {
            ItemState state = (ItemState) it.next();
            if (state.isNode()) {
                // node created
                NodeState n = (NodeState) state;
                NodeId parentId = n.getParentId();
                NodeState parent;
                // unknown if parent node is also new
                if (stateMgr.hasItemState(parentId)) {
                    parent = (NodeState) stateMgr.getItemState(parentId);
                } else {
                    parent = (NodeState) changes.get(parentId);
                }
                NodeTypeImpl nodeType = getNodeType(parent, session);
                Set mixins = parent.getMixinTypeNames();
                Path path = getPath(n.getNodeId(), hmgr);
                events.add(EventState.childNodeAdded(parentId,
                        getParent(path),
                        n.getNodeId(),
                        path.getNameElement(),
                        nodeType,
                        mixins,
                        session));
            } else {
                // property created / set
                NodeState n = (NodeState) changes.get(state.getParentId());
                NodeTypeImpl nodeType = getNodeType(n, session);
                Set mixins = n.getMixinTypeNames();
                Path path = getPath(state.getId(), hmgr);
                events.add(EventState.propertyAdded(state.getParentId(),
                        getParent(path),
                        path.getNameElement(),
                        nodeType,
                        mixins,
                        session));
View Full Code Here

     */
    private boolean initVersionHistories(Iterator iter) throws RepositoryException {
        // walk through list of transient items and search for new versionable nodes
        boolean createdTransientState = false;
        while (iter.hasNext()) {
            ItemState itemState = (ItemState) iter.next();
            if (itemState.isNode()) {
                NodeImpl node = (NodeImpl) itemMgr.getItem(itemState.getId());
                if (node.isNodeType(QName.MIX_VERSIONABLE)) {
                    if (!node.hasProperty(QName.JCR_VERSIONHISTORY)) {
                        VersionManager vMgr = session.getVersionManager();
                        NodeState nodeState = (NodeState) itemState;
                        /**
 
View Full Code Here

                         * persistent item that has been transiently modified or removed
                         * and the underlying persistent state has been externally
                         * modified since the transient modification/removal.
                         */
                    case ItemState.STATUS_STALE_MODIFIED:
                        ItemState persistentState = state.getOverlayedState();
                        /**
                         * the state is a transient wrapper for the underlying
                         * persistent state, therefore restore the
                         * persistent state and resurrect this item instance
                         * if necessary
View Full Code Here

             * old and new parents are saved)
             */
            for (Iterator it =
                    new IteratorChain(dirty.iterator(), removed.iterator());
                 it.hasNext();) {
                ItemState transientState = (ItemState) it.next();
                if (transientState.isNode()) {
                    NodeState nodeState = (NodeState) transientState;
                    Set dependentIDs = new HashSet();
                    if (nodeState.hasOverlayedState()) {
                        NodeId oldParentId =
                                nodeState.getOverlayedState().getParentId();
                        NodeId newParentId = nodeState.getParentId();
                        if (oldParentId != null) {
                            if (newParentId == null) {
                                // node has been removed, add old parent
                                // to dependencies
                                dependentIDs.add(oldParentId);
                            } else {
                                if (!oldParentId.equals(newParentId)) {
                                    // node has been moved, add old and new parent
                                    // to dependencies
                                    dependentIDs.add(oldParentId);
                                    dependentIDs.add(newParentId);
                                }
                            }
                        }
                    }
                    // removed child node entries
                    for (Iterator cneIt =
                            nodeState.getRemovedChildNodeEntries().iterator();
                         cneIt.hasNext();) {
                        NodeState.ChildNodeEntry cne =
                                (NodeState.ChildNodeEntry) cneIt.next();
                        dependentIDs.add(cne.getId());
                    }
                    // added child node entries
                    for (Iterator cneIt =
                            nodeState.getAddedChildNodeEntries().iterator();
                         cneIt.hasNext();) {
                        NodeState.ChildNodeEntry cne =
                                (NodeState.ChildNodeEntry) cneIt.next();
                        dependentIDs.add(cne.getId());
                    }

                    // now walk through dependencies and check whether they
                    // are within the scope of this save operation
                    Iterator depIt = dependentIDs.iterator();
                    while (depIt.hasNext()) {
                        NodeId id = (NodeId) depIt.next();
                        if (!affectedIds.contains(id)) {
                            // need to save the parent as well
                            String msg = itemMgr.safeGetJCRPath(id)
                                    + " needs to be saved as well.";
                            log.debug(msg);
                            throw new ConstraintViolationException(msg);
                        }
                    }
                }
            }

            /**
             * validate access and node type constraints
             * (this will also validate child removals)
             */
            validateTransientItems(dirty.iterator(), removed.iterator());

            // start the update operation
            try {
                stateMgr.edit();
            } catch (IllegalStateException e) {
                String msg = "Unable to start edit operation";
                log.debug(msg);
                throw new RepositoryException(msg, e);
            }

            boolean succeeded = false;

            try {

                // process transient items marked as 'removed'
                removeTransientItems(removed.iterator());

                // initialize version histories for new nodes (might generate new transient state)
                if (initVersionHistories(dirty.iterator())) {
                    // re-build the list of transient states because the previous call
                    // generated new transient state
                    dirty = getTransientStates();
                }

                // process 'new' or 'modified' transient states
                persistTransientItems(dirty.iterator());

                // dispose the transient states marked 'new' or 'modified'
                // at this point item state data is pushed down one level,
                // node instances are disconnected from the transient
                // item state and connected to the 'overlayed' item state.
                // transient item states must be removed now. otherwise
                // the session item state provider will return an orphaned
                // item state which is not referenced by any node instance.
                for (Iterator it = dirty.iterator(); it.hasNext();) {
                    ItemState transientState = (ItemState) it.next();
                    // dispose the transient state, it is no longer used
                    stateMgr.disposeTransientItemState(transientState);
                }

                // end update operation
                stateMgr.update();
                // update operation succeeded
                succeeded = true;
            } catch (StaleItemStateException e) {
                throw new InvalidItemStateException(e.getMessage());
            } catch (ItemStateException e) {
                String msg = safeGetJCRPath() + ": unable to update item.";
                log.debug(msg);
                throw new RepositoryException(msg, e);
            } finally {
                if (!succeeded) {
                    // update operation failed, cancel all modifications
                    stateMgr.cancel();

                    // JCR-288: if an exception has been thrown during
                    // update() the transient changes have already been
                    // applied by persistTransientItems() and we need to
                    // restore transient state, i.e. undo the effect of
                    // persistTransientItems()
                    restoreTransientItems(dirty.iterator());
                }
            }

            // now it is safe to dispose the transient states:
            // dispose the transient states marked 'removed'.
            // item states in attic are removed after store, because
            // the observation mechanism needs to build paths of removed
            // items in store().
            for (Iterator it = removed.iterator(); it.hasNext();) {
                ItemState transientState = (ItemState) it.next();
                // dispose the transient state, it is no longer used
                stateMgr.disposeTransientItemStateInAttic(transientState);
            }
        }
    }
View Full Code Here

            }
        }

        // list of transient items that should be discarded
        ArrayList list = new ArrayList();
        ItemState transientState;

        // check status of this item's state
        if (isTransient()) {
            transientState = state;
            switch (transientState.getStatus()) {
                case ItemState.STATUS_STALE_MODIFIED:
                case ItemState.STATUS_STALE_DESTROYED:
                case ItemState.STATUS_EXISTING_MODIFIED:
                    // add this item's state to the list
                    list.add(transientState);
                    break;

                case ItemState.STATUS_NEW:
                    {
                        String msg = safeGetJCRPath() + ": cannot refresh a new item.";
                        log.debug(msg);
                        throw new RepositoryException(msg);
                    }

                default:
                    log.debug("unexpected state status (" + transientState.getStatus() + ")");
                    // ignore
                    break;
            }
        }

        if (isNode()) {
            // build list of 'new', 'modified' or 'stale' descendants
            Iterator iter = stateMgr.getDescendantTransientItemStates((NodeId) id);
            while (iter.hasNext()) {
                transientState = (ItemState) iter.next();
                switch (transientState.getStatus()) {
                    case ItemState.STATUS_STALE_MODIFIED:
                    case ItemState.STATUS_STALE_DESTROYED:
                    case ItemState.STATUS_NEW:
                    case ItemState.STATUS_EXISTING_MODIFIED:
                        // add new or modified state to the list
                        list.add(transientState);
                        break;

                    default:
                        log.debug("unexpected state status (" + transientState.getStatus() + ")");
                        // ignore
                        break;
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.jackrabbit.core.state.ItemState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.