Search in sources :

Example 1 with NodeTypeDefinition

use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.

the class ReadWriteNodeTypeManager method registerNodeTypes.

@Override
public final NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] ntds, boolean allowUpdate) throws RepositoryException {
    Root root = getWriteRoot();
    try {
        Tree tree = getOrCreateNodeTypes(root);
        for (NodeTypeDefinition ntd : ntds) {
            NodeTypeTemplateImpl template;
            if (ntd instanceof NodeTypeTemplateImpl) {
                template = (NodeTypeTemplateImpl) ntd;
            } else {
                // some external template implementation, copy before proceeding
                template = new NodeTypeTemplateImpl(getNamePathMapper(), ntd);
            }
            template.writeTo(tree, allowUpdate);
        }
        root.commit();
        refresh();
        List<NodeType> types = new ArrayList<NodeType>(ntds.length);
        for (NodeTypeDefinition ntd : ntds) {
            types.add(getNodeType(ntd.getName()));
        }
        return new NodeTypeIteratorAdapter(types);
    } catch (CommitFailedException e) {
        String message = "Failed to register node types.";
        throw e.asRepositoryException(message);
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) NodeType(javax.jcr.nodetype.NodeType) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ArrayList(java.util.ArrayList) Tree(org.apache.jackrabbit.oak.api.Tree) NodeTypeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 2 with NodeTypeDefinition

use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.

the class NodeTypeTest method updateNodeType.

@Test
public void updateNodeType() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    ValueFactory vf = session.getValueFactory();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    Node n = root.addNode("q1", "nt:query");
    n.setProperty("jcr:statement", vf.createValue("statement"));
    session.save();
    NodeTypeDefinition ntd = manager.getNodeType("nt:query");
    NodeTypeTemplate ntt = manager.createNodeTypeTemplate(ntd);
    try {
        manager.registerNodeType(ntt, true);
    // no changes to the type, so the registration should be a no-op
    } catch (ConstraintViolationException unexpected) {
        fail();
    }
    // make the (still missing) jcr:language property mandatory
    @SuppressWarnings("unchecked") List<PropertyDefinitionTemplate> pdts = ntt.getPropertyDefinitionTemplates();
    for (PropertyDefinitionTemplate pdt : pdts) {
        if ("jcr:language".equals(pdt.getName())) {
            pdt.setMandatory(true);
        }
    }
    try {
        manager.registerNodeType(ntt, true);
        fail();
    } catch (ConstraintViolationException expected) {
    // the registration fails because of the would-be invalid content
    }
    // add the jcr:language property so it can be made mandatory
    n.setProperty("jcr:language", vf.createValue("language"));
    session.save();
    try {
        manager.registerNodeType(ntt, true);
    // now the mandatory property exists, so the type change is OK
    } catch (ConstraintViolationException unexpected) {
        fail();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) Node(javax.jcr.Node) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ValueFactory(javax.jcr.ValueFactory) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 3 with NodeTypeDefinition

use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.

the class NodeTypeUtils method createNodeType.

/**
 * Creates a node type with the given properties.
 *
 * @param session the session
 * @param name the name
 * @param properties the properties
 * @param superTypes the super types
 * @param childrenTypes the children types
 * @param baseType the base type
 * @param isMixin the is mixin
 * @return the string
 * @throws RepositoryException the repository exception
 */
@SuppressWarnings("unchecked")
public static String createNodeType(Session session, String name, String[] properties, int[] propTypes, String[] superTypes, String[] childrenTypes, String baseType, boolean isMixin) throws RepositoryException {
    NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    if (baseType != null) {
        NodeTypeDefinition ntd = ntm.getNodeType(baseType);
        ntt = ntm.createNodeTypeTemplate(ntd);
    }
    if ((superTypes != null) && (superTypes.length != 0)) {
        ntt.setDeclaredSuperTypeNames(superTypes);
    }
    ntt.setOrderableChildNodes(false);
    ntt.setName(name);
    if (properties != null) {
        for (int count = 0; count < properties.length; count++) {
            ntt.getPropertyDefinitionTemplates().add(createPropertyDefTemplate(ntm, properties[count], propTypes[count]));
        }
    }
    if (childrenTypes != null) {
        ntt.getNodeDefinitionTemplates().add(createNodeDefTemplate(ntm, childrenTypes));
    }
    ntt.setMixin(isMixin);
    ntm.registerNodeType(ntt, true);
    return ntt.getName();
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition)

Example 4 with NodeTypeDefinition

use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit by apache.

the class CompactNodeTypeDefWriter method write.

/**
 * Writes the given list of QNodeTypeDefinition to the output writer
 * including the used namespaces.
 *
 * @param defs collection of definitions
 * @param nsMapping the mapping from prefix to namespace URI.
 * @param out output writer
 * @throws java.io.IOException if an I/O error occurs
 */
public static void write(Collection<NodeTypeDefinition> defs, NamespaceMapping nsMapping, Writer out) throws IOException {
    CompactNodeTypeDefWriter w = new CompactNodeTypeDefWriter(out, nsMapping, true);
    for (NodeTypeDefinition def : defs) {
        w.write(def);
    }
    w.close();
}
Also used : NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition)

Example 5 with NodeTypeDefinition

use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit by apache.

the class NodeTypeManagerImpl method registerNodeTypes.

/**
 * @see NodeTypeManager#registerNodeTypes(javax.jcr.nodetype.NodeTypeDefinition[], boolean)
 */
public NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] ntds, boolean allowUpdate) throws RepositoryException {
    List<QNodeTypeDefinition> defs = new ArrayList<QNodeTypeDefinition>(ntds.length);
    for (NodeTypeDefinition definition : ntds) {
        QNodeTypeDefinition qdef = new QNodeTypeDefinitionImpl(definition, getNamePathResolver(), mgrProvider.getQValueFactory());
        if (!allowUpdate && hasNodeType(qdef.getName())) {
            throw new NodeTypeExistsException("NodeType " + definition.getName() + " already exists.");
        }
        defs.add(qdef);
    }
    getNodeTypeRegistry().registerNodeTypes(defs, allowUpdate);
    List<NodeType> nts = new ArrayList<NodeType>();
    for (QNodeTypeDefinition def : defs) {
        nts.add(getNodeType(def.getName()));
    }
    return new NodeTypeIteratorAdapter(nts);
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) 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) QNodeTypeDefinitionImpl(org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)

Aggregations

NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)17 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)8 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)6 ArrayList (java.util.ArrayList)5 Session (javax.jcr.Session)5 Test (org.junit.Test)5 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)4 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)4 Node (javax.jcr.Node)3 ValueFactory (javax.jcr.ValueFactory)3 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)3 NodeType (javax.jcr.nodetype.NodeType)3 NodeTypeExistsException (javax.jcr.nodetype.NodeTypeExistsException)3 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)3 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)3 StringReader (java.io.StringReader)2 List (java.util.List)2 Root (org.apache.jackrabbit.oak.api.Root)2 QNodeTypeDefinitionImpl (org.apache.jackrabbit.spi.commons.QNodeTypeDefinitionImpl)2 NodeTypeDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefinitionImpl)2