Package org.apache.ojb.broker.accesslayer

Examples of org.apache.ojb.broker.accesslayer.OJBIterator


     * @return OJBIterator
     */
    private OJBIterator getReportQueryIteratorFromQuery(Query query, ClassDescriptor cld) throws PersistenceBrokerException
    {
        RsIteratorFactory factory = ReportRsIteratorFactoryImpl.getInstance();
        OJBIterator result = getRsIteratorFromQuery(query, cld, factory);

        if (query.usePaging())
        {
            result = new PagingIterator(result, query.getStartAtIndex(), query.getEndAtIndex());
        }
View Full Code Here


    {
        if (log.isDebugEnabled()) log.debug("getCollectionByQuery (" + collectionClass + ", " + itemClass + ", " + query + ")");

        ClassDescriptor cld = pb.getClassDescriptor(itemClass);
        ManageableCollection result = null;
        OJBIterator iter = null;
        boolean isRetrievalTasksCreated = (batchRetrieval && (m_retrievalTasks == null));
        int fullSize = -1;
        int size = 0;

        if (isRetrievalTasksCreated)
        {
            // Maps ReferenceDescriptors to HashSets of owners
            m_retrievalTasks = new HashMap();
        }

        try
        {
            result = (ManageableCollection) collectionClass.newInstance();
            // now iterate over all elements and add them to the new collection
            iter = pb.getIteratorFromQuery(query, cld);

            // BRJ : get fullSizefor Query
            // to be removed when Query.fullSize is removed
            if (iter instanceof PagingIterator)
            {
                fullSize = iter.fullSize();
            }

            while (iter.hasNext())
            {
                Object candidate = iter.next();

                /**
                 * MBAIRD
                 * candidate CAN be null in the case of materializing from an iterator based
                 * on a query for a class that is mapped to a table that has other classes
                 * mapped to that table as well, but aren't extents.
                 */
                if (candidate != null)
                {
                    IndirectionHandler handler = ProxyHelper.getIndirectionHandler(candidate);

                    if ((handler != null)
                            || itemClass.isAssignableFrom(candidate.getClass()))
                    {
                        result.ojbAdd(candidate);

                        // BRJ: count added objects
                        // to be removed when Query.fullSize is removed
                        size++;
                    }
                    else
                    {
                        //warn the user
                        log.warn("Candidate object ["+candidate
                                    +"] class ["+candidate.getClass().getName()
                                    +"] is not a subtype of ["+itemClass.getName()
                                    +"] or any type of proxy. NOT INCLUDED in result collection");
                    }
                    if (prefetchProxies && (handler != null)
                            && (cld.getProxyPrefetchingLimit() > 0)
                            && addRetrievalTask(candidate, this))
                    {
                        new PBMaterializationListener(candidate, m_retrievalTasks,
                                this, cld.getProxyPrefetchingLimit());
                    }
                }
            }


            if (isRetrievalTasksCreated)
            {
                // turn off auto prefetching for related proxies
                Class saveClassToPrefetch = classToPrefetch;
                classToPrefetch = null;
                performRetrievalTasks();
                classToPrefetch = saveClassToPrefetch;
            }
        }
        catch (InstantiationException ex)
        {
            log.error(ex);
            throw new PersistenceBrokerException(ex);
        }
        catch (IllegalAccessException ex)
        {
            log.error(ex);
            throw new PersistenceBrokerException(ex);
        }
        finally
        {
            if (iter != null)
            {
                iter.releaseDbResources();
            }
            if (isRetrievalTasksCreated)
            {
                m_retrievalTasks = null;
            }
View Full Code Here

        // 1. set query start equals end
        query.setStartAtIndex(4);
        query.setEndAtIndex(4);

        // 2. check if only one item is found
        OJBIterator ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
        assertEquals("size of iterator should be one", 1, ojbIter.size());
        InterfaceArticle test4 = (InterfaceArticle) ojbIter.next();
        ojbIter.releaseDbResources();
        assertTrue("Id should be a number of the generated articles", range.containsInteger(test4.getArticleId()));
    }
View Full Code Here

    /**
     * Size returned by Iterator must be same as size of Collection
     */
    public void testIteratorSize()
    {
        OJBIterator ojbIter;
        Criteria crit;
        QueryByCriteria query;
        int collSize;
        int iterSize;

        crit = new Criteria();
        query = QueryFactory.newQuery(Article.class, crit);

        collSize = broker.getCollectionByQuery(query).size();

        ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
        iterSize = ojbIter.size();

        assertEquals("collSize == iterSize", collSize , iterSize);
        ojbIter.releaseDbResources();
    }
View Full Code Here

        ojbIter.releaseDbResources();
    }

    public void testPaging()
    {
        OJBIterator ojbIter;
        Criteria crit;
        QueryByCriteria query;

        // All Articles index in range
        crit = new Criteria();
        query = QueryFactory.newQuery(Article.class, crit);
        int fullSize = broker.getCollectionByQuery(query).size();

        query.setStartAtIndex(10);
        query.setEndAtIndex(14);
        ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
        assertEquals("index 10 - 14 expecting 5 rows", 5,ojbIter.size());
        ojbIter.releaseDbResources();
    }
View Full Code Here

            a.setStock(i);
            broker.store(a);
        }
        broker.commitTransaction();

        OJBIterator ojbIter;
        Criteria crit;
        QueryByCriteria query;
        Collection fullColl, pagedColl;
        InterfaceArticle article;
       
        // All Articles index in range
        crit = new Criteria();
        crit.addEqualTo("articleName", name);
        query = QueryFactory.newQuery(Article.class, crit);
        query.addOrderByAscending("stock");
        fullColl = broker.getCollectionByQuery(query);
        assertEquals(20, fullColl.size());

        // limited query
        query.setStartAtIndex(10);
        query.setEndAtIndex(14);
        pagedColl = broker.getCollectionByQuery(query);
                     
        ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
       
        assertEquals("collection- and iterator-size must match", pagedColl.size(), ojbIter.size());
        assertEquals("index 10 - 14 expecting 5 rows", 5,ojbIter.size());
       
        ojbIter.absolute(2);
        article = (InterfaceArticle)ojbIter.next();
        assertEquals("Article stock=12", article.getStock(), 12);
       
        ojbIter.relative(-1);
        article = (InterfaceArticle)ojbIter.next();
        assertEquals("Article id=12", article.getStock(), 12);

        ojbIter.relative(-1);
        article = (InterfaceArticle)ojbIter.next();
        assertEquals("Article id=12", article.getStock(), 12);
       
        // last
        ojbIter.absolute(12);
        article = (InterfaceArticle)ojbIter.next();
        assertEquals("Article id=15", article.getStock(), 15);

        // first
        ojbIter.absolute(-12);
        article = (InterfaceArticle)ojbIter.next();
        assertEquals("Article id=10", article.getStock(), 10);
       
        ojbIter.releaseDbResources();
    }
View Full Code Here

        ojbIter.releaseDbResources();
    }
   
    public void testPagingIndicesOutOfRange()
    {
        OJBIterator ojbIter;
        Criteria crit;
        QueryByCriteria query;
        int fullSize;

        // All Articles index out of  range
        crit = new Criteria();
        query = QueryFactory.newQuery(Article.class, crit);
        fullSize = broker.getCollectionByQuery(query).size();

        query.setStartAtIndex(fullSize + 5);
        query.setEndAtIndex(fullSize + 14);
        ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
        assertEquals("indices out of range expecting 0 rows", 0,ojbIter.size());
        ojbIter.releaseDbResources();
    }
View Full Code Here

        ojbIter.releaseDbResources();
    }

    public void testPagingEndIndexOutOfRange()
    {
        OJBIterator ojbIter;
        Criteria crit;
        QueryByCriteria query;
        int fullSize;

        // All Articles index out of  range
        crit = new Criteria();
        query = QueryFactory.newQuery(Article.class, crit);
        fullSize = broker.getCollectionByQuery(query).size();

        query.setStartAtIndex(fullSize - 9);
        query.setEndAtIndex(fullSize + 9);
        ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
        assertEquals("end index out of range expecting 10 rows", 10,ojbIter.size());
        ojbIter.releaseDbResources();
    }
View Full Code Here

        ojbIter.releaseDbResources();
    }

    public void testPagingEmptyIterator()
    {
        OJBIterator ojbIter;
        Criteria crit;
        QueryByCriteria query;

        // looking for inexistent Article
        crit = new Criteria();
        crit.addEqualTo("articleId",new Integer(-777));
        query = QueryFactory.newQuery(Article.class, crit);
        int fullSize = broker.getCollectionByQuery(query).size();

        query.setStartAtIndex(10);
        query.setEndAtIndex(14);
        ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
        assertEquals("index 10 - 14 expecting 0 rows for empty iterator", 0,ojbIter.size());
        ojbIter.releaseDbResources();
    }
View Full Code Here

    {
        if (log.isDebugEnabled()) log.debug("getCollectionByQuery (" + collectionClass + ", " + itemClass + ", " + query + ")");

        ClassDescriptor cld = pb.getClassDescriptor(itemClass);
        ManageableCollection result = null;
        OJBIterator iter = null;
        int fullSize = -1;
        int size = 0;

        final boolean isRetrievalTasksCreated = batchRetrieval && m_retrievalTasks == null;
        if (isRetrievalTasksCreated)
        {
            // Maps ReferenceDescriptors to HashSets of owners
            m_retrievalTasks = new HashMap();
        }

        // ==> enable materialization cache
        pb.getInternalCache().enableMaterializationCache();
        try
        {
            result = (ManageableCollection) collectionClass.newInstance();
            // now iterate over all elements and add them to the new collection
            iter = pb.getIteratorFromQuery(query, cld);

            // BRJ : get fullSizefor Query
            // to be removed when Query.fullSize is removed
            if (iter instanceof PagingIterator)
            {
                fullSize = iter.fullSize();
            }

            while (iter.hasNext())
            {
                Object candidate = iter.next();

                /**
                 * MBAIRD
                 * candidate CAN be null in the case of materializing from an iterator based
                 * on a query for a class that is mapped to a table that has other classes
                 * mapped to that table as well, but aren't extents.
                 */
                if (candidate != null)
                {
                    IndirectionHandler handler = ProxyHelper.getIndirectionHandler(candidate);

                    if ((handler != null)
                            || itemClass.isAssignableFrom(candidate.getClass()))
                    {
                        result.ojbAdd(candidate);

                        // BRJ: count added objects
                        // to be removed when Query.fullSize is removed
                        size++;
                    }
                    else
                    {
                        //warn the user
                        log.warn("Candidate object ["+candidate
                                    +"] class ["+candidate.getClass().getName()
                                    +"] is not a subtype of ["+itemClass.getName()
                                    +"] or any type of proxy. NOT INCLUDED in result collection");
                    }
                    if (prefetchProxies && (handler != null)
                            && (cld.getProxyPrefetchingLimit() > 0)
                            && addRetrievalTask(candidate, this))
                    {
                        new PBMaterializationListener(candidate, m_retrievalTasks,
                                this, cld.getProxyPrefetchingLimit());
                    }
                }
            }

            if (isRetrievalTasksCreated)
            {
                // turn off auto prefetching for related proxies
                final Class saveClassToPrefetch = classToPrefetch;
                classToPrefetch = null;
                try
                {
                    performRetrievalTasks();
                }
                finally
                {
                    classToPrefetch = saveClassToPrefetch;
                }
            }

            // ==> disable materialization cache
            pb.getInternalCache().disableMaterializationCache();
        }
        catch(RuntimeException e)
        {
            // ==> clear materialization cache
            pb.getInternalCache().doLocalClear();
            throw e;
        }
        catch (InstantiationException ex)
        {
            // ==> clear materialization cache
            pb.getInternalCache().doLocalClear();
            log.error(ex);
            throw new PersistenceBrokerException(ex);
        }
        catch (IllegalAccessException ex)
        {
            pb.getInternalCache().doLocalClear();
            log.error(ex);
            throw new PersistenceBrokerException(ex);
        }
        finally
        {
            if (iter != null)
            {
                iter.releaseDbResources();
            }
            if (isRetrievalTasksCreated)
            {
                m_retrievalTasks = null;
            }
View Full Code Here

TOP

Related Classes of org.apache.ojb.broker.accesslayer.OJBIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.