Search in sources :

Example 6 with PropertyState

use of org.apache.jackrabbit.jcr2spi.state.PropertyState in project jackrabbit by apache.

the class NodeEntryImpl method addNewPropertyEntry.

/**
     * @see NodeEntry#addNewPropertyEntry(Name, QPropertyDefinition, QValue[], int)
     */
public PropertyEntry addNewPropertyEntry(Name propName, QPropertyDefinition definition, QValue[] values, int propertyType) throws ItemExistsException, RepositoryException {
    // check for an existing property
    PropertyEntry existing = properties.get(propName);
    if (existing != null) {
        try {
            PropertyState existingState = existing.getPropertyState();
            int status = existingState.getStatus();
            if (Status.isTerminal(status)) {
                // an old property-entry that is not valid any more
                properties.remove(existing);
            } else if (status == Status.EXISTING_REMOVED) {
                // transiently removed -> move it to the attic
                propertiesInAttic.put(propName, existing);
            } else {
                // existing is still existing -> cannot add same-named property
                throw new ItemExistsException(propName.toString());
            }
        } catch (ItemNotFoundException e) {
            // entry does not exist on the persistent layer
            // -> therefore remove from properties map
            properties.remove(existing);
        } catch (RepositoryException e) {
            // some other error -> remove from properties map
            properties.remove(existing);
        }
    }
    PropertyEntry entry = factory.createPropertyEntry(this, propName);
    PropertyState state = getItemStateFactory().createNewPropertyState(entry, definition, values, propertyType);
    entry.setItemState(state);
    // add the property entry if creating the new state was successful
    properties.add(entry);
    return entry;
}
Also used : ItemExistsException(javax.jcr.ItemExistsException) RepositoryException(javax.jcr.RepositoryException) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 7 with PropertyState

use of org.apache.jackrabbit.jcr2spi.state.PropertyState in project jackrabbit by apache.

the class SessionImpl method hasPermission.

/**
     * @see Session#hasPermission(String, String)
     */
public boolean hasPermission(String absPath, String actions) throws RepositoryException {
    checkIsAlive();
    // build the array of actions to be checked
    String[] actionsArr = actions.split(",");
    Path targetPath = getQPath(absPath);
    boolean isGranted;
    // The given abs-path may point to a non-existing item
    if (itemManager.nodeExists(targetPath)) {
        NodeState nState = getHierarchyManager().getNodeState(targetPath);
        isGranted = getAccessManager().isGranted(nState, actionsArr);
    } else if (itemManager.propertyExists(targetPath)) {
        PropertyState pState = getHierarchyManager().getPropertyState(targetPath);
        isGranted = getAccessManager().isGranted(pState, actionsArr);
    } else {
        NodeState parentState = null;
        Path parentPath = targetPath;
        while (parentState == null) {
            parentPath = parentPath.getAncestor(1);
            if (itemManager.nodeExists(parentPath)) {
                parentState = getHierarchyManager().getNodeState(parentPath);
            }
        }
        // parentState is the nearest existing nodeState or the root state.
        Path relPath = parentPath.computeRelativePath(targetPath);
        isGranted = getAccessManager().isGranted(parentState, relPath, actionsArr);
    }
    return isGranted;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Example 8 with PropertyState

use of org.apache.jackrabbit.jcr2spi.state.PropertyState in project jackrabbit by apache.

the class SessionImporter method importNode.

/**
     *
     * @param nodeInfo
     * @param parent
     * @return
     * @throws ConstraintViolationException
     * @throws ItemNotFoundException
     * @throws RepositoryException
     */
private NodeState importNode(NodeInfo nodeInfo, NodeState parent) throws ConstraintViolationException, ItemNotFoundException, RepositoryException {
    Name[] parentNtNames = parent.getAllNodeTypeNames();
    if (parent.hasPropertyName(nodeInfo.getName())) {
        /**
             * a property with the same name already exists; if this property
             * has been imported as well (e.g. through document view import
             * where an element can have the same name as one of the attributes
             * of its parent element) we have to rename the conflicting property;
             *
             * see http://issues.apache.org/jira/browse/JCR-61
             */
        PropertyState conflicting = parent.getPropertyState(nodeInfo.getName());
        if (conflicting.getStatus() == Status.NEW) {
            // assume this property has been imported as well;
            // rename conflicting property
            // TODO: use better reversible escaping scheme to create unique name
            Name newName = session.getNameFactory().create(nodeInfo.getName().getNamespaceURI(), nodeInfo.getName().getLocalName() + "_");
            if (parent.hasPropertyName(newName)) {
                newName = session.getNameFactory().create(newName.getNamespaceURI(), newName.getLocalName() + "_");
            }
            // since name changes, need to find new applicable definition
            QPropertyDefinition propDef;
            if (conflicting.getValues().length == 1) {
                // could be single- or multi-valued (n == 1)
                try {
                    // try single-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), false);
                } catch (ConstraintViolationException cve) {
                    // try multi-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
                }
            } else {
                // can only be multi-valued (n == 0 || n > 1)
                propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
            }
            Operation ap = AddProperty.create(parent, newName, conflicting.getType(), propDef, conflicting.getValues());
            stateMgr.execute(ap);
            Operation rm = Remove.create(conflicting);
            stateMgr.execute(rm);
        }
    }
    // do create new nodeState
    QNodeDefinition def = session.getItemDefinitionProvider().getQNodeDefinition(parentNtNames, nodeInfo.getName(), nodeInfo.getNodeTypeName());
    if (def.isProtected()) {
        log.debug("Skipping protected nodeState (" + nodeInfo.getName() + ")");
        return null;
    } else {
        Name ntName = nodeInfo.getNodeTypeName();
        if (ntName == null) {
            // use default node type
            ntName = def.getDefaultPrimaryType();
        }
        Operation an = AddNode.create(parent, nodeInfo.getName(), ntName, nodeInfo.getUUID());
        stateMgr.execute(an);
        // retrieve id of state that has been created during execution of AddNode
        NodeState childState = (NodeState) ((AddNode) an).getAddedStates().get(0);
        // and set mixin types
        Name[] mixinNames = nodeInfo.getMixinNames();
        if (mixinNames != null && mixinNames.length > 0) {
            Operation sm = SetMixin.create(childState, nodeInfo.getMixinNames());
            stateMgr.execute(sm);
        }
        return childState;
    }
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Example 9 with PropertyState

use of org.apache.jackrabbit.jcr2spi.state.PropertyState in project jackrabbit by apache.

the class SessionImporter method importProperty.

/**
     *
     * @param pi
     * @param parentState
     * @param resolver
     * @throws RepositoryException
     * @throws ConstraintViolationException
     */
private void importProperty(PropInfo pi, NodeState parentState, NamePathResolver resolver) throws RepositoryException, ConstraintViolationException {
    Name propName = pi.getName();
    TextValue[] tva = pi.getValues();
    int infoType = pi.getType();
    PropertyState propState = null;
    QPropertyDefinition def = null;
    NodeEntry parentEntry = (NodeEntry) parentState.getHierarchyEntry();
    PropertyEntry pEntry = parentEntry.getPropertyEntry(propName);
    if (pEntry != null) {
        // a property with that name already exists...
        try {
            PropertyState existing = pEntry.getPropertyState();
            def = existing.getDefinition();
            if (def.isProtected()) {
                // skip protected property
                log.debug("skipping protected property " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
                return;
            }
            if (def.isAutoCreated() && (existing.getType() == infoType || infoType == PropertyType.UNDEFINED) && def.isMultiple() == existing.isMultiValued()) {
                // this property has already been auto-created, no need to create it
                propState = existing;
            } else {
                throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
            }
        } catch (ItemNotFoundException e) {
        // property doesn't exist any more
        // -> ignore
        }
    }
    Name[] parentNtNames = parentState.getAllNodeTypeNames();
    if (def == null) {
        // there's no property with that name, find applicable definition
        if (tva.length == 1) {
            // could be single- or multi-valued (n == 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType);
        } else {
            // can only be multi-valued (n == 0 || n > 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType, true);
        }
        if (def.isProtected()) {
            // skip protected property
            log.debug("skipping protected property " + propName);
            return;
        }
    }
    // retrieve the target property type needed for creation of QValue(s)
    // including an eventual conversion. the targetType is then needed for
    // setting/updating the type of the property-state.
    int targetType = def.getRequiredType();
    if (targetType == PropertyType.UNDEFINED) {
        if (infoType == PropertyType.UNDEFINED) {
            targetType = PropertyType.STRING;
        } else {
            targetType = infoType;
        }
    }
    QValue[] values = getPropertyValues(pi, targetType, def.isMultiple(), resolver);
    if (propState == null) {
        // create new property
        Operation ap = AddProperty.create(parentState, propName, targetType, def, values);
        stateMgr.execute(ap);
        propState = parentEntry.getPropertyEntry(propName).getPropertyState();
    } else {
        // modify value of existing property
        Operation sp = SetPropertyValue.create(propState, values, targetType);
        stateMgr.execute(sp);
    }
    // store reference for later resolution
    if (propState.getType() == PropertyType.REFERENCE) {
        refTracker.processedReference(propState);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) PropertyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 10 with PropertyState

use of org.apache.jackrabbit.jcr2spi.state.PropertyState in project jackrabbit by apache.

the class VersionManagerImpl method isCheckedOut.

/**
     * Search nearest ancestor that is versionable. If no versionable ancestor
     * can be found, <code>true</code> is returned.
     *
     * @param nodeState
     * @return
     * @throws RepositoryException
     */
public boolean isCheckedOut(NodeState nodeState) throws RepositoryException {
    // shortcut: if state is new, its ancestor must be checkout
    if (nodeState.getStatus() == Status.NEW) {
        return true;
    }
    NodeEntry nodeEntry = nodeState.getNodeEntry();
    try {
        // save or upon executing the workspace operation.
        while (!nodeEntry.hasPropertyEntry(NameConstants.JCR_ISCHECKEDOUT)) {
            NodeEntry parent = nodeEntry.getParent();
            if (parent == null) {
                // reached root state without finding a jcr:isCheckedOut property
                return true;
            }
            nodeEntry = parent;
        }
        PropertyState propState = nodeEntry.getPropertyEntry(NameConstants.JCR_ISCHECKEDOUT).getPropertyState();
        Boolean b = Boolean.valueOf(propState.getValue().getString());
        return b.booleanValue();
    } catch (ItemNotFoundException e) {
    // error while accessing jcr:isCheckedOut property state.
    // -> assume that checkedOut status is ok. see above for general
    // notes about the capabilities of the jcr2spi implementation.
    }
    return true;
}
Also used : NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

PropertyState (org.apache.jackrabbit.jcr2spi.state.PropertyState)12 ItemNotFoundException (javax.jcr.ItemNotFoundException)5 NodeState (org.apache.jackrabbit.jcr2spi.state.NodeState)5 Operation (org.apache.jackrabbit.jcr2spi.operation.Operation)3 Name (org.apache.jackrabbit.spi.Name)3 NodeId (org.apache.jackrabbit.spi.NodeId)3 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)3 ItemExistsException (javax.jcr.ItemExistsException)2 RepositoryException (javax.jcr.RepositoryException)2 NodeEntry (org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry)2 QValue (org.apache.jackrabbit.spi.QValue)2 Item (javax.jcr.Item)1 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)1 PropertyEntry (org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry)1 AddNode (org.apache.jackrabbit.jcr2spi.operation.AddNode)1 Checkpoint (org.apache.jackrabbit.jcr2spi.operation.Checkpoint)1 ItemState (org.apache.jackrabbit.jcr2spi.state.ItemState)1 Path (org.apache.jackrabbit.spi.Path)1 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)1