Revision: 1064 http://openutils.svn.sourceforge.net/openutils/?rev=1064&view=rev Author: fgrilli Date: 2009-02-28 20:20:59 +0000 (Sat, 28 Feb 2009) Log Message: ----------- implemented querying Magnolia with limit Modified Paths: -------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaWithLimitImpl.java 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 2009-02-25 21:38:20 UTC (rev 1063) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaWithLimitImpl.java 2009-02-28 20:20:59 UTC (rev 1064) @@ -1,15 +1,26 @@ package net.sourceforge.openutils.mgnlcriteria.jcr.query.xpath.impl; import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.DefaultContent; import info.magnolia.cms.core.ItemType; +import info.magnolia.cms.core.Path; 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.security.AccessManager; +import info.magnolia.cms.security.Permission; +import info.magnolia.content2bean.Content2BeanException; +import info.magnolia.content2bean.Content2BeanUtil; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; import java.util.Map; +import javax.jcr.Node; +import javax.jcr.NodeIterator; import javax.jcr.RepositoryException; import javax.jcr.query.InvalidQueryException; @@ -22,9 +33,7 @@ /** * 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+) XXX Cannot be used now! FIXME Something is wrong with - * subsetting the result set. Strange things happens eg offset = 0 && limit = 20 on a resultset which is 30+ nodes - * returns only 2 nodes + * the underlying JCR implementation (e.g. Jackrabbit 1.4+) * @author Federico Grilli * @version $Id$ */ @@ -45,6 +54,16 @@ this.itemType = itemType; } + public MagnoliaCriteriaWithLimitImpl(String path, QueryManager queryManager, String itemType, Class< ? > classType) + { + super(path, queryManager, itemType, classType); + + if (StringUtils.isBlank(this.itemType)) + this.itemType = ItemType.CONTENT.getSystemName(); + else + this.itemType = itemType; + } + @SuppressWarnings("unchecked") @Override public Collection< ? > list() throws JCRQueryException @@ -67,7 +86,7 @@ try { - Class managerClass = queryManager.getClass(); + Class< ? extends QueryManager> managerClass = queryManager.getClass(); java.lang.reflect.Field field = managerClass.getDeclaredField(QUERY_MANAGER); field.setAccessible(true); @@ -99,8 +118,6 @@ stop = System.currentTimeMillis(); log.debug("Query executed in {} milliseconds", stop - start); - Map<String, Collection<Content>> objectStore = new HashMap<String, Collection<Content>>(); - Map<String, String> dirtyHandles = new HashMap<String, String>(); Collection retVal = new ArrayList(); field = managerClass.getDeclaredField(ACCESS_MANAGER); field.setAccessible(true); @@ -110,22 +127,40 @@ log.debug("getting content..."); start = System.currentTimeMillis(); - try + QueryResultImpl filteredResult = new QueryResultImpl(result, accessManager); + retVal = filteredResult.getContent(itemType); + + stop = System.currentTimeMillis(); + + if (Content.class.isAssignableFrom(classType)) { - build(itemType, retVal, objectStore, result, dirtyHandles, accessManager); + log.debug("returning a Collection holding objects of type {} ", Content.class.getName()); + retVal = retVal == null ? new ArrayList<Content>() : retVal; + log.debug("got {} node(s) in {} ms", new Object[]{retVal.size(), stop - start }); + + return retVal; } - catch (RepositoryException re) + else { - log.error(re.getMessage()); - throw new JCRQueryException(re); + log.debug("returning a Collection holding objects of type {} ", classType.getName()); + Iterator iter = retVal.iterator(); + List list = new ArrayList(); + while (iter.hasNext()) + { + Content node = (Content) iter.next(); + try + { + list.add(classType.cast(Content2BeanUtil.toBean(node, true, classType))); + } + catch (Content2BeanException e) + { + log.error(e.getMessage()); + throw new JCRQueryException(e); + } + } + log.debug("got {} node(s) in {} ms", new Object[]{list.size(), stop - start }); + return list; } - - stop = System.currentTimeMillis(); - retVal = retVal == null ? new ArrayList() : retVal; - log.debug("got {} node(s) in {} milliseconds", new Object[]{retVal.size(), stop - start }); - - return retVal; - } catch (InvalidQueryException e) { @@ -159,50 +194,117 @@ } } - private void build(String nodeType, Collection<Content> collection, Map<String, Collection<Content>> objectStore, - javax.jcr.query.QueryResult result, Map<String, String> dirtyHandles, - info.magnolia.cms.security.AccessManager accessManager) throws RepositoryException + /** + * This is {@link QueryResultImpl} slightly modified + */ + protected static final class QueryResultImpl implements QueryResult { - objectStore.put(nodeType, collection); - javax.jcr.NodeIterator nodeIterator = result.getNodes(); - while (nodeIterator.hasNext()) + + /** + * Unfiltered result object + */ + private javax.jcr.query.QueryResult result; + + private AccessManager accessManager; + + private Map<String, String> dirtyHandles = new Hashtable<String, String>(); + + protected QueryResultImpl(javax.jcr.query.QueryResult result, AccessManager accessManager) { - javax.jcr.Node node = nodeIterator.nextNode(); - try + this.result = result; + this.accessManager = accessManager; + } + + public AccessManager getAccessManager() + { + return accessManager; + } + + public javax.jcr.query.QueryResult getJcrResult() + { + return result; + } + + /** + * Build required result objects + */ + private void build(String nodeType, Collection<DefaultContent> collection) throws RepositoryException + { + NodeIterator nodeIterator = this.result.getNodes(); + while (nodeIterator.hasNext()) { - build(node, nodeType, collection, dirtyHandles, accessManager); + Node node = nodeIterator.nextNode(); + try + { + build(node, nodeType, collection); + } + catch (RepositoryException re) + { + log.error("{} caught while iterating on query results: {}", re.getClass().getName(), re + .getMessage()); + if (log.isDebugEnabled()) + { + log.debug(re.getClass().getName() + + " caught while iterating on query results: " + + re.getMessage(), re); + } + } } - catch (RepositoryException re) - { - log.error("{} caught while iterating on query results: {}", re.getClass().getName(), re.getMessage()); - throw new JCRQueryException(re); - } } - } - private void build(javax.jcr.Node node, String nodeType, Collection<Content> collection, - Map<String, String> dirtyHandles, info.magnolia.cms.security.AccessManager accessManager) - throws RepositoryException - { - if ((node.isNodeType(nodeType) || StringUtils.isEmpty(nodeType)) - && !node.isNodeType(info.magnolia.cms.core.ItemType.NT_RESOURCE)) + /** + * Build required result objects + */ + private void build(Node node, String nodeType, Collection<DefaultContent> collection) + throws RepositoryException { - if (dirtyHandles.get(node.getPath()) == null) + /** + * All custom node types + */ + if ((node.isNodeType(nodeType) || StringUtils.isEmpty(nodeType)) && !node.isNodeType(ItemType.NT_RESOURCE)) { - boolean isAllowed = accessManager.isGranted( - info.magnolia.cms.core.Path.getAbsolutePath(node.getPath()), - info.magnolia.cms.security.Permission.READ); - if (isAllowed) + if (this.dirtyHandles.get(node.getPath()) == null) { - collection.add(new info.magnolia.cms.core.DefaultContent(node, accessManager)); - dirtyHandles.put(node.getPath(), StringUtils.EMPTY); + boolean isAllowed = this.accessManager.isGranted( + Path.getAbsolutePath(node.getPath()), + Permission.READ); + if (isAllowed) + { + collection.add(new DefaultContent(node, this.accessManager)); + this.dirtyHandles.put(node.getPath(), StringUtils.EMPTY); + } } + return; } - return; + if (node.getDepth() > 0) + { + this.build(node.getParent(), nodeType, collection); + } } - if (node.getDepth() > 0) + + /** + * @see info.magnolia.cms.core.search.QueryResult#getContent() + */ + public Collection<DefaultContent> getContent() { - build(node.getParent(), nodeType, collection, dirtyHandles, accessManager); + return getContent(ItemType.CONTENT.getSystemName()); } + + /** + * @see info.magnolia.cms.core.search.QueryResult#getContent(java.lang.String) + */ + public Collection<DefaultContent> getContent(String nodeType) + { + Collection<DefaultContent> resultSet = new ArrayList<DefaultContent>(); + try + { + this.build(nodeType, resultSet); + } + catch (RepositoryException re) + { + log.error(re.getMessage()); + } + return resultSet; + } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |