Search in sources :

Example 46 with BlockingState

use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.

the class EventsStreamBuilder method buildForEntitlement.

private EventsStream buildForEntitlement(final List<BlockingState> blockingStatesForAccount, final ImmutableAccountData account, final SubscriptionBaseBundle bundle, @Nullable final SubscriptionBase baseSubscription, final SubscriptionBase subscription, final List<SubscriptionBase> allSubscriptionsForBundle, final Map<UUID, Integer> bcdCache, final InternalTenantContext internalTenantContext) throws EntitlementApiException {
    // Optimization: build lookup tables for blocking states states
    final Collection<BlockingState> accountBlockingStates = new LinkedList<BlockingState>();
    final Map<UUID, List<BlockingState>> blockingStatesPerSubscription = new HashMap<UUID, List<BlockingState>>();
    final Map<UUID, List<BlockingState>> blockingStatesPerBundle = new HashMap<UUID, List<BlockingState>>();
    for (final BlockingState blockingState : blockingStatesForAccount) {
        if (BlockingStateType.SUBSCRIPTION.equals(blockingState.getType())) {
            if (blockingStatesPerSubscription.get(blockingState.getBlockedId()) == null) {
                blockingStatesPerSubscription.put(blockingState.getBlockedId(), new LinkedList<BlockingState>());
            }
            blockingStatesPerSubscription.get(blockingState.getBlockedId()).add(blockingState);
        } else if (BlockingStateType.SUBSCRIPTION_BUNDLE.equals(blockingState.getType())) {
            if (blockingStatesPerBundle.get(blockingState.getBlockedId()) == null) {
                blockingStatesPerBundle.put(blockingState.getBlockedId(), new LinkedList<BlockingState>());
            }
            blockingStatesPerBundle.get(blockingState.getBlockedId()).add(blockingState);
        } else if (BlockingStateType.ACCOUNT.equals(blockingState.getType()) && account.getId().equals(blockingState.getBlockedId())) {
            accountBlockingStates.add(blockingState);
        }
    }
    final List<BlockingState> bundleBlockingStates = MoreObjects.firstNonNull(blockingStatesPerBundle.get(subscription.getBundleId()), ImmutableList.<BlockingState>of());
    final List<BlockingState> subscriptionBlockingStatesOnDisk = MoreObjects.firstNonNull(blockingStatesPerSubscription.get(subscription.getId()), ImmutableList.<BlockingState>of());
    // We cannot always use blockingStatesForAccount here: we need subscriptionBlockingStates to contain the events not on disk when building an EventsStream
    // for an add-on - which means going through the magic of ProxyBlockingStateDao, which will recursively
    // create EventsStream objects. To avoid an infinite recursion, bypass ProxyBlockingStateDao when it's not
    // needed, i.e. if this EventStream is for a standalone or a base subscription
    final Collection<BlockingState> subscriptionBlockingStates;
    if (baseSubscription == null || subscription.getId().equals(baseSubscription.getId())) {
        // Note: we come here during the recursion from OptimizedProxyBlockingStateDao#getBlockingHistory
        // (called by blockingStateDao.getBlockingHistory below)
        subscriptionBlockingStates = subscriptionBlockingStatesOnDisk;
    } else {
        subscriptionBlockingStates = blockingStateDao.getBlockingHistory(ImmutableList.<BlockingState>copyOf(subscriptionBlockingStatesOnDisk), blockingStatesForAccount, account, bundle, baseSubscription, subscription, allSubscriptionsForBundle, internalTenantContext);
    }
    // Merge the BlockingStates
    final Collection<BlockingState> blockingStateSet = new LinkedHashSet<BlockingState>(accountBlockingStates);
    blockingStateSet.addAll(bundleBlockingStates);
    blockingStateSet.addAll(subscriptionBlockingStates);
    final List<BlockingState> blockingStates = ProxyBlockingStateDao.sortedCopy(blockingStateSet);
    return buildForEntitlement(account, bundle, baseSubscription, subscription, allSubscriptionsForBundle, blockingStates, bcdCache, internalTenantContext);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) LinkedList(java.util.LinkedList)

Aggregations

BlockingState (org.killbill.billing.entitlement.api.BlockingState)46 UUID (java.util.UUID)24 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)22 Test (org.testng.annotations.Test)15 DateTime (org.joda.time.DateTime)13 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)6 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)6 LocalDate (org.joda.time.LocalDate)5 ImmutableList (com.google.common.collect.ImmutableList)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Account (org.killbill.billing.account.api.Account)4 BillingEvent (org.killbill.billing.junction.BillingEvent)4 Optional (com.google.common.base.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 TreeSet (java.util.TreeSet)3 AccountApiException (org.killbill.billing.account.api.AccountApiException)3 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)3 BlockingStateType (org.killbill.billing.entitlement.api.BlockingStateType)3