use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class BlockingCalculator method eventsToRemove.
protected SortedSet<BillingEvent> eventsToRemove(final List<DisabledDuration> disabledDuration, final SortedSet<BillingEvent> billingEvents, final SubscriptionBase subscription) {
final SortedSet<BillingEvent> result = new TreeSet<BillingEvent>();
final SortedSet<BillingEvent> filteredBillingEvents = filter(billingEvents, subscription);
for (final DisabledDuration duration : disabledDuration) {
for (final BillingEvent event : filteredBillingEvents) {
if (duration.getEnd() == null || event.getEffectiveDate().isBefore(duration.getEnd())) {
if (event.getEffectiveDate().isAfter(duration.getStart())) {
//between the pair
result.add(event);
}
} else {
//after the last event of the pair no need to keep checking
break;
}
}
}
return result;
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class BlockingCalculator method insertBlockingEvents.
/**
* Given a set of billing events, add corresponding blocking (overdue) billing events.
*
* @param billingEvents the original list of billing events to update (without overdue events)
*/
public boolean insertBlockingEvents(final SortedSet<BillingEvent> billingEvents, final Set<UUID> skippedSubscriptions, final InternalTenantContext context) throws CatalogApiException {
if (billingEvents.size() <= 0) {
return false;
}
final Hashtable<UUID, List<SubscriptionBase>> bundleMap = createBundleSubscriptionMap(billingEvents);
final SortedSet<BillingEvent> billingEventsToAdd = new TreeSet<BillingEvent>();
final SortedSet<BillingEvent> billingEventsToRemove = new TreeSet<BillingEvent>();
final List<BlockingState> blockingEvents = blockingApi.getBlockingAllForAccount(context);
final Iterable<BlockingState> accountBlockingEvents = Iterables.filter(blockingEvents, new Predicate<BlockingState>() {
@Override
public boolean apply(final BlockingState input) {
return BlockingStateType.ACCOUNT == input.getType();
}
});
final Map<UUID, List<BlockingState>> perBundleBlockingEvents = getPerTypeBlockingEvents(BlockingStateType.SUBSCRIPTION_BUNDLE, blockingEvents);
final Map<UUID, List<BlockingState>> perSubscriptionBlockingEvents = getPerTypeBlockingEvents(BlockingStateType.SUBSCRIPTION, blockingEvents);
for (final UUID bundleId : bundleMap.keySet()) {
final List<BlockingState> bundleBlockingEvents = perBundleBlockingEvents.get(bundleId) != null ? perBundleBlockingEvents.get(bundleId) : ImmutableList.<BlockingState>of();
for (final SubscriptionBase subscription : bundleMap.get(bundleId)) {
// Avoid inserting additional events for subscriptions that don't even have a START event
if (skippedSubscriptions.contains(subscription.getId())) {
continue;
}
final List<BlockingState> subscriptionBlockingEvents = perSubscriptionBlockingEvents.get(subscription.getId()) != null ? perSubscriptionBlockingEvents.get(subscription.getId()) : ImmutableList.<BlockingState>of();
final List<BlockingState> aggregateSubscriptionBlockingEvents = getAggregateBlockingEventsPerSubscription(subscription.getEndDate(), subscriptionBlockingEvents, bundleBlockingEvents, accountBlockingEvents);
final List<DisabledDuration> accountBlockingDurations = createBlockingDurations(aggregateSubscriptionBlockingEvents);
billingEventsToAdd.addAll(createNewEvents(accountBlockingDurations, billingEvents, subscription, context));
billingEventsToRemove.addAll(eventsToRemove(accountBlockingDurations, billingEvents, subscription));
}
}
for (final BillingEvent eventToAdd : billingEventsToAdd) {
billingEvents.add(eventToAdd);
}
for (final BillingEvent eventToRemove : billingEventsToRemove) {
billingEvents.remove(eventToRemove);
}
return !(billingEventsToAdd.isEmpty() && billingEventsToRemove.isEmpty());
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class DefaultBillingEventSet method getUsages.
@Override
public Map<String, Usage> getUsages() {
final Iterable<Usage> allUsages = Iterables.concat(Iterables.transform(this, new Function<BillingEvent, List<Usage>>() {
@Override
public List<Usage> apply(final BillingEvent input) {
return input.getUsages();
}
}));
if (!allUsages.iterator().hasNext()) {
return Collections.emptyMap();
}
final Map<String, Usage> result = new HashMap<String, Usage>();
for (Usage cur : Sets.<Usage>newHashSet(allUsages)) {
result.put(cur.getName(), cur);
}
return result;
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class TestDefaultBillingEvent method testEventOrderingMix.
@Test(groups = "fast")
public void testEventOrderingMix() {
final BillingEvent event0 = createEvent(subscription(ID_ZERO), new DateTime("2012-01-01T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
final BillingEvent event1 = createEvent(subscription(ID_ZERO), new DateTime("2012-01-02T00:02:04.000Z"), SubscriptionBaseTransitionType.CHANGE);
final BillingEvent event2 = createEvent(subscription(ID_ONE), new DateTime("2012-01-01T00:02:04.000Z"), SubscriptionBaseTransitionType.CANCEL);
final SortedSet<BillingEvent> set = new TreeSet<BillingEvent>();
set.add(event2);
set.add(event1);
set.add(event0);
final Iterator<BillingEvent> it = set.iterator();
Assert.assertEquals(event0, it.next());
Assert.assertEquals(event1, it.next());
Assert.assertEquals(event2, it.next());
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class TestDefaultBillingEvent method testEventOrderingSubscription.
@Test(groups = "fast")
public void testEventOrderingSubscription() {
final BillingEvent event0 = createEvent(subscription(ID_ZERO), new DateTime("2012-01-31T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
final BillingEvent event1 = createEvent(subscription(ID_ONE), new DateTime("2012-01-31T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
final BillingEvent event2 = createEvent(subscription(ID_TWO), new DateTime("2012-01-31T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
final SortedSet<BillingEvent> set = new TreeSet<BillingEvent>();
set.add(event2);
set.add(event1);
set.add(event0);
final Iterator<BillingEvent> it = set.iterator();
Assert.assertEquals(event0, it.next());
Assert.assertEquals(event1, it.next());
Assert.assertEquals(event2, it.next());
}
Aggregations