Search in sources :

Example 1 with RolledUpUsageModelDao

use of org.killbill.billing.usage.dao.RolledUpUsageModelDao in project killbill by killbill.

the class DefaultUsageUserApi method getRolledUpUnits.

private List<RolledUpUnit> getRolledUpUnits(final List<RolledUpUsageModelDao> usageForSubscription) {
    final Map<String, Long> tmp = new HashMap<String, Long>();
    for (RolledUpUsageModelDao cur : usageForSubscription) {
        Long currentAmount = tmp.get(cur.getUnitType());
        Long updatedAmount = (currentAmount != null) ? currentAmount + cur.getAmount() : cur.getAmount();
        tmp.put(cur.getUnitType(), updatedAmount);
    }
    final List<RolledUpUnit> result = new ArrayList<RolledUpUnit>(tmp.size());
    for (final String unitType : tmp.keySet()) {
        result.add(new DefaultRolledUpUnit(unitType, tmp.get(unitType)));
    }
    return result;
}
Also used : RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao)

Example 2 with RolledUpUsageModelDao

use of org.killbill.billing.usage.dao.RolledUpUsageModelDao in project killbill by killbill.

the class DefaultUsageUserApi method getAllUsageForSubscription.

@Override
public List<RolledUpUsage> getAllUsageForSubscription(final UUID subscriptionId, final List<LocalDate> transitionTimes, final TenantContext tenantContext) {
    final InternalTenantContext internalCallContext = internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContext);
    List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
    LocalDate prevDate = null;
    for (LocalDate curDate : transitionTimes) {
        if (prevDate != null) {
            final List<RolledUpUsageModelDao> usageForSubscription = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, prevDate, curDate, internalCallContext);
            final List<RolledUpUnit> rolledUpAmount = getRolledUpUnits(usageForSubscription);
            result.add(new DefaultRolledUpUsage(subscriptionId, prevDate, curDate, rolledUpAmount));
        }
        prevDate = curDate;
    }
    return result;
}
Also used : RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao) LocalDate(org.joda.time.LocalDate)

Example 3 with RolledUpUsageModelDao

use of org.killbill.billing.usage.dao.RolledUpUsageModelDao in project killbill by killbill.

the class DefaultUsageUserApi method recordRolledUpUsage.

@Override
public void recordRolledUpUsage(final SubscriptionUsageRecord record, final CallContext callContext) throws UsageApiException {
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(record.getSubscriptionId(), ObjectType.SUBSCRIPTION, callContext);
    // check if we have (at least) one row with the supplied tracking id
    if (!Strings.isNullOrEmpty(record.getTrackingId()) && recordsWithTrackingIdExist(record, internalCallContext)) {
        throw new UsageApiException(ErrorCode.USAGE_RECORD_TRACKING_ID_ALREADY_EXISTS, record.getTrackingId());
    }
    final List<RolledUpUsageModelDao> usages = new ArrayList<RolledUpUsageModelDao>();
    for (final UnitUsageRecord unitUsageRecord : record.getUnitUsageRecord()) {
        for (final UsageRecord usageRecord : unitUsageRecord.getDailyAmount()) {
            usages.add(new RolledUpUsageModelDao(record.getSubscriptionId(), unitUsageRecord.getUnitType(), usageRecord.getDate(), usageRecord.getAmount(), record.getTrackingId()));
        }
    }
    rolledUpUsageDao.record(usages, internalCallContext);
}
Also used : UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord) UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) UsageRecord(org.killbill.billing.usage.api.UsageRecord) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao) UsageApiException(org.killbill.billing.usage.api.UsageApiException)

Aggregations

ArrayList (java.util.ArrayList)3 RolledUpUsageModelDao (org.killbill.billing.usage.dao.RolledUpUsageModelDao)3 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)2 HashMap (java.util.HashMap)1 LocalDate (org.joda.time.LocalDate)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)1 SubscriptionUsageRecord (org.killbill.billing.usage.api.SubscriptionUsageRecord)1 UnitUsageRecord (org.killbill.billing.usage.api.UnitUsageRecord)1 UsageApiException (org.killbill.billing.usage.api.UsageApiException)1 UsageRecord (org.killbill.billing.usage.api.UsageRecord)1