Search in sources :

Example 6 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class DefaultHandler method getContentNode.

/**
     * Retrieves/creates the node that will be used to import properties and
     * data. In case of a non-collection this includes and additional content node
     * to be created beside the 'file' node.<br>
     * Please note: If the jcr:content node already exists and contains child
     * nodes, those will be removed in order to make sure, that the import
     * really replaces the existing content of the file-node.
     *
     * @param context
     * @param isCollection
     * @return
     * @throws RepositoryException
     */
protected Node getContentNode(ImportContext context, boolean isCollection) throws RepositoryException {
    Node parentNode = (Node) context.getImportRoot();
    String name = context.getSystemId();
    if (parentNode.hasNode(name)) {
        parentNode = parentNode.getNode(name);
    } else {
        String ntName = (isCollection) ? getCollectionNodeType() : getNodeType();
        parentNode = parentNode.addNode(name, ntName);
    }
    Node contentNode = null;
    if (isCollection) {
        contentNode = parentNode;
    } else {
        if (parentNode.hasNode(JcrConstants.JCR_CONTENT)) {
            contentNode = parentNode.getNode(JcrConstants.JCR_CONTENT);
            // check if nodetype is compatible (might be update of an existing file)
            if (contentNode.isNodeType(getContentNodeType()) || !forceCompatibleContentNodes()) {
                // includes properties (DefaultHandler) and nodes (e.g. ZipHandler)
                if (contentNode.hasNodes()) {
                    NodeIterator it = contentNode.getNodes();
                    while (it.hasNext()) {
                        it.nextNode().remove();
                    }
                }
            } else {
                contentNode.remove();
                contentNode = null;
            }
        }
        if (contentNode == null) {
            // when the underlying repository allows it to be used
            if (parentNode.getPrimaryNodeType().canAddChildNode(JcrConstants.JCR_CONTENT, getContentNodeType())) {
                contentNode = parentNode.addNode(JcrConstants.JCR_CONTENT, getContentNodeType());
            } else {
                contentNode = parentNode.addNode(JcrConstants.JCR_CONTENT);
            }
        }
    }
    return contentNode;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node)

Example 7 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class PropertyTypeTest method typeCheckChildren.

private void typeCheckChildren(Node parentNode) throws RepositoryException {
    NodeIterator nodes = parentNode.getNodes();
    while (nodes.hasNext()) {
        Node node = nodes.nextNode();
        PropertyIterator props = node.getProperties();
        while (props.hasNext()) {
            Property prop = props.nextProperty();
            int reqType = prop.getDefinition().getRequiredType();
            int type = PropertyType.UNDEFINED;
            boolean isEmptyMultipleArray = false;
            if (prop.getDefinition().isMultiple()) {
                if (prop.getValues().length > 0) {
                    type = prop.getValues()[0].getType();
                } else {
                    isEmptyMultipleArray = true;
                }
            } else {
                type = prop.getValue().getType();
            }
            if (!isEmptyMultipleArray && reqType != PropertyType.UNDEFINED) {
                assertFalse("The type of a property must not " + "be UNDEFINED", type == PropertyType.UNDEFINED);
                assertEquals("The type of a property has to match " + "the type of the property definition.", type, reqType);
            }
        }
        typeCheckChildren(node);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 8 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class PropertyUtil method searchMultivalProp.

/**
     * Helper method to find a multivalue property.
     *
     * @param node the node to start the search from.
     * @return a multivalue property or null if not found any.
     */
public static Property searchMultivalProp(Node node) throws RepositoryException {
    Property multiVal = null;
    for (PropertyIterator props = node.getProperties(); props.hasNext(); ) {
        Property property = props.nextProperty();
        if (property.getDefinition().isMultiple()) {
            multiVal = property;
            break;
        }
    }
    if (multiVal == null) {
        for (NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
            Node n = nodes.nextNode();
            multiVal = searchMultivalProp(n);
            if (multiVal != null) {
                break;
            }
        }
    }
    return multiVal;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 9 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class PropertyUtil method searchMultivalProp.

/**
     * Helper method to find a multivalue property of a given type.
     *
     * @param node the node to start the search from.
     * @param type the property type.
     * @return a multivalue property or null if not found any.
     */
public static Property searchMultivalProp(Node node, int type) throws RepositoryException {
    Property multiVal = null;
    for (PropertyIterator props = node.getProperties(); props.hasNext(); ) {
        Property property = props.nextProperty();
        if (property.getDefinition().isMultiple() && property.getType() == type) {
            multiVal = property;
            break;
        }
    }
    if (multiVal == null) {
        for (NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
            Node n = nodes.nextNode();
            multiVal = searchMultivalProp(n, type);
            if (multiVal != null) {
                break;
            }
        }
    }
    return multiVal;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 10 with NodeIterator

use of javax.jcr.NodeIterator in project jackrabbit by apache.

the class SelectorTest method testSelector.

public void testSelector() throws RepositoryException {
    // make sure there's at least one node with this node type
    testRootNode.addNode(nodeName1, testNodeType);
    superuser.save();
    QueryObjectModel qom = qf.createQuery(qf.selector(testNodeType, "s"), null, null, null);
    forQOMandSQL2(qom, new Callable() {

        public Object call(Query query) throws RepositoryException {
            QueryResult result = query.execute();
            String[] names = result.getSelectorNames();
            assertNotNull(names);
            assertEquals(1, names.length);
            assertEquals("s", names[0]);
            NodeIterator it = result.getNodes();
            while (it.hasNext()) {
                assertTrue("Wrong node type", it.nextNode().isNodeType(testNodeType));
            }
            return null;
        }
    });
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) RepositoryException(javax.jcr.RepositoryException)

Aggregations

NodeIterator (javax.jcr.NodeIterator)307 Node (javax.jcr.Node)214 Session (javax.jcr.Session)55 QueryResult (javax.jcr.query.QueryResult)52 RepositoryException (javax.jcr.RepositoryException)40 Query (javax.jcr.query.Query)40 Test (org.junit.Test)36 QueryManager (javax.jcr.query.QueryManager)34 PropertyIterator (javax.jcr.PropertyIterator)30 ArrayList (java.util.ArrayList)26 Property (javax.jcr.Property)24 Version (javax.jcr.version.Version)23 NoSuchElementException (java.util.NoSuchElementException)19 Value (javax.jcr.Value)19 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)19 HashSet (java.util.HashSet)13 PathNotFoundException (javax.jcr.PathNotFoundException)12 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)11 NodeImpl (org.apache.jackrabbit.core.NodeImpl)11 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)11