Search in sources :

Example 26 with PropertyState

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

the class BatchedItemOperations method copyPropertyState.

/**
     * Copies the specified property state.
     *
     * @param srcState the property state to copy.
     * @param parentId the id of the parent node.
     * @param propName the name of the property.
     * @param def      the definition of the property.
     * @return a copy of the property state.
     * @throws RepositoryException if an error occurs while copying.
     */
private PropertyState copyPropertyState(PropertyState srcState, NodeId parentId, Name propName, QPropertyDefinition def) throws RepositoryException {
    PropertyState newState = stateMgr.createNew(propName, parentId);
    newState.setType(srcState.getType());
    newState.setMultiValued(srcState.isMultiValued());
    InternalValue[] values = srcState.getValues();
    if (values != null) {
        /**
             * special handling required for properties with special semantics
             * (e.g. those defined by mix:referenceable, mix:versionable,
             * mix:lockable, et.al.)
             *
             * todo FIXME delegate to 'node type instance handler'
             */
        if (propName.equals(NameConstants.JCR_UUID) && def.getDeclaringNodeType().equals(NameConstants.MIX_REFERENCEABLE)) {
            // set correct value of jcr:uuid property
            newState.setValues(new InternalValue[] { InternalValue.create(parentId.toString()) });
        } else {
            InternalValue[] newValues = new InternalValue[values.length];
            for (int i = 0; i < values.length; i++) {
                newValues[i] = values[i].createCopy();
            }
            newState.setValues(newValues);
        }
    }
    return newState;
}
Also used : InternalValue(org.apache.jackrabbit.core.value.InternalValue) PropertyState(org.apache.jackrabbit.core.state.PropertyState)

Example 27 with PropertyState

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

the class BatchedItemOperations method createPropertyState.

/**
     * Creates a new property based on the given definition.
     * <p>
     * Note that access rights are <b><i>not</i></b> enforced!
     * <p>
     * <b>Precondition:</b> the state manager needs to be in edit mode.
     *
     * @param parent
     * @param propName
     * @param type
     * @param def
     * @return
     * @throws ItemExistsException
     * @throws RepositoryException
     */
public PropertyState createPropertyState(NodeState parent, Name propName, int type, QPropertyDefinition def) throws ItemExistsException, RepositoryException {
    // check for name collisions with existing properties
    if (parent.hasPropertyName(propName)) {
        PropertyId errorId = new PropertyId(parent.getNodeId(), propName);
        throw new ItemExistsException(safeGetJCRPath(errorId));
    }
    // create property
    PropertyState prop = stateMgr.createNew(propName, parent.getNodeId());
    if (def.getRequiredType() != PropertyType.UNDEFINED) {
        prop.setType(def.getRequiredType());
    } else if (type != PropertyType.UNDEFINED) {
        prop.setType(type);
    } else {
        prop.setType(PropertyType.STRING);
    }
    prop.setMultiValued(def.isMultiple());
    // compute system generated values if necessary
    new NodeTypeInstanceHandler(session.getUserID()).setDefaultValues(prop, parent, def);
    // now add new property entry to parent
    parent.addPropertyName(propName);
    // store parent
    stateMgr.store(parent);
    return prop;
}
Also used : ItemExistsException(javax.jcr.ItemExistsException) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState)

Example 28 with PropertyState

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

the class NodeIndexer method createDoc.

/**
     * Creates a lucene Document.
     *
     * @return the lucene Document with the index layout.
     * @throws RepositoryException if an error occurs while reading property
     *                             values from the <code>ItemStateProvider</code>.
     */
public Document createDoc() throws RepositoryException {
    doNotUseInExcerpt.clear();
    Document doc = new Document();
    doc.setBoost(getNodeBoost());
    // special fields
    // UUID
    doc.add(new IDField(node.getNodeId()));
    try {
        // parent UUID
        if (node.getParentId() == null) {
            // root node
            Field parent = new Field(FieldNames.PARENT, false, "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO);
            parent.setIndexOptions(FieldInfo.IndexOptions.DOCS_ONLY);
            doc.add(parent);
            addNodeName(doc, "", "");
        } else if (node.getSharedSet().isEmpty()) {
            addParentChildRelation(doc, node.getParentId());
        } else {
            // shareable node
            for (NodeId id : node.getSharedSet()) {
                addParentChildRelation(doc, id);
            }
            // mark shareable nodes
            doc.add(new Field(FieldNames.SHAREABLE_NODE, false, "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
        }
    } catch (NoSuchItemStateException e) {
        throwRepositoryException(e);
    } catch (ItemStateException e) {
        throwRepositoryException(e);
    } catch (NamespaceException e) {
    // will never happen, because this.mappings will dynamically add
    // unknown uri<->prefix mappings
    }
    Set<Name> props = node.getPropertyNames();
    for (Name propName : props) {
        if (isIndexed(propName)) {
            PropertyId id = new PropertyId(node.getNodeId(), propName);
            try {
                PropertyState propState = (PropertyState) stateProvider.getItemState(id);
                // beginning with V2
                if (indexFormatVersion.getVersion() >= IndexFormatVersion.V2.getVersion()) {
                    addPropertyName(doc, propState.getName());
                }
                InternalValue[] values = propState.getValues();
                for (InternalValue value : values) {
                    addValue(doc, value, propState.getName());
                }
                if (values.length > 1) {
                    // real multi-valued
                    addMVPName(doc, propState.getName());
                }
            } catch (NoSuchItemStateException e) {
                throwRepositoryException(e);
            } catch (ItemStateException e) {
                throwRepositoryException(e);
            }
        }
    }
    // now add fields that are not used in excerpt (must go at the end)
    for (Fieldable field : doNotUseInExcerpt) {
        doc.add(field);
    }
    return doc;
}
Also used : Document(org.apache.lucene.document.Document) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Field(org.apache.lucene.document.Field) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) Fieldable(org.apache.lucene.document.Fieldable) NodeId(org.apache.jackrabbit.core.id.NodeId) NamespaceException(javax.jcr.NamespaceException)

Example 29 with PropertyState

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

the class LockManagerImpl method removeLockProperties.

/**
     *
     * @param node
     * @throws RepositoryException
     */
protected void removeLockProperties(NodeImpl node) throws RepositoryException {
    boolean success = false;
    SessionImpl editingSession = (SessionImpl) node.getSession();
    WorkspaceImpl wsp = (WorkspaceImpl) editingSession.getWorkspace();
    UpdatableItemStateManager stateMgr = wsp.getItemStateManager();
    try {
        acquireLockPropertiesLock();
        // add properties to content
        if (stateMgr.inEditMode()) {
            throw new RepositoryException("Unable to remove lock properties.");
        }
        stateMgr.edit();
        try {
            NodeId nodeId = node.getNodeId();
            NodeState nodeState = (NodeState) stateMgr.getItemState(nodeId);
            if (nodeState.hasPropertyName(NameConstants.JCR_LOCKOWNER)) {
                PropertyState propState = (PropertyState) stateMgr.getItemState(new PropertyId(nodeId, NameConstants.JCR_LOCKOWNER));
                nodeState.removePropertyName(NameConstants.JCR_LOCKOWNER);
                stateMgr.destroy(propState);
                stateMgr.store(nodeState);
            }
            if (nodeState.hasPropertyName(NameConstants.JCR_LOCKISDEEP)) {
                PropertyState propState = (PropertyState) stateMgr.getItemState(new PropertyId(nodeId, NameConstants.JCR_LOCKISDEEP));
                nodeState.removePropertyName(NameConstants.JCR_LOCKISDEEP);
                stateMgr.destroy(propState);
                stateMgr.store(nodeState);
            }
            stateMgr.update();
            success = true;
        } catch (ItemStateException e) {
            throw new RepositoryException("Error while removing lock.", e);
        } finally {
            if (!success) {
                // failed to set lock meta-data content, cleanup
                stateMgr.cancel();
            }
        }
    } finally {
        releaseLockPropertiesLock();
    }
}
Also used : UpdatableItemStateManager(org.apache.jackrabbit.core.state.UpdatableItemStateManager) WorkspaceImpl(org.apache.jackrabbit.core.WorkspaceImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) PropertyState(org.apache.jackrabbit.core.state.PropertyState) PropertyId(org.apache.jackrabbit.core.id.PropertyId) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 30 with PropertyState

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

the class LockManagerImpl method writeLockProperties.

/**
     * Add the lock related properties to the target node.
     *
     * @param node
     * @param lockOwner
     * @param isDeep
     */
protected void writeLockProperties(NodeImpl node, String lockOwner, boolean isDeep) throws RepositoryException {
    boolean success = false;
    SessionImpl editingSession = (SessionImpl) node.getSession();
    WorkspaceImpl wsp = (WorkspaceImpl) editingSession.getWorkspace();
    UpdatableItemStateManager stateMgr = wsp.getItemStateManager();
    try {
        acquireLockPropertiesLock();
        if (stateMgr.inEditMode()) {
            throw new RepositoryException("Unable to write lock properties.");
        }
        stateMgr.edit();
        try {
            // add properties to content
            NodeId nodeId = node.getNodeId();
            NodeState nodeState = (NodeState) stateMgr.getItemState(nodeId);
            PropertyState propState;
            if (!nodeState.hasPropertyName(NameConstants.JCR_LOCKOWNER)) {
                propState = stateMgr.createNew(NameConstants.JCR_LOCKOWNER, nodeId);
                propState.setType(PropertyType.STRING);
                propState.setMultiValued(false);
            } else {
                propState = (PropertyState) stateMgr.getItemState(new PropertyId(nodeId, NameConstants.JCR_LOCKOWNER));
            }
            propState.setValues(new InternalValue[] { InternalValue.create(lockOwner) });
            nodeState.addPropertyName(NameConstants.JCR_LOCKOWNER);
            stateMgr.store(nodeState);
            if (!nodeState.hasPropertyName(NameConstants.JCR_LOCKISDEEP)) {
                propState = stateMgr.createNew(NameConstants.JCR_LOCKISDEEP, nodeId);
                propState.setType(PropertyType.BOOLEAN);
                propState.setMultiValued(false);
            } else {
                propState = (PropertyState) stateMgr.getItemState(new PropertyId(nodeId, NameConstants.JCR_LOCKISDEEP));
            }
            propState.setValues(new InternalValue[] { InternalValue.create(isDeep) });
            nodeState.addPropertyName(NameConstants.JCR_LOCKISDEEP);
            stateMgr.store(nodeState);
            stateMgr.update();
            success = true;
        } catch (ItemStateException e) {
            throw new RepositoryException("Error while creating lock.", e);
        } finally {
            if (!success) {
                // failed to set lock meta-data content, cleanup
                stateMgr.cancel();
                try {
                    unlock(node);
                } catch (RepositoryException e) {
                    // cleanup failed
                    log.error("error while cleaning up after failed lock attempt", e);
                }
            }
        }
    } finally {
        releaseLockPropertiesLock();
    }
}
Also used : UpdatableItemStateManager(org.apache.jackrabbit.core.state.UpdatableItemStateManager) WorkspaceImpl(org.apache.jackrabbit.core.WorkspaceImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) PropertyState(org.apache.jackrabbit.core.state.PropertyState) PropertyId(org.apache.jackrabbit.core.id.PropertyId) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Aggregations

PropertyState (org.apache.jackrabbit.core.state.PropertyState)53 PropertyId (org.apache.jackrabbit.core.id.PropertyId)25 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)25 NodeState (org.apache.jackrabbit.core.state.NodeState)25 RepositoryException (javax.jcr.RepositoryException)22 Name (org.apache.jackrabbit.spi.Name)22 InternalValue (org.apache.jackrabbit.core.value.InternalValue)19 NodeId (org.apache.jackrabbit.core.id.NodeId)14 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)14 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)13 InvalidItemStateException (javax.jcr.InvalidItemStateException)9 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)5 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)5 ItemExistsException (javax.jcr.ItemExistsException)4 Value (javax.jcr.Value)4 NodeTypeManagerImpl (org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl)4 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)4