Search in sources :

Example 51 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class NodeTypeRegistry method checkNtBaseSubtyping.

/**
 * Checks if the given node type def has the correct supertypes in respect
 * to nt:base. all mixin nodetypes must not have a nt:base, the primary
 * ones only if they don't inherit it from another supertype.
 *
 * @param ntd the node type def to check
 * @param ntdCache cache for lookup
 * @return the node type definition that was given to check or a new
 *          instance if it had to be fixed up.
 */
private static QNodeTypeDefinition checkNtBaseSubtyping(QNodeTypeDefinition ntd, Map<Name, QNodeTypeDefinition> ntdCache) {
    if (NameConstants.NT_BASE.equals(ntd.getName())) {
        return ntd;
    }
    Set<Name> supertypes = new TreeSet<Name>(Arrays.asList(ntd.getSupertypes()));
    if (supertypes.isEmpty()) {
        return ntd;
    }
    boolean modified;
    if (ntd.isMixin()) {
        // if mixin, remove possible nt:base supertype
        modified = supertypes.remove(NameConstants.NT_BASE);
    } else {
        // check if all supertypes (except nt:base) are mixins
        boolean allMixins = true;
        for (Name name : supertypes) {
            if (!name.equals(NameConstants.NT_BASE)) {
                QNodeTypeDefinition def = ntdCache.get(name);
                if (def != null && !def.isMixin()) {
                    allMixins = false;
                    break;
                }
            }
        }
        if (allMixins) {
            // ntd is a primary node type and has only mixins as supertypes,
            // so it needs a nt:base
            modified = supertypes.add(NameConstants.NT_BASE);
        } else {
            // ntd is a primary node type and at least one of the supertypes
            // is too, so ensure that no nt:base is added. note that the
            // trivial case, where there would be no supertype left is handled
            // in the QNodeTypeDefinition directly
            modified = supertypes.remove(NameConstants.NT_BASE);
        }
    }
    if (modified) {
        ntd = new QNodeTypeDefinitionImpl(ntd.getName(), supertypes.toArray(new Name[supertypes.size()]), ntd.getSupportedMixinTypes(), ntd.isMixin(), ntd.isAbstract(), ntd.isQueryable(), ntd.hasOrderableChildNodes(), ntd.getPrimaryItemName(), ntd.getPropertyDefs(), ntd.getChildNodeDefs());
    }
    return ntd;
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) TreeSet(java.util.TreeSet) Name(org.apache.jackrabbit.spi.Name) QNodeTypeDefinitionImpl(org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)

Example 52 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class NodeTypeManagerImpl method registerNodeTypes.

// --------------------------------------------------< new JSR 283 methods >
/**
 * Registers or updates the specified <code>Collection</code> of
 * <code>NodeTypeDefinition</code> objects. This method is used to register
 * or update a set of node types with mutual dependencies. Returns an
 * iterator over the resulting <code>NodeType</code> objects.
 * <p>
 * The effect of the method is "all or nothing"; if an error occurs, no node
 * types are registered or updated.
 * <p>
 * Throws an <code>InvalidNodeTypeDefinitionException</code> if a
 * <code>NodeTypeDefinition</code> within the <code>Collection</code> is
 * invalid or if the <code>Collection</code> contains an object of a type
 * other than <code>NodeTypeDefinition</code>.
 * <p>
 * Throws a <code>NodeTypeExistsException</code> if <code>allowUpdate</code>
 * is <code>false</code> and a <code>NodeTypeDefinition</code> within the
 * <code>Collection</code> specifies a node type name that is already
 * registered.
 * <p>
 * Throws an <code>UnsupportedRepositoryOperationException</code> if this
 * implementation does not support node type registration.
 *
 * @param definitions a collection of <code>NodeTypeDefinition</code>s
 * @param allowUpdate a boolean
 * @return the registered node types.
 * @throws InvalidNodeTypeDefinitionException if a
 *  <code>NodeTypeDefinition</code> within the <code>Collection</code> is
 *  invalid or if the <code>Collection</code> contains an object of a type
 *  other than <code>NodeTypeDefinition</code>.
 * @throws NodeTypeExistsException if <code>allowUpdate</code> is
 *  <code>false</code> and a <code>NodeTypeDefinition</code> within the
 *  <code>Collection</code> specifies a node type name that is already
 *  registered.
 * @throws UnsupportedRepositoryOperationException if this implementation
 *  does not support node type registration.
 * @throws RepositoryException if another error occurs.
 * @since JCR 2.0
 */
public NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] definitions, boolean allowUpdate) throws InvalidNodeTypeDefinitionException, NodeTypeExistsException, UnsupportedRepositoryOperationException, RepositoryException {
    // make sure the editing session is allowed to register node types.
    context.getAccessManager().checkRepositoryPermission(Permission.NODE_TYPE_DEF_MNGMT);
    NodeTypeRegistry registry = context.getNodeTypeRegistry();
    // split the node types into new and already registered node types.
    // this way we can register new node types together with already
    // registered node types which make circular dependencies possible
    List<QNodeTypeDefinition> addedDefs = new ArrayList<QNodeTypeDefinition>();
    List<QNodeTypeDefinition> modifiedDefs = new ArrayList<QNodeTypeDefinition>();
    for (NodeTypeDefinition definition : definitions) {
        // convert to QNodeTypeDefinition
        QNodeTypeDefinition def = toNodeTypeDef(definition);
        if (registry.isRegistered(def.getName())) {
            if (allowUpdate) {
                modifiedDefs.add(def);
            } else {
                throw new NodeTypeExistsException(definition.getName());
            }
        } else {
            addedDefs.add(def);
        }
    }
    try {
        ArrayList<NodeType> result = new ArrayList<NodeType>();
        // register new node types
        result.addAll(registerNodeTypes(addedDefs));
        // re-register already existing node types
        for (QNodeTypeDefinition nodeTypeDef : modifiedDefs) {
            registry.reregisterNodeType(nodeTypeDef);
            result.add(getNodeType(nodeTypeDef.getName()));
        }
        return new NodeTypeIteratorAdapter(result);
    } catch (InvalidNodeTypeDefException e) {
        throw new InvalidNodeTypeDefinitionException(e.getMessage(), e);
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) InvalidNodeTypeDefinitionException(javax.jcr.nodetype.InvalidNodeTypeDefinitionException) NodeTypeExistsException(javax.jcr.nodetype.NodeTypeExistsException) NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) NodeTypeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)

Example 53 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class NodeTypeManagerImpl method registerNodeTypes.

// --------------------------------------------< JackrabbitNodeTypeManager >
/**
 * Internal helper method for registering a list of node type definitions.
 * Returns a collection containing the registered node types.
 *
 * @param defs a collection of <code>QNodeTypeDefinition<code> objects
 * @return registered node types
 * @throws InvalidNodeTypeDefException if a nodetype is invalid
 * @throws RepositoryException if an error occurs
 */
private Collection<NodeType> registerNodeTypes(List<QNodeTypeDefinition> defs) throws InvalidNodeTypeDefException, RepositoryException {
    context.getNodeTypeRegistry().registerNodeTypes(defs);
    Set<NodeType> types = new HashSet<NodeType>();
    for (QNodeTypeDefinition def : defs) {
        try {
            types.add(getNodeType(def.getName()));
        } catch (NoSuchNodeTypeException e) {
        // ignore
        }
    }
    return types;
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NodeType(javax.jcr.nodetype.NodeType) HashSet(java.util.HashSet) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException)

Example 54 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class NodeTypeManagerImpl method getNodeType.

/**
 * @param name node type name
 * @return node type
 * @throws NoSuchNodeTypeException if the nodetype does not exit
 */
@Override
public NodeTypeImpl getNodeType(Name name) throws NoSuchNodeTypeException {
    synchronized (ntCache) {
        NodeTypeImpl nt = ntCache.get(name);
        if (nt == null) {
            NodeTypeRegistry registry = context.getNodeTypeRegistry();
            EffectiveNodeType ent = registry.getEffectiveNodeType(name);
            QNodeTypeDefinition def = registry.getNodeTypeDef(name);
            nt = new NodeTypeImpl(ent, def, this, context, context.getValueFactory(), context.getDataStore());
            ntCache.put(name, nt);
        }
        return nt;
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition)

Example 55 with QNodeTypeDefinition

use of org.apache.jackrabbit.spi.QNodeTypeDefinition in project jackrabbit by apache.

the class RepositoryCopier method copyNodeTypes.

private void copyNodeTypes() throws RepositoryException {
    NodeTypeRegistry sourceRegistry = source.getNodeTypeRegistry();
    NodeTypeRegistry targetRegistry = target.getNodeTypeRegistry();
    logger.info("Copying registered node types");
    Collection<Name> existing = Arrays.asList(targetRegistry.getRegisteredNodeTypes());
    Collection<QNodeTypeDefinition> register = new ArrayList<QNodeTypeDefinition>();
    for (Name name : sourceRegistry.getRegisteredNodeTypes()) {
        // TODO: what about modified node types?
        if (!existing.contains(name)) {
            register.add(sourceRegistry.getNodeTypeDef(name));
        }
    }
    try {
        targetRegistry.registerNodeTypes(register);
    } catch (InvalidNodeTypeDefException e) {
        throw new RepositoryException("Unable to copy node types", e);
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) ArrayList(java.util.ArrayList) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Aggregations

QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)58 Name (org.apache.jackrabbit.spi.Name)21 ArrayList (java.util.ArrayList)13 RepositoryException (javax.jcr.RepositoryException)11 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)9 NodeType (javax.jcr.nodetype.NodeType)7 QNodeTypeDefinitionImpl (org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)6 HashMap (java.util.HashMap)5 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)5 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)5 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)4 InvalidNodeTypeDefException (org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException)4 NodeTypeRegistry (org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)4 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)4 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)4 QNodeTypeDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QNodeTypeDefinitionBuilder)4 CompactNodeTypeDefReader (org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)3 NamespaceMapping (org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping)3 QDefinitionBuilderFactory (org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory)3 QNodeDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QNodeDefinitionBuilder)3