alfresco-community-repo/source/java/org/alfresco/repo/blog/cannedqueries/DraftsAndPublishedBlogPostsCannedQuery.java
Neil McErlean ce540080f7 Merged BRANCHES/DEV/SWIFT to HEAD:
28466: Fix for ALF-6541. maintainAspectRatio does not default to true as documented.
        Fixed the javadoc to reflect reality.

Merged BRANCHES/DEV/SWIFT to HEAD:
   28482 Implementation of ALF-8969 Lucene removal: Blog webscripts.
        ** Checking this in on Swift branch, as I have the work there. Will merge to HEAD.

        The blog webscript controllers have been ported from JavaScript to Java.
        A new foundation service, the BlogService has been added and the impls of the webscript controllers delegate into that service, thus encapsulating business logic within the service.
        The API for this service is based on the requirements of the existing webscripts, but is for the most part a 'sensible' API. One controller (blogposts.get.js) had very domain-specific requirements (get all of my drafts and all published posts) and it is implemented as a deprecated public method on the service.
        The API is not complete, but represents a good starting point for any future feature development.
        The various Lucene queries have been replaced with calls to the nodeservice (as an impl detail within the BlogService) which get all blog post nodes and then post-filter them based on property values, aspect/property presence etc. This will       be refactored into a CannedQuery in a subsequent check-in.
 
        I've written new test cases aimed at this API & have extended the REST API tests.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28483 Prevent NPEs in some circumstances. Related to ALF-8969.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28484 Fixing activity reports for Blog posting. Following on from previous chagnes related to ALF-8969.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28597 ALF-8969. Introduction of brute force Canned Queries for BlogService query methods.
        This will be merged to HEAD after a chat with Jan/Derek.

        Introduced 'brute force' Canned Queries for the various BlogService query methods.
          These use the underlying nodeService to retrieve result sets.
          They must use the small-n nodeService in order to get full result sets.
          Therefore I have had to add some AFTER_ACL_ENTRY checks to the BlogService_security bean for the query methods.
        Added various CannedQuery classes for the BlogService queries. They currently split into two:
          1. a GetBlogPostsCannedQuery which goes some way towards providing configurable query support, albeit driven by the needs of the Blog Service REST API.
          2. a DraftsAndPublishedBlogPostsCannedQuery, which is a very specific CQ aimed at a very specific scenario in the REST API.
        Changed the BlogService API to return a BlogPostInfo (simple POJO) rather than the less extensible NodeRef.
          This affected the webscript implementations.
        Added BlogPostInfo as an acceptable return type for security-based filtering in ACLEntryAfterInvocationProvider.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28598 Repackaged the CannedQuery-related classes to a dedicated subpackage. ALF-8969.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28602 Replacement of some JS controllers with Java-based ports. Part of ALF-8969.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28603 Disabling two test cases pending a refactoring. Related to ALF-8969.

Merged BRANCHES/DEV/SWIFT to HEAD:
   r28604 Fixing a compilation error.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28606 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-06-27 10:10:07 +00:00

175 lines
7.0 KiB
Java

/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.blog.cannedqueries;
import java.lang.reflect.Method;
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.BlogPostInfo;
import org.alfresco.repo.blog.BlogService;
import org.alfresco.repo.security.permissions.impl.acegi.AbstractCannedQueryPermissions;
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityInterceptor;
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.tagging.TaggingService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
/**
* This is a {@link CannedQuery} for the rather particular 'get my drafts and all published' blog-post query.
*
* @author Neil Mc Erlean
* @since 4.0
*
* @see BlogService#getMyDraftsAndAllPublished(NodeRef, Date, Date, String, org.alfresco.query.PagingRequest)
*/
public class DraftsAndPublishedBlogPostsCannedQuery extends AbstractCannedQueryPermissions<BlogPostInfo>
{
private final NodeService rawNodeService;
private final TaggingService taggingService;
public DraftsAndPublishedBlogPostsCannedQuery(
NodeService rawNodeService,
TaggingService taggingService,
MethodSecurityInterceptor methodSecurityInterceptor,
Method method,
CannedQueryParameters params,
String queryExecutionId)
{
super(params, queryExecutionId, methodSecurityInterceptor, method);
this.rawNodeService = rawNodeService;
this.taggingService = taggingService;
}
@Override
protected List<BlogPostInfo> queryAndFilter(CannedQueryParameters parameters)
{
Object paramBeanObj = parameters.getParameterBean();
if (paramBeanObj == null)
throw new NullPointerException("Null GetBlogPosts query params");
DraftsAndPublishedBlogPostsCannedQueryParams paramBean = (DraftsAndPublishedBlogPostsCannedQueryParams) paramBeanObj;
String requestedTag = paramBean.getTag();
Date createdFromDate = paramBean.getCreatedFromDate();
Date createdToDate = paramBean.getCreatedToDate();
List<ChildAssociationRef> childAssocs = getAllBlogNodes(paramBean.getBlogContainerNode());
List<BlogPostInfo> filteredNodeRefs = new ArrayList<BlogPostInfo>();
for (ChildAssociationRef chAssRef : childAssocs)
{
NodeRef nextBlogNode = chAssRef.getChildRef();
// Is this next node in the list to be included in the results?
boolean nextNodeIsAcceptable = true;
// Return all published Blog Posts
if (rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_PUBLISHED) != null)
{
// Intentionally empty
}
else
{
// We're relying on cm:published being null below i.e. we are dealing with draft blog posts.
if (!rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_CREATOR).equals(paramBean.getCmCreator()))
{
nextNodeIsAcceptable = false;
}
}
// Only return blogs created within the specified dates
Date actualCreatedDate = (Date) rawNodeService.getProperty(nextBlogNode, ContentModel.PROP_CREATED);
if (actualCreatedDate != null)
{
if (createdFromDate != null && actualCreatedDate.before(createdFromDate))
{
nextNodeIsAcceptable = false;
}
if (createdToDate != null && actualCreatedDate.after(createdToDate))
{
nextNodeIsAcceptable = false;
}
}
// Only return blog posts tagged with the specified tag string.
if (requestedTag != null && !taggingService.getTags(nextBlogNode).contains(requestedTag))
{
nextNodeIsAcceptable = false;
}
if (nextNodeIsAcceptable)
{
filteredNodeRefs.add(new BlogPostInfoImpl(nextBlogNode, rawNodeService.getProperties(nextBlogNode)));
}
}
List<Pair<? extends Object, SortOrder>> sortPairs = parameters.getSortDetails().getSortPairs();
// For now, the BlogService only sorts by a single property.
if (sortPairs != null && !sortPairs.isEmpty())
{
Pair<? extends Object, SortOrder> sortPair = sortPairs.get(0);
QName sortProperty = (QName) sortPair.getFirst();
final PropertyBasedComparator createdDateComparator = new PropertyBasedComparator(sortProperty, rawNodeService);
if (sortPair.getSecond() == SortOrder.DESCENDING)
{
Collections.sort(filteredNodeRefs, Collections.reverseOrder(createdDateComparator));
}
}
return filteredNodeRefs;
}
private List<ChildAssociationRef> getAllBlogNodes(NodeRef containerNode)
{
final Set<QName> childNodeTypes = new HashSet<QName>();
childNodeTypes.add(ContentModel.TYPE_CONTENT);
// 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;
}
@Override
protected boolean isApplyPostQuerySorting()
{
// No post-query sorting. It's done within the queryAndFilter() method above.
return false;
}
@Override
protected boolean isApplyPostQueryPermissions()
{
return true;
}
}