/*
* 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 .
*/
package org.alfresco.repo.query;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.query.AbstractCannedQueryFactory;
import org.alfresco.query.CannedQueryPageDetails;
import org.alfresco.query.CannedQuerySortDetails;
import org.alfresco.query.PagingRequest;
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.domain.query.CannedQueryDAO;
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityBean;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* An intermediate {@link AbstractCannedQueryFactory} layer, for various
* implementations that need to know about QName IDs and similar
*
* @author Nick Burch
* @since 4.0
*/
public abstract class AbstractQNameAwareCannedQueryFactory extends AbstractCannedQueryFactory
{
private Log logger = LogFactory.getLog(getClass());
protected MethodSecurityBean methodSecurity;
protected NodeDAO nodeDAO;
protected QNameDAO qnameDAO;
protected TenantService tenantService;
protected CannedQueryDAO cannedQueryDAO;
public void setNodeDAO(NodeDAO nodeDAO)
{
this.nodeDAO = nodeDAO;
}
public void setQnameDAO(QNameDAO qnameDAO)
{
this.qnameDAO = qnameDAO;
}
public void setCannedQueryDAO(CannedQueryDAO cannedQueryDAO)
{
this.cannedQueryDAO = cannedQueryDAO;
}
public void setTenantService(TenantService tenantService)
{
this.tenantService = tenantService;
}
public void setMethodSecurity(MethodSecurityBean methodSecurity)
{
this.methodSecurity = methodSecurity;
}
@Override
public void afterPropertiesSet() throws Exception
{
super.afterPropertiesSet();
PropertyCheck.mandatory(this, "methodSecurity", methodSecurity);
PropertyCheck.mandatory(this, "nodeDAO", nodeDAO);
PropertyCheck.mandatory(this, "qnameDAO", qnameDAO);
PropertyCheck.mandatory(this, "cannedQueryDAO", cannedQueryDAO);
PropertyCheck.mandatory(this, "tenantService", tenantService);
}
/**
* Creates a Canned Query sort details, for the given list of properties
* and if they should be Ascending or Descending
*/
protected CannedQuerySortDetails createCQSortDetails(List> sort)
{
List> details = new ArrayList>();
for(Pair sortProp : sort)
{
details.add(new Pair(
sortProp.getFirst(),
(sortProp.getSecond() ? SortOrder.ASCENDING : SortOrder.DESCENDING)
));
}
return new CannedQuerySortDetails(details);
}
protected CannedQueryPageDetails createCQPageDetails(PagingRequest pagingReq)
{
int skipCount = pagingReq.getSkipCount();
if (skipCount == -1)
{
skipCount = CannedQueryPageDetails.DEFAULT_SKIP_RESULTS;
}
int maxItems = pagingReq.getMaxItems();
if (maxItems == -1)
{
maxItems = CannedQueryPageDetails.DEFAULT_PAGE_SIZE;
}
// page details
CannedQueryPageDetails cqpd = new CannedQueryPageDetails(skipCount, maxItems);
return cqpd;
}
protected Long getQNameId(QName qname)
{
Pair qnamePair = qnameDAO.getQName(qname);
if (qnamePair == null)
{
if (logger.isTraceEnabled())
{
logger.trace("QName does not exist: " + qname); // possible ... eg. blg:blogPost if a blog has never been posted externally
}
return null;
}
return qnamePair.getFirst();
}
protected Long getNodeId(NodeRef nodeRef)
{
Pair nodePair = nodeDAO.getNodePair(tenantService.getName(nodeRef));
if (nodePair == null)
{
throw new InvalidNodeRefException("Node ref does not exist: " + nodeRef, nodeRef);
}
return nodePair.getFirst();
}
}