Search in sources :

Example 61 with NodeTypeManager

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

the class NodeSetPrimaryTypeTest method getPrimaryTypeName.

private static String getPrimaryTypeName(Session session, Node node) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = manager.getPrimaryNodeTypes();
    while (nts.hasNext()) {
        String name = nts.nextNodeType().getName();
        if (!name.equals(node.getPrimaryNodeType().getName())) {
            return name;
        }
    }
    return null;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 62 with NodeTypeManager

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

the class TreeComparator method createComplexTree.

/**
     * Creates a complex example tree that uses as many features of the
     * repository as possible
     *
     * @param save Save the tree to the workspace. If false, the tree remains in
     *             the session.
     */
public void createComplexTree(boolean save) {
    Node rootNode = null;
    Node nt = null;
    Node pt = null;
    Node mvp = null;
    Node referenceable = null;
    try {
        Node src = (Node) session.getItem(sourceFolder);
        rootNode = src.addNode(sc.rootNodeName);
        nt = rootNode.addNode(sc.nodeTypesTestNode);
        rootNode.addNode(sc.mixinTypeTestNode);
        pt = rootNode.addNode(sc.propertyTypesTestNode);
        rootNode.addNode(sc.sameNameChildrenTestNode);
        mvp = rootNode.addNode(sc.multiValuePropertiesTestNode);
        referenceable = rootNode.addNode(sc.referenceableNodeTestNode);
        rootNode.addNode(sc.orderChildrenTestNode);
        rootNode.addNode(sc.namespaceTestNode);
    } catch (RepositoryException e) {
        sc.log("Error while creating example tree: " + e.getMessage());
    }
    // Add nodes with mixin types
    NodeTypeManager ntmgr = null;
    NodeTypeIterator types = null;
    try {
        ntmgr = workspace.getNodeTypeManager();
        types = ntmgr.getMixinNodeTypes();
    } catch (RepositoryException e) {
        fail("Cannot access NodeType iterator: " + e);
    }
    while (types.hasNext()) {
        NodeType t = (NodeType) types.next();
        String name = t.getName();
        name = "Node_" + name.replaceAll(":", "_");
        Node n = null;
        try {
            n = nt.addNode(name);
            n.addMixin(t.getName());
            // try saving, because some exceptions are thrown only at save time
            session.save();
        } catch (RepositoryException e) {
            sc.log("Cannot create node with mixin node type: " + e);
            // if saving failed for a node, then remove it again (or else the next save will fail on it)
            try {
                if (n != null) {
                    n.remove();
                }
            } catch (RepositoryException e1) {
                sc.log("Could not remove node: " + e);
            }
        }
    }
    // Create all property types
    try {
        // String
        pt.setProperty(sc.stringTestProperty, "This is a string.");
        // Binary
        byte[] byteArray = { (byte) 0, (byte) 255, (byte) 167, (byte) 100, (byte) 21, (byte) 6, (byte) 19, (byte) 71, (byte) 221 };
        pt.setProperty(sc.binaryTestProperty, new ByteArrayInputStream(byteArray));
        // Date
        Calendar c = Calendar.getInstance();
        c.set(2005, 6, 21, 13, 30, 5);
        pt.setProperty(sc.dateTestProperty, c);
        // Long
        pt.setProperty(sc.longTestProperty, (long) (1 / 3));
        // Double
        pt.setProperty(sc.doubleTestProperty, (double) Math.PI);
        // Boolean
        pt.setProperty(sc.booleanTestProperty, true);
        // Name
        pt.setProperty(sc.nameTestProperty, session.getValueFactory().createValue(sc.jcrPrimaryType, PropertyType.NAME));
        // Path
        pt.setProperty(sc.pathTestProperty, session.getValueFactory().createValue("paths/dont/have/to/point/anywhere", PropertyType.PATH));
        // Reference: Note that I only check if the node exists. We do not specify what happens with
        // the UUID during serialization.
        sc.ensureMixinType(referenceable, sc.mixReferenceable);
        // some implementations may require a save after addMixin()
        session.save();
        pt.setProperty(sc.referenceTestProperty, referenceable);
        // Create a boolean property on the root node, so I can test noRecurse and skipBinary at the same time
        rootNode.setProperty(sc.binaryTestProperty, new ByteArrayInputStream(byteArray));
        session.save();
    } catch (Exception e) {
        fail("Could not add property: " + e);
    }
    // multi value properties
    String[] s = { "one", "two", "three" };
    try {
        mvp.setProperty(sc.multiValueTestProperty, s);
        session.save();
    } catch (RepositoryException e) {
        sc.log("Could not create multi-value property: " + e);
    }
    // Save to the workspace. Note that export is from session anyway.
    if (save) {
        try {
            session.save();
        } catch (RepositoryException e) {
            fail("Cannot save the example tree to the repository: " + e);
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) Calendar(java.util.Calendar) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException)

Example 63 with NodeTypeManager

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

the class RepositorySidegradeTest method verifyCustomNodeTypes.

@Test
public void verifyCustomNodeTypes() throws Exception {
    Session session = createAdminSession();
    try {
        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        assertTrue(manager.hasNodeType("test:unstructured"));
        NodeType type = manager.getNodeType("test:unstructured");
        assertFalse(type.isMixin());
        assertTrue(type.isNodeType("nt:unstructured"));
        boolean foundDefaultString = false;
        boolean foundDefaultPath = false;
        for (PropertyDefinition pDef : type.getPropertyDefinitions()) {
            if ("defaultString".equals(pDef.getName())) {
                assertEquals(PropertyType.STRING, pDef.getRequiredType());
                assertNotNull(pDef.getDefaultValues());
                assertEquals(1, pDef.getDefaultValues().length);
                assertEquals("stringValue", pDef.getDefaultValues()[0].getString());
                foundDefaultString = true;
            } else if ("defaultPath".equals(pDef.getName())) {
                assertEquals(PropertyType.PATH, pDef.getRequiredType());
                assertNotNull(pDef.getDefaultValues());
                assertEquals(1, pDef.getDefaultValues().length);
                assertEquals("/jcr:path/nt:value", pDef.getDefaultValues()[0].getString());
                foundDefaultPath = true;
            }
        }
        assertTrue("Expected property definition with name \"defaultString\"", foundDefaultString);
        assertTrue("Expected property definition with name \"defaultPath\"", foundDefaultPath);
    } finally {
        session.logout();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test)

Example 64 with NodeTypeManager

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

the class RepositorySidegradeTest method createSourceContent.

@SuppressWarnings("unchecked")
protected NodeStore createSourceContent() throws Exception {
    NodeStore source = SegmentNodeStoreBuilders.builder(new MemoryStore()).build();
    Repository repository = new Jcr(new Oak(source)).createRepository();
    Session session = repository.login(CREDENTIALS);
    try {
        JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
        NamespaceRegistry registry = workspace.getNamespaceRegistry();
        registry.registerNamespace("test", "http://www.example.org/");
        NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
        NodeTypeTemplate template = nodeTypeManager.createNodeTypeTemplate();
        template.setName("test:unstructured");
        template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
        PropertyDefinitionTemplate pDef1 = nodeTypeManager.createPropertyDefinitionTemplate();
        pDef1.setName("defaultString");
        pDef1.setRequiredType(PropertyType.STRING);
        Value stringValue = session.getValueFactory().createValue("stringValue");
        pDef1.setDefaultValues(new Value[] { stringValue });
        template.getPropertyDefinitionTemplates().add(pDef1);
        PropertyDefinitionTemplate pDef2 = nodeTypeManager.createPropertyDefinitionTemplate();
        pDef2.setName("defaultPath");
        pDef2.setRequiredType(PropertyType.PATH);
        Value pathValue = session.getValueFactory().createValue("/jcr:path/nt:value", PropertyType.PATH);
        pDef2.setDefaultValues(new Value[] { pathValue });
        template.getPropertyDefinitionTemplates().add(pDef2);
        nodeTypeManager.registerNodeType(template, false);
        template = nodeTypeManager.createNodeTypeTemplate();
        template.setName("test:referenceable");
        template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured", "mix:referenceable" });
        nodeTypeManager.registerNodeType(template, false);
        Node root = session.getRootNode();
        Node referenceable = root.addNode("referenceable", "test:unstructured");
        referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
        Node versionable = root.addNode("versionable", NT_UNSTRUCTURED);
        versionable.addMixin(MIX_VERSIONABLE);
        Node child = versionable.addNode("child", "test:referenceable");
        child.addNode("child2", NT_UNSTRUCTURED);
        session.save();
        session.getWorkspace().getVersionManager().checkin("/versionable");
        Node properties = root.addNode("properties", "test:unstructured");
        properties.setProperty("boolean", true);
        Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
        try {
            properties.setProperty("binary", binary);
        } finally {
            binary.dispose();
        }
        properties.setProperty("date", DATE);
        properties.setProperty("decimal", new BigDecimal(123));
        properties.setProperty("double", Math.PI);
        properties.setProperty("long", 9876543210L);
        properties.setProperty("reference", referenceable);
        properties.setProperty("weak_reference", session.getValueFactory().createValue(referenceable, true));
        properties.setProperty("mv_reference", new Value[] { session.getValueFactory().createValue(versionable, false) });
        properties.setProperty("mv_weak_reference", new Value[] { session.getValueFactory().createValue(versionable, true) });
        properties.setProperty("string", "test");
        properties.setProperty("multiple", "a,b,c".split(","));
        session.save();
        binary = properties.getProperty("binary").getBinary();
        try {
            InputStream stream = binary.getStream();
            try {
                for (byte aBINARY : BINARY) {
                    assertEquals(aBINARY, (byte) stream.read());
                }
                assertEquals(-1, stream.read());
            } finally {
                stream.close();
            }
        } finally {
            binary.dispose();
        }
        return source;
    } finally {
        session.logout();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) BigDecimal(java.math.BigDecimal) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) Repository(javax.jcr.Repository) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) Jcr(org.apache.jackrabbit.oak.jcr.Jcr) Oak(org.apache.jackrabbit.oak.Oak) Value(javax.jcr.Value) Binary(javax.jcr.Binary) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 65 with NodeTypeManager

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

the class RepositoryUpgradeTest method verifyReplacedBuiltinNodeTypes.

@Test
public void verifyReplacedBuiltinNodeTypes() throws Exception {
    Session session = createAdminSession();
    try {
        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        NodeType nt = manager.getNodeType(UserConstants.NT_REP_GROUP);
        assertTrue("Migrated repository must have new nodetype definitions", nt.isNodeType(UserConstants.NT_REP_MEMBER_REFERENCES));
    } finally {
        session.logout();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) Session(javax.jcr.Session) Test(org.junit.Test)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)93 NodeType (javax.jcr.nodetype.NodeType)41 Node (javax.jcr.Node)32 Session (javax.jcr.Session)30 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)29 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)27 Test (org.junit.Test)24 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)14 RepositoryException (javax.jcr.RepositoryException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)8 Workspace (javax.jcr.Workspace)8 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)8 NodeDefinition (javax.jcr.nodetype.NodeDefinition)8 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)8 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 InputStream (java.io.InputStream)6