Package org.apache.jackrabbit.core.state

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


         * 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<ItemState> it = changes.modifiedStates(); it.hasNext();) {
            ItemState state = 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/reordered
                // 6) node reordered
                // 7) shareable node added
                // 8) shareable node removed
                // 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.
                // in case 7) parent of added shareable node has new child node
                // entry.
                // in case 8) parent of removed shareable node has removed child
                // node entry.
                NodeState n = (NodeState) state;

                if (n.hasOverlayedState()) {
                    NodeId oldParentId = n.getOverlayedState().getParentId();
                    NodeId newParentId = n.getParentId();
                    if (newParentId != null && !oldParentId.equals(newParentId) &&
                            !n.isShareable()) {

                        // 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<Name> mixins = oldParent.getMixinTypeNames();
                        Path newPath = getPath(n.getNodeId(), hmgr);
                        Path oldPath = getZombiePath(n.getNodeId(), hmgr);
                        events.add(EventState.childNodeRemoved(oldParentId,
                                getParent(oldPath), n.getNodeId(),
                                oldPath.getNameElement(),
                                oldParentNodeType.getQName(),
                                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.getQName(),
                                mixins, session));

                        events.add(EventState.nodeMoved(newParentId,
                                newPath, n.getNodeId(), oldPath,
                                newParentNodeType.getQName(), mixins,
                                session, false));
                    } 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
                            ChildNodeEntry moved = null;
                            for (ChildNodeEntry child : parent.getRemovedChildNodeEntries()) {
                                if (child.getId().equals(n.getNodeId())) {
                                    // found node re-added with different name
                                    moved = child;
                                }
                            }
                            if (moved != null) {
                                NodeTypeImpl nodeType = getNodeType(parent, session);
                                Set<Name> mixins = parent.getMixinTypeNames();
                                Path newPath = getPath(state.getId(), hmgr);
                                Path parentPath = getParent(newPath);
                                Path oldPath;
                                try {
                                    if (moved.getIndex() == 0) {
                                        oldPath = PathFactoryImpl.getInstance().create(parentPath, moved.getName(), false);
                                    } else {
                                        oldPath = PathFactoryImpl.getInstance().create(
                                                parentPath, moved.getName(), moved.getIndex(), false);
                                    }
                                } catch (RepositoryException 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.getQName(), mixins, session));

                                events.add(EventState.childNodeAdded(
                                        parent.getNodeId(), parentPath,
                                        n.getNodeId(), newPath.getNameElement(),
                                        nodeType.getQName(), mixins, session));

                                events.add(EventState.nodeMoved(
                                        parent.getNodeId(), newPath, n.getNodeId(),
                                        oldPath, nodeType.getQName(), mixins,
                                        session, false));
                            }
                        }
                    }
                }

                // check if child nodes of modified node state have been reordered
                List<ChildNodeEntry> reordered = n.getReorderedChildNodeEntries();
                NodeTypeImpl nodeType = getNodeType(n, session);
                Set<Name> mixins = n.getMixinTypeNames();
                if (reordered.size() > 0) {
                    // create a node removed and a node added event for every
                    // reorder
                    for (ChildNodeEntry child : reordered) {
                        Path.Element addedElem = getPathElement(child);
                        Path parentPath = getPath(n.getNodeId(), hmgr);
                        // get removed index
                        NodeState overlayed = (NodeState) n.getOverlayedState();
                        ChildNodeEntry entry = overlayed.getChildNodeEntry(child.getId());
                        if (entry == null) {
                            throw new ItemStateException("Unable to retrieve old child index for item: " + child.getId());
                        }
                        Path.Element removedElem = getPathElement(entry);

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

                        events.add(EventState.childNodeAdded(n.getNodeId(),
                                parentPath, child.getId(), addedElem,
                                nodeType.getQName(), mixins, session));

                        List<ChildNodeEntry> cne = n.getChildNodeEntries();
                        // index of the child node entry before which this
                        // child node entry was reordered
                        int idx = cne.indexOf(child) + 1;
                        Path.Element beforeElem = null;
                        if (idx < cne.size()) {
                            beforeElem = getPathElement(cne.get(idx));
                        }

                        events.add(EventState.nodeReordered(n.getNodeId(),
                                parentPath, child.getId(), addedElem,
                                removedElem, beforeElem, nodeType.getQName(), mixins,
                                session, false));
                    }
                }

                // create events if n is shareable
                createShareableNodeEvents(n, changes, hmgr, stateMgr);
            } else {
                // property changed
                Path path = getPath(state.getId(), hmgr);
                NodeState parent = (NodeState) stateMgr.getItemState(state.getParentId());
                NodeTypeImpl nodeType = getNodeType(parent, session);
                Set<Name> mixins = parent.getMixinTypeNames();
                events.add(EventState.propertyChanged(state.getParentId(),
                        getParent(path), path.getNameElement(),
                        nodeType.getQName(), mixins, session));
            }
        }

        // 2. removed items

        for (Iterator<ItemState> it = changes.deletedStates(); it.hasNext();) {
            ItemState state = it.next();
            if (state.isNode()) {
                // node deleted
                NodeState n = (NodeState) state;
                NodeState parent = (NodeState) stateMgr.getItemState(n.getParentId());
                NodeTypeImpl nodeType = getNodeType(parent, session);
                Set<Name> mixins = parent.getMixinTypeNames();
                Path path = getZombiePath(state.getId(), hmgr);
                events.add(EventState.childNodeRemoved(n.getParentId(),
                        getParent(path),
                        n.getNodeId(),
                        path.getNameElement(),
                        nodeType.getQName(),
                        mixins,
                        session));

                // create events if n is shareable
                createShareableNodeEvents(n, changes, hmgr, stateMgr);
            } 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<Name> mixins = n.getMixinTypeNames();
                    Path path = getZombiePath(state.getId(), hmgr);
                    events.add(EventState.propertyRemoved(state.getParentId(),
                            getParent(path),
                            path.getNameElement(),
                            nodeType.getQName(),
                            mixins,
                            session));
                } catch (NoSuchItemStateException e) {
                    // node removed as well -> do not create an event
                }
            }
        }

        // 3. added items

        for (Iterator<ItemState> it = changes.addedStates(); it.hasNext();) {
            ItemState state = it.next();
            if (state.isNode()) {
                // node created
                NodeState n = (NodeState) state;
                NodeId parentId = n.getParentId();
                // the parent of an added item is always modified or new
                NodeState parent = (NodeState) changes.get(parentId);
                NodeTypeImpl nodeType = getNodeType(parent, session);
                Set<Name> mixins = parent.getMixinTypeNames();
                Path path = getPath(n.getNodeId(), hmgr);
                events.add(EventState.childNodeAdded(parentId,
                        getParent(path),
                        n.getNodeId(),
                        path.getNameElement(),
                        nodeType.getQName(),
                        mixins,
                        session));

                // create events if n is shareable
                createShareableNodeEvents(n, changes, hmgr, stateMgr);
            } else {
                // property created / set
                NodeState n = (NodeState) changes.get(state.getParentId());
                NodeTypeImpl nodeType = getNodeType(n, session);
                Set<Name> mixins = n.getMixinTypeNames();
                Path path = getPath(state.getId(), hmgr);
                events.add(EventState.propertyAdded(state.getParentId(),
                        getParent(path),
                        path.getNameElement(),
                        nodeType.getQName(),
                        mixins,
                        session));
View Full Code Here

        /**
         * walk through list of transient items marked 'removed' and
         * definitively remove each one
         */
        while (iter.hasNext()) {
            ItemState transientState = iter.next();
            ItemState persistentState = transientState.getOverlayedState();
            /**
             * remove persistent state
             *
             * this will indirectly (through stateDestroyed listener method)
             * permanently invalidate all Item instances wrapping it
View Full Code Here

    private void persistTransientItems(Iterator<ItemState> iter)
            throws RepositoryException {

        // walk through list of transient items and persist each one
        while (iter.hasNext()) {
            ItemState state = iter.next();
            ItemImpl item = itemMgr.getItem(state.getId());
            // persist state of transient item
            item.makePersistent();
        }
    }
View Full Code Here

    }

    private void restoreTransientItems(Iterator<ItemState> iter) {
        // walk through list of transient states and re-apply transient changes
        while (iter.hasNext()) {
            ItemState itemState = iter.next();
            ItemId id = itemState.getId();
            ItemImpl item;

            try {
                if (stateMgr.isItemStateInAttic(id)) {
                    // If an item has been removed and then again created, the
                    // item is lost after persistTransientItems() and the
                    // TransientItemStateManager will bark because of a deleted
                    // state in its attic. We therefore have to forge a new item
                    // instance ourself.
                    item = itemMgr.createItemInstance(itemState);
                    itemState.setStatus(ItemState.STATUS_NEW);
                } else {
                    try {
                        item = itemMgr.getItem(id);
                    } catch (ItemNotFoundException infe) {
                        // itemState probably represents a 'new' item and the
                        // ItemImpl instance wrapping it has already been gc'ed;
                        // we have to re-create the ItemImpl instance
                        item = itemMgr.createItemInstance(itemState);
                        itemState.setStatus(ItemState.STATUS_NEW);
                    }
                }
                if (!item.isTransient()) {
                    // re-apply transient changes (i.e. undo effect of item.makePersistent())
                    if (item.isNode()) {
View Full Code Here

     * has been removed, throw.</li>
     * </ul>
     */
    private void processShareableNodes(Iterator<ItemState> iter) throws RepositoryException {
        while (iter.hasNext()) {
            ItemState is = iter.next();
            if (is.isNode()) {
                NodeState ns = (NodeState) is;
                boolean wasShareable = false;
                if (ns.hasOverlayedState()) {
                    NodeState old = (NodeState) ns.getOverlayedState();
                    EffectiveNodeType ntOld = getEffectiveNodeType(old);
View Full Code Here

     */
    private boolean initVersionHistories(Iterator<ItemState> iter) throws RepositoryException {
        // walk through list of transient items and search for new versionable nodes
        boolean createdTransientState = false;
        while (iter.hasNext()) {
            ItemState itemState = iter.next();
            if (itemState.isNode()) {
                NodeState nodeState = (NodeState) itemState;
                EffectiveNodeType nt = getEffectiveNodeType(nodeState);
                if (nt.includesNodeType(NameConstants.MIX_VERSIONABLE)) {
                    if (!nodeState.hasPropertyName(NameConstants.JCR_VERSIONHISTORY)) {
                        NodeImpl node = (NodeImpl) itemMgr.getItem(itemState.getId());
                        VersionManager vMgr = session.getVersionManager();
                        /**
                         * check if there's already a version history for that
                         * node; this would e.g. be the case if a versionable
                         * node had been exported, removed and re-imported with
                         * either IMPORT_UUID_COLLISION_REMOVE_EXISTING or
                         * IMPORT_UUID_COLLISION_REPLACE_EXISTING;
                         * otherwise create a new version history
                         */
                        VersionHistoryInfo history =
                            vMgr.getVersionHistory(session, nodeState, null);
                        InternalValue historyId = InternalValue.create(
                                history.getVersionHistoryId());
                        InternalValue versionId = InternalValue.create(
                                history.getRootVersionId());
                        node.internalSetProperty(
                                NameConstants.JCR_VERSIONHISTORY, historyId);
                        node.internalSetProperty(
                                NameConstants.JCR_BASEVERSION, versionId);
                        node.internalSetProperty(
                                NameConstants.JCR_ISCHECKEDOUT,
                                InternalValue.create(true));
                        node.internalSetProperty(
                                NameConstants.JCR_PREDECESSORS,
                                new InternalValue[] { versionId });
                        createdTransientState = true;
                    }
                } else if (nt.includesNodeType(NameConstants.MIX_SIMPLE_VERSIONABLE)) {
                    // we need to check the version manager for an existing
                    // version history, since simple versioning does not
                    // expose it's reference in a property
                    VersionManager vMgr = session.getVersionManager();
                    vMgr.getVersionHistory(session, nodeState, null);

                    // create isCheckedOutProperty if not already exists
                    NodeImpl node = (NodeImpl) itemMgr.getItem(itemState.getId());
                    if (!nodeState.hasPropertyName(NameConstants.JCR_ISCHECKEDOUT)) {
                        node.internalSetProperty(
                                NameConstants.JCR_ISCHECKEDOUT,
                                InternalValue.create(true));
                        createdTransientState = true;
View Full Code Here

    /**
     * {@inheritDoc}
     */
    public boolean isNew() {
        final ItemState state = getItemState();
        return state.isTransient() && state.getOverlayedState() == null;
    }
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.