Search in sources :

Example 6 with PropertyDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl in project jackrabbit by apache.

the class NodeImpl method createChildNode.

protected synchronized NodeImpl createChildNode(Name name, NodeTypeImpl nodeType, NodeId id) throws RepositoryException {
    // create a new node state
    NodeState nodeState = stateMgr.createTransientNodeState(id, nodeType.getQName(), getNodeId(), ItemState.STATUS_NEW);
    // create Node instance wrapping new node state
    NodeImpl node;
    try {
        // NOTE: since the node is not yet connected to its parent, avoid
        // calling ItemManager#getItem(ItemId) which may include a permission
        // check (with subsequent usage of the hierarachy-mgr -> error).
        // just let the mgr create the new node that is known to exist and
        // which has not been accessed before.
        node = (NodeImpl) itemMgr.createItemInstance(nodeState);
    } catch (RepositoryException re) {
        // something went wrong
        stateMgr.disposeTransientItemState(nodeState);
        // re-throw
        throw re;
    }
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, nodeState.getNodeId());
    // add 'auto-create' properties defined in node type
    for (PropertyDefinition aPda : nodeType.getAutoCreatedPropertyDefinitions()) {
        PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
        node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
    }
    // recursively add 'auto-create' child nodes defined in node type
    for (NodeDefinition aNda : nodeType.getAutoCreatedNodeDefinitions()) {
        NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
        node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
    }
    return node;
}
Also used : NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 7 with PropertyDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl in project jackrabbit by apache.

the class NodeImpl method getOrCreateProperty.

/**
 * @param name
 * @param type
 * @param multiValued
 * @param exactTypeMatch
 * @param status
 * @return
 * @throws ConstraintViolationException if no applicable property definition
 *                                      could be found
 * @throws RepositoryException          if another error occurs
 */
protected synchronized PropertyImpl getOrCreateProperty(Name name, int type, boolean multiValued, boolean exactTypeMatch, BitSet status) throws ConstraintViolationException, RepositoryException {
    status.clear();
    if (isNew() && !hasProperty(name)) {
        // this is a new node and the property does not exist yet
        // -> no need to check item manager
        PropertyDefinitionImpl def = getApplicablePropertyDefinition(name, type, multiValued, exactTypeMatch);
        PropertyImpl prop = createChildProperty(name, type, def);
        status.set(CREATED);
        return prop;
    }
    /*
         * Please note, that this implementation does not win a price for beauty
         * or speed. It's never a good idea to use exceptions for semantical
         * control flow.
         * However, compared to the previous version, this one is thread save
         * and makes the test/get block atomic in respect to transactional
         * commits. the test/set can still fail.
         *
         * Old Version:

            NodeState thisState = (NodeState) state;
            if (thisState.hasPropertyName(name)) {
                /**
                 * the following call will throw ItemNotFoundException if the
                 * current session doesn't have read access
                 /
                return getProperty(name);
            }
            [...create block...]

        */
    PropertyId propId = new PropertyId(getNodeId(), name);
    try {
        return (PropertyImpl) itemMgr.getItem(propId);
    } catch (AccessDeniedException ade) {
        throw new ItemNotFoundException(name.toString());
    } catch (ItemNotFoundException e) {
        // does not exist yet or has been removed transiently:
        // find definition for the specified property and (re-)create property
        PropertyDefinitionImpl def = getApplicablePropertyDefinition(name, type, multiValued, exactTypeMatch);
        PropertyImpl prop;
        if (stateMgr.hasTransientItemStateInAttic(propId)) {
            // remove from attic
            try {
                stateMgr.disposeTransientItemStateInAttic(stateMgr.getAttic().getItemState(propId));
            } catch (ItemStateException ise) {
                // shouldn't happen because we checked if it is in the attic
                throw new RepositoryException(ise);
            }
            prop = (PropertyImpl) itemMgr.getItem(propId);
            PropertyState state = (PropertyState) prop.getOrCreateTransientItemState();
            state.setMultiValued(multiValued);
            state.setType(type);
            getNodeState().addPropertyName(name);
        } else {
            prop = createChildProperty(name, type, def);
        }
        status.set(CREATED);
        return prop;
    }
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) PropertyId(org.apache.jackrabbit.core.id.PropertyId) ItemNotFoundException(javax.jcr.ItemNotFoundException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) PropertyState(org.apache.jackrabbit.core.state.PropertyState)

Example 8 with PropertyDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl in project jackrabbit by apache.

the class AddMixinOperation method perform.

public Object perform(SessionContext context) throws RepositoryException {
    int permissions = Permission.NODE_TYPE_MNGMT;
    // is required in addition.
    if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
        permissions |= Permission.VERSION_MNGMT;
    }
    context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
    NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
    NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
    if (!mixin.isMixin()) {
        throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
    }
    Name primaryTypeName = node.getNodeState().getNodeTypeName();
    NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
    if (primaryType.isDerivedFrom(mixinName)) {
        // new mixin is already included in primary type
        return this;
    }
    // build effective node type of mixin's & primary type in order
    // to detect conflicts
    NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
    EffectiveNodeType entExisting;
    try {
        // existing mixin's
        Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
        // build effective node type representing primary type including
        // existing mixin's
        entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
        if (entExisting.includesNodeType(mixinName)) {
            // new mixin is already included in existing mixin type(s)
            return this;
        }
        // add new mixin
        mixins.add(mixinName);
        // try to build new effective node type (will throw in case
        // of conflicts)
        ntReg.getEffectiveNodeType(primaryTypeName, mixins);
    } catch (NodeTypeConflictException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
    // try to revert the changes in case an exception occurs
    try {
        // modify the state of this node
        NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
        // add mixin name
        Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
        mixins.add(mixinName);
        thisState.setMixinTypeNames(mixins);
        // set jcr:mixinTypes property
        node.setMixinTypesProperty(mixins);
        // add 'auto-create' properties defined in mixin type
        for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
            PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
            // make sure that the property is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
            }
        }
        // recursively add 'auto-create' child nodes defined in mixin type
        for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
            NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
            // make sure that the child node is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
            }
        }
    } catch (RepositoryException re) {
        // try to undo the modifications by removing the mixin
        try {
            node.removeMixin(mixinName);
        } catch (RepositoryException re1) {
        // silently ignore & fall through
        }
        throw re;
    }
    return this;
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) HashSet(java.util.HashSet)

Example 9 with PropertyDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl in project jackrabbit by apache.

the class NodeTypeDefinitionImpl method getDeclaredPropertyDefinitions.

/**
 * {@inheritDoc}
 */
public PropertyDefinition[] getDeclaredPropertyDefinitions() {
    QPropertyDefinition[] pda = ntd.getPropertyDefs();
    PropertyDefinition[] propDefs = new PropertyDefinition[pda.length];
    for (int i = 0; i < pda.length; i++) {
        propDefs[i] = new PropertyDefinitionImpl(pda[i], null, resolver, valueFactory);
    }
    return propDefs;
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 10 with PropertyDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl in project jackrabbit by apache.

the class NodeTypeManagerImpl method getPropertyDefinition.

/**
 * @param def prop def
 * @return the property definition
 */
@Override
public PropertyDefinitionImpl getPropertyDefinition(QPropertyDefinition def) {
    synchronized (pdCache) {
        PropertyDefinitionImpl pdi = pdCache.get(def);
        if (pdi == null) {
            pdi = new PropertyDefinitionImpl(def, this, context, context.getValueFactory());
            pdCache.put(def, pdi);
        }
        return pdi;
    }
}
Also used : PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)

Aggregations

PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)12 RepositoryException (javax.jcr.RepositoryException)7 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)6 NodeState (org.apache.jackrabbit.core.state.NodeState)6 Name (org.apache.jackrabbit.spi.Name)6 PropertyId (org.apache.jackrabbit.core.id.PropertyId)5 NodeDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl)5 HashSet (java.util.HashSet)4 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)4 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)4 NodeTypeConflictException (org.apache.jackrabbit.core.nodetype.NodeTypeConflictException)4 NodeTypeManagerImpl (org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl)4 NodeTypeRegistry (org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)4 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)4 PropertyState (org.apache.jackrabbit.core.state.PropertyState)4 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)4 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 Value (javax.jcr.Value)3 ValueFormatException (javax.jcr.ValueFormatException)3 NodeDefinition (javax.jcr.nodetype.NodeDefinition)3