/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.repo.usage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.node.db.NodeDaoService.ObjectArrayQueryCallback;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.tenant.Tenant;
import org.alfresco.repo.tenant.TenantAdminService;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.transaction.TransactionServiceImpl;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.usage.UsageService;
import org.alfresco.service.namespace.QName;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
import org.springframework.extensions.surf.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
/**
* User Usage Tracking Component - to allow user usages to be collapsed or re-calculated
*
* - used by UserUsageCollapseJob to collapse usage deltas.
* - used on bootstrap to either clear all usages or (re-)calculate all missing usages.
*/
public class UserUsageTrackingComponent extends AbstractLifecycleBean
{
private static Log logger = LogFactory.getLog(UserUsageTrackingComponent.class);
private TransactionServiceImpl transactionService;
private ContentUsageImpl contentUsageImpl;
private NodeService nodeService;
private NodeDaoService nodeDaoService;
private UsageService usageService;
private TenantAdminService tenantAdminService;
private TenantService tenantService;
private StoreRef personStoreRef;
private int clearBatchSize = 50;
private int updateBatchSize = 50;
private boolean enabled = true;
private Lock writeLock = new ReentrantLock();
public void setTransactionService(TransactionServiceImpl transactionService)
{
this.transactionService = transactionService;
}
public void setContentUsageImpl(ContentUsageImpl contentUsageImpl)
{
this.contentUsageImpl = contentUsageImpl;
}
public void setPersonStoreUrl(String storeUrl)
{
this.personStoreRef = new StoreRef(storeUrl);
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setNodeDaoService(NodeDaoService nodeDaoService)
{
this.nodeDaoService = nodeDaoService;
}
public void setUsageService(UsageService usageService)
{
this.usageService = usageService;
}
public void setTenantAdminService(TenantAdminService tenantAdminService)
{
this.tenantAdminService = tenantAdminService;
}
public void setTenantService(TenantService tenantService)
{
this.tenantService = tenantService;
}
public void setClearBatchSize(int clearBatchSize)
{
this.clearBatchSize = clearBatchSize;
}
public void setUpdateBatchSize(int updateBatchSize)
{
this.updateBatchSize = updateBatchSize;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
public void execute()
{
if (enabled == false || transactionService.isReadOnly())
{
return;
}
boolean locked = writeLock.tryLock();
if (locked)
{
// collapse usages - note: for MT environment, will collapse for all tenants
try
{
collapseUsages();
}
finally
{
writeLock.unlock();
}
}
}
@Override
protected void onBootstrap(ApplicationEvent event)
{
// default domain
bootstrapInternal();
if (tenantAdminService.isEnabled())
{
List tenants = tenantAdminService.getAllTenants();
for (Tenant tenant : tenants)
{
AuthenticationUtil.runAs(new RunAsWork