Search in sources :

Example 1 with QuotaTypes

use of org.apache.cloudstack.quota.constant.QuotaTypes in project cloudstack by apache.

the class QuotaResponseBuilderImpl method createQuotaStatementResponse.

@Override
public QuotaStatementResponse createQuotaStatementResponse(final List<QuotaUsageVO> quotaUsage) {
    if (quotaUsage == null || quotaUsage.isEmpty()) {
        throw new InvalidParameterValueException("There is no usage data found for period mentioned.");
    }
    QuotaStatementResponse statement = new QuotaStatementResponse();
    HashMap<Integer, QuotaTypes> quotaTariffMap = new HashMap<Integer, QuotaTypes>();
    Collection<QuotaTypes> result = QuotaTypes.listQuotaTypes().values();
    for (QuotaTypes quotaTariff : result) {
        quotaTariffMap.put(quotaTariff.getQuotaType(), quotaTariff);
        // add dummy record for each usage type
        QuotaUsageVO dummy = new QuotaUsageVO(quotaUsage.get(0));
        dummy.setUsageType(quotaTariff.getQuotaType());
        dummy.setQuotaUsed(new BigDecimal(0));
        quotaUsage.add(dummy);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("createQuotaStatementResponse Type=" + quotaUsage.get(0).getUsageType() + " usage=" + quotaUsage.get(0).getQuotaUsed().setScale(2, RoundingMode.HALF_EVEN) + " rec.id=" + quotaUsage.get(0).getUsageItemId() + " SD=" + quotaUsage.get(0).getStartDate() + " ED=" + quotaUsage.get(0).getEndDate());
    }
    Collections.sort(quotaUsage, new Comparator<QuotaUsageVO>() {

        public int compare(QuotaUsageVO o1, QuotaUsageVO o2) {
            if (o1.getUsageType() == o2.getUsageType())
                return 0;
            return o1.getUsageType() < o2.getUsageType() ? -1 : 1;
        }
    });
    List<QuotaStatementItemResponse> items = new ArrayList<QuotaStatementItemResponse>();
    QuotaStatementItemResponse lineitem;
    int type = -1;
    BigDecimal usage = new BigDecimal(0);
    BigDecimal totalUsage = new BigDecimal(0);
    // boundary
    quotaUsage.add(new QuotaUsageVO());
    QuotaUsageVO prev = quotaUsage.get(0);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("createQuotaStatementResponse record count=" + quotaUsage.size());
    }
    for (final QuotaUsageVO quotaRecord : quotaUsage) {
        if (type != quotaRecord.getUsageType()) {
            if (type != -1) {
                lineitem = new QuotaStatementItemResponse(type);
                lineitem.setQuotaUsed(usage);
                lineitem.setAccountId(prev.getAccountId());
                lineitem.setDomainId(prev.getDomainId());
                lineitem.setUsageUnit(quotaTariffMap.get(type).getQuotaUnit());
                lineitem.setUsageName(quotaTariffMap.get(type).getQuotaName());
                lineitem.setObjectName("quotausage");
                items.add(lineitem);
                totalUsage = totalUsage.add(usage);
                usage = new BigDecimal(0);
            }
            type = quotaRecord.getUsageType();
        }
        prev = quotaRecord;
        usage = usage.add(quotaRecord.getQuotaUsed());
    }
    statement.setLineItem(items);
    statement.setTotalQuota(totalUsage);
    statement.setCurrency(QuotaConfig.QuotaCurrencySymbol.value());
    statement.setObjectName("statement");
    return statement;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaTypes(org.apache.cloudstack.quota.constant.QuotaTypes) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 2 with QuotaTypes

use of org.apache.cloudstack.quota.constant.QuotaTypes in project cloudstack by apache.

the class QuotaResponseBuilderImpl method updateQuotaTariffPlan.

@Override
public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
    final int quotaType = cmd.getUsageType();
    final BigDecimal quotaCost = new BigDecimal(cmd.getValue());
    final Date effectiveDate = _quotaService.computeAdjustedTime(cmd.getStartDate());
    final Date now = _quotaService.computeAdjustedTime(new Date());
    // if effective date is in the past return error
    if (effectiveDate.compareTo(now) < 0) {
        throw new InvalidParameterValueException("Incorrect effective date for tariff " + effectiveDate + " is less than now " + now);
    }
    QuotaTypes quotaConstant = QuotaTypes.listQuotaTypes().get(quotaType);
    if (quotaConstant == null) {
        throw new InvalidParameterValueException("Quota type does not exists " + quotaType);
    }
    QuotaTariffVO result = null;
    result = new QuotaTariffVO(quotaType);
    result.setUsageName(quotaConstant.getQuotaName());
    result.setUsageUnit(quotaConstant.getQuotaUnit());
    result.setUsageDiscriminator(quotaConstant.getDiscriminator());
    result.setCurrencyValue(quotaCost);
    result.setEffectiveOn(effectiveDate);
    result.setUpdatedOn(now);
    result.setUpdatedBy(cmd.getEntityOwnerId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug(String.format("Updating Quota Tariff Plan: New value=%s for resource type=%d effective on date=%s", quotaCost, quotaType, effectiveDate));
    }
    _quotaTariffDao.addQuotaTariff(result);
    return result;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaTypes(org.apache.cloudstack.quota.constant.QuotaTypes) QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 BigDecimal (java.math.BigDecimal)2 QuotaTypes (org.apache.cloudstack.quota.constant.QuotaTypes)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 QuotaTariffVO (org.apache.cloudstack.quota.vo.QuotaTariffVO)1 QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)1