public long calculateSecondaryStorageForAccount(long accountId) { long totalVolumesSize = _volumeDao.secondaryStorageUsedForAccount(accountId); long totalSnapshotsSize = 0; long totalTemplatesSize = 0; SearchCriteria<SumCount> sc = templateSizeSearch.create(); sc.setParameters("downloadState", Status.DOWNLOADED); sc.setParameters("destroyed", false); sc.setJoinParameters("templates", "accountId", accountId); List<SumCount> templates = _vmTemplateStoreDao.customSearch(sc, null); if (templates != null) { totalTemplatesSize = templates.get(0).sum; } SearchCriteria<SumCount> sc2 = snapshotSizeSearch.create(); sc2.setParameters("state", ObjectInDataStoreStateMachine.State.Ready); sc2.setParameters("storeRole", DataStoreRole.Image); sc2.setJoinParameters("snapshots", "accountId", accountId); List<SumCount> snapshots = _snapshotDataStoreDao.customSearch(sc2, null); if (snapshots != null) { totalSnapshotsSize = snapshots.get(0).sum; } return totalVolumesSize + totalSnapshotsSize + totalTemplatesSize; }