From: <fg...@us...> - 2011-03-13 09:09:13
|
Revision: 3376 http://openutils.svn.sourceforge.net/openutils/?rev=3376&view=rev Author: fgiust Date: 2011-03-13 09:09:06 +0000 (Sun, 13 Mar 2011) Log Message: ----------- extends CRIT-34 also to getItems(class) which uses content2bean Modified Paths: -------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/OrderingTest.java Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java 2011-03-08 10:07:11 UTC (rev 3375) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java 2011-03-13 09:09:06 UTC (rev 3376) @@ -281,6 +281,64 @@ throw new JCRQueryException(statement, e); } + if (applyLocalPaging && itemsPerPage > 0) + { + final int offset = (Math.max(pageNumberStartingFromOne, 1) - 1) * itemsPerPage; + + // removing preceding records + rows.skip(offset); + + // removing folllowing records and alter getSize() + return new ResultIteratorImpl<K>(rows, this.hm) + { + + /** + * {@inheritDoc} + */ + @Override + public boolean hasNext() + { + return super.getPosition() - offset < getSize() && super.hasNext(); + } + + /** + * {@inheritDoc} + */ + @Override + public long getSize() + { + return Math.min(super.getSize() - offset, itemsPerPage); + } + + @SuppressWarnings("unchecked") + @Override + protected K wrap(Row row) + { + try + { + Item jcrNode = JcrCompatUtils.getJCRNode(row); + if (jcrNode == null) + { + return null; + } + + return (K) Content2BeanUtil.toBean( + new AdvancedResultItemImpl(row, jcrNode, this.hm), + true, + theclass); + } + catch (RepositoryException e) + { + throw new RuntimeException(e); + } + catch (Content2BeanException e) + { + throw new RuntimeException(e); + } + } + }; + } + return new ResultIteratorImpl<K>(rows, hm) { Modified: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/OrderingTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/OrderingTest.java 2011-03-08 10:07:11 UTC (rev 3375) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/OrderingTest.java 2011-03-13 09:09:06 UTC (rev 3376) @@ -178,4 +178,42 @@ PAGINATION_LENGTH, "Wrong number of results returned by the iterator when calling hasNext() more than once."); } + + /** + * Retrieves all letters. + * @throws Exception + */ + @Test + public void testWithBeans() throws Exception + { + + Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE); + criteria.setBasePath("/letters"); + criteria.add(Restrictions.eq("@jcr:primaryType", "mgnl:content")); + criteria.setPaging(PAGINATION_LENGTH, 1); + criteria.setForcePagingWithDocumentOrder(true); + + log.debug(criteria.toXpathExpression()); + + AdvancedResult advResult = criteria.execute(); + + Assert.assertEquals(advResult.getTotalSize(), LETTERS_ARRAY.length, "Unset total size."); + + ResultIterator<Content2BeanTest.Page> resultIterator = advResult.getItems(Content2BeanTest.Page.class); + Assert.assertEquals(resultIterator.getSize(), PAGINATION_LENGTH, "Wrong iterator size."); + + int i = 0; + for (Content2BeanTest.Page currentResult : resultIterator) + { + Assert.assertEquals(currentResult.getTitle(), LETTERS_ARRAY[i], "Position " + + i + + ": found " + + currentResult.getTitle() + + " instead of " + + LETTERS_ARRAY[i]); + i++; + } + Assert.assertEquals(i, PAGINATION_LENGTH, "Wrong number of results returned by the iterator."); + + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |