mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
ALF-9347 (SVC 42) - Blog CQ impl
- initial refactor - we have the option to push-down some of the prop filtering - note: BlogServiceImpl did not need to change - TODO: review tag req w/ Neil (either remove and/or push down) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28728 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -21,50 +21,54 @@ package org.alfresco.repo.blog.cannedqueries;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.query.CannedQuery;
|
||||
import org.alfresco.query.CannedQueryParameters;
|
||||
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
|
||||
import org.alfresco.repo.blog.BlogService;
|
||||
import org.alfresco.repo.blog.BlogService.BlogPostInfo;
|
||||
import org.alfresco.repo.blog.cannedqueries.AbstractBlogPostsCannedQueryFactory.PropertyBasedComparator;
|
||||
import org.alfresco.repo.domain.node.AuditablePropertiesEntity;
|
||||
import org.alfresco.repo.domain.query.CannedQueryDAO;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.AbstractCannedQueryPermissions;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityBean;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* This class provides support for several {@link CannedQuery canned queries} used by the
|
||||
* {@link BlogService}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @author Neil Mc Erlean, janv
|
||||
* @since 4.0
|
||||
*/
|
||||
public class GetBlogPostsCannedQuery extends AbstractCannedQueryPermissions<BlogPostInfo>
|
||||
{
|
||||
/*
|
||||
* This must be the small n nodeService, not the big N NodeService. See below.
|
||||
*/
|
||||
private final NodeService rawNodeService;
|
||||
private Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private static final String QUERY_NAMESPACE = "alfresco.blog";
|
||||
private static final String QUERY_SELECT_GET_BLOGS = "select_GetBlogsCannedQuery";
|
||||
|
||||
private final CannedQueryDAO cannedQueryDAO;
|
||||
|
||||
public GetBlogPostsCannedQuery(
|
||||
NodeService rawNodeService,
|
||||
CannedQueryDAO cannedQueryDAO,
|
||||
MethodSecurityBean<BlogPostInfo> methodSecurity,
|
||||
CannedQueryParameters params)
|
||||
{
|
||||
super(params, methodSecurity);
|
||||
this.rawNodeService = rawNodeService;
|
||||
this.cannedQueryDAO = cannedQueryDAO;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<BlogPostInfo> queryAndFilter(CannedQueryParameters parameters)
|
||||
{
|
||||
Long start = (logger.isDebugEnabled() ? System.currentTimeMillis() : null);
|
||||
|
||||
Object paramBeanObj = parameters.getParameterBean();
|
||||
if (paramBeanObj == null)
|
||||
throw new NullPointerException("Null GetBlogPosts query params");
|
||||
@@ -74,66 +78,57 @@ public class GetBlogPostsCannedQuery extends AbstractCannedQueryPermissions<Blog
|
||||
boolean isPublished = paramBean.getIsPublished();
|
||||
Date publishedFromDate = paramBean.getPublishedFromDate();
|
||||
Date publishedToDate = paramBean.getPublishedToDate();
|
||||
List<QName> requiredAspects = paramBean.getRequiredAspects();
|
||||
|
||||
// Retrieve all blog-post nodes under the blogContainer root. This could potentially
|
||||
// be a long list of NodeRefs and it is possible that future optimisation towards DB queries
|
||||
// would avoid the retrieval of potentially long lists like this.
|
||||
// It is however important to retrieve the full list of relevant nodes before any sorting
|
||||
// is applied. Otherwise it would be possible to have nodes that were not retrieved, which after sorting
|
||||
// could be at the front of this list.
|
||||
// For that reason, we must use the small n nodeService, and not the large N NodeService, because the
|
||||
// latter truncates results.
|
||||
List<ChildAssociationRef> childAssocs = getAllBlogNodes(paramBean.getBlogContainerNode());
|
||||
// note: refer to SQL for specific DB filtering (eg.parent node and optionally blog integration aspect, etc)
|
||||
List<BlogEntity> results = cannedQueryDAO.executeQuery(QUERY_NAMESPACE, QUERY_SELECT_GET_BLOGS, paramBean, 0, Integer.MAX_VALUE);
|
||||
|
||||
List<BlogPostInfo> filteredNodeRefs = new ArrayList<BlogPostInfo>();
|
||||
for (ChildAssociationRef chAssRef : childAssocs)
|
||||
List<BlogEntity> filtered = new ArrayList<BlogEntity>(results.size());
|
||||
for (BlogEntity result : results)
|
||||
{
|
||||
// Is the nextBlogPostNode going to be included or not?
|
||||
boolean nextNodeIsAcceptable = true;
|
||||
|
||||
NodeRef nextBlogNode = chAssRef.getChildRef();
|
||||
Date actualPublishedDate = DefaultTypeConverter.INSTANCE.convert(Date.class, result.getPublishedDate());
|
||||
|
||||
// Only return blog-posts whose cm:published status matches that requested.
|
||||
final boolean nextBlogNodeIsPublished = rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_PUBLISHED) != null;
|
||||
if (nextBlogNodeIsPublished != isPublished)
|
||||
// Only return blog-posts whose cm:published status matches that requested (ie. a blog "is published" when published date is not null)
|
||||
boolean blogIsPublished = (actualPublishedDate != null);
|
||||
if (blogIsPublished != isPublished)
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
}
|
||||
|
||||
// Only return blog posts whose creator matches the given username, if there is one.
|
||||
if (requestedCreator != null && !rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_CREATOR).equals(requestedCreator))
|
||||
if (requestedCreator != null)
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
AuditablePropertiesEntity auditProps = result.getNode().getAuditableProperties();
|
||||
if ((auditProps == null) || (! requestedCreator.equals(auditProps.getAuditCreator())))
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Only return blogs published within the specified dates
|
||||
Date actualPublishedDate = (Date) rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_PUBLISHED);
|
||||
if (actualPublishedDate != null)
|
||||
if ((publishedFromDate != null) || (publishedToDate != null))
|
||||
{
|
||||
if (publishedFromDate != null && actualPublishedDate.before(publishedFromDate))
|
||||
if (actualPublishedDate != null)
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
if (publishedFromDate != null && actualPublishedDate.before(publishedFromDate))
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
}
|
||||
if (publishedToDate != null && actualPublishedDate.after(publishedToDate))
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
}
|
||||
}
|
||||
if (publishedToDate != null && actualPublishedDate.after(publishedToDate))
|
||||
else
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Only those with the required aspects.
|
||||
for (QName aspect : requiredAspects)
|
||||
{
|
||||
if (!rawNodeService.hasAspect(nextBlogNode, aspect))
|
||||
{
|
||||
nextNodeIsAcceptable = false;
|
||||
}
|
||||
}
|
||||
|
||||
// If all the above conditions are true...
|
||||
if (nextNodeIsAcceptable)
|
||||
{
|
||||
filteredNodeRefs.add(new BlogPostInfo(nextBlogNode, (String)rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_NAME)));
|
||||
filtered.add(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,27 +140,27 @@ public class GetBlogPostsCannedQuery extends AbstractCannedQueryPermissions<Blog
|
||||
Pair<? extends Object, SortOrder> sortPair = sortPairs.get(0);
|
||||
|
||||
QName sortProperty = (QName) sortPair.getFirst();
|
||||
final PropertyBasedComparator createdDateComparator = new PropertyBasedComparator(sortProperty, rawNodeService);
|
||||
|
||||
final PropertyBasedComparator comparator = new PropertyBasedComparator(sortProperty);
|
||||
|
||||
if (sortPair.getSecond() == SortOrder.DESCENDING)
|
||||
{
|
||||
Collections.sort(filteredNodeRefs, Collections.reverseOrder(createdDateComparator));
|
||||
Collections.sort(filtered, Collections.reverseOrder(comparator));
|
||||
}
|
||||
}
|
||||
|
||||
return filteredNodeRefs;
|
||||
}
|
||||
|
||||
private List<ChildAssociationRef> getAllBlogNodes(NodeRef containerNode)
|
||||
{
|
||||
final Set<QName> childNodeTypes = new HashSet<QName>();
|
||||
childNodeTypes.add(ContentModel.TYPE_CONTENT);
|
||||
List<BlogPostInfo> blogPostInfos = new ArrayList<BlogPostInfo>(filtered.size());
|
||||
for (BlogEntity result : filtered)
|
||||
{
|
||||
blogPostInfos.add(new BlogPostInfo(result.getNodeRef(), result.getName()));
|
||||
}
|
||||
|
||||
// This will, of course, retrieve all the blog posts which may be a very long list.
|
||||
List<ChildAssociationRef> childAssocs = rawNodeService.getChildAssocs(containerNode, childNodeTypes);
|
||||
return childAssocs;
|
||||
if (start != null)
|
||||
{
|
||||
logger.debug("Base query: "+blogPostInfos.size()+" in "+(System.currentTimeMillis()-start)+" msecs");
|
||||
}
|
||||
|
||||
return blogPostInfos;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean isApplyPostQuerySorting()
|
||||
|
Reference in New Issue
Block a user