Search in sources :

Example 41 with NodeTypeIterator

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

the class NodeMixinUtil method getNotAssignedMixinName.

public static String getNotAssignedMixinName(Session session, Node node) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator mixins = manager.getMixinNodeTypes();
    Set<String> existingMixins = new HashSet<String>();
    for (NodeType nt : node.getMixinNodeTypes()) {
        existingMixins.add(nt.getName());
    }
    while (mixins.hasNext()) {
        String ntName = mixins.nextNodeType().getName();
        if (!existingMixins.contains(ntName)) {
            return ntName;
        }
    }
    return null;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) HashSet(java.util.HashSet)

Example 42 with NodeTypeIterator

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

the class NodeMixinUtil method getAddableMixinName.

/**
     * @return the name of a mixin node type that can be added by the requested
     *         <code>node</code>
     */
public static String getAddableMixinName(Session session, Node node) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator mixins = manager.getMixinNodeTypes();
    // Skip mix:shareable since not supported by removeMixin
    String mixShareable = session.getNamespacePrefix(AbstractJCRTest.NS_MIX_URI) + ":shareable";
    while (mixins.hasNext()) {
        String name = mixins.nextNodeType().getName();
        if (node.canAddMixin(name) && !node.isNodeType(name) && !mixShareable.equals(name)) {
            return name;
        }
    }
    return null;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 43 with NodeTypeIterator

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

the class CanAddChildNodeCallWithNodeTypeTest method testCanAddAbstractType.

/**
     * Tests if <code>NodeType.canAddChildNode(String childNodeName, String nodeTypeName)</code>
     * returns false if <code>nodeTypeName</code> represents an abstract node type.
     */
public void testCanAddAbstractType() throws NotExecutableException, RepositoryException {
    NodeDefinition nodeDef = NodeTypeUtil.locateChildNodeDef(session, false, false, false);
    if (nodeDef == null) {
        throw new NotExecutableException("No testable node type found.");
    }
    NodeType nodeType = nodeDef.getDeclaringNodeType();
    String childNodeName = nodeDef.getName();
    String abstractName = null;
    NodeTypeIterator it = manager.getPrimaryNodeTypes();
    while (it.hasNext() && abstractName == null) {
        NodeType nt = it.nextNodeType();
        if (nt.isAbstract()) {
            abstractName = nt.getName();
        }
    }
    if (abstractName == null) {
        throw new NotExecutableException("No abstract type found.");
    }
    assertFalse("NodeType.canAddChildNode(String childNodeName, String nodeTypeName) " + "must return false if nodeTypeName represents an abstract node type.", nodeType.canAddChildNode(childNodeName, abstractName));
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 44 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator 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 45 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator 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)

Aggregations

NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)65 NodeType (javax.jcr.nodetype.NodeType)53 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)28 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 Node (javax.jcr.Node)9 RepositoryException (javax.jcr.RepositoryException)9 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)8 ArrayList (java.util.ArrayList)6 Session (javax.jcr.Session)6 HashSet (java.util.HashSet)4 Value (javax.jcr.Value)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Property (javax.jcr.Property)2