Search in sources :

Example 61 with NodeType

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

the class LuceneQueryFactory method getComparisonQuery.

protected Query getComparisonQuery(DynamicOperand left, int transform, String operator, StaticOperand rigth, Map<String, NodeType> selectorMap) throws RepositoryException {
    if (left instanceof PropertyValue) {
        PropertyValue pv = (PropertyValue) left;
        String field = npResolver.getJCRName(session.getQName(pv.getPropertyName()));
        int type = PropertyType.UNDEFINED;
        NodeType nt = selectorMap.get(pv.getSelectorName());
        if (nt != null) {
            for (PropertyDefinition pd : nt.getPropertyDefinitions()) {
                if (pd.getName().equals(pv.getPropertyName())) {
                    type = pd.getRequiredType();
                    break;
                }
            }
        }
        return getPropertyValueQuery(field, operator, evaluator.getValue(rigth), type, transform);
    } else if (left instanceof NodeName) {
        return getNodeNameQuery(transform, operator, rigth);
    } else if (left instanceof NodeLocalName) {
        return getNodeLocalNameQuery(transform, operator, rigth);
    } else {
        throw new UnsupportedRepositoryOperationException(// FIXME
        "Unknown operand type: " + left);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) NodeName(javax.jcr.query.qom.NodeName) NodeType(javax.jcr.nodetype.NodeType) PropertyValue(javax.jcr.query.qom.PropertyValue) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Constraint(javax.jcr.query.qom.Constraint) NodeLocalName(javax.jcr.query.qom.NodeLocalName)

Example 62 with NodeType

use of javax.jcr.nodetype.NodeType 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 63 with NodeType

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

the class NodeAddMixinTest method testAddSuccessfully.

/**
 * Tests if <code>Node.addMixin(String mixinName)</code> adds the requested
 * mixin and stores it in property <code>jcr:mixinTypes</code>
 */
public void testAddSuccessfully() throws NotExecutableException, RepositoryException {
    Session session = testRootNode.getSession();
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    String mixinName = NodeMixinUtil.getAddableMixinName(session, node);
    if (mixinName == null) {
        throw new NotExecutableException("No testable mixin node type found");
    }
    node.addMixin(mixinName);
    // test if mixin is written to property jcr:mixinTypes immediately
    Value[] mixinValues = node.getProperty(jcrMixinTypes).getValues();
    boolean found = false;
    for (int i = 0; i < mixinValues.length; i++) {
        found |= mixinName.equals(mixinValues[i].getString());
    }
    if (!found) {
        fail("Mixin type must be added to property " + jcrMixinTypes + " immediately.");
    }
    // it is implementation-specific if a added mixin is available
    // before or after save therefore save before further tests
    testRootNode.getSession().save();
    // test if added mixin is available by node.getMixinNodeTypes()
    NodeType[] mixins = node.getMixinNodeTypes();
    found = false;
    for (int i = 0; i < mixins.length; i++) {
        found |= mixinName.equals(mixins[i].getName());
    }
    if (!found) {
        fail("Mixin '" + mixinName + "' type not added.");
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value) Session(javax.jcr.Session)

Example 64 with NodeType

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

the class AddNodeTest method testAbstractNodeType.

/**
 * Tests if addNode() throws a ConstraintViolationException in case
 * of an abstract node type.
 */
public void testAbstractNodeType() throws RepositoryException {
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = ntMgr.getPrimaryNodeTypes();
    while (nts.hasNext()) {
        NodeType nt = nts.nextNodeType();
        if (nt.isAbstract()) {
            try {
                testRootNode.addNode(nodeName1, nt.getName());
                superuser.save();
                fail("Expected ConstraintViolationException.");
            } catch (ConstraintViolationException e) {
            // correct.
            } finally {
                superuser.refresh(false);
            }
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 65 with NodeType

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

the class NodeDiscoveringNodeTypesTest method testGetMixinNodeTypes.

/**
 * Test if getMixinNodeType returns the node types according to the property
 * "jcr:mixinTypes". Therefore a node with mixin types is located recursively
 * in the entire repository. A NotExecutableException is thrown when no such
 * node is found.
 */
public void testGetMixinNodeTypes() throws NotExecutableException, RepositoryException {
    if (childNode == null) {
        throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node.");
    }
    Node node = locateNodeWithMixinNodeTypes(testRootNode);
    if (node == null) {
        throw new NotExecutableException("Workspace does not contain a node with mixin node types defined");
    }
    Value[] names = node.getProperty(jcrMixinTypes).getValues();
    NodeType[] types = node.getMixinNodeTypes();
    assertEquals("getMixinNodeTypes() does not return the same number of " + "node types as " + "getProperty(\"jcr:mixinTypes\").getValues()", types.length, names.length);
    StringBuffer namesString = new StringBuffer();
    for (int i = 0; i < names.length; i++) {
        namesString.append("|" + names[i].getString() + "|");
    }
    for (int i = 0; i < types.length; i++) {
        String pattern = "|" + types[i].getName() + "|";
        assertTrue("getMixinNodeTypes() does not return the same node" + "types as getProperty(\"jcr:mixinTypes\").getValues()", namesString.indexOf(pattern) != -1);
        assertTrue("All nodes returned by getMixinNodeTypes() must be" + "mixin", types[i].isMixin());
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value)

Aggregations

NodeType (javax.jcr.nodetype.NodeType)272 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)84 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)81 Node (javax.jcr.Node)63 Value (javax.jcr.Value)60 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)55 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)46 NodeDefinition (javax.jcr.nodetype.NodeDefinition)45 RepositoryException (javax.jcr.RepositoryException)41 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 Session (javax.jcr.Session)29 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)16 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)15 Name (org.apache.jackrabbit.spi.Name)15 HashSet (java.util.HashSet)14 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)12 NodeIterator (javax.jcr.NodeIterator)7 Property (javax.jcr.Property)7 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)7