quota: added a method to get the last balance in balancedao

This commit is contained in:
Abhinandan Prateek 2015-07-28 15:32:50 +05:30
parent b64aee0a5e
commit ca6dfc99ac
3 changed files with 53 additions and 27 deletions

View File

@ -84,33 +84,6 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
return response;
}
@Override
public QuotaBalanceResponse createQuotaLastBalanceResponse(List<QuotaBalanceVO> quotaBalance, Date startDate) {
if (quotaBalance.size() == 0) {
new InvalidParameterValueException("There are no balance entries on or before the requested date.");
}
Collections.sort(quotaBalance, new Comparator<QuotaBalanceVO>() {
public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
return o2.getUpdatedOn().compareTo(o1.getUpdatedOn()); // desc
}
});
QuotaBalanceResponse resp = new QuotaBalanceResponse();
BigDecimal lastCredits = new BigDecimal(0);
for (Iterator<QuotaBalanceVO> it = quotaBalance.iterator(); it.hasNext();) {
QuotaBalanceVO entry = it.next();
s_logger.info("Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId());
if (lastCredits.compareTo(new BigDecimal(0)) == 0) {
resp.setStartQuota(entry.getCreditBalance());
resp.setStartDate(startDate);
}
lastCredits = lastCredits.add(entry.getCreditBalance());
resp.addCredits(entry);
}
resp.setEndQuota(lastCredits);
resp.setEndDate(_quotaService.computeAdjustedTime(new Date()));
resp.setObjectName("balance");
return resp;
}
@Override
public QuotaBalanceResponse createQuotaBalanceResponse(List<QuotaBalanceVO> quotaBalance, Date startDate, Date endDate) {
@ -344,6 +317,35 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
}
return false;
}
@Override
public QuotaBalanceResponse createQuotaLastBalanceResponse(List<QuotaBalanceVO> quotaBalance, Date startDate) {
if (quotaBalance.size() == 0) {
new InvalidParameterValueException("There are no balance entries on or before the requested date.");
}
Collections.sort(quotaBalance, new Comparator<QuotaBalanceVO>() {
public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
return o2.getUpdatedOn().compareTo(o1.getUpdatedOn()); // desc
}
});
QuotaBalanceResponse resp = new QuotaBalanceResponse();
BigDecimal lastCredits = new BigDecimal(0);
for (Iterator<QuotaBalanceVO> it = quotaBalance.iterator(); it.hasNext();) {
QuotaBalanceVO entry = it.next();
s_logger.info("Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId());
if (lastCredits.compareTo(new BigDecimal(0)) == 0) {
resp.setStartQuota(entry.getCreditBalance());
resp.setStartDate(startDate);
}
lastCredits = lastCredits.add(entry.getCreditBalance());
resp.addCredits(entry);
}
resp.setEndQuota(lastCredits);
resp.setEndDate(_quotaService.computeAdjustedTime(new Date()));
resp.setObjectName("balance");
return resp;
}
@Override
public List<QuotaUsageVO> getQuotaUsage(QuotaStatementCmd cmd) {

View File

@ -16,6 +16,7 @@
//under the License.
package org.apache.cloudstack.quota.dao;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@ -37,4 +38,6 @@ public interface QuotaBalanceDao extends GenericDao<QuotaBalanceVO, Long> {
List<QuotaBalanceVO> findQuotaBalance(Long accountId, Long domainId, Date startDate);
BigDecimal createQuotaLastBalanceResponse(Long accountId, Long domainId, Date startDate);
}

View File

@ -16,7 +16,10 @@
//under the License.
package org.apache.cloudstack.quota.dao;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
@ -24,9 +27,11 @@ import java.util.List;
import javax.ejb.Local;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.api.response.QuotaBalanceResponse;
import org.apache.cloudstack.quota.vo.QuotaBalanceVO;
import org.apache.log4j.Logger;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchCriteria;
@ -177,5 +182,21 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
TransactionLegacy.open(opendb).close();
return trimmedRecords;
}
@Override
public BigDecimal createQuotaLastBalanceResponse(final Long accountId, final Long domainId, Date startDate) {
List<QuotaBalanceVO> quotaBalance = findQuotaBalance(accountId, domainId, startDate);
if (quotaBalance.size() == 0) {
new InvalidParameterValueException("There are no balance entries on or before the requested date.");
}
BigDecimal finalBalance = new BigDecimal(0);
for (Iterator<QuotaBalanceVO> it = quotaBalance.iterator(); it.hasNext();) {
QuotaBalanceVO entry = it.next();
s_logger.info("Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId());
finalBalance.add(entry.getCreditBalance());
}
return finalBalance;
}
}