Search in sources :

Example 31 with NodeTypeManager

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

the class NodeSetPrimaryTypeTest method testSetMixinAsPrimaryType.

/**
 * Tests if <code>Node.setPrimaryType(String)</code> throws a
 * <code>ConstraintViolationException</code> if the
 * name of a mixin type is passed
 */
public void testSetMixinAsPrimaryType() throws RepositoryException {
    Session session = testRootNode.getSession();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = manager.getMixinNodeTypes();
    while (nts.hasNext()) {
        try {
            Node node = testRootNode.addNode(nodeName1, testNodeType);
            node.setPrimaryType(nts.nextNodeType().getName());
            fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to a mixin.");
        } catch (ConstraintViolationException e) {
        // success
        } finally {
            // reset the changes.
            session.refresh(false);
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session)

Example 32 with NodeTypeManager

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

the class NodeSetPrimaryTypeTest method testSetPrimaryType.

// TODO: test if node definition is properly reset
// TODO: test if child items are properly reset upon changing definition
// TODO: test if conflicts are properly detected
/**
 * Tests a successful call to <code>Node.setPrimaryType(String)</code>
 */
public void testSetPrimaryType() throws RepositoryException {
    Session session = testRootNode.getSession();
    Session otherSession = null;
    String nonExistingMixinName = NodeMixinUtil.getNonExistingMixinName(session);
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    superuser.save();
    // TODO improve. retrieve settable node type name from config.
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = manager.getPrimaryNodeTypes();
    while (nts.hasNext()) {
        NodeType nt = nts.nextNodeType();
        String ntName = nt.getName();
        if (!nt.isAbstract() && !ntFrozenNode.equals(ntName) && !ntActivity.equals(ntName)) {
            try {
                node.setPrimaryType(ntName);
                // property value must be adjusted immediately
                assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, node.getProperty(jcrPrimaryType).getString());
                // save changes -> reflected upon Node.getPrimaryNodeType and Property.getValue
                superuser.save();
                assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, node.getPrimaryNodeType().getName());
                assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, node.getProperty(jcrPrimaryType).getString());
                otherSession = getHelper().getReadOnlySession();
                assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, otherSession.getNode(node.getPath()).getPrimaryNodeType().getName());
                assertEquals("The value of the jcr:primaryType property must change upon setPrimaryType.", ntName, otherSession.getNode(node.getPath()).getProperty(jcrPrimaryType).getString());
                // was successful
                return;
            } catch (ConstraintViolationException e) {
            // may happen as long as arbitrary primary types are used for testing -> ignore
            } finally {
                if (otherSession != null) {
                    otherSession.logout();
                }
                // revert any unsaved changes.
                session.refresh(false);
            }
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session)

Example 33 with NodeTypeManager

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

the class LuceneQueryBuilder method visit.

public Object visit(NodeTypeQueryNode node, Object data) {
    List<Term> terms = new ArrayList<Term>();
    try {
        String mixinTypesField = resolver.getJCRName(NameConstants.JCR_MIXINTYPES);
        String primaryTypeField = resolver.getJCRName(NameConstants.JCR_PRIMARYTYPE);
        NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
        NodeType base = ntMgr.getNodeType(session.getJCRName(node.getValue()));
        if (base.isMixin()) {
            // search for nodes where jcr:mixinTypes is set to this mixin
            Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, resolver.getJCRName(node.getValue())));
            terms.add(t);
        } else {
            // search for nodes where jcr:primaryType is set to this type
            Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, resolver.getJCRName(node.getValue())));
            terms.add(t);
        }
        // now search for all node types that are derived from base
        NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
        while (allTypes.hasNext()) {
            NodeType nt = allTypes.nextNodeType();
            NodeType[] superTypes = nt.getSupertypes();
            if (Arrays.asList(superTypes).contains(base)) {
                Name n = session.getQName(nt.getName());
                String ntName = nsMappings.translateName(n);
                Term t;
                if (nt.isMixin()) {
                    // search on jcr:mixinTypes
                    t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, ntName));
                } else {
                    // search on jcr:primaryType
                    t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, ntName));
                }
                terms.add(t);
            }
        }
    } catch (NameException e) {
        exceptions.add(e);
    } catch (RepositoryException e) {
        exceptions.add(e);
    }
    if (terms.size() == 0) {
        // exception occured
        return new BooleanQuery();
    } else if (terms.size() == 1) {
        return new JackrabbitTermQuery(terms.get(0));
    } else {
        BooleanQuery b = new BooleanQuery();
        for (Term term : terms) {
            b.add(new JackrabbitTermQuery(term), Occur.SHOULD);
        }
        return b;
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) Term(org.apache.lucene.index.Term) Name(org.apache.jackrabbit.spi.Name) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeType(javax.jcr.nodetype.NodeType)

Example 34 with NodeTypeManager

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

the class RepositoryCopierTest method createSourceRepository.

private RepositoryImpl createSourceRepository() throws Exception {
    RepositoryImpl repository = RepositoryImpl.create(RepositoryConfig.install(SOURCE));
    Session session = repository.login(CREDENTIALS);
    try {
        NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
        registry.registerNamespace("test", "http://www.example.org/");
        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        NodeTypeTemplate template = manager.createNodeTypeTemplate();
        template.setName("test:unstructured");
        template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
        manager.registerNodeType(template, false);
        Node root = session.getRootNode();
        Node referenceable = root.addNode("referenceable", "test:unstructured");
        referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
        session.save();
        identifier = referenceable.getIdentifier();
        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("string", "test");
        properties.setProperty("multiple", "a,b,c".split(","));
        session.save();
        binary = properties.getProperty("binary").getBinary();
        try {
            InputStream stream = binary.getStream();
            try {
                for (int i = 0; i < BINARY.length; i++) {
                    assertEquals(BINARY[i], (byte) stream.read());
                }
                assertEquals(-1, stream.read());
            } finally {
                stream.close();
            }
        } finally {
            binary.dispose();
        }
    } finally {
        session.logout();
    }
    return repository;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary) BigDecimal(java.math.BigDecimal) Session(javax.jcr.Session)

Example 35 with NodeTypeManager

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

the class AbstractRepositoryOperationTest method testRegisterNodeType.

public void testRegisterNodeType() throws Exception {
    assertDefaultPrivileges(NameConstants.JCR_NODE_TYPE_DEFINITION_MANAGEMENT);
    assertPermission(Permission.NODE_TYPE_DEF_MNGMT, false);
    Workspace testWsp = getTestWorkspace();
    NodeTypeManager ntm = testWsp.getNodeTypeManager();
    NodeTypeTemplate ntd = ntm.createNodeTypeTemplate();
    ntd.setName("testNodeType");
    ntd.setMixin(true);
    try {
        ntm.registerNodeType(ntd, true);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
    try {
        ntm.registerNodeType(ntd, false);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
    NodeTypeTemplate[] ntds = new NodeTypeTemplate[2];
    ntds[0] = ntd;
    ntds[1] = ntm.createNodeTypeTemplate();
    ntds[1].setName("anotherNodeType");
    ntds[1].setDeclaredSuperTypeNames(new String[] { "nt:file" });
    try {
        ntm.registerNodeTypes(ntds, true);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
    try {
        ntm.registerNodeTypes(ntds, false);
        fail("Node type registration should be denied.");
    } catch (AccessDeniedException e) {
    // success
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) Workspace(javax.jcr.Workspace)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)103 NodeType (javax.jcr.nodetype.NodeType)47 Node (javax.jcr.Node)38 Session (javax.jcr.Session)35 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)31 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)29 Test (org.junit.Test)25 RepositoryException (javax.jcr.RepositoryException)18 ByteArrayInputStream (java.io.ByteArrayInputStream)15 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)10 Workspace (javax.jcr.Workspace)10 InputStream (java.io.InputStream)9 NodeDefinition (javax.jcr.nodetype.NodeDefinition)9 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)9 InputStreamReader (java.io.InputStreamReader)8 NamespaceRegistry (javax.jcr.NamespaceRegistry)8 Value (javax.jcr.Value)8