/**
* Copyright (C) 2008 - Abiquo Holdings S.L. All rights reserved.
*
* Please see /opt/abiquo/tomcat/webapps/legal/ on Abiquo server
* or contact contact@abiquo.com for licensing information.
*/
package com.abiquo.server.core.statistics;
import javax.persistence.EntityManager;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Repository;
import com.abiquo.server.core.common.persistence.DefaultDAOBase;
import com.softwarementors.bzngine.engines.hibernate.HibernateEntityManagerHelper;
@Repository("jpaEnterpriseResourcesDAO")
public class EnterpriseResourcesDAO extends DefaultDAOBase<Integer, EnterpriseResources>
{
public EnterpriseResourcesDAO()
{
super(EnterpriseResources.class);
}
public EnterpriseResourcesDAO(EntityManager entityManager)
{
super(EnterpriseResources.class, entityManager);
}
public EnterpriseResources sumTotalResourcesByEnterprise()
{
// TODO: Include aggregators functionality in bzengine?
Session ses = HibernateEntityManagerHelper.getSession(getEntityManager());
Criteria crit = ses.createCriteria(EnterpriseResources.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.sum(EnterpriseResources.V_CPU_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.V_CPU_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.MEMORY_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.MEMORY_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.LOCAL_STORAGE_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.LOCAL_STORAGE_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.PUBLIC_I_PS_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.PUBLIC_I_PS_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.VLAN_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.VLAN_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.EXT_STORAGE_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.EXT_STORAGE_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.REPOSITORY_RESERVED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.REPOSITORY_USED_PROPERTY));
proList.add(Projections.sum(EnterpriseResources.REPOSITORY_USED_PROPERTY));
crit.setProjection(proList);
Object[] obj = (Object[]) crit.uniqueResult(); // Returns Object[] ->
EnterpriseResources result = new EnterpriseResources();
int cont = 0;
result.setVirtualCpuReserved((Long) obj[cont++]);
result.setVirtualCpuUsed((Long) obj[cont++]);
result.setMemoryReserved((Long) obj[cont++]);
result.setMemoryUsed((Long) obj[cont++]);
result.setLocalStorageReserved((Long) obj[cont++]);
result.setLocalStorageUsed((Long) obj[cont++]);
result.setPublicIPsReserved((Long) obj[cont++]);
result.setPublicIPsUsed((Long) obj[cont++]);
result.setVlanReserved((Long) obj[cont++]);
result.setVlanUsed((Long) obj[cont++]);
result.setExtStorageReserved((Long) obj[cont++]);
result.setExtStorageUsed((Long) obj[cont++]);
result.setRepositoryReserved((Long) obj[cont++]);
result.setRepositoryUsed((Long) obj[cont++]);
return result;
}
}