Search in sources :

Example 56 with ItemNotFoundException

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

Example 57 with ItemNotFoundException

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

the class XATest method testAddNodeRollback.

/**
     * Add a node inside a transaction and rollback changes.
     * @throws Exception
     */
public void testAddNodeRollback() throws Exception {
    // get user transaction object
    UserTransaction utx = new UserTransactionImpl(superuser);
    // start transaction
    utx.begin();
    // add node and save
    Node n = testRootNode.addNode(nodeName1, testNodeType);
    n.addMixin(mixReferenceable);
    testRootNode.save();
    // assertion: node exists in this session
    String uuid = n.getUUID();
    try {
        superuser.getNodeByUUID(uuid);
    } catch (ItemNotFoundException e) {
        fail("New node not visible after save()");
    }
    // rollback
    utx.rollback();
    // assertion: node does not exist in this session
    try {
        superuser.getNodeByUUID(uuid);
        fail("Node still visible after rollback()");
    } catch (ItemNotFoundException e) {
    /* expected */
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Node(javax.jcr.Node) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 58 with ItemNotFoundException

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

the class OverlappingNodeAddTest method testWith2Folders.

/**
     * Performs add operations on a single node (no SNS) through 2 sessions
     */
public void testWith2Folders() throws Exception {
    boolean bWasSaved = false;
    String testpath = testfolder.getPath();
    Node f1 = s1.getNode(testpath).addNode("folder", "nt:folder");
    Node c1 = f1.addNode("a", "nt:file");
    Node r1 = c1.addNode("jcr:content", "nt:resource");
    r1.setProperty("jcr:data", "foo");
    Node f2 = s2.getNode(testpath).addNode("folder", "nt:folder");
    Node c2 = f2.addNode("b", "nt:file");
    Node r2 = c2.addNode("jcr:content", "nt:resource");
    r2.setProperty("jcr:data", "bar");
    s1.save();
    String s1FolderId = f1.getIdentifier();
    try {
        s2.save();
        bWasSaved = true;
    } catch (InvalidItemStateException ex) {
        // retry; adding refresh doesn't change anything here
        try {
            s2.save();
            bWasSaved = true;
        } catch (InvalidItemStateException ex2) {
        // we would be cool with this
        }
    }
    // we don't have changes in s1, so the keepChanges flag should be
    // irrelevant
    s1.refresh(false);
    // be nice and get a new Node instance
    Node newf1 = s1.getNode(testpath + "/folder");
    // if bWasSaved it should now be visible to Session 1
    assertEquals("'b' was saved, so session 1 should see it", bWasSaved, newf1.hasNode("b"));
    // 'a' was saved by Session 1 earlier on
    if (!newf1.hasNode("a")) {
        String message = "child node 'a' not present";
        if (bWasSaved && !s1FolderId.equals(newf1.getIdentifier())) {
            message += ", and also the folder's identifier changed from " + s1FolderId + " to " + newf1.getIdentifier();
        }
        Node oldf1 = null;
        try {
            oldf1 = s1.getNodeByIdentifier(s1FolderId);
        } catch (ItemNotFoundException ex) {
            message += "; node with id " + s1FolderId + " can't be retrieved using getNodeByIdentifier either";
        }
        if (oldf1 != null) {
            try {
                oldf1.getPath();
            } catch (Exception ex) {
                message += "; node with id " + s1FolderId + " can be retrieved using getNodeByIdentifier, but getPath() fails with: " + ex.getMessage();
            }
        }
        fail(message);
    }
}
Also used : InvalidItemStateException(javax.jcr.InvalidItemStateException) Node(javax.jcr.Node) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemNotFoundException(javax.jcr.ItemNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 59 with ItemNotFoundException

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

the class GetPersistentQueryPathLevel1Test method testGetStoredQueryPath.

/**
     * Tests if a non-persistent query throws an {@link ItemNotFoundException}
     * when {@link Query#getStoredQueryPath()} is called.
     */
public void testGetStoredQueryPath() throws RepositoryException {
    String statement = "/" + jcrRoot;
    Query q = session.getWorkspace().getQueryManager().createQuery(statement, qsXPATH);
    try {
        q.getStoredQueryPath();
        fail("Query.getStoredQueryPath() on a transient query must throw an ItemNotFoundException.");
    } catch (ItemNotFoundException e) {
    // success
    }
}
Also used : Query(javax.jcr.query.Query) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 60 with ItemNotFoundException

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

the class WorkspaceItemStateFactory method createPropertyState.

/**
     * Creates the PropertyState with information retrieved from the <code>RepositoryService</code>.
     */
public PropertyState createPropertyState(PropertyId propertyId, PropertyEntry entry) throws ItemNotFoundException, RepositoryException {
    try {
        // Get item info from cache and use it if up to date
        Entry<PropertyInfo> cached = cache.getPropertyInfo(propertyId);
        ItemInfo info;
        if (isUpToDate(cached, entry)) {
            info = cached.info;
        } else {
            // otherwise retrieve item info from service and cache the whole batch
            Iterator<? extends ItemInfo> infos = service.getItemInfos(sessionInfo, propertyId);
            info = first(infos, cache, entry.getGeneration());
            if (info == null || info.denotesNode()) {
                throw new ItemNotFoundException("PropertyId: " + propertyId);
            }
        }
        assertMatchingPath(info, entry);
        return createPropertyState((PropertyInfo) info, entry);
    } catch (PathNotFoundException e) {
        throw new ItemNotFoundException(e);
    }
}
Also used : ItemInfo(org.apache.jackrabbit.spi.ItemInfo) PathNotFoundException(javax.jcr.PathNotFoundException) PropertyInfo(org.apache.jackrabbit.spi.PropertyInfo) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

ItemNotFoundException (javax.jcr.ItemNotFoundException)139 RepositoryException (javax.jcr.RepositoryException)61 Node (javax.jcr.Node)44 Path (org.apache.jackrabbit.spi.Path)25 PathNotFoundException (javax.jcr.PathNotFoundException)23 NodeId (org.apache.jackrabbit.core.id.NodeId)22 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)16 IOException (java.io.IOException)14 InvalidItemStateException (javax.jcr.InvalidItemStateException)14 NodeState (org.apache.jackrabbit.core.state.NodeState)14 Name (org.apache.jackrabbit.spi.Name)14 AccessDeniedException (javax.jcr.AccessDeniedException)13 HttpResponse (org.apache.http.HttpResponse)13 DavException (org.apache.jackrabbit.webdav.DavException)13 ItemExistsException (javax.jcr.ItemExistsException)12 Session (javax.jcr.Session)12 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)11 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)10 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)10