From: <fg...@us...> - 2010-08-29 07:20:13
|
Revision: 2922 http://openutils.svn.sourceforge.net/openutils/?rev=2922&view=rev Author: fgiust Date: 2010-08-29 07:20:05 +0000 (Sun, 29 Aug 2010) Log Message: ----------- CRIT-21 + CRIT-22 and some cleanup Modified Paths: -------------- 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/AdvancedResultItemResultIterator.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/AdvancedResult.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIterator.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/LogicalExpression.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/lucene/AclQueryDecorator.java trunk/openutils-mgnlcriteria/src/test/resources/crit-bootstrap/website.contains.xml Added Paths: ----------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIteratorImpl.java trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/Content2BeanTest.java Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultImpl.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -19,7 +19,11 @@ package net.sourceforge.openutils.mgnlcriteria.advanced.impl; +import info.magnolia.cms.core.Content; import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.security.AccessDeniedException; +import info.magnolia.content2bean.Content2BeanException; +import info.magnolia.content2bean.Content2BeanUtil; import javax.jcr.RepositoryException; import javax.jcr.Value; @@ -32,6 +36,7 @@ import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRQueryException; import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIterator; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIteratorImpl; import org.apache.jackrabbit.core.query.lucene.QueryResultImpl; import org.slf4j.Logger; @@ -184,4 +189,62 @@ return spellCheckerSuggestion; } + /** + * {@inheritDoc} + */ + public AdvancedResultItem getFirstResult() + { + ResultIterator<AdvancedResultItem> items = getItems(); + if (items.hasNext()) + { + return items.next(); + } + return null; + } + + /** + * {@inheritDoc} + */ + public <K> ResultIterator<K> getItems(final Class<K> theclass) + { + RowIterator rows; + try + { + rows = jcrQueryResult.getRows(); + } + catch (RepositoryException e) + { + JCRQueryException jqe = new JCRQueryException(statement, e); + throw jqe; + } + + return new ResultIteratorImpl<K>(rows, hm) + { + + @SuppressWarnings("unchecked") + @Override + protected K wrap(Row row) + { + try + { + Content content = new AdvancedResultItemImpl(row, this.hm); + + return (K) Content2BeanUtil.toBean(content, true, theclass); + } + catch (AccessDeniedException e) + { + throw new RuntimeException(e); + } + catch (RepositoryException e) + { + throw new RuntimeException(e); + } + catch (Content2BeanException e) + { + throw new RuntimeException(e); + } + } + }; + } + } Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemResultIterator.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemResultIterator.java 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemResultIterator.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -27,7 +27,7 @@ import javax.jcr.query.RowIterator; import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; -import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIterator; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIteratorImpl; /** @@ -35,7 +35,7 @@ * @author fgiust * @version $Id$ */ -public class AdvancedResultItemResultIterator extends ResultIterator<AdvancedResultItem> +public class AdvancedResultItemResultIterator extends ResultIteratorImpl<AdvancedResultItem> { /** Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/AdvancedResult.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/AdvancedResult.java 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/AdvancedResult.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -19,8 +19,6 @@ package net.sourceforge.openutils.mgnlcriteria.jcr.query; - - /** * The result of an advanced jcr query. * @author fgiust @@ -31,43 +29,60 @@ /** * Gets the maximum number of results per page - * @return + * @return the maximum number of results per page */ int getItemsPerPage(); /** * Gets the page number (1, 2, 3...) - * @return + * @return the page number (1, 2, 3...) */ int getPage(); /** - * Gets the total number of results that would be retrieved without pagination - * @return + * Gets the total number of results that would be retrieved without pagination. Note that jackrabbit may return -1 + * if the query doesn't have a sort condition, in order to optimize execution. Always add an order by clause (e.g. + * "order by @jcr:score") if you need to get the total size. + * @return the total number of results that would be retrieved without pagination. */ int getTotalSize(); /** * Gets the total number of pages - * @return + * @return total number of pages */ int getNumberOfPages(); /** * Gets the suggestion from the spell checker - * @return + * @return the suggestion from the spell checker */ String getSpellCheckerSuggestion(); /** * Gets an iterator over the results - * @return + * @return an iterator over the results */ ResultIterator<AdvancedResultItem> getItems(); + /** + * Gets an iterator over the results, transforming objects using content2bean while iterating + * @param <K> destination class. + * @return an iterator over the results + */ + <K> ResultIterator<K> getItems(Class<K> theclass); + + /** + * Returns the fist result if available, null otherwise. + * @return the fist result if available, null otherwise. + */ + AdvancedResultItem getFirstResult(); + public static AdvancedResult EMPTY_RESULT = new AdvancedResult() { + ResultIterator<AdvancedResultItem> iterator = new EmptyResultIterator(); + public int getTotalSize() { return 0; @@ -90,14 +105,61 @@ public ResultIterator<AdvancedResultItem> getItems() { - // @todo return an empty result - return null; + return iterator; } public int getNumberOfPages() { return 0; } + + public AdvancedResultItem getFirstResult() + { + return null; + } + + public <K> ResultIterator<K> getItems(Class<K> theclass) + { + return null; + } }; + /** + * @author fgiust + * @version $Id$ + */ + static final class EmptyResultIterator implements ResultIterator<AdvancedResultItem> + { + + public void skip(long skipNum) + { + // nothing to do + } + + public long getSize() + { + return 0; + } + + public long getPosition() + { + return 0; + } + + public boolean hasNext() + { + return false; + } + + public AdvancedResultItem next() + { + return null; + } + + public void remove() + { + // nothing to do + } + } + } Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIterator.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIterator.java 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIterator.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -19,11 +19,7 @@ package net.sourceforge.openutils.mgnlcriteria.jcr.query; -import info.magnolia.cms.core.HierarchyManager; - import javax.jcr.RangeIterator; -import javax.jcr.query.Row; -import javax.jcr.query.RowIterator; /** @@ -31,75 +27,8 @@ * @author fgiust * @version $Id$ */ -public abstract class ResultIterator<T> implements RangeIterator +public interface ResultIterator<T> extends RangeIterator { - protected RowIterator rowIterator; - - protected final HierarchyManager hm; - - /** - * @param rowIterator - */ - public ResultIterator(RowIterator rowIterator, HierarchyManager hm) - { - this.rowIterator = rowIterator; - this.hm = hm; - } - - /** - * {@inheritDoc} - */ - public boolean hasNext() - { - return rowIterator.hasNext(); - } - - /** - * {@inheritDoc} - */ - public void remove() - { - rowIterator.remove(); - } - - /** - * {@inheritDoc} - */ - public void skip(long skipNum) - { - rowIterator.skip(skipNum); - } - - /** - * {@inheritDoc} - */ - public long getSize() - { - return rowIterator.getSize(); - } - - /** - * {@inheritDoc} - */ - public long getPosition() - { - return rowIterator.getPosition(); - } - - /** - * {@inheritDoc} - */ - public T next() - { - return wrap(rowIterator.nextRow()); - } - - /** - * Transforms a Row instance, adapting it to a specific type. - * @param row - * @return a transformed version - */ - protected abstract T wrap(Row row); - + T next(); } Added: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIteratorImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIteratorImpl.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIteratorImpl.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -0,0 +1,109 @@ +/** + * + * 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 info.magnolia.cms.core.HierarchyManager; + +import javax.jcr.query.Row; +import javax.jcr.query.RowIterator; + + +/** + * Wraps a RowIterator, requiring subclasses to adapt each Row to a specific type. + * @author fgiust + * @version $Id$ + */ +public abstract class ResultIteratorImpl<T> implements ResultIterator<T> +{ + + protected RowIterator rowIterator; + + protected final HierarchyManager hm; + + /** + * Local variable storing the next accessible result. Method hasNext() fetches it, method next() resets it. + */ + private AdvancedResultItem next; + + /** + * @param rowIterator + */ + public ResultIteratorImpl(RowIterator rowIterator, HierarchyManager hm) + { + this.rowIterator = rowIterator; + this.hm = hm; + } + + /** + * {@inheritDoc} + */ + public boolean hasNext() + { + return rowIterator.hasNext(); + } + + /** + * {@inheritDoc} + */ + public void remove() + { + rowIterator.remove(); + } + + /** + * {@inheritDoc} + */ + public void skip(long skipNum) + { + rowIterator.skip(skipNum); + } + + /** + * {@inheritDoc} + */ + public long getSize() + { + return rowIterator.getSize(); + } + + /** + * {@inheritDoc} + */ + public long getPosition() + { + return rowIterator.getPosition(); + } + + /** + * {@inheritDoc} + */ + public T next() + { + return wrap(rowIterator.nextRow()); + } + + /** + * Transforms a Row instance, adapting it to a specific type. + * @param row + * @return a transformed version + */ + protected abstract T wrap(Row row); + +} Property changes on: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/ResultIteratorImpl.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/criterion/Junction.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/Junction.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -39,8 +39,7 @@ private static final long serialVersionUID = 4745761472724863693L; - @SuppressWarnings("unchecked") - private final List criteria = new ArrayList(); + private final List<Criterion> criteria = new ArrayList<Criterion>(); private final String op; @@ -49,7 +48,6 @@ this.op = op; } - @SuppressWarnings("unchecked") public Junction add(Criterion criterion) { criteria.add(criterion); @@ -61,7 +59,6 @@ return op; } - @SuppressWarnings("unchecked") public String toXPathString(Criteria crit) throws JCRQueryException { @@ -71,12 +68,12 @@ } StringBuilder buffer = new StringBuilder().append('('); - Iterator iter = criteria.iterator(); + Iterator<Criterion> iter = criteria.iterator(); boolean isfirst = true; while (iter.hasNext()) { - String xPathString = ((Criterion) iter.next()).toXPathString(crit); + String xPathString = (iter.next()).toXPathString(crit); if (StringUtils.isNotBlank(xPathString)) { if (!isfirst && StringUtils.isNotBlank(xPathString)) Modified: 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 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/criterion/LogicalExpression.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -32,8 +32,8 @@ { /** - * - */ + * + */ private static final long serialVersionUID = 4524284746715983618L; private final Criterion lhs; Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/lucene/AclQueryDecorator.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/lucene/AclQueryDecorator.java 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/lucene/AclQueryDecorator.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -50,7 +50,7 @@ private final SessionImpl session; /** - * + * */ public AclQueryDecorator(SessionImpl session, SearchIndex index) { @@ -58,11 +58,10 @@ this.session = session; } - @SuppressWarnings("unchecked") public Query applyAcl(Query query) throws RepositoryException { // creates a lucene query for each acl rule - List aclQueries = new ArrayList(); + List<Object[]> aclQueries = new ArrayList<Object[]>(); AccessManager accessManager = MgnlContext.getAccessManager(session.getWorkspace().getName()); List<Permission> permissions = accessManager.getPermissionList(); if (!permissions.isEmpty()) @@ -127,7 +126,7 @@ os[0] = Occur.MUST; for (int i = 1; i < qs.length; i++) { - Object[] qo = (Object[]) aclQueries.get(i - 1); + Object[] qo = aclQueries.get(i - 1); qs[i] = (Query) qo[0]; os[i] = (Occur) qo[1]; } Added: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/Content2BeanTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/Content2BeanTest.java (rev 0) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/Content2BeanTest.java 2010-08-29 07:20:05 UTC (rev 2922) @@ -0,0 +1,150 @@ +/** + * + * 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 info.magnolia.cms.beans.config.ContentRepository; +import info.magnolia.context.MgnlContext; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResult; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.Criteria; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRCriteriaFactory; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIterator; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Order; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Restrictions; +import net.sourceforge.openutils.mgnlcriteria.tests.RepositoryTestNgTestcase; + +import org.apache.commons.lang.StringUtils; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + + +/** + * @author fgiust + * @version $Id$ + */ +public class Content2BeanTest extends RepositoryTestNgTestcase +{ + + /** + * {@inheritDoc} + */ + @Override + @BeforeClass + protected void setUp() throws Exception + { + setRepositoryConfigFileName("/crit-repository/test-repositories.xml"); + setJackrabbitRepositoryConfigFileName("/crit-repository/jackrabbit-test-configuration.xml"); + + super.setUp(); + + bootstrapSingleResource("/crit-bootstrap/website.contains.xml"); + MgnlContext.getHierarchyManager(ContentRepository.WEBSITE).save(); + } + + @Test + public void testLoremAndIpsum() throws Exception + { + Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE); + criteria.setBasePath(StringUtils.EMPTY); + criteria.add(Restrictions.eq("@jcr:primaryType", "mgnl:content")); + criteria.add(Restrictions.contains("@title", "lorem ipsum")); + criteria.addOrder(Order.desc("@jcr:score")); + + AdvancedResult advResult = criteria.execute(); + Assert.assertNotNull(advResult); + Assert.assertEquals(advResult.getTotalSize(), 1); + ResultIterator<AdvancedResultItem> items = advResult.getItems(); + AdvancedResultItem item = items.next(); + Assert.assertEquals(item.getTitle(), "lorem ipsum"); + + ResultIterator<Page> itemsTransformed = advResult.getItems(Page.class); + Assert.assertNotNull(itemsTransformed); + Page page = itemsTransformed.next(); + Assert.assertEquals(page.getTitle(), "lorem ipsum"); + Assert.assertEquals(page.getText(), "ohoh"); + Assert.assertEquals(page.getNumber(), 5); + + } + + public static class Page + { + + private String title; + + private String text; + + private int number; + + /** + * Returns the title. + * @return the title + */ + public String getTitle() + { + return title; + } + + /** + * Sets the title. + * @param title the title to set + */ + public void setTitle(String title) + { + this.title = title; + } + + /** + * Returns the text. + * @return the text + */ + public String getText() + { + return text; + } + + /** + * Sets the text. + * @param text the text to set + */ + public void setText(String text) + { + this.text = text; + } + + /** + * Returns the number. + * @return the number + */ + public int getNumber() + { + return number; + } + + /** + * Sets the number. + * @param number the number to set + */ + public void setNumber(int number) + { + this.number = number; + } + } +} Property changes on: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/Content2BeanTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnlcriteria/src/test/resources/crit-bootstrap/website.contains.xml =================================================================== --- trunk/openutils-mgnlcriteria/src/test/resources/crit-bootstrap/website.contains.xml 2010-08-29 07:16:56 UTC (rev 2921) +++ trunk/openutils-mgnlcriteria/src/test/resources/crit-bootstrap/website.contains.xml 2010-08-29 07:20:05 UTC (rev 2922) @@ -202,6 +202,12 @@ <sv:property sv:name="jcr:createdBy" sv:type="String"> <sv:value>admin</sv:value> </sv:property> + <sv:property sv:name="number" sv:type="Long"> + <sv:value>5</sv:value> + </sv:property> + <sv:property sv:name="text" sv:type="String"> + <sv:value>ohoh</sv:value> + </sv:property> <sv:property sv:name="title" sv:type="String"> <sv:value>lorem ipsum</sv:value> </sv:property> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |