From: <fg...@us...> - 2009-02-20 17:43:20
|
Revision: 1047 http://openutils.svn.sourceforge.net/openutils/?rev=1047&view=rev Author: fgrilli Date: 2009-02-20 17:43:14 +0000 (Fri, 20 Feb 2009) Log Message: ----------- sharing project Added Paths: ----------- trunk/openutils-mgnlcriteria/pom.xml trunk/openutils-mgnlcriteria/src/ trunk/openutils-mgnlcriteria/src/main/ trunk/openutils-mgnlcriteria/src/main/java/ trunk/openutils-mgnlcriteria/src/main/java/net/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ 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/JCRCriteriaFactory.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRQueryException.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/BetweenExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Conjunction.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Criterion.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Disjunction.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNotNullExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNullExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/JCRFunctionExpression.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/criterion/LikeExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LogicalExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/MatchMode.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/NotExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Order.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Restrictions.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/SimpleExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/XPathSelect.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/ 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 trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaImpl.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaWithLimitImpl.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/XPathTextUtils.java trunk/openutils-mgnlcriteria/src/site/ trunk/openutils-mgnlcriteria/src/site/apt/ trunk/openutils-mgnlcriteria/src/site/apt/index.apt trunk/openutils-mgnlcriteria/src/site/apt/usage.apt trunk/openutils-mgnlcriteria/src/site/changes/ trunk/openutils-mgnlcriteria/src/site/site.xml trunk/openutils-mgnlcriteria/src/test/ trunk/openutils-mgnlcriteria/src/test/java/ Property Changed: ---------------- trunk/openutils-mgnlcriteria/ Property changes on: trunk/openutils-mgnlcriteria ___________________________________________________________________ Added: svn:ignore + .settings target .checkstyle .classpath .project Added: trunk/openutils-mgnlcriteria/pom.xml =================================================================== --- trunk/openutils-mgnlcriteria/pom.xml (rev 0) +++ trunk/openutils-mgnlcriteria/pom.xml 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,35 @@ +<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> + <artifactId>openutils</artifactId> + <version>10</version> + <relativePath>..</relativePath> + </parent> + <artifactId>openutils-mgnlcriteria</artifactId> + <name>openutils-mgnlcriteria</name> + <version>1.0-SNAPSHOT</version> + <description>A Hibernate's Criteria-like API to programmatically generate JCR queries with Magnolia</description> + <dependencies> + <dependency> + <groupId>info.magnolia</groupId> + <artifactId>magnolia-core</artifactId> + <version>3.6.3</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.5.0</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl104-over-slf4j</artifactId> + <version>1.5.0</version> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.4</version> + </dependency> + </dependencies> +</project> \ No newline at end of file Property changes on: trunk/openutils-mgnlcriteria/pom.xml ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: 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 (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,95 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query; + + +import java.util.Collection; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Criterion; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Order; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Restrictions; + + +/** + * <tt>Criteria</tt> is a simplified API for retrieving JCR Nodes by composing <tt>Criterion</tt> objects. This is a + * very convenient approach for functionality like "search" screens where there is a variable number of conditions to be + * placed upon the result set.<br> + * The <tt>JCRCriteriaFactory</tt> is a factory for <tt>Criteria</tt>. <tt>Criterion</tt> instances are usually obtained + * via the factory methods on <tt>Restrictions</tt>. eg. + * + * <pre> + * Calendar begin = Calendar.getInstance(); + * begin.set(2004, 0, 1); + * Calendar end = Calendar.getInstance(); + * end.set(2008, 11, 1); + * + * Collection pets = JCRCriteriaFactory.createMgnlCriteria("//dogs//*", MgnlContext.getQueryManager("website"), "nt:base").add( + * Restrictions.contains("@name", "Nana")).add( + * Restrictions.gt("@weight", new Float(10))).add( + * Restrictions.between("@birthDate", begin, end).addOrder( + * Order.desc("@jcr:score()")).list(); + * </pre> + * + * will be translated (if an xpath Criteria implementation is chosen) 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 @jcr:score() + * // descending + * </pre> + * + * This API is blatantly inspired by and respectfully borrows code from 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 Restrictions + * @see Order + * @see Criteria + * @author Federico Grilli + * @version $Id$ + */ +public interface Criteria +{ + + /** + * Add a {@link Criterion restriction} to constrain the results to be retrieved. + * @param criterion The {@link Criterion criterion} object representing the restriction to be applied. + * @return this (for method chaining) + */ + public Criteria add(Criterion criterion); + + /** + * Add an {@link Order ordering} to the result set. Only <strong>one Order criterion per query</strong> can be + * applied. Any Order added after the first one will be ignored. + * @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); + + /** + * 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); + + /** + * 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); + + /** + * Set a timeout for the underlying query. + * @param timeout The timeout value to apply. + * @return this (for method chaining) FIXME is it possible in JCR? + */ + public Criteria setTimeout(int timeout); + + /** + * Get the results. <strong>The implementation should guarantee the caller that the returned Collection is never + * null</strong> + * @return The Collection of matched query results. + */ + public Collection< ? > list() throws JCRQueryException; +} \ No newline at end of file Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRCriteriaFactory.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRCriteriaFactory.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRCriteriaFactory.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,58 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.AbstractMagnoliaCriteriaImpl; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.MagnoliaCriteriaImpl; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.MagnoliaCriteriaWithLimitImpl; + + +/** + * @author fgrilli + * @version $Id$ + */ +public class JCRCriteriaFactory +{ + + /** + * A factory for Magnolia CMS specific Criteria implementation + * @param path String - the path in JCR repository to search for Nodes. This is the path which precedes the predicate + * @param queryManager - an instance of info.magnolia.cms.core.search.QueryManager + * @param itemType String - if not specified defaults to "mgnl:content" + * @return an instance of a concrete subclass of {@link AbstractMagnoliaCriteriaImpl} + * @see MagnoliaCriteriaImpl + * @see MagnoliaCriteriaWithLimitImpl + */ + public static Criteria createMgnlCriteria(String path, info.magnolia.cms.core.search.QueryManager queryManager, + String itemType) + { + return new MagnoliaCriteriaImpl(path, queryManager, itemType); + } + + /** + * A factory for Magnolia CMS specific Criteria implementation + * @param path String - the path in JCR repository to search for Nodes. This is the path which precedes the predicate + * @param queryManager - an instance of info.magnolia.cms.core.search.QueryManager + * @param itemType String - if not specified defaults to "mgnl:content" + * @param classType Class<?> - the list method of this implementation must return instances of this type + * @return an instance of a concrete subclass of {@link AbstractMagnoliaCriteriaImpl} + * @see MagnoliaCriteriaImpl + * @see MagnoliaCriteriaWithLimitImpl + */ + public static Criteria createMgnlCriteria(String path, info.magnolia.cms.core.search.QueryManager queryManager, + String itemType, Class< ? > classType) + { + return new MagnoliaCriteriaImpl(path, queryManager, itemType, classType); + } + + /** + * @param path String - the path in JCR repository to search for Nodes. This is the path which precedes the + * predicate + * @param queryManager - an instance of javax.jcr.query.QueryManager + * @param itemType String - depends on the implementation + * @return an instance of a JCR-specific implementation of {@link Criteria} + * @throws UnsupportedOperationException as it is not implemented yet + */ + public static Criteria createJCRCriteria(String path, javax.jcr.query.QueryManager queryManager, String itemType) + { + throw new UnsupportedOperationException(); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRCriteriaFactory.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,63 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query; + + +import java.util.Iterator; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.AbstractCriteriaImpl; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.AbstractMagnoliaCriteriaImpl; + + +/** + * @author Federico Grilli + * @version $Id$ + */ +public class JCRMagnoliaCriteriaQueryTranslator +{ + + private final AbstractMagnoliaCriteriaImpl criteria; + + public JCRMagnoliaCriteriaQueryTranslator(final AbstractMagnoliaCriteriaImpl criteria) throws JCRQueryException + { + + this.criteria = criteria; + } + + public AbstractCriteriaImpl getRootCriteria() + { + return criteria; + } + + public String getPredicate() + { + StringBuilder condition = new StringBuilder(30); + Iterator<AbstractMagnoliaCriteriaImpl.CriterionEntry> criterionIterator = criteria.iterateExpressionEntries(); + while (criterionIterator.hasNext()) + { + AbstractMagnoliaCriteriaImpl.CriterionEntry entry = criterionIterator.next(); + String xpathString = entry.getCriterion().toXPathString(entry.getCriteria()); + condition.append(xpathString); + if (criterionIterator.hasNext()) + { + condition.append(" and "); + } + } + return condition.toString(); + } + + public String getOrderBy() + { + StringBuilder orderBy = new StringBuilder(30); + Iterator<AbstractMagnoliaCriteriaImpl.OrderEntry> criterionIterator = criteria.iterateOrderings(); + while (criterionIterator.hasNext()) + { + AbstractMagnoliaCriteriaImpl.OrderEntry oe = criterionIterator.next(); + orderBy.append(oe.getOrder().toXPathString(oe.getCriteria())); + break; + // ignore the rest + /* + * if ( criterionIterator.hasNext() ) { orderBy.append( ", " ); } + */ + } + return orderBy.toString(); + } +} \ No newline at end of file Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRQueryException.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRQueryException.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRQueryException.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,31 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query; + +import org.apache.commons.lang.exception.NestableRuntimeException; + + +/** + * Runtime exception wrapper for jcr checked exceptions. + * @author fgrilli + * @version $Id$ + */ +public class JCRQueryException extends NestableRuntimeException +{ + + private static final long serialVersionUID = -8737641628360563743L; + + public JCRQueryException(String msg, Throwable cause) + { + super(msg, cause); + } + + public JCRQueryException(String msg) + { + super(msg); + } + + public JCRQueryException(Throwable cause) + { + super(cause); + } + +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRQueryException.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/BetweenExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/BetweenExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/BetweenExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,87 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import java.util.Calendar; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + +/** + * @author fgrilli + * @version $Id$ + */ +public class BetweenExpression implements Criterion +{ + + private static final long serialVersionUID = 6686395240415024541L; + + private final String propertyName; + + private final Object lo; + + private final Object hi; + + protected BetweenExpression(String propertyName, Object lo, Object hi) + { + this.propertyName = propertyName; + this.lo = lo; + this.hi = hi; + } + + @Override + public String toString() + { + return propertyName + " between " + lo + " and " + hi; + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + StringBuilder fragment = new StringBuilder(); + fragment.append(" (").append(propertyName).append(" >= "); + + if (lo instanceof String && hi instanceof String) + fragment.append("'" + lo + "' and " + propertyName + " <= '" + hi + "'"); + else if (lo instanceof Number && hi instanceof Number) + fragment.append(lo + " and " + propertyName + " <= " + hi); + else if (lo instanceof Calendar && hi instanceof Calendar) + { + Calendar cal = (Calendar) lo; + Calendar cal2 = (Calendar) hi; + StringBuilder date = new StringBuilder(); + date.append(XS_DATETIME_FUNCTION + "('"); + date + .append(cal.get(Calendar.YEAR)) + .append(HYPHEN) + .append( + cal.get(Calendar.MONTH) < MONTH_MAX + ? "0" + (cal.get(Calendar.MONTH) + 1) + : cal.get(Calendar.MONTH) + 1) + .append(HYPHEN) + .append( + cal.get(Calendar.DAY_OF_MONTH) < DAY_MAX ? "0" + cal.get(Calendar.DAY_OF_MONTH) : cal + .get(Calendar.DAY_OF_MONTH)); + date.append(MIDNIGHT); + date.append("') "); + + StringBuilder date2 = new StringBuilder(); + date2.append(XS_DATETIME_FUNCTION + "('"); + date2.append(cal2.get(Calendar.YEAR)).append(HYPHEN).append( + cal2.get(Calendar.MONTH) < MONTH_MAX + ? "0" + (cal2.get(Calendar.MONTH) + 1) + : cal2.get(Calendar.MONTH) + 1).append(HYPHEN).append( + cal2.get(Calendar.DAY_OF_MONTH) < DAY_MAX ? "0" + cal2.get(Calendar.DAY_OF_MONTH) : cal2 + .get(Calendar.DAY_OF_MONTH)); + date2.append(T235959); + date2.append("') "); + fragment.append(date.toString() + " and " + propertyName + " <= " + date2.toString()); + } + else + { + String msg = "values provided are not of the accepted types String, Number, Calendar"; + log.error(msg); + throw new IllegalArgumentException(msg); + } + fragment.append(")"); + log.debug("xpathString is {} ", fragment); + return fragment.toString(); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/BetweenExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Conjunction.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Conjunction.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Conjunction.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,15 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +/** + * @author fgrilli + * @version $Id$ + */ +public class Conjunction extends Junction +{ + private static final long serialVersionUID = 7590346442271897522L; + + public Conjunction() + { + super("and"); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Conjunction.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Criterion.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Criterion.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Criterion.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,61 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + + +import java.io.Serializable; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * An object-oriented representation of a query criterion that may be used as a restriction in a <tt>Criteria</tt> + * query. Built-in criterion types are provided by the <tt>Restrictions</tt> factory class. This interface might be + * implemented by application classes that define custom restriction criteria. + * @see Restrictions + * @see Criteria + * @author Federico Grilli + * @version $Id$ + */ +public interface Criterion extends Serializable +{ + + public static final Logger log = LoggerFactory.getLogger(Criterion.class); + + public static final String ATTRIBUTE_SELECTOR = "@"; + + public static final String JCR_PREFIX = "jcr:"; + + public static final String JCR_ROOT = "/jcr:root"; + + public static final String ALL_ELEMENTS = "//*"; + + public static final String ANY_PROPERTY = "."; + + public static final String XS_DATETIME_FUNCTION = "xs:dateTime"; + + public static final String MIDNIGHT = "T00:00:00.000+00:00"; + + public static final String T235959 = "T23:59:59.000+00:00"; + + public static final String NT_BASE = "nt:base"; + + public static final String NOT = " not("; + + public static final String HYPHEN = "-"; + + public static final int DAY_MAX = 10; + + public static final int MONTH_MAX = 9; + + /** + * Render the XPath fragment + * @param criteria + * @return String + * @throws JCRQueryException + */ + public String toXPathString(Criteria criteria) throws JCRQueryException; + +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Criterion.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Disjunction.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Disjunction.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Disjunction.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,15 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +/** + * @author Federico Grilli + * @version $Id$ + */ +public class Disjunction extends Junction +{ + private static final long serialVersionUID = 1573336261639362776L; + + protected Disjunction() + { + super("or"); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Disjunction.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNotNullExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNotNullExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNotNullExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,35 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * @author fgrilli + * @version $Id$ + */ +public class IsNotNullExpression implements Criterion +{ + + private static final long serialVersionUID = -1955669039985775985L; + + protected static final String WHITESPACE = " "; + + private String nodeName; + + public IsNotNullExpression(String nodeName) + { + this.nodeName = nodeName; + } + + @Override + public String toString() + { + return nodeName; + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + return WHITESPACE + nodeName + WHITESPACE; + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNotNullExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNullExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNullExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNullExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,36 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * @author fgrilli + * @version $Id$ + */ +public class IsNullExpression implements Criterion +{ + + private static final long serialVersionUID = -1600960388638847909L; + + private String nodeName; + + public IsNullExpression(String nodeName) + { + this.nodeName = nodeName; + } + + @Override + public String toString() + { + return nodeName; + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + StringBuilder fragment = new StringBuilder(); + fragment.append(Criterion.NOT).append(nodeName).append(") "); + log.debug("xpathString is {} ", fragment); + return fragment.toString(); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/IsNullExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/JCRFunctionExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/JCRFunctionExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/JCRFunctionExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,49 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * @author fgrilli + * @version $Id$ + */ +public class JCRFunctionExpression implements Criterion +{ + + private static final long serialVersionUID = -5570839091762158385L; + + protected final String propertyName; + + protected final Object value; + + protected final String function; + + public JCRFunctionExpression(String propertyName, Object value, String function) + { + this.propertyName = propertyName; + this.value = value; + this.function = function; + } + + protected final String getFunction() + { + return function; + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + StringBuilder fragment = new StringBuilder(); + fragment.append(" (" + function + "("); + fragment.append(propertyName); + fragment.append(", '" + value).append("') ) "); + log.debug("xpathString is {} ", fragment); + return fragment.toString(); + } + + @Override + public String toString() + { + return propertyName + " " + function + " " + value; + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/JCRFunctionExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: 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 (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,72 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + +import org.apache.commons.lang.StringUtils; + + +/** + * A sequence of a logical expressions combined by some associative logical operator + * @author Federico Grilli + * @version $Id$ + */ +public class Junction implements Criterion +{ + + private static final long serialVersionUID = 4745761472724863693L; + + @SuppressWarnings("unchecked") + private final List criteria = new ArrayList(); + + private final String op; + + protected Junction(String op) + { + this.op = op; + } + + @SuppressWarnings("unchecked") + public Junction add(Criterion criterion) + { + criteria.add(criterion); + return this; + } + + public String getOp() + { + return op; + } + + @SuppressWarnings("unchecked") + public String toXPathString(Criteria crit) throws JCRQueryException + { + + if (criteria.size() == 0) + return ""; + + StringBuilder buffer = new StringBuilder().append('('); + Iterator iter = criteria.iterator(); + while (iter.hasNext()) + { + buffer.append(((Criterion) iter.next()).toXPathString(crit)); + if (iter.hasNext()) + buffer.append(' ').append(op).append(' '); + } + return buffer.append(')').toString(); + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() + { + return '(' + StringUtils.join(criteria.iterator(), ' ' + op + ' ') + ')'; + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LikeExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LikeExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LikeExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,36 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * @author fgrilli + * @version $Id$ + */ +public class LikeExpression extends JCRFunctionExpression +{ + + private static final long serialVersionUID = 1810624472706401714L; + + private MatchMode matchMode; + + public LikeExpression(String propertyName, Object value, String function, MatchMode matchMode) + { + super(propertyName, value, function); + if (matchMode == null) + throw new IllegalArgumentException("MatchMode can't be null"); + this.matchMode = matchMode; + } + + @Override + public String toXPathString(Criteria criteria) throws JCRQueryException + { + StringBuilder fragment = new StringBuilder(); + fragment.append(" (" + function + "("); + fragment.append(propertyName); + fragment.append(", '" + matchMode.toMatchString(value.toString()) + "') ) "); + log.debug("xpathString is {} ", fragment); + return fragment.toString(); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LikeExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LogicalExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LogicalExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LogicalExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,51 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * Superclass of binary logical expressions + * @author Federico Grilli + * @version $Id$ + */ +public class LogicalExpression implements Criterion +{ + + /** + * + */ + private static final long serialVersionUID = 4524284746715983618L; + + private final Criterion lhs; + + private final Criterion rhs; + + private final String op; + + protected LogicalExpression(Criterion lhs, Criterion rhs, String op) + { + this.lhs = lhs; + this.rhs = rhs; + this.op = op; + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + + String fragment = '(' + lhs.toXPathString(criteria) + ' ' + getOp() + ' ' + rhs.toXPathString(criteria) + ')'; + log.debug("xpathString is {} ", fragment); + return fragment; + } + + public String getOp() + { + return op; + } + + @Override + public String toString() + { + return lhs.toString() + ' ' + getOp() + ' ' + rhs.toString(); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LogicalExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/MatchMode.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/MatchMode.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/MatchMode.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,85 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + + +/** + * Represents an strategy for matching strings using "like". + * @author Federico Grilli + */ +@SuppressWarnings("serial") +public abstract class MatchMode implements Serializable +{ + + private static final long serialVersionUID = -7446324572335777782L; + + private final String name; + + private static final Map<String, MatchMode> INSTANCES = new HashMap<String, MatchMode>(); + + protected MatchMode(String name) + { + this.name = name; + } + + public String toString() + { + return name; + } + + /** + * Match the start of the string to the pattern + */ + public static final MatchMode START = new MatchMode("START") + { + + public String toMatchString(String pattern) + { + return pattern + '%'; + } + }; + + /** + * Match the end of the string to the pattern + */ + public static final MatchMode END = new MatchMode("END") + { + + public String toMatchString(String pattern) + { + return '%' + pattern; + } + }; + + /** + * Match the pattern anywhere in the string + */ + public static final MatchMode ANYWHERE = new MatchMode("ANYWHERE") + { + + public String toMatchString(String pattern) + { + return '%' + pattern + '%'; + } + }; + + static + { + INSTANCES.put(END.name, END); + INSTANCES.put(START.name, START); + INSTANCES.put(ANYWHERE.name, ANYWHERE); + } + + private Object readResolve() + { + return INSTANCES.get(name); + } + + /** + * convert the pattern, by appending/prepending "%" + */ + public abstract String toMatchString(String pattern); + +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/MatchMode.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/NotExpression.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/NotExpression.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/NotExpression.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,36 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * @author fgrilli + * @version $Id$ + */ +public class NotExpression implements Criterion +{ + + private static final long serialVersionUID = -5057676844499041929L; + + private Criterion expression; + + public NotExpression(Criterion expression) + { + this.expression = expression; + } + + @Override + public String toString() + { + return expression.toString(); + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + StringBuilder fragment = new StringBuilder(Criterion.NOT); + fragment.append(expression.toXPathString(criteria)).append(") "); + log.debug("xpathString is {} ", fragment); + return fragment.toString(); + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/NotExpression.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Order.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Order.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Order.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,68 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; + + +/** + * Represents an order imposed upon a <tt>Criteria</tt> result set + * @author Federico Grilli + */ +public class Order implements Criterion +{ + + /** + * + */ + private static final long serialVersionUID = -1228583450961430360L; + + private boolean ascending; + + private String nodeName; + + @Override + public String toString() + { + return nodeName + ' ' + (ascending ? "ascending" : "descending"); + } + + /** + * Constructor for Order. + */ + protected Order(String nodeName, boolean ascending) + { + this.nodeName = nodeName; + this.ascending = ascending; + } + + public String toXPathString(Criteria criteria) throws JCRQueryException + { + StringBuilder fragment = new StringBuilder(" "); + fragment.append(nodeName); + fragment.append(ascending ? " ascending" : " descending"); + + log.debug("xpathString is {} ", fragment); + return fragment.toString(); + + } + + /** + * Ascending order + * @param propertyName + * @return Order + */ + public static Order asc(String propertyName) + { + return new Order(propertyName, true); + } + + /** + * Descending order + * @param propertyName + * @return Order + */ + public static Order desc(String propertyName) + { + return new Order(propertyName, false); + } +} \ No newline at end of file Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Order.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Restrictions.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Restrictions.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Restrictions.java 2009-02-20 17:43:14 UTC (rev 1047) @@ -0,0 +1,337 @@ +package net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion; + + +import java.util.Calendar; +import java.util.Collection; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.utils.XPathTextUtils; + + +/** + * The <tt>criterion</tt> package may be used by applications as a framework for building new kinds of + * <tt>Criterion</tt>. However, it is intended that most applications will simply use the built-in criterion types via + * the static factory methods of this class. + * @see Criteria + * @author Federico Grilli + * @version $Id$ + */ +public class Restrictions +{ + + Restrictions() + { + // cannot be instantiated + } + + /** + * Apply an "equal" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static SimpleExpression eq(String nodeName, Object value) + { + return new SimpleExpression(nodeName, value, "="); + } + + /** + * Apply a "not equal" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static SimpleExpression ne(String nodeName, Object value) + { + return new SimpleExpression(nodeName, value, "!="); + } + + /** + * Apply a "like" constraint of type {@link MatchMode#ANYWHERE} to the named node <br> + * <br> + * <strong>Be warned when using <em>jcr:like</em> function, as it can be very slow.</strong> <br> + * <br> + * The following account of why it is so, was taken <a href= + * "http://www.nabble.com/Explanation-and-solutions-of-some-Jackrabbit-queries-regarding-performance-td15028655.html" + * >here</a> <br> + * <em> + * <ul> + * <li>Question: My xpath is '//*[jcr:like(@propertyName, + * '%somevalue%')]' and it takes minutes to complete. + * + * <li>Answer: a jcr:like with % will be translated to a WildcardQuery lucene + * query. In order to prevent extremely slow WildcardQueries, a Wildcard + * term should not start with one of the wildcards * or ?. So this is not a + * Jackrabbit implementation detail, but a general Lucene (and I think + * inverted indexes in general) issue [1] + * + * <li>Conclusion: Avoid % prefixes in jcr:like. Use jcr:contains when + * searching for a specific word. If jcr:contains is not suitable, you can + * work around the problem by creating a custom lucene analyzer for the + * specific propery (see IndexingConfiguration [2] at Index Analyzers). + * </ul> + * </em> + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number}. + * @return Criterion + */ + public static JCRFunctionExpression like(String nodeName, Object value) + { + return new LikeExpression( + nodeName, + XPathTextUtils.stringToJCRSearchExp(value.toString()), + " jcr:like", + MatchMode.ANYWHERE); + + } + + /** + * Apply a "like" constraint to the named node <br> + * <br> + * <strong>Be warned when using <em>jcr:like</em> function, as it can be very slow.</strong> <br> + * <br> + * The following account of why it is so, was taken <a href= + * "http://www.nabble.com/Explanation-and-solutions-of-some-Jackrabbit-queries-regarding-performance-td15028655.html" + * >here</a> <br> + * <em> + * <ul> + * <li>Question: My xpath is '//*[jcr:like(@propertyName, + * '%somevalue%')]' and it takes minutes to complete. + * + * <li>Answer: a jcr:like with % will be translated to a WildcardQuery lucene + * query. In order to prevent extremely slow WildcardQueries, a Wildcard + * term should not start with one of the wildcards * or ?. So this is not a + * Jackrabbit implementation detail, but a general Lucene (and I think + * inverted indexes in general) issue [1] + * + * <li>Conclusion: Avoid % prefixes in jcr:like. Use jcr:contains when + * searching for a specific word. If jcr:contains is not suitable, you can + * work around the problem by creating a custom lucene analyzer for the + * specific propery (see IndexingConfiguration [2] at Index Analyzers). + * </ul> + * </em> + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number}. + * @param matchMode - one of {@link MatchMode#START} or {@link MatchMode#END} or {@link MatchMode#ANYWHERE} + * @return Criterion + */ + public static JCRFunctionExpression like(String nodeName, Object value, MatchMode matchMode) + { + return new LikeExpression( + nodeName, + XPathTextUtils.stringToJCRSearchExp(value.toString()), + " jcr:like", + matchMode); + } + + /** + * Apply a "contains" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number}. + * @return Criterion + */ + public static JCRFunctionExpression contains(String nodeName, Object value) + { + return new JCRFunctionExpression( + nodeName, + XPathTextUtils.stringToJCRSearchExp(value.toString()), + " jcr:contains"); + } + + /** + * Apply a "greater than" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static SimpleExpression gt(String nodeName, Object value) + { + return new SimpleExpression(nodeName, value, ">"); + } + + /** + * Apply a "less than" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static SimpleExpression lt(String nodeName, Object value) + { + return new SimpleExpression(nodeName, value, "<"); + } + + /** + * Apply a "less than or equal" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static SimpleExpression le(String nodeName, Object value) + { + return new SimpleExpression(nodeName, value, "<="); + } + + /** + * Apply a "greater than or equal" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static SimpleExpression ge(String nodeName, Object value) + { + return new SimpleExpression(nodeName, value, ">="); + } + + /** + * Apply a "between" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param lo value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @param hi value - must be an instance of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static Criterion between(String nodeName, Object lo, Object hi) + { + return new BetweenExpression(nodeName, lo, hi); + } + + /** + * Apply an "in" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param values - must be instances of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static Criterion in(String nodeName, Object[] values) + { + throw new UnsupportedOperationException(); + } + + /** + * Apply an "in" constraint to the named node + * @param nodeName - String a qualified (eg. nt:somenode) or unqualified (eg. somenode) node name. When a node is an + * attribute it must be preceded by the '@'character (eg. @nt:somenode) + * @param values - must be instances of either {@link String} or {@link Number} or {@link Calendar}. + * @return Criterion + */ + public static Criterion in(String nodeName, Collection values) + { + throw new UnsupportedOperationException(); + } + + /** + * Apply an "is null" constraint to the named node + * @return Criterion + */ + public static Criterion isNull(String nodeName) + { + return new IsNullExpression(nodeName); + } + + /** + * Apply an "is not null" constraint to the named node + * @return Criterion + */ + public static Criterion isNotNull(String nodeName) + { + return new IsNotNullExpression(nodeName); + } + + /** + * Return the conjuction of two expressions + * @param lhs + * @param rhs + * @return Criterion + */ + public static LogicalExpression and(Criterion lhs, Criterion rhs) + { + return new LogicalExpression(lhs, rhs, "and"); + } + + /** + * Return the disjuction of two expressions + * @param lhs + * @param rhs + * @return Criterion + */ + public static LogicalExpression or(Criterion lhs, Criterion rhs) + { + return new LogicalExpression(lhs, rhs, "or"); + } + + /** + * Return the negation of an expression + * @param expression + * @return Criterion + */ + public static Criterion not(Criterion expression) + { + return new NotExpression(expression); + } + + /** + * Apply a constraint expressed in xpath. + * @param xpa... [truncated message content] |