From: <die...@us...> - 2010-05-13 10:25:52
|
Revision: 2441 http://openutils.svn.sourceforge.net/openutils/?rev=2441&view=rev Author: diego_schivo Date: 2010-05-13 10:25:46 +0000 (Thu, 13 May 2010) Log Message: ----------- CRIT-11 javadocs Modified Paths: -------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/CriteriaTest.java Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java 2010-05-13 10:00:56 UTC (rev 2440) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java 2010-05-13 10:25:46 UTC (rev 2441) @@ -35,46 +35,53 @@ * * <pre> * Calendar begin = Calendar.getInstance(); - * begin.set(2004, Calendar.JANUARY, 1); + * begin.set(1999, Calendar.JANUARY, 1); * Calendar end = Calendar.getInstance(); - * end.set(2008, Calendar.DECEMBER, 1); + * end.set(2001, Calendar.DECEMBER, 31); * - * Criteria criteria = JCRCriteriaFactory - * .createCriteria() - * .setWorkspace(ContentRepository.WEBSITE) - * .setBasePath("/dogs") - * .add(Restrictions.contains("@name", "Nana")) - * .add(Restrictions.gt("@weight", Float.valueOf(10))) + * Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE) + * .setBasePath("/pets") + * .add(Restrictions.contains("@title", "Lucky")) + * .add(Restrictions.eq("@petType", "dog")) * .add(Restrictions.between("@birthDate", begin, end)) - * .addOrder(Order.desc("@name")); + * .addOrder(Order.desc("@title")); * </pre> * * will be translated into the following xpath statement * * <pre> - * //dogs//*[((jcr:contains(@name, 'Nana')) and (@weight>10.0) and (@birthDate >=xs:dateTime('2004-01-01T00:00:00.000+00:00') and @birthDate <=xs:dateTime('2008-12-01T23:59:59.000+00:00')))] order by @name descending + * //pets//*[((jcr:contains(@title, 'Lucky')) and (@petType='dog') and (@birthDate >=xs:dateTime('1999-01-01T00:00:00.000+00:00') and @birthDate <=xs:dateTime('2001-12-31T23:59:59.000+00:00')))] order by @title descending * </pre> * - * Furthermore, you may want to have only a subset of the whole result set returned, much like in - * a MySQL limit clause. In this case, you will use the <code>JCRCriteriaFactory.createMgnlCriteriaWithLimit</code> - * factory method. For this to work, the underlying JCR repository implementation must support this feature (Jackrabbit - * 1.4+ does). Here is an example. + * Furthermore, you may want to have only a subset of the whole result set returned, much like in a MySQL limit clause. + * In this case, you will use the <code>setFirstResult</code> and <code>setMaxResults</code> methods. Here is an + * example. * * <pre> - * Collection<Pet> pets = JCRCriteriaFactory.createMgnlCriteriaWithLimit("//dogs//*", MgnlContext.getQueryManager("website"), "mgnl:content", Pet.class).add( - * Restrictions.contains("@name", "Nana")).add( - * Restrictions.gt("@weight", Float.valueOf(10))).add( - * Restrictions.between("@birthDate", begin, end). - * setFirstResult(5). - * setMaxResults(10). - * addOrder(Order.desc("@name")).list(); + * Criteria criteria = JCRCriteriaFactory + * .createCriteria() + * .setWorkspace(ContentRepository.WEBSITE) + * .setBasePath("/pets") + * .add(Restrictions.between("@birthDate", begin, end)) + * .addOrder(Order.asc("@birthDate")) + * .setFirstResult(5) + * .setMaxResults(5); *</pre> * - * Notice the <code>setFirstResult(int)</code> and <code>setMaxResults(int)</code> methods. Now calling - * <code>list()</code> will return a subset of only five <code>Pet</code> objects, starting from the 6th item (counting - * starts from 0). If you dont specify these two calls, <code>list()</code> will behave as usual by returning the entire - * result set. If you only call <code>setMaxResults(int)</code>, the result set will be the subset of elements - * <code>[0, maxResults]</code> (firstResultValue is 0 by default). <br> + * Notice the <code>setFirstResult(int)</code> and <code>setMaxResults(int)</code> methods. Now executing the query will + * return a subset of no more than five results, starting from the 6th item (counting starts from 0). If you dont + * specify these two calls, the entire result set will be returned. If you only call <code>setMaxResults(int)</code>, + * the result set will be the subset of elements <code>[0, maxResults]</code> (firstResultValue is 0 by default).<br> + * Another way to paginate results is by using the <code>setPaging</code> method: + * + * <pre> + * Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE) + * .setBasePath("/pets") + * .add(Restrictions.between("@birthDate", begin, end)) + * .addOrder(Order.asc("@birthDate")) + * .setPaging(5, 2); + *</pre> + * * <br> * A word of warning about implementations returned by <code>JCRCriteriaFactory</code>. They are <strong>NOT</strong> * thread-safe, therefore client code wishing to use one of them as a shared global variable <strong>MUST</strong> @@ -142,7 +149,6 @@ Criteria setBasePath(String path); /** - * * @param itemsPerPage maximum number of results per page (i.e. page size) * @param pageNumberStartingFromOne page number to retrieve (1, 2, 3, ...) * @return this (for method chaining) @@ -150,7 +156,6 @@ Criteria setPaging(int itemsPerPage, int pageNumberStartingFromOne); /** - * * @param spellCheckString * @return this (for method chaining) */ Modified: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/CriteriaTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/CriteriaTest.java 2010-05-13 10:00:56 UTC (rev 2440) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/CriteriaTest.java 2010-05-13 10:25:46 UTC (rev 2441) @@ -79,7 +79,7 @@ @Test public void testToXpathExpression() throws Exception { - Criteria criteria = javadocXpathExampleCriteria(); + Criteria criteria = toXpathExpressionJavadocExampleCriteria(); String expectedStmt = "//pets//*" + "[((jcr:contains(@title, 'Lucky')) and (@petType='dog')" @@ -97,7 +97,7 @@ @Test public void testExecute() throws Exception { - Criteria criteria = javadocXpathExampleCriteria(); + Criteria criteria = toXpathExpressionJavadocExampleCriteria(); AdvancedResult result = criteria.execute(); Assert.assertEquals(1, result.getTotalSize()); @@ -108,14 +108,42 @@ } /** + * @return + */ + private Criteria toXpathExpressionJavadocExampleCriteria() + { + Calendar begin = Calendar.getInstance(); + begin.set(1999, Calendar.JANUARY, 1); + Calendar end = Calendar.getInstance(); + end.set(2001, Calendar.DECEMBER, 31); + + Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE).setBasePath( + "/pets").add(Restrictions.contains("@title", "Lucky")).add(Restrictions.eq("@petType", "dog")).add( + Restrictions.between("@birthDate", begin, end)).addOrder(Order.desc("@title")); + return criteria; + } + + /** * Tests pagination of results. * @throws Exception */ @Test - public void testSetPaging() throws Exception + public void testSetFirstResultAndMaxResults() throws Exception { - Criteria criteria = javadocPaginationExampleCriteria(); + Calendar begin = Calendar.getInstance(); + begin.set(1999, Calendar.JANUARY, 1); + Calendar end = Calendar.getInstance(); + end.set(2001, Calendar.DECEMBER, 31); + Criteria criteria = JCRCriteriaFactory + .createCriteria() + .setWorkspace(ContentRepository.WEBSITE) + .setBasePath("/pets") + .add(Restrictions.between("@birthDate", begin, end)) + .addOrder(Order.asc("@birthDate")) + .setFirstResult(5) + .setMaxResults(5); + AdvancedResult result = criteria.execute(); // first page: // --- 9 (title=Lucky, petType=bird, birthDate=1999-08-06) @@ -137,9 +165,11 @@ } /** - * @return + * Tests pagination of results. + * @throws Exception */ - private Criteria javadocXpathExampleCriteria() + @Test + public void testSetPaging() throws Exception { Calendar begin = Calendar.getInstance(); begin.set(1999, Calendar.JANUARY, 1); @@ -147,26 +177,28 @@ end.set(2001, Calendar.DECEMBER, 31); Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE).setBasePath( - "/pets").add(Restrictions.contains("@title", "Lucky")).add(Restrictions.eq("@petType", "dog")).add( - Restrictions.between("@birthDate", begin, end)).addOrder(Order.desc("@title")); - return criteria; - } - - /** - * @return - */ - private Criteria javadocPaginationExampleCriteria() - { - Calendar begin = Calendar.getInstance(); - begin.set(1999, Calendar.JANUARY, 1); - Calendar end = Calendar.getInstance(); - end.set(2001, Calendar.DECEMBER, 31); - - Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE).setBasePath( "/pets").add(Restrictions.between("@birthDate", begin, end)).addOrder(Order.asc("@birthDate")).setPaging( 5, 2); - return criteria; + + AdvancedResult result = criteria.execute(); + // first page: + // --- 9 (title=Lucky, petType=bird, birthDate=1999-08-06) + // --- 6 (title=George, petType=snake, birthDate=2000-01-20) + // --- 4 (title=Jewel, petType=dog, birthDate=2000-03-07) + // --- 11 (title=Freddy, petType=bird, birthDate=2000-03-09) + // --- 12 (title=Lucky, petType=dog, birthDate=2000-06-24) + // second page: + // --- 1 (title=Leo, petType=cat, birthDate=2000-09-07) + // --- 5 (title=Iggy, petType=lizard, birthDate=2000-11-30) + // --- 3 (title=Rosy, petType=dog, birthDate=2001-04-17) + Assert.assertEquals(8, result.getTotalSize()); + + ResultIterator<AdvancedResultItem> iterator = result.getItems(); + Assert.assertEquals(3, iterator.getSize()); + Assert.assertEquals("1", iterator.next().getName()); + Assert.assertEquals("5", iterator.next().getName()); + Assert.assertEquals("3", iterator.next().getName()); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |