Derek Hulley efbd951a10 Merged DEV/SWIFT to HEAD
27125: Subtasks of ALF-7072: RSOLR 013: Remote API to get ACLs and readers
          - ALF-8334: RSOLR 013: Modify ACL schema to record change times
          - ALF-8336: RSOLR 013: DB upgrade scripts for ACL changes
          - TODO: Query APIs
   27128: Added TooManyResultsException as a concurrency detection trigger
          - Usually too many results indicates that the DB table key is not as specific as it should be,
            but it's AVM that showed this up.
   27132: Clean up: javadocs; non-javadocs; uncommented fields; @since tags; etc.
   27134: Removed empty directory
   27135: Fix for ALF-8333: CMIS query: JOIN on an aspect results in CmisInvalidArgumentException
          - incorrect scope used when building orderings
   27139: Fixed SORL transaction tracking queries
          - Queries were using incompatible boolean comparisons
          - Added SOLRDAO to test suite
          - Cleaned up code and reformatted code
   27141: Minor additions to CannedQuery interface
          - get parameter bean
          - construct sort details from a list
          - ALF-7167: Canned queries
   27146: RINF 09 / RINF 10: DB-based paged query for get children (DocLib & CMIS) 
          - milestone check-in for sprint demo & review (WIP)
          - added new FileFolderService (paged) list query (public API is subject to change)
          - moved temp JavaScript sorting to Java
          - example usage by DocLib (via ScriptNode) and CMIS (via AlfrescoCmisService)
          - implemented as demo "canned query" including embedded use of "list" permission interceptor
          - ALF-7402 / ALF-7168
   27150: RINF 09 / RINF 10: DB-based paged query for get children (DocLib & CMIS) 
          - missed file (follow-on to r27146)
   27158: ALF-7070, ALF-7072: SOLR tracking (node and changeset)
          - Pulled non-DAO code into SOLRTrackingComponent
          - DAO code and related tests just test basic CRUD
          - SOLRTrackingComponent does complex cross-schema manipulation
   27159: Fixed line ending and removed svn:eol-style
   27160: ALF-8334: RSOLR 013: Fixed SQL Server syntax
   27165: RINF 09 / RINF 10: DB-based paged query for get children (DocLib & CMIS) 
          - fix listDeepFolders (causing Imap*Test to fail) 
          - all private methods now order files followed by folders
		    (consistent with existing public APIs such as FileFolderService.search & ScriptNode.childFileFolders*)
          - follow-on to r27146
   28271: Consolidate diagnostic logging for max perm checks (ALF-8388 + ALF-8419)
          - note: this should be a trivial merge to HEAD

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28292 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-06-08 17:29:54 +00:00

319 lines
11 KiB
Java

/*
* Copyright (C) 2005-2010 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.model.filefolder;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import net.sf.acegisecurity.Authentication;
import net.sf.acegisecurity.ConfigAttributeDefinition;
import net.sf.acegisecurity.context.Context;
import net.sf.acegisecurity.context.ContextHolder;
import net.sf.acegisecurity.context.security.SecureContext;
import org.alfresco.model.ContentModel;
import org.alfresco.query.AbstractCannedQuery;
import org.alfresco.query.CannedQueryParameters;
import org.alfresco.query.CannedQuerySortDetails;
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityInterceptor;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.MLText;
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.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Canned query to get children of a parent node
*
* @author janv
* @since 4.0
*/
public class GetChildrenCannedQuery extends AbstractCannedQuery<NodeRef>
{
private Log logger = LogFactory.getLog(getClass());
private NodeService nodeService;
private MethodSecurityInterceptor methodSecurityInterceptor;
public GetChildrenCannedQuery(
NodeService nodeService,
MethodSecurityInterceptor methodSecurityInterceptor,
CannedQueryParameters params,
String queryExecutionId)
{
super(params, queryExecutionId);
this.nodeService = nodeService;
this.methodSecurityInterceptor = methodSecurityInterceptor;
}
@Override
protected List<NodeRef> query(CannedQueryParameters parameters)
{
long start = System.currentTimeMillis();
GetChildrenCannedQueryParams paramBean = (GetChildrenCannedQueryParams)parameters.getParameterBean();
List<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(paramBean.getParentRef(), paramBean.getSearchTypeQNames());
List<NodeRef> result = new ArrayList<NodeRef>(childAssocRefs.size());
for (ChildAssociationRef assocRef : childAssocRefs)
{
result.add(assocRef.getChildRef());
}
if (logger.isDebugEnabled())
{
logger.debug("Raw query: "+result.size()+" in "+(System.currentTimeMillis()-start)+" msecs");
}
return result;
}
@Override
protected boolean isApplyPostQuerySorting()
{
return true;
}
@Override
protected List<NodeRef> applyPostQuerySorting(List<NodeRef> results, CannedQuerySortDetails sortDetails)
{
if (sortDetails.getSortPairs().size() == 0)
{
// Nothing to sort on
return results;
}
long start = System.currentTimeMillis();
List<NodeRef> ret = new ArrayList<NodeRef>(results);
List<Pair<? extends Object, SortOrder>> sortPairs = sortDetails.getSortPairs();
Collections.sort(ret, new PropComparatorAsc(sortPairs));
if (logger.isDebugEnabled())
{
logger.debug("Post query sort: "+ret.size()+" in "+(System.currentTimeMillis()-start)+" msecs");
}
return ret;
}
private class PropComparatorAsc implements Comparator<NodeRef>
{
private List<Pair<? extends Object, SortOrder>> sortProps;
public PropComparatorAsc(List<Pair<? extends Object, SortOrder>> sortProps)
{
this.sortProps = sortProps;
}
public int compare(NodeRef n1, NodeRef n2)
{
return compareImpl(n1, n2, sortProps);
}
private int compareImpl(NodeRef node1In, NodeRef node2In, List<Pair<? extends Object, SortOrder>> sortProps)
{
Object pv1 = null;
Object pv2 = null;
QName sortPropQName = (QName)sortProps.get(0).getFirst();
boolean sortAscending = (sortProps.get(0).getSecond() == SortOrder.ASCENDING);
NodeRef node1 = node1In;
NodeRef node2 = node2In;
if (sortAscending == false)
{
node1 = node2In;
node2 = node1In;
}
int result = 0;
if (sortPropQName.equals(QName.createQName(".size")) || sortPropQName.equals(QName.createQName(".mimetype")))
{
// content data properties (size or mimetype)
ContentData cd1 = (ContentData)nodeService.getProperty(node1, ContentModel.PROP_CONTENT);
ContentData cd2 = (ContentData)nodeService.getProperty(node2, ContentModel.PROP_CONTENT);
if (cd1 == null)
{
return (cd2 == null ? 0 : 1);
}
else if (cd2 == null)
{
return -1;
}
if (sortPropQName.equals(QName.createQName(".size")))
{
result = ((Long)cd1.getSize()).compareTo((Long)cd2.getSize());
}
else if (sortPropQName.equals(QName.createQName(".mimetype")))
{
result = (cd1.getMimetype()).compareTo(cd2.getMimetype());
}
}
else
{
// property other than content size / mimetype
pv1 = nodeService.getProperty(node1, sortPropQName);
pv2 = nodeService.getProperty(node2, sortPropQName);
if (pv1 == null)
{
return (pv2 == null ? 0 : 1);
}
else if (pv2 == null)
{
return -1;
}
if (pv1 instanceof String)
{
result = (((String)pv1).compareTo((String)pv2));
}
else if (pv1 instanceof MLText) // eg. title, description
{
String sv1 = DefaultTypeConverter.INSTANCE.convert(String.class, (MLText)pv1);
String sv2 = DefaultTypeConverter.INSTANCE.convert(String.class, (MLText)pv2);
result = (sv1.compareTo(sv2));
}
else if (pv1 instanceof Date)
{
result = (((Date)pv1).compareTo((Date)pv2));
}
else
{
// TODO other comparisons
throw new RuntimeException("Unsupported sort type");
}
}
if ((result == 0) && (sortProps.size() > 1))
{
return compareImpl(node1In, node2In, sortProps.subList(1, sortProps.size()-1));
}
return result;
}
}
@Override
protected boolean isApplyPostQueryPermissions()
{
return true;
}
@SuppressWarnings("unchecked")
@Override
protected List<NodeRef> applyPostQueryPermissions(List<NodeRef> results, String authenticationToken, int requestedCount)
{
long start = System.currentTimeMillis();
// TODO push down cut-off (as option within permission interceptor)
//boolean cutoffAllowed = !getParameters().isReturnTotalResultCount();
List<NodeRef> ret = new ArrayList<NodeRef>(results.size());
Context context = ContextHolder.getContext();
if ((context == null) || !(context instanceof SecureContext))
{
return ret; // empty list
}
Authentication authentication = (((SecureContext) context).getAuthentication());
Method listMethod = null;
for (Method method : FileFolderServiceImpl.class.getMethods())
{
if (method.getName().equals("list"))
{
// found one of the list methods
listMethod = method;
break;
}
}
if (listMethod == null)
{
return ret; // empty list
}
// TODO ALF-8419
ConfigAttributeDefinition listCad = methodSecurityInterceptor.getObjectDefinitionSource().getAttributes(new InternalMethodInvocation(listMethod));
ret = (List<NodeRef>)methodSecurityInterceptor.getAfterInvocationManager().decide(authentication, null, listCad, results); // need to be able to cut-off etc ...
if (logger.isDebugEnabled())
{
logger.debug("Post query perms: "+ret.size()+" in "+(System.currentTimeMillis()-start)+" msecs");
}
return ret;
}
class InternalMethodInvocation implements MethodInvocation {
Method method;
public InternalMethodInvocation(Method method) {
this.method = method;
}
protected InternalMethodInvocation() {
throw new UnsupportedOperationException();
}
public Object[] getArguments() {
throw new UnsupportedOperationException();
}
public Method getMethod() {
return this.method;
}
public AccessibleObject getStaticPart() {
throw new UnsupportedOperationException();
}
public Object getThis() {
throw new UnsupportedOperationException();
}
public Object proceed() throws Throwable {
throw new UnsupportedOperationException();
}
}
}