Search in sources :

Example 1 with NodeTypeTemplate

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

the class AddMixinTest method testAddItemsDefinedByMixin.

public void testAddItemsDefinedByMixin() throws NotExecutableException, RepositoryException {
    // register mixin
    NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testMixin");
    ntd.setMixin(true);
    NodeDefinitionTemplate nodeDef = ntm.createNodeDefinitionTemplate();
    nodeDef.setName("child");
    nodeDef.setRequiredPrimaryTypeNames(new String[] { "nt:folder" });
    ntd.getNodeDefinitionTemplates().add(nodeDef);
    ntm.registerNodeType(ntd, true);
    // create node and add mixin
    Node node = testRootNode.addNode(nodeName1, "nt:resource");
    node.setProperty("jcr:data", "abc");
    node.addMixin("testMixin");
    superuser.save();
    // create a child node defined by the mixin
    node.addNode("child", "nt:folder");
    node.save();
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node)

Example 2 with NodeTypeTemplate

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

the class AddMixinTest method testAddItemsDefinedByMixin3.

public void testAddItemsDefinedByMixin3() throws NotExecutableException, RepositoryException {
    // register mixin
    NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testMixin");
    ntd.setMixin(true);
    NodeDefinitionTemplate nodeDef = ntm.createNodeDefinitionTemplate();
    nodeDef.setName("child");
    nodeDef.setRequiredPrimaryTypeNames(new String[] { "nt:folder" });
    nodeDef.setDefaultPrimaryTypeName("nt:folder");
    ntd.getNodeDefinitionTemplates().add(nodeDef);
    ntm.registerNodeType(ntd, true);
    // create node and add mixin
    Node node = testRootNode.addNode(nodeName1, "nt:resource");
    node.setProperty("jcr:data", "abc");
    node.addMixin("testMixin");
    superuser.save();
    // create a child node defined by the mixin without specifying the
    // node type -> must succeed since default primary type is specified
    // in the child node def
    Node c = node.addNode("child");
    assertEquals("nt:folder", c.getPrimaryNodeType().getName());
    superuser.save();
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node)

Example 3 with NodeTypeTemplate

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

the class AddMixinTest method testAddItemsDefinedByMixin2.

public void testAddItemsDefinedByMixin2() throws NotExecutableException, RepositoryException {
    // register mixin
    NodeTypeManager ntm = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testMixin");
    ntd.setMixin(true);
    NodeDefinitionTemplate nodeDef = ntm.createNodeDefinitionTemplate();
    nodeDef.setName("child");
    nodeDef.setRequiredPrimaryTypeNames(new String[] { "nt:folder" });
    ntd.getNodeDefinitionTemplates().add(nodeDef);
    ntm.registerNodeType(ntd, true);
    // create node and add mixin
    Node node = testRootNode.addNode(nodeName1, "nt:resource");
    node.setProperty("jcr:data", "abc");
    node.addMixin("testMixin");
    superuser.save();
    // node type
    try {
        node.addNode("child");
        fail();
    } catch (ConstraintViolationException e) {
    // success as ChildNode Definition doesn't specify a default primary
    // type -> see comment in ItemDefinitionProvider#getQNodeDefinition
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 4 with NodeTypeTemplate

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

the class NodeTypeCreationTest method testInvalidJCRNames.

public void testInvalidJCRNames() throws Exception {
    String invalidName = ":ab[2]";
    // invalid name(s) passed to NT-template methods
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    try {
        ntt.setName(invalidName);
        fail("ConstraintViolationException expected. Nt-name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setDeclaredSuperTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the super type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setPrimaryItemName(invalidName);
        fail("ConstraintViolationException expected. Primary item name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to NodeDefinitionTemplate
    NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
    try {
        ndt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setRequiredPrimaryTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the required primary type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setDefaultPrimaryTypeName(invalidName);
        fail("ConstraintViolationException expected. Default primary type name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to PropertyDefinitionTemplate
    PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
    try {
        pdt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 5 with NodeTypeTemplate

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

the class NodeTypeDefinitionFactory method create.

/**
     * Create a new JCR node type definition from the given
     * <code>QNodeTypeDefinition</code>.
     *
     * @param qNtd A SPI node type definition.
     * @return the corresponding JCR node type definition.
     * @throws RepositoryException if an error occurs.
     */
@SuppressWarnings("unchecked")
public NodeTypeDefinition create(QNodeTypeDefinition qNtd) throws RepositoryException {
    NodeTypeTemplate nt = ntMgr.createNodeTypeTemplate();
    nt.setName(getJCRName(qNtd.getName()));
    nt.setDeclaredSuperTypeNames(getJCRNames(qNtd.getSupertypes()));
    nt.setAbstract(qNtd.isAbstract());
    nt.setMixin(qNtd.isMixin());
    nt.setOrderableChildNodes(qNtd.hasOrderableChildNodes());
    nt.setPrimaryItemName(getJCRName(qNtd.getPrimaryItemName()));
    nt.setQueryable(qNtd.isQueryable());
    List nodeDefs = nt.getNodeDefinitionTemplates();
    for (QNodeDefinition qNd : qNtd.getChildNodeDefs()) {
        nodeDefs.add(create(qNd));
    }
    List propDefs = nt.getPropertyDefinitionTemplates();
    for (QPropertyDefinition qPd : qNtd.getPropertyDefs()) {
        propDefs.add(create(qPd));
    }
    return nt;
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ArrayList(java.util.ArrayList) List(java.util.List) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition)

Aggregations

NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)46 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)29 Node (javax.jcr.Node)17 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)13 Test (org.junit.Test)12 Session (javax.jcr.Session)11 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)11 List (java.util.List)7 Workspace (javax.jcr.Workspace)7 NamespaceRegistry (javax.jcr.NamespaceRegistry)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)5 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4 RepositoryException (javax.jcr.RepositoryException)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 BigDecimal (java.math.BigDecimal)3