From: <fg...@us...> - 2010-03-02 07:44:23
|
Revision: 2078 http://openutils.svn.sourceforge.net/openutils/?rev=2078&view=rev Author: fgiust Date: 2010-03-02 07:44:15 +0000 (Tue, 02 Mar 2010) Log Message: ----------- CRIT-7 reworked API with the addition of direct jcr queries Modified Paths: -------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteria.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedCriteriaImpl.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java 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/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/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaSearchTest.java Added Paths: ----------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/DirectJcrQuery.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/ExecutableQuery.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/QueryExecutorHelper.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/JCRMagnoliaCriteriaQueryTranslator.java Removed Paths: ------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaFactory.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java Property Changed: ---------------- trunk/openutils-mgnlcriteria/ Property changes on: trunk/openutils-mgnlcriteria ___________________________________________________________________ Modified: svn:ignore - .settings target .checkstyle .classpath .project temp-testng-customsuite.xml + .settings target .checkstyle .classpath .project temp-testng-customsuite.xml pom.xml.releaseBackup release.properties Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteria.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteria.java 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteria.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -31,7 +31,7 @@ * @author fgiust * @version $Id$ */ -public interface AdvancedCriteria extends Criteria +public interface AdvancedCriteria extends Criteria, ExecutableQuery { AdvancedCriteria setBasePath(String path); @@ -50,8 +50,6 @@ @Deprecated Collection< ? > list() throws JCRQueryException; - AdvancedResult execute(); - AdvancedCriteria setPaging(int itemsPerPage, int pageNumberStartingFromOne); } Deleted: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaFactory.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaFactory.java 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaFactory.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -1,37 +0,0 @@ -/** - * - * Magnolia Criteria API (http://www.openmindlab.com/lab/products/mgnlcriteria.html) - * Copyright(C) 2009-2010, Openmind S.r.l. 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.advanced; - -import net.sourceforge.openutils.mgnlcriteria.advanced.impl.AdvancedCriteriaImpl; - - -/** - * Advanced criterias require jackrabbit 1.6 or up. - * @author fgiust - * @version $Id$ - */ -public class AdvancedCriteriaFactory -{ - - public static AdvancedCriteria createAdvancedCriteria() - { - return new AdvancedCriteriaImpl(); - } -} Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/DirectJcrQuery.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/DirectJcrQuery.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/DirectJcrQuery.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -0,0 +1,91 @@ +package net.sourceforge.openutils.mgnlcriteria.advanced; + +import info.magnolia.cms.core.HierarchyManager; +import net.sourceforge.openutils.mgnlcriteria.advanced.impl.QueryExecutorHelper; + + +/** + * @author fgiust + * @version $Id$ + */ +public class DirectJcrQuery implements ExecutableQuery +{ + + private HierarchyManager hm; + + private String query; + + private String language; + + private String spellCheckString; + + private int maxResults; + + private int offset; + + /** + * @param hm + * @param query + * @param language + */ + public DirectJcrQuery(HierarchyManager hm, String query, String language) + { + this.hm = hm; + this.query = query; + this.language = language; + } + + /** + * Sets the spellCheckString. + * @param spellCheckString the spellCheckString to set + */ + public DirectJcrQuery setSpellCheckString(String spellCheckString) + { + this.spellCheckString = spellCheckString; + return this; + } + + /** + * Sets the maxResults. + * @param maxResults the maxResults to set + */ + public DirectJcrQuery setMaxResultsPerPage(int maxResults) + { + this.maxResults = maxResults; + return this; + } + + /** + * Sets the offset. + * @param offset the offset to set + */ + public DirectJcrQuery setOffset(int offset) + { + this.offset = offset; + return this; + } + + /** + * Utility method for setting offset easier. If this method is called you should not use setOffset/setMaxResults + * directly anymore. + * @param itemsPerPage + * @param pageNumberStartingFromOne + * @return + */ + public DirectJcrQuery setPaging(int itemsPerPage, int pageNumberStartingFromOne) + { + this.maxResults = itemsPerPage; + this.offset = (Math.max(pageNumberStartingFromOne, 1) - 1) * maxResults; + return this; + } + + /** + * {@inheritDoc} + */ + public AdvancedResult execute() + { + + return QueryExecutorHelper.execute(query, language, hm, maxResults, offset, spellCheckString); + } + +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/DirectJcrQuery.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/advanced/ExecutableQuery.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/ExecutableQuery.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/ExecutableQuery.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -0,0 +1,11 @@ +package net.sourceforge.openutils.mgnlcriteria.advanced; + +/** + * @author fgiust + * @version $Id$ + */ +public interface ExecutableQuery +{ + + AdvancedResult execute(); +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/ExecutableQuery.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedCriteriaImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedCriteriaImpl.java 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedCriteriaImpl.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -25,22 +25,14 @@ import java.util.Collection; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.query.InvalidQueryException; -import javax.jcr.query.Query; - import net.sourceforge.openutils.mgnlcriteria.advanced.AdvancedCriteria; import net.sourceforge.openutils.mgnlcriteria.advanced.AdvancedResult; import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; 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.xpath.impl.AbstractCriteriaImpl; -import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.utils.XPathTextUtils; import org.apache.commons.lang.StringUtils; -import org.apache.jackrabbit.core.query.QueryImpl; -import org.apache.jackrabbit.core.query.lucene.QueryResultImpl; /** @@ -52,8 +44,6 @@ private String workspace = ContentRepository.WEBSITE; - private int pageNumberStartingFromOne = 1; - private String spellCheckString; public AdvancedCriteriaImpl() @@ -66,8 +56,19 @@ */ public AdvancedCriteria setBasePath(String path) { - // @todo fixme, shoulb be mangled on query composition - this.path = path + "//*"; + + String finalPath = path; + if (!StringUtils.startsWith(path, Criterion.JCR_ROOT) && !StringUtils.startsWith(path, Criterion.JCR_ROOT)) + { + finalPath = Criterion.JCR_ROOT + path; + } + if (!StringUtils.contains(finalPath, "*")) + { + finalPath = finalPath + "//*"; + } + + this.path = finalPath; + return this; } @@ -95,8 +96,6 @@ public AdvancedCriteria setPaging(int itemsPerPage, int pageNumberStartingFromOne) { this.maxResults = itemsPerPage; - this.pageNumberStartingFromOne = Math.max(1, pageNumberStartingFromOne); - this.offset = (Math.max(pageNumberStartingFromOne, 1) - 1) * maxResults; return this; } @@ -124,7 +123,6 @@ /** * {@inheritDoc} */ - @Override public Collection< ? > list() throws JCRQueryException { throw new UnsupportedOperationException("list() is not supported in AdvancedCriteria, please call execute()"); @@ -135,59 +133,10 @@ */ public AdvancedResult execute() { + String language = javax.jcr.query.Query.XPATH; + String stmt = toXpathExpression(); HierarchyManager hm = MgnlContext.getHierarchyManager(workspace); - Session jcrSession = hm.getWorkspace().getSession(); - javax.jcr.query.QueryManager jcrQueryManager; - - String stmt = toXpathExpression(); - - try - { - jcrQueryManager = jcrSession.getWorkspace().getQueryManager(); - - QueryImpl query = (QueryImpl) jcrQueryManager.createQuery(stmt, javax.jcr.query.Query.XPATH); - - if (this.maxResults > 0) - { - query.setLimit(this.maxResults); - } - - if (this.offset > 0) - { - query.setOffset(this.offset); - } - - Query spellCheckerQuery = null; - - if (StringUtils.isNotBlank(spellCheckString)) - { - spellCheckerQuery = jcrQueryManager.createQuery("/jcr:root[rep:spellcheck('" - + XPathTextUtils.stringToJCRSearchExp(spellCheckString) - + "')]/(rep:spellcheck())", Query.XPATH); - } - - return new AdvancedResultImpl( - (QueryResultImpl) query.execute(), - maxResults, - pageNumberStartingFromOne, - stmt, - hm, - spellCheckerQuery); - - } - catch (InvalidQueryException e) - { - JCRQueryException jqe = new JCRQueryException(stmt, e); - log.error(jqe.getMessage()); - throw jqe; - } - catch (RepositoryException e) - { - JCRQueryException jqe = new JCRQueryException(stmt, e); - log.error(jqe.getMessage()); - throw jqe; - } - + return QueryExecutorHelper.execute(stmt, language, hm, maxResults, offset, spellCheckString); } } 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 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -77,10 +77,11 @@ { this.jcrQueryResult = jcrQueryResult; this.itemsPerPage = itemsPerPage; - this.pageNumberStartingFromOne = pageNumberStartingFromOne; this.statement = statement; this.hm = hm; this.spellCheckerQuery = spellCheckerQuery; + + this.pageNumberStartingFromOne = pageNumberStartingFromOne; } /** Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/QueryExecutorHelper.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/QueryExecutorHelper.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/QueryExecutorHelper.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -0,0 +1,94 @@ +package net.sourceforge.openutils.mgnlcriteria.advanced.impl; + +import info.magnolia.cms.core.HierarchyManager; + +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.query.InvalidQueryException; +import javax.jcr.query.Query; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.utils.XPathTextUtils; + +import org.apache.commons.lang.StringUtils; +import org.apache.jackrabbit.core.query.QueryImpl; +import org.apache.jackrabbit.core.query.lucene.QueryResultImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Warning, provisional class, users should not use this directly. + * @author fgiust + * @version $Id$ + */ +public class QueryExecutorHelper +{ + + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(QueryExecutorHelper.class); + + public static AdvancedResultImpl execute(String stmt, String language, HierarchyManager hm, int maxResults, + int offset, String spellCheckString) + { + + Session jcrSession = hm.getWorkspace().getSession(); + javax.jcr.query.QueryManager jcrQueryManager; + + try + { + jcrQueryManager = jcrSession.getWorkspace().getQueryManager(); + + QueryImpl query = (QueryImpl) jcrQueryManager.createQuery(stmt, language); + + if (maxResults > 0) + { + query.setLimit(maxResults); + } + + if (offset > 0) + { + query.setOffset(offset); + } + + int pageNumberStartingFromOne = 1; + if (maxResults > 0 && offset > maxResults) + { + pageNumberStartingFromOne = (offset / maxResults) + 1; + } + + Query spellCheckerQuery = null; + + if (StringUtils.isNotBlank(spellCheckString)) + { + spellCheckerQuery = jcrQueryManager.createQuery("/jcr:root[rep:spellcheck('" + + XPathTextUtils.stringToJCRSearchExp(spellCheckString) + + "')]/(rep:spellcheck())", Query.XPATH); + } + + return new AdvancedResultImpl( + (QueryResultImpl) query.execute(), + maxResults, + pageNumberStartingFromOne, + stmt, + hm, + spellCheckerQuery); + + } + catch (InvalidQueryException e) + { + JCRQueryException jqe = new JCRQueryException(stmt, e); + log.error(jqe.getMessage()); + throw jqe; + } + catch (RepositoryException e) + { + JCRQueryException jqe = new JCRQueryException(stmt, e); + log.error(jqe.getMessage()); + throw jqe; + } + + } +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/QueryExecutorHelper.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native 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-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/Criteria.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -21,6 +21,7 @@ import java.util.Collection; +import net.sourceforge.openutils.mgnlcriteria.advanced.ExecutableQuery; 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; @@ -114,7 +115,7 @@ * @author Federico Grilli * @version $Id$ */ -public interface Criteria +public interface Criteria extends ExecutableQuery { /** @@ -146,17 +147,13 @@ */ 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. + * @deprecated use execute() */ + @Deprecated Collection< ? > list() throws JCRQueryException; /** Modified: 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 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRCriteriaFactory.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -19,6 +19,11 @@ package net.sourceforge.openutils.mgnlcriteria.jcr.query; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.util.DeprecationUtil; +import net.sourceforge.openutils.mgnlcriteria.advanced.AdvancedCriteria; +import net.sourceforge.openutils.mgnlcriteria.advanced.DirectJcrQuery; +import net.sourceforge.openutils.mgnlcriteria.advanced.impl.AdvancedCriteriaImpl; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.MagnoliaCriteriaImpl; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl.MagnoliaCriteriaWithLimitImpl; @@ -34,6 +39,16 @@ { } + public static AdvancedCriteria createAdvancedCriteria() + { + return new AdvancedCriteriaImpl(); + } + + public static DirectJcrQuery createDirectJcrQuery(HierarchyManager hm, String query, String language) + { + return new DirectJcrQuery(hm, query, language); + } + /** * A factory for Magnolia CMS specific Criteria implementation <br> * <strong>Warning: the object returned by this method is not thread-safe</strong>To use it as a shared global @@ -83,10 +98,13 @@ * @param queryManager - an instance of info.magnolia.cms.core.search.QueryManager * @param itemType String - if not specified defaults to "mgnl:content" * @return a factory for Magnolia CMS specific Criteria implementation + * @deprecated if you need paging/query limits please use createAdvancedCriteria() */ + @Deprecated public static Criteria createMgnlCriteriaWithLimit(String path, info.magnolia.cms.core.search.QueryManager queryManager, String itemType) { + DeprecationUtil.isDeprecated("please use createAdvancedCriteria() instead"); return new MagnoliaCriteriaWithLimitImpl(path, queryManager, itemType); } @@ -107,10 +125,15 @@ * @param itemType String - if not specified defaults to "mgnl:content" * @param clazz Class<?> - the list method of this implementation must return instances of this type * @return a factory for Magnolia CMS specific Criteria implementation + * @deprecated if you need paging/query limits please use createAdvancedCriteria() */ + @Deprecated public static Criteria createMgnlCriteriaWithLimit(String path, info.magnolia.cms.core.search.QueryManager queryManager, String itemType, Class< ? > clazz) { + + DeprecationUtil.isDeprecated("please use createAdvancedCriteria() instead"); return new MagnoliaCriteriaWithLimitImpl(path, queryManager, itemType, clazz); } + } Deleted: 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 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/JCRMagnoliaCriteriaQueryTranslator.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -1,89 +0,0 @@ -/** - * - * Magnolia Criteria API (http://www.openmindlab.com/lab/products/mgnlcriteria.html) - * Copyright (C)2009 - 2010, Openmind S.r.l. 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; - -import net.sourceforge.openutils.mgnlcriteria.jcr.query.TranslatableCriteria.CriterionEntry; -import net.sourceforge.openutils.mgnlcriteria.jcr.query.TranslatableCriteria.OrderEntry; - -import org.apache.commons.lang.StringUtils; - - -/** - * @author Federico Grilli - * @version $Id$ - */ -public class JCRMagnoliaCriteriaQueryTranslator -{ - - private final TranslatableCriteria criteria; - - public JCRMagnoliaCriteriaQueryTranslator(final TranslatableCriteria criteria) throws JCRQueryException - { - - this.criteria = criteria; - } - - public Criteria getRootCriteria() - { - return criteria; - } - - public String getPredicate() - { - StringBuilder condition = new StringBuilder(30); - - boolean isfirst = true; - - for (CriterionEntry entry : criteria.getCriterionEntries()) - { - String xpathString = entry.getCriterion().toXPathString(entry.getCriteria()); - - if (StringUtils.isNotBlank(xpathString)) - { - if (!isfirst && StringUtils.isNotBlank(xpathString)) - { - condition.append(" and "); - } - - condition.append(xpathString); - isfirst = false; - } - } - - return condition.toString(); - } - - public String getOrderBy() - { - StringBuilder orderBy = new StringBuilder(30); - - for (OrderEntry oe : criteria.getOrderEntries()) - { - 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 Copied: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/JCRMagnoliaCriteriaQueryTranslator.java (from rev 2075, 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/xpath/JCRMagnoliaCriteriaQueryTranslator.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/JCRMagnoliaCriteriaQueryTranslator.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -0,0 +1,92 @@ +/** + * + * Magnolia Criteria API (http://www.openmindlab.com/lab/products/mgnlcriteria.html) + * Copyright (C)2009 - 2010, Openmind S.r.l. 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; + +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.TranslatableCriteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.TranslatableCriteria.CriterionEntry; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.TranslatableCriteria.OrderEntry; + +import org.apache.commons.lang.StringUtils; + + +/** + * @author Federico Grilli + * @version $Id$ + */ +public class JCRMagnoliaCriteriaQueryTranslator +{ + + private final TranslatableCriteria criteria; + + public JCRMagnoliaCriteriaQueryTranslator(final TranslatableCriteria criteria) throws JCRQueryException + { + + this.criteria = criteria; + } + + public Criteria getRootCriteria() + { + return criteria; + } + + public String getPredicate() + { + StringBuilder condition = new StringBuilder(30); + + boolean isfirst = true; + + for (CriterionEntry entry : criteria.getCriterionEntries()) + { + String xpathString = entry.getCriterion().toXPathString(entry.getCriteria()); + + if (StringUtils.isNotBlank(xpathString)) + { + if (!isfirst && StringUtils.isNotBlank(xpathString)) + { + condition.append(" and "); + } + + condition.append(xpathString); + isfirst = false; + } + } + + return condition.toString(); + } + + public String getOrderBy() + { + StringBuilder orderBy = new StringBuilder(30); + + for (OrderEntry oe : criteria.getOrderEntries()) + { + 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 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-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractCriteriaImpl.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -25,11 +25,10 @@ import java.util.List; import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; -import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRMagnoliaCriteriaQueryTranslator; -import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; import net.sourceforge.openutils.mgnlcriteria.jcr.query.TranslatableCriteria; 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.xpath.JCRMagnoliaCriteriaQueryTranslator; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.XPathSelect; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.utils.XPathTextUtils; @@ -156,6 +155,4 @@ return stmt; } - public abstract Collection< ? > list() throws JCRQueryException; - } \ 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-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/AbstractMagnoliaCriteriaImpl.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -20,8 +20,12 @@ package net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl; import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; import info.magnolia.cms.core.ItemType; import info.magnolia.cms.core.search.QueryManager; +import net.sourceforge.openutils.mgnlcriteria.advanced.AdvancedResult; +import net.sourceforge.openutils.mgnlcriteria.advanced.impl.QueryExecutorHelper; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.utils.XPathTextUtils; import org.apache.commons.lang.StringUtils; @@ -61,4 +65,41 @@ this.classType = classType == null ? Content.class : classType; } + /** + * {@inheritDoc} + */ + public AdvancedResult execute() + { + + String language = javax.jcr.query.Query.XPATH; + String stmt = toXpathExpression(); + + return QueryExecutorHelper.execute( + stmt, + language, + getPrivateHierarchyManagerFromQuery(stmt), + maxResults, + offset, + null); + } + + private HierarchyManager getPrivateHierarchyManagerFromQuery(String stmt) + { + java.lang.reflect.Field field; + try + { + field = queryManager.getClass().getDeclaredField("hm"); + field.setAccessible(true); + HierarchyManager hm = (HierarchyManager) field.get(queryManager); + return hm; + } + catch (Throwable e) + { + + log.error(e.getMessage()); + throw new JCRQueryException(stmt, e); + } + + } + } \ No newline at end of file Modified: 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/MagnoliaCriteriaImpl.java 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaImpl.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -23,6 +23,7 @@ import info.magnolia.cms.core.search.Query; import info.magnolia.cms.core.search.QueryManager; import info.magnolia.cms.core.search.QueryResult; +import info.magnolia.cms.util.DeprecationUtil; import info.magnolia.content2bean.Content2BeanException; import info.magnolia.content2bean.Content2BeanUtil; @@ -58,9 +59,10 @@ } @SuppressWarnings("unchecked") - @Override public Collection< ? > list() throws JCRQueryException { + DeprecationUtil.isDeprecated("please use execute() instead"); + if (queryManager == null) { throw new IllegalStateException("QueryManager cannot be null"); Modified: 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/impl/MagnoliaCriteriaWithLimitImpl.java 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaWithLimitImpl.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -44,8 +44,8 @@ import javax.jcr.RepositoryException; import javax.jcr.query.InvalidQueryException; -import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRMagnoliaCriteriaQueryTranslator; import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.JCRMagnoliaCriteriaQueryTranslator; import net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.XPathSelect; import org.apache.commons.lang.StringUtils; @@ -56,9 +56,14 @@ /** * Magnolia XPATH implementation of the <tt>Criteria</tt> interface which supports limiting the result set according to * the underlying JCR implementation (e.g. Jackrabbit 1.4+) + * @deprecated due to the way results are collected there is no guarantee that the offset specified is respected: for + * example if your query matches both a page and a paragraph in it, and you ask for results of type mgnl:content those + * two results will be merged into one. If you need paging use AdvancedCriteria and be sure to specify the desired + * return type in the query directly. * @author Federico Grilli * @version $Id$ */ +@Deprecated public class MagnoliaCriteriaWithLimitImpl extends AbstractMagnoliaCriteriaImpl { @@ -82,11 +87,12 @@ } @SuppressWarnings("unchecked") - @Override public Collection< ? > list() throws JCRQueryException { if (queryManager == null) + { throw new IllegalStateException("QueryManager cannot be null"); + } log.debug("creating statement..."); long start = System.currentTimeMillis(); Modified: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaSearchTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaSearchTest.java 2010-03-01 23:28:56 UTC (rev 2077) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/AdvancedCriteriaSearchTest.java 2010-03-02 07:44:15 UTC (rev 2078) @@ -28,6 +28,7 @@ import java.util.List; import junit.framework.Assert; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRCriteriaFactory; import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Disjunction; import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Order; import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Restrictions; @@ -85,6 +86,18 @@ } @Test + public void testSearchFranciaPaginated() throws Exception + { + + AdvancedResult advResult = search("francia", 2, 3); + + Assert.assertEquals(4, advResult.getTotalSize()); + Collection< ? extends Content> result = collectCollectionFromResult(advResult); + + assertSortedResults(new String[]{"federale" }, result, "francia"); + } + + @Test public void testSearchDante() throws Exception { // Dante Alighieri @@ -233,7 +246,7 @@ */ protected AdvancedCriteria createCriteria(String repo, String startnode) { - AdvancedCriteria criteria = AdvancedCriteriaFactory.createAdvancedCriteria().setWorkspace(repo).addOrder( + AdvancedCriteria criteria = JCRCriteriaFactory.createAdvancedCriteria().setWorkspace(repo).addOrder( Order.desc("@jcr:score")).add(Restrictions.eq("@jcr:primaryType", "mgnl:content")); if (startnode != null) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |