Search in sources :

Example 86 with NodeTypeManager

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

the class NodeTypeUtil method locateAllChildNodeDef.

/**
     * Locate all non-protected child node def declared by a non-abstract node type
     * parsing all node types
     *
     * @param session                  the session to access the node types
     * @param regardDefaultPrimaryType if true, the default primary type of the
     *                                 returned <code>NodeDef</code> is
     *                                 according to param <code>defaultPrimaryType</code>.
     *                                 If false, the returned <code>NodeDef</code>
     *                                 might have a default primary type or
     *                                 not.
     * @param defaultPrimaryType       if <code>regardDefaultPrimaryType</code>
     *                                 is true: if true, the returned
     *                                 <code>NodeDef</code> has a default
     *                                 primary type, else not
     * @param residual                 if true, the returned <code>NodeDef</code>
     *                                 is of the residual name "*", else not
     * @return
     * @throws RepositoryException
     */
public static List<NodeDefinition> locateAllChildNodeDef(Session session, boolean regardDefaultPrimaryType, boolean defaultPrimaryType, boolean residual) throws RepositoryException {
    List<NodeDefinition> nodeTypes = new ArrayList<NodeDefinition>();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator types = manager.getAllNodeTypes();
    boolean skip = false;
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        // node types with more than one residual child node definition
        // will cause trouble in test cases. the implementation
        // might pick another definition than the definition returned by
        // this method, when a child node is set.
        NodeDefinition[] childDefs = type.getChildNodeDefinitions();
        int residuals = 0;
        for (int i = 0; i < childDefs.length; i++) {
            if (childDefs[i].getName().equals("*")) {
                residuals++;
            }
        }
        if (residuals > 1) {
            // more than one residual, not suitable for tests
            continue;
        }
        NodeDefinition[] nodeDefs = type.getDeclaredChildNodeDefinitions();
        for (int i = 0; i < nodeDefs.length; i++) {
            NodeDefinition nodeDef = nodeDefs[i];
            if (nodeDef.getDeclaringNodeType().isAbstract()) {
                continue;
            }
            if (nodeDef.isProtected()) {
                continue;
            }
            if (nodeDef.getRequiredPrimaryTypes().length > 1) {
                // of primary node types is not specified
                continue;
            }
            if (regardDefaultPrimaryType) {
                if (defaultPrimaryType && nodeDef.getDefaultPrimaryType() == null) {
                    continue;
                }
                if (!defaultPrimaryType && nodeDef.getDefaultPrimaryType() != null) {
                    continue;
                }
            }
            if (residual && !nodeDef.getName().equals("*")) {
                continue;
            }
            if (!residual) {
                // if another child node def is a residual definition
                // skip the current node type
                NodeDefinition[] nodeDefsAll = type.getChildNodeDefinitions();
                for (int j = 0; j < nodeDefsAll.length; j++) {
                    if (nodeDefsAll[j].getName().equals("*")) {
                        skip = true;
                        break;
                    }
                }
                if (skip) {
                    // break the loop of the current child not defs
                    skip = false;
                    break;
                }
            }
            nodeTypes.add(nodeDef);
        }
    }
    return nodeTypes;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 87 with NodeTypeManager

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

the class ColumnTest method testExpandColumnsForNodeType.

/**
     * From the spec:
     * <p>
     * If propertyName is not specified, a column is included for each
     * single-valued non-residual property of the node type specified by the
     * nodeType attribute of the selector selectorName.
     * <p>
     * [..] If propertyName is not specified,
     * columnName must not be specified, and the included columns will be
     * named "selectorName.propertyName".
     */
public void testExpandColumnsForNodeType() throws RepositoryException {
    QueryObjectModel qom = qf.createQuery(qf.selector(testNodeType, SELECTOR_1), null, null, new Column[] { qf.column(SELECTOR_1, null, null) });
    forQOMandSQL2(qom, new Callable() {

        public Object call(Query query) throws RepositoryException {
            QueryResult result = query.execute();
            NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
            NodeType nt = ntMgr.getNodeType(testNodeType);
            PropertyDefinition[] propDefs = nt.getPropertyDefinitions();
            Set<String> names = new HashSet<String>();
            for (int i = 0; i < propDefs.length; i++) {
                PropertyDefinition propDef = propDefs[i];
                if (!propDef.isMultiple() && !propDef.getName().equals("*")) {
                    String columnName = SELECTOR_1 + "." + propDef.getName();
                    names.add(columnName);
                }
            }
            for (String columnName : result.getColumnNames()) {
                names.remove(columnName);
            }
            assertTrue("Missing required column(s): " + names, names.isEmpty());
            return null;
        }
    });
}
Also used : QueryResult(javax.jcr.query.QueryResult) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Set(java.util.Set) HashSet(java.util.HashSet) Query(javax.jcr.query.Query) NodeType(javax.jcr.nodetype.NodeType) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 88 with NodeTypeManager

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

the class SelectorTest method testUnknownNodeType.

public void testUnknownNodeType() throws RepositoryException {
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    String ntName = testNodeType;
    for (; ; ) {
        try {
            ntMgr.getNodeType(ntName);
            ntName += "x";
        } catch (NoSuchNodeTypeException e) {
            break;
        }
    }
    try {
        qf.createQuery(qf.selector(ntName, "s"), null, null, null).execute();
        fail("Selector with unknown node type must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
    try {
        String stmt = "SELECT * FROM [" + ntName + "] AS nt";
        qm.createQuery(stmt, Query.JCR_SQL2).execute();
        fail("Selector with unknown node type must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) InvalidQueryException(javax.jcr.query.InvalidQueryException) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException)

Example 89 with NodeTypeManager

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

the class SameNodeTest method testChildNodesDoNotMatchSelector.

public void testChildNodesDoNotMatchSelector() throws RepositoryException, NotExecutableException {
    testRootNode.addNode(nodeName1, testNodeType);
    superuser.save();
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator it = ntMgr.getPrimaryNodeTypes();
    NodeType testNt = ntMgr.getNodeType(testNodeType);
    while (it.hasNext()) {
        NodeType nt = it.nextNodeType();
        if (!testNt.isNodeType(nt.getName())) {
            // perform test
            QueryObjectModel qom = qf.createQuery(qf.selector(nt.getName(), "s"), qf.sameNode("s", testRoot + "/" + nodeName1), null, null);
            checkQOM(qom, new Node[] {});
            return;
        }
    }
    throw new NotExecutableException("No suitable node type found to " + "perform test against '" + testNodeType + "' nodes");
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel)

Example 90 with NodeTypeManager

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

the class NodeTypeUtil method locateChildNodeDef.

/**
     * Locate a non-protected child node def declared by a non-abstract node type
     * parsing all node types
     *
     * @param session                  the session to access the node types
     * @param regardDefaultPrimaryType if true, the default primary type of the
     *                                 returned <code>NodeDef</code> is
     *                                 according to param <code>defaultPrimaryType</code>.
     *                                 If false, the returned <code>NodeDef</code>
     *                                 might have a default primary type or
     *                                 not.
     * @param defaultPrimaryType       if <code>regardDefaultPrimaryType</code>
     *                                 is true: if true, the returned
     *                                 <code>NodeDef</code> has a default
     *                                 primary type, else not
     * @param residual                 if true, the returned <code>NodeDef</code>
     *                                 is of the residual name "*", else not
     * @return
     * @throws RepositoryException
     */
public static NodeDefinition locateChildNodeDef(Session session, boolean regardDefaultPrimaryType, boolean defaultPrimaryType, boolean residual) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator types = manager.getAllNodeTypes();
    boolean skip = false;
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        // node types with more than one residual child node definition
        // will cause trouble in test cases. the implementation
        // might pick another definition than the definition returned by
        // this method, when a child node is set.
        NodeDefinition[] childDefs = type.getChildNodeDefinitions();
        int residuals = 0;
        for (int i = 0; i < childDefs.length; i++) {
            if (childDefs[i].getName().equals("*")) {
                residuals++;
            }
        }
        if (residuals > 1) {
            // more than one residual, not suitable for tests
            continue;
        }
        NodeDefinition[] nodeDefs = type.getDeclaredChildNodeDefinitions();
        for (int i = 0; i < nodeDefs.length; i++) {
            NodeDefinition nodeDef = nodeDefs[i];
            if (nodeDef.getDeclaringNodeType().isAbstract()) {
                continue;
            }
            if (nodeDef.isProtected()) {
                continue;
            }
            if (nodeDef.getRequiredPrimaryTypes().length > 1) {
                // of primary node types is not specified
                continue;
            }
            if (regardDefaultPrimaryType) {
                if (defaultPrimaryType && nodeDef.getDefaultPrimaryType() == null) {
                    continue;
                }
                if (!defaultPrimaryType && nodeDef.getDefaultPrimaryType() != null) {
                    continue;
                }
            }
            if (residual && !nodeDef.getName().equals("*")) {
                continue;
            }
            if (!residual) {
                // if another child node def is a residual definition
                // skip the current node type
                NodeDefinition[] nodeDefsAll = type.getChildNodeDefinitions();
                for (int j = 0; j < nodeDefsAll.length; j++) {
                    if (nodeDefsAll[j].getName().equals("*")) {
                        skip = true;
                        break;
                    }
                }
                if (skip) {
                    // break the loop of the current child not defs
                    skip = false;
                    break;
                }
            }
            return nodeDef;
        }
    }
    return null;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeDefinition(javax.jcr.nodetype.NodeDefinition) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

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