Search in sources :

Example 56 with NodeType

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

the class NodeStateMergerTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    Reader cnd = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(TEST_NODETYPES));
    CndImporter.registerNodeTypes(cnd, superuser);
    cnd.close();
    NodeTypeIterator it = superuser.getWorkspace().getNodeTypeManager().getMixinNodeTypes();
    while (it.hasNext()) {
        NodeType nt = it.nextNodeType();
        if (nt.getName().startsWith("test:")) {
            testMixins.add(nt.getName());
        }
    }
    testNode = testRootNode.addNode(nodeName1, "nt:unstructured");
    superuser.save();
    sessionB = getHelper().getSuperuserSession();
    testNodeB = sessionB.getNode(testNode.getPath());
}
Also used : InputStreamReader(java.io.InputStreamReader) NodeType(javax.jcr.nodetype.NodeType) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 57 with NodeType

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

the class NodeStateMergerTest method testMixinRemovedInSessionA.

// ---------------- Tests removing jcr:mixinType property in the SessionA ---
/**
 * Remove the jcr:mixinType property by removing all mixin types. Merge
 * to changes made in the overlayed state should fail.
 * @throws Exception
 */
public void testMixinRemovedInSessionA() throws Exception {
    for (int i = 1; i <= 5; i++) {
        testNode.addMixin("test:mixinProp_" + i);
    }
    superuser.save();
    testNodeB.refresh(false);
    // remove all mixin types
    for (NodeType mixin : testNode.getMixinNodeTypes()) {
        testNode.removeMixin(mixin.getName());
    }
    assertFalse(testNode.hasProperty("jcr:mixinTypes"));
    testNodeB.addNode(nodeName1, "nt:unstructured");
    testNodeB.setProperty(propertyName1, "anyValue");
    sessionB.save();
    try {
        superuser.save();
        fail();
    } catch (InvalidItemStateException e) {
    // expected
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) NodeType(javax.jcr.nodetype.NodeType)

Example 58 with NodeType

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

the class NodeSetPrimaryTypeTest method testSetAbstractAsPrimaryType.

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

Example 59 with NodeType

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

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

the class LuceneQueryFactory method execute.

/**
 * @param columns
 * @param selector
 * @param constraint
 * @param externalSort
 *            if <code>true</code> it means that the lqf should just let the
 *            QueryEngine take care of sorting and applying applying offset
 *            and limit constraints
 * @param offsetIn
 *            used in pagination
 * @param limitIn
 *            used in pagination
 * @return a list of rows
 * @throws RepositoryException
 * @throws IOException
 */
public List<Row> execute(Map<String, PropertyValue> columns, Selector selector, Constraint constraint, Sort sort, boolean externalSort, long offsetIn, long limitIn) throws RepositoryException, IOException {
    final IndexReader reader = index.getIndexReader(true);
    final int offset = offsetIn < 0 ? 0 : (int) offsetIn;
    final int limit = limitIn < 0 ? Integer.MAX_VALUE : (int) limitIn;
    QueryHits hits = null;
    try {
        JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader, index.getContext().getItemStateManager());
        searcher.setSimilarity(index.getSimilarity());
        Predicate filter = Predicate.TRUE;
        BooleanQuery query = new BooleanQuery();
        QueryPair qp = new QueryPair(query);
        query.add(create(selector), MUST);
        if (constraint != null) {
            String name = selector.getSelectorName();
            NodeType type = ntManager.getNodeType(selector.getNodeTypeName());
            filter = mapConstraintToQueryAndFilter(qp, constraint, Collections.singletonMap(name, type), searcher, reader);
        }
        List<Row> rows = new ArrayList<Row>();
        // TODO depending on the filters, we could push the offset info
        // into the searcher
        hits = searcher.evaluate(qp.mainQuery, sort, offset + limit);
        int currentNode = 0;
        int addedNodes = 0;
        ScoreNode node = hits.nextScoreNode();
        while (node != null) {
            Row row = null;
            try {
                row = new SelectorRow(columns, evaluator, selector.getSelectorName(), session.getNodeById(node.getNodeId()), node.getScore());
            } catch (ItemNotFoundException e) {
            // skip the node
            }
            if (row != null && filter.evaluate(row)) {
                if (externalSort) {
                    // return everything and not worry about sort
                    rows.add(row);
                } else {
                    // apply limit and offset rules locally
                    if (currentNode >= offset && currentNode - offset < limit) {
                        rows.add(row);
                        addedNodes++;
                    }
                    currentNode++;
                    // end the loop when going over the limit
                    if (addedNodes == limit) {
                        break;
                    }
                }
            }
            node = hits.nextScoreNode();
        }
        return rows;
    } finally {
        if (hits != null) {
            hits.close();
        }
        Util.closeOrRelease(reader);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) Constraint(javax.jcr.query.qom.Constraint) Predicate(org.apache.jackrabbit.commons.predicate.Predicate) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) NodeType(javax.jcr.nodetype.NodeType) IndexReader(org.apache.lucene.index.IndexReader) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) Row(javax.jcr.query.Row) ItemNotFoundException(javax.jcr.ItemNotFoundException)

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