Package org.apache.jackrabbit.core.state

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


        if (status == STATUS_INVALIDATED || status == STATUS_DESTROYED) {
            // this instance is already 'invalid', get outta here
            return;
        }

        ItemState transientState = getOrCreateTransientItemState();
        if (transientState.getStatus() == ItemState.STATUS_NEW) {
            // this is a 'new' item, simply dispose the transient state
            // (it is no longer used); this will indirectly (through
            // stateDiscarded listener method) invalidate this instance permanently
            stateMgr.disposeTransientItemState(transientState);
        } else {
            // this is an 'existing' item (i.e. it is backed by persistent
            // state), mark it as 'removed'
            transientState.setStatus(ItemState.STATUS_EXISTING_REMOVED);
            // transfer the transient state to the attic
            stateMgr.moveTransientItemStateToAttic(transientState);

            // set state of this instance to 'invalid'
            status = STATUS_INVALIDATED;
View Full Code Here


     */
    private Collection getTransientStates()
            throws InvalidItemStateException, RepositoryException {
        // list of transient states that should be persisted
        ArrayList dirty = new ArrayList();
        ItemState transientState;

        // fail-fast test: check status of this item's state
        if (isTransient()) {
            switch (state.getStatus()) {
                case ItemState.STATUS_EXISTING_MODIFIED:
                    // add this item's state to the list
                    dirty.add(state);
                    break;

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

                case ItemState.STATUS_STALE_MODIFIED:
                    {
                        String msg = safeGetJCRPath()
                                + ": the item cannot be saved because it has been modified externally.";
                        log.debug(msg);
                        throw new InvalidItemStateException(msg);
                    }

                case ItemState.STATUS_STALE_DESTROYED:
                    {
                        String msg = safeGetJCRPath()
                                + ": the item cannot be saved because it has been deleted externally.";
                        log.debug(msg);
                        throw new InvalidItemStateException(msg);
                    }

                case ItemState.STATUS_UNDEFINED:
                    {
                        String msg = safeGetJCRPath()
                                + ": the item cannot be saved; it seems to have been removed externally.";
                        log.debug(msg);
                        throw new InvalidItemStateException(msg);
                    }

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

        if (isNode()) {
            // build list of 'new' or 'modified' descendants
            Iterator iter = stateMgr.getDescendantTransientItemStates((NodeId) id);
            while (iter.hasNext()) {
                transientState = (ItemState) iter.next();
                // fail-fast test: check status of transient state
                switch (transientState.getStatus()) {
                    case ItemState.STATUS_NEW:
                    case ItemState.STATUS_EXISTING_MODIFIED:
                        // add modified state to the list
                        dirty.add(transientState);
                        break;

                    case ItemState.STATUS_STALE_MODIFIED:
                        {
                            String msg = transientState.getId()
                                    + ": the item cannot be saved because it has been modified externally.";
                            log.debug(msg);
                            throw new InvalidItemStateException(msg);
                        }

                    case ItemState.STATUS_STALE_DESTROYED:
                        {
                            String msg = transientState.getId()
                                    + ": the item cannot be saved because it has been deleted externally.";
                            log.debug(msg);
                            throw new InvalidItemStateException(msg);
                        }

                    case ItemState.STATUS_UNDEFINED:
                        {
                            String msg = safeGetJCRPath()
                                    + ": the item cannot be saved; it seems to have been removed externally.";
                            log.debug(msg);
                            throw new InvalidItemStateException(msg);
                        }

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

     * @throws RepositoryException
     */
    private Collection getRemovedStates()
            throws InvalidItemStateException, RepositoryException {
        ArrayList removed = new ArrayList();
        ItemState transientState;

        if (isNode()) {
            Iterator iter = stateMgr.getDescendantTransientItemStatesInAttic((NodeId) id);
            while (iter.hasNext()) {
                transientState = (ItemState) iter.next();
                // check if stale
                if (transientState.getStatus() == ItemState.STATUS_STALE_MODIFIED) {
                    String msg = transientState.getId()
                            + ": the item cannot be removed because it has been modified externally.";
                    log.debug(msg);
                    throw new InvalidItemStateException(msg);
                }
                if (transientState.getStatus() == ItemState.STATUS_STALE_DESTROYED) {
                    String msg = transientState.getId()
                            + ": the item cannot be removed because it has already been deleted externally.";
                    log.debug(msg);
                    throw new InvalidItemStateException(msg);
                }
                removed.add(transientState);
View Full Code Here

         */

        AccessManager accessMgr = session.getAccessManager();
        // walk through list of dirty transient items and validate each
        while (dirtyIter.hasNext()) {
            ItemState itemState = (ItemState) dirtyIter.next();

            if (itemState.getStatus() != ItemState.STATUS_NEW) {
                // transient item is not 'new', therefore it has to be 'modified'

                // check WRITE permission
                ItemId id = itemState.getId();
                if (!accessMgr.isGranted(id, AccessManager.WRITE)) {
                    String msg = itemMgr.safeGetJCRPath(id)
                            + ": not allowed to modify item";
                    log.debug(msg);
                    throw new AccessDeniedException(msg);
                }
            }

            if (itemState.isNode()) {
                // the transient item is a node
                NodeState nodeState = (NodeState) itemState;
                ItemId id = nodeState.getNodeId();
                NodeImpl node = (NodeImpl) itemMgr.getItem(id);
                NodeDefinition def = node.getDefinition();
                // primary type
                NodeTypeImpl pnt = (NodeTypeImpl) node.getPrimaryNodeType();
                // effective node type (primary type incl. mixins)
                EffectiveNodeType ent = node.getEffectiveNodeType();
                /**
                 * if the transient node was added (i.e. if it is 'new'),
                 * check its node's node type against the required node type
                 * in its definition
                 */
                if (nodeState.getStatus() == ItemState.STATUS_NEW) {
                    NodeType[] nta = def.getRequiredPrimaryTypes();
                    for (int i = 0; i < nta.length; i++) {
                        NodeTypeImpl ntReq = (NodeTypeImpl) nta[i];
                        if (!(pnt.getQName().equals(ntReq.getQName())
                                || pnt.isDerivedFrom(ntReq.getQName()))) {
                            /**
                             * the transient node's primary node type does not
                             * satisfy the 'required primary types' constraint
                             */
                            String msg = node.safeGetJCRPath()
                                    + " must be of node type " + ntReq.getName();
                            log.debug(msg);
                            throw new ConstraintViolationException(msg);
                        }
                    }
                }

                // mandatory child properties
                PropDef[] pda = ent.getMandatoryPropDefs();
                for (int i = 0; i < pda.length; i++) {
                    PropDef pd = pda[i];
                    if (pd.getDeclaringNodeType().equals(QName.MIX_VERSIONABLE)) {
                        /**
                         * todo FIXME workaround for mix:versionable:
                         * the mandatory properties are initialized at a
                         * later stage and might not exist yet
                         */
                        continue;
                    }
                    if (!nodeState.hasPropertyName(pd.getName())) {
                        String msg = node.safeGetJCRPath()
                                + ": mandatory property " + pd.getName()
                                + " does not exist";
                        log.debug(msg);
                        throw new ConstraintViolationException(msg);
                    }
                }
                // mandatory child nodes
                NodeDef[] cnda = ent.getMandatoryNodeDefs();
                for (int i = 0; i < cnda.length; i++) {
                    NodeDef cnd = cnda[i];
                    if (!nodeState.hasChildNodeEntry(cnd.getName())) {
                        String msg = node.safeGetJCRPath()
                                + ": mandatory child node " + cnd.getName()
                                + " does not exist";
                        log.debug(msg);
                        throw new ConstraintViolationException(msg);
                    }
                }
            } else {
                // the transient item is a property
                PropertyState propState = (PropertyState) itemState;
                ItemId propId = propState.getPropertyId();
                PropertyImpl prop = (PropertyImpl) itemMgr.getItem(propId);
                PropertyDefinitionImpl def =
                        (PropertyDefinitionImpl) prop.getDefinition();

                /**
                 * check value constraints
                 * (no need to check value constraints of protected properties
                 * as those are set by the implementation only, i.e. they
                 * cannot be set by the user through the api)
                 */
                if (!def.isProtected()) {
                    String[] constraints = def.getValueConstraints();
                    if (constraints != null) {
                        InternalValue[] values = propState.getValues();
                        try {
                            EffectiveNodeType.checkSetPropertyValueConstraints(
                                    def.unwrap(), values);
                        } catch (RepositoryException e) {
                            // repack exception for providing verboser error message
                            String msg = prop.safeGetJCRPath() + ": " + e.getMessage();
                            log.debug(msg);
                            throw new ConstraintViolationException(msg);
                        }

                        /**
                         * need to manually check REFERENCE value constraints
                         * as this requires a session (target node needs to
                         * be checked)
                         */
                        if (constraints.length > 0
                                && def.getRequiredType() == PropertyType.REFERENCE) {
                            for (int i = 0; i < values.length; i++) {
                                boolean satisfied = false;
                                try {
                                    UUID targetUUID = (UUID) values[i].internalValue();
                                    Node targetNode = session.getNodeByUUID(targetUUID);
                                    /**
                                     * constraints are OR-ed, i.e. at least one
                                     * has to be satisfied
                                     */
                                    for (int j = 0; j < constraints.length; j++) {
                                        /**
                                         * a REFERENCE value constraint specifies
                                         * the name of the required node type of
                                         * the target node
                                         */
                                        String ntName = constraints[j];
                                        if (targetNode.isNodeType(ntName)) {
                                            satisfied = true;
                                            break;
                                        }
                                    }
                                } catch (RepositoryException re) {
                                    String msg = prop.safeGetJCRPath()
                                            + ": failed to check REFERENCE value constraint";
                                    log.debug(msg);
                                    throw new ConstraintViolationException(msg, re);
                                }
                                if (!satisfied) {
                                    String msg = prop.safeGetJCRPath()
                                            + ": does not satisfy the value constraint "
                                            + constraints[0];   // just report the 1st
                                    log.debug(msg);
                                    throw new ConstraintViolationException(msg);
                                }
                            }
                        }
                    }
                }

                /**
                 * no need to check the protected flag as this is checked
                 * in PropertyImpl.setValue(Value)
                 */
            }
        }

        // walk through list of removed transient items and check REMOVE permission
        while (removedIter.hasNext()) {
            ItemState itemState = (ItemState) removedIter.next();
            ItemId id = itemState.getId();
            // check REMOVE permission
            if (!accessMgr.isGranted(id, AccessManager.REMOVE)) {
                String msg = itemMgr.safeGetJCRPath(id)
                        + ": not allowed to remove item";
                log.debug(msg);
View Full Code Here

        /**
         * walk through list of transient items marked 'removed' and
         * definitively remove each one
         */
        while (iter.hasNext()) {
            ItemState transientState = (ItemState) 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 iter)
            throws RepositoryException {

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

    private void restoreTransientItems(Iterator iter) {

        // walk through list of transient states and reapply transient changes
        while (iter.hasNext()) {
            ItemState 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.
                    if (itemState.isNode()) {
                        item = itemMgr.createNodeInstance((NodeState) itemState);
                    } else {
                        item = itemMgr.createPropertyInstance((PropertyState) 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
                        if (itemState.isNode()) {
                            item = itemMgr.createNodeInstance((NodeState) itemState);
                        } else {
                            item = itemMgr.createPropertyInstance((PropertyState) itemState);
                        }
                        itemState.setStatus(ItemState.STATUS_NEW);
                    }
                }
                if (!item.isTransient()) {
                    // reapply transient changes (i.e. undo effect of item.makePersistent())
                    if (item.isNode()) {
View Full Code Here

        // shortcut
        if (id.equals(rootNodeId)) {
            return 0;
        }
        try {
            ItemState state = getItemState(id);
            NodeId parentId = getParentId(state);
            int depth = 0;
            while (parentId != null) {
                depth++;
                state = getItemState(parentId);
View Full Code Here

        if (ancestorId.equals(descendantId)) {
            return 0;
        }
        int depth = 1;
        try {
            ItemState state = getItemState(descendantId);
            NodeId parentId = getParentId(state);
            while (parentId != null) {
                if (parentId.equals(ancestorId)) {
                    return depth;
                }
View Full Code Here

        if (nodeId.equals(itemId)) {
            // can't be ancestor of self
            return false;
        }
        try {
            ItemState state = getItemState(itemId);
            NodeId parentId = getParentId(state);
            while (parentId != null) {
                if (parentId.equals(nodeId)) {
                    return true;
                }
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.