From: <fg...@us...> - 2010-10-27 11:16:42
|
Revision: 3119 http://openutils.svn.sourceforge.net/openutils/?rev=3119&view=rev Author: fgiust Date: 2010-10-27 11:16:36 +0000 (Wed, 27 Oct 2010) Log Message: ----------- CRIT-29 Remove the dependency on the jcr 2.0 API jar Modified Paths: -------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AccessibleResultItemResultIterator.java trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemImpl.java trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/ScoreAnalizerAndSortTest.java Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AccessibleResultItemResultIterator.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AccessibleResultItemResultIterator.java 2010-10-25 09:58:25 UTC (rev 3118) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AccessibleResultItemResultIterator.java 2010-10-27 11:16:36 UTC (rev 3119) @@ -24,11 +24,15 @@ import java.util.NoSuchElementException; +import javax.jcr.ItemNotFoundException; import javax.jcr.query.RowIterator; import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author dschivo * @version $Id$ @@ -37,6 +41,11 @@ { /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(AccessibleResultItemResultIterator.class); + + /** * Local variable storing the next accessible result. Method hasNext() fetches it, method next() resets it. */ private AdvancedResultItem next; @@ -74,9 +83,18 @@ } catch (RuntimeException e) { - // if it is an access-denied exception then ignore it - if (!(e.getCause() instanceof AccessDeniedException)) + + if (e.getCause() instanceof AccessDeniedException) { + // if it is an access-denied exception then ignore it + } + else if (e.getCause() instanceof ItemNotFoundException) + { + // if it is an access-denied exception then ignore it + log.warn("Ignoring invalid node while iterating {}", e.getCause().getMessage()); + } + else + { throw e; } } Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemImpl.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemImpl.java 2010-10-25 09:58:25 UTC (rev 3118) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/advanced/impl/AdvancedResultItemImpl.java 2010-10-27 11:16:36 UTC (rev 3119) @@ -19,7 +19,6 @@ 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; @@ -33,7 +32,7 @@ import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.beanutils.PropertyUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,69 +49,27 @@ /** * Logger. */ - private Logger log = LoggerFactory.getLogger(AdvancedResultItemImpl.class); + private static Logger log = LoggerFactory.getLogger(AdvancedResultItemImpl.class); - private final static Method JCR_ROW_GETNODE; - - private final static Method JCR_ROW_GETSCORE; - - static + private static Item getJCRNode(Row row) throws RepositoryException { - Method jcrRowGetNode = null; + try { - jcrRowGetNode = Row.class.getDeclaredMethod("getNode", (Class[]) null); + return (Item) PropertyUtils.getProperty(row, "node"); } - catch (SecurityException e) + catch (IllegalAccessException e) { } - catch (NoSuchMethodException e) + catch (InvocationTargetException e) { } - JCR_ROW_GETNODE = jcrRowGetNode; - - Method jcrRowGetScore = null; - try - { - jcrRowGetScore = Row.class.getDeclaredMethod("getScore", new Class[]{String.class }); - } - catch (SecurityException e) - { - } catch (NoSuchMethodException e) { + log + .error("Unsupported version of jackrabbit detected, you need at least 1.6.x or a jcr 2.0 compliant version"); } - JCR_ROW_GETSCORE = jcrRowGetScore; - } - public static Item getJCRNode(Row row, HierarchyManager hierarchyManager) throws RepositoryException - { - if (JCR_ROW_GETNODE != null) - { - try - { - return (Item) JCR_ROW_GETNODE.invoke(row, (Object[]) null); - } - catch (IllegalAccessException e) - { - } - catch (InvocationTargetException e) - { - } - } - else - { - Value path = row.getValue("jcr:path"); - if (path != null && StringUtils.isNotBlank(path.getString())) - { - Content node = hierarchyManager.getContent(path.getString()); - if (node != null) - { - return node.getJCRNode(); - } - } - - } return null; } @@ -130,7 +87,7 @@ throws RepositoryException, AccessDeniedException { - super(getJCRNode(row, hierarchyManager), hierarchyManager); + super(getJCRNode(row), hierarchyManager); this.row = row; } @@ -181,7 +138,25 @@ */ public double getScore() { - return getScore("."); + try + { + return (Double) PropertyUtils.getSimpleProperty(row, "score"); + } + catch (IllegalAccessException e) + { + log.warn("Error getting score for {}", this.getHandle(), e); + } + catch (InvocationTargetException e) + { + log.warn("Error getting score for {}", this.getHandle(), e); + } + catch (NoSuchMethodException e) + { + log + .error("Unsupported version of jackrabbit detected, you need at least 1.6.x or a jcr 2.0 compliant version"); + } + + return 0; } /** @@ -189,50 +164,51 @@ */ public double getScore(String selector) { - if (JCR_ROW_GETSCORE != null) + + Method jcrRowGetScore = null; + try { + jcrRowGetScore = row.getClass().getDeclaredMethod("getScore", new Class[]{String.class }); + jcrRowGetScore.setAccessible(true); + } + catch (SecurityException e) + { + } + catch (NoSuchMethodException e) + { + } + + if (jcrRowGetScore != null) + { try { - return (Double) JCR_ROW_GETSCORE.invoke(row, new Object[]{selector }); + if (selector == null) + { + return (Double) PropertyUtils.getSimpleProperty(row, "score"); + } + return (Double) jcrRowGetScore.invoke(row, new Object[]{selector }); } catch (IllegalArgumentException e) { - log.warn("Error getting score for {}", this.getHandle(), e); + log.warn("Error getting score for " + this.getHandle(), e); } catch (IllegalAccessException e) { - log.warn("Error getting score for {}", this.getHandle(), e); + log.warn("Error getting score for " + this.getHandle(), e); } catch (InvocationTargetException e) { - log.warn("Error getting score for {}", this.getHandle(), e); + log.warn("Error getting score for " + this.getHandle(), e.getTargetException()); } + catch (NoSuchMethodException e) + { + log.warn("Error getting score for " + this.getHandle(), e); + } } else { - Value scoreValue; - try - { - scoreValue = row.getValue("jcr:score(" + selector + ")"); - } - catch (RepositoryException e) - { - log.warn("Error getting excerpt for " + this.getHandle(), e); - return 0; - } - - if (scoreValue != null) - { - try - { - return scoreValue.getDouble(); - } - catch (RepositoryException e) - { - log.warn("Error getting score for " + this.getHandle(), e); - return 0; - } - } + log + .error("Unsupported version of jackrabbit detected, you need at least 1.6.x or a jcr 2.0 compliant version"); } return 0; } Modified: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/ScoreAnalizerAndSortTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/ScoreAnalizerAndSortTest.java 2010-10-25 09:58:25 UTC (rev 3118) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/ScoreAnalizerAndSortTest.java 2010-10-27 11:16:36 UTC (rev 3119) @@ -29,6 +29,8 @@ import java.util.List; import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResult; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.AdvancedResultItem; +import net.sourceforge.openutils.mgnlcriteria.jcr.query.ResultIterator; import net.sourceforge.openutils.mgnlcriteria.tests.CriteriaTestUtils; import org.testng.Assert; @@ -80,6 +82,16 @@ Collection< ? extends Content> result = CriteriaTestUtils.collectCollectionFromResult(advResult); CriteriaTestUtils.assertNumOfResults(3, result, "fagiano"); + + ResultIterator<AdvancedResultItem> iterator = advResult.getItems(); + + Assert.assertTrue(iterator.next().getScore() > iterator.next().getScore()); + + iterator = advResult.getItems(); + + // not sure what the selector name "s" means, but that's the only valid selector for this query, according to + // jackrabbit + Assert.assertTrue(iterator.next().getScore("s") > iterator.next().getScore("s")); } @Test This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |