From: <fg...@us...> - 2010-01-09 14:03:44
|
Revision: 1618 http://openutils.svn.sourceforge.net/openutils/?rev=1618&view=rev Author: fgiust Date: 2010-01-09 14:03:37 +0000 (Sat, 09 Jan 2010) Log Message: ----------- CRIT-4 Adds a public API to get the translated xpath statement + tests! Modified Paths: -------------- trunk/openutils-mgnlcriteria/pom.xml 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/criterion/Junction.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractCriteriaImpl.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractMagnoliaCriteriaImpl.java Added Paths: ----------- trunk/openutils-mgnlcriteria/src/test/java/net/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java Modified: trunk/openutils-mgnlcriteria/pom.xml =================================================================== --- trunk/openutils-mgnlcriteria/pom.xml 2010-01-09 14:00:53 UTC (rev 1617) +++ trunk/openutils-mgnlcriteria/pom.xml 2010-01-09 14:03:37 UTC (rev 1618) @@ -1,4 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.sourceforge.openutils</groupId> @@ -62,6 +63,12 @@ <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.7</version> + <scope>test</scope> + </dependency> </dependencies> <repositories> <repository> 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-01-09 14:00:53 UTC (rev 1617) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java 2010-01-09 14:03:37 UTC (rev 1618) @@ -37,7 +37,7 @@ * begin.set(2004, Calendar.JANUARY, 1); * Calendar end = Calendar.getInstance(); * end.set(2008, Calendar.DECEMBER, 1); - * + * * Collection pets = JCRCriteriaFactory.createMgnlCriteria("//dogs//*", MgnlContext.getQueryManager("website"), "nt:base").add( * Restrictions.contains("@name", "Nana")).add( * Restrictions.gt("@weight", Float.valueOf(10))).add( @@ -76,11 +76,11 @@ * * Content nodes returned by the above query will be automatically converted to and populate instances of the * <code>Pet</code> type. 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. + * 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. * - *<pre> + * <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( @@ -92,16 +92,17 @@ * * 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><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> coordinate access - * to it. These objects are actually meant to be instantiated and used within a method scope (e.g. a service method), - * where no concurrent issues arise. <br> + * 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> * <br> - * This API was inspired by Hibernate's Criteria API. <br><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> + * coordinate access to it. These objects are actually meant to be instantiated and used within a method scope (e.g. a + * service method), where no concurrent issues arise. <br> + * <br> + * This API was inspired by Hibernate's Criteria API. <br> + * <br> * @see JCRCriteriaFactory#createMgnlCriteria(String, info.magnolia.cms.core.search.QueryManager, String) * @see JCRCriteriaFactory#createMgnlCriteria(String, info.magnolia.cms.core.search.QueryManager, String, Class) * @see JCRCriteriaFactory#createMgnlCriteriaWithLimit(String, info.magnolia.cms.core.search.QueryManager, String) @@ -120,7 +121,7 @@ * @param criterion The {@link Criterion criterion} object representing the restriction to be applied. * @return this (for method chaining) */ - public Criteria add(Criterion criterion); + Criteria add(Criterion criterion); /** * Add an {@link Order ordering} to the result set. Only <strong>one Order criterion per query</strong> can be @@ -128,21 +129,21 @@ * @param order The {@link Order order} object representing an ordering to be applied to the results. * @return this (for method chaining) */ - public Criteria addOrder(Order order); + Criteria addOrder(Order order); /** * Set a limit upon the number of objects to be retrieved. * @param maxResults the maximum number of results * @return this (for method chaining) */ - public Criteria setMaxResults(int maxResults); + Criteria setMaxResults(int maxResults); /** * Set the first result to be retrieved. * @param firstResult the first result to retrieve, numbered from <tt>0</tt> * @return this (for method chaining) */ - public Criteria setFirstResult(int firstResult); + Criteria setFirstResult(int firstResult); /* * Set a timeout for the underlying query. @@ -155,5 +156,11 @@ * null</strong> * @return The Collection of matched query results. */ - public Collection< ? > list() throws JCRQueryException; + Collection< ? > list() throws JCRQueryException; + + /** + * Returns the generated xpath expression + * @return the generated xpath expression + */ + String toXpathExpression(); } \ No newline at end of file Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java 2010-01-09 14:00:53 UTC (rev 1617) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java 2010-01-09 14:03:37 UTC (rev 1618) @@ -65,15 +65,27 @@ { if (criteria.size() == 0) + { return ""; + } StringBuilder buffer = new StringBuilder().append('('); Iterator iter = criteria.iterator(); + + boolean isfirst = true; while (iter.hasNext()) { - buffer.append(((Criterion) iter.next()).toXPathString(crit)); - if (iter.hasNext()) - buffer.append(' ').append(op).append(' '); + String xPathString = ((Criterion) iter.next()).toXPathString(crit); + if (StringUtils.isNotBlank(xPathString)) + { + if (!isfirst && StringUtils.isNotBlank(xPathString)) + { + buffer.append(' ').append(op).append(" "); + } + buffer.append(xPathString); + isfirst = false; + } + } return buffer.append(')').toString(); } Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractCriteriaImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractCriteriaImpl.java 2010-01-09 14:00:53 UTC (rev 1617) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractCriteriaImpl.java 2010-01-09 14:03:37 UTC (rev 1618) @@ -205,4 +205,5 @@ return order.toString(); } } + } \ No newline at end of file Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractMagnoliaCriteriaImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractMagnoliaCriteriaImpl.java 2010-01-09 14:00:53 UTC (rev 1617) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractMagnoliaCriteriaImpl.java 2010-01-09 14:03:37 UTC (rev 1618) @@ -21,6 +21,8 @@ import info.magnolia.cms.core.Content; import info.magnolia.cms.core.ItemType; import info.magnolia.cms.core.search.QueryManager; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRMagnoliaCriteriaQueryTranslator; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.XPathSelect; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.utils.XPathTextUtils; import org.apache.commons.lang.StringUtils; @@ -59,4 +61,18 @@ // defaults to info.magnolia.cms.core.Content this.classType = classType == null ? Content.class : classType; } + + /** + * @return + */ + public String toXpathExpression() + { + JCRMagnoliaCriteriaQueryTranslator translator = new JCRMagnoliaCriteriaQueryTranslator(this); + XPathSelect statement = new XPathSelect(); + statement.setRoot(this.path); + statement.setPredicate(translator.getPredicate()); + statement.setOrderByClause(translator.getOrderBy()); + String stmt = statement.toStatementString(); + return stmt; + } } \ No newline at end of file Added: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java 2010-01-09 14:03:37 UTC (rev 1618) @@ -0,0 +1,71 @@ +/** + * + * Copyright Openmind http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl; + +import junit.framework.Assert; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRCriteriaFactory; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Junction; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Restrictions; + +import org.junit.Test; + + +/** + * @author fgiust + * @version $Id$ + */ +public class MagnoliaCriteriaTest +{ + + @Test + public void testSimple() + { + Criteria criteria = JCRCriteriaFactory.createMgnlCriteria("//site//*", null, "mgnl:content"); + + Junction conjunction = Restrictions.conjunction(); + criteria.add(conjunction); + conjunction.add(Restrictions.eq("@property", "test")); + conjunction.add(Restrictions.eq("@anotherproperty", "anothertest")); + + String xpathExpression = criteria.toXpathExpression(); + Assert.assertEquals( + "//site//*[(( (@property='test') and (@anotherproperty='anothertest') ) )] ", + xpathExpression); + + } + + /** + * Test for CRIT-3 + */ + @Test + public void testEmptyConjuntion() + { + Criteria criteria = JCRCriteriaFactory.createMgnlCriteria("//site//*", null, "mgnl:content"); + + criteria.add(Restrictions.eq("@property", "test")); + + Junction conjunction = Restrictions.conjunction(); + criteria.add(conjunction); + + String xpathExpression = criteria.toXpathExpression(); + Assert.assertEquals("//site//*[( (@property='test') )] ", xpathExpression); + + } +} Property changes on: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |