Search in sources :

Example 1 with AccountEntitlements

use of org.killbill.billing.entitlement.AccountEntitlements in project killbill by killbill.

the class DefaultSubscriptionApi method getSubscriptionBundlesForAccount.

private List<SubscriptionBundle> getSubscriptionBundlesForAccount(final UUID accountId, final TenantContext tenantContext) throws SubscriptionApiException {
    final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
    // Retrieve entitlements
    final AccountEntitlements accountEntitlements;
    try {
        accountEntitlements = entitlementInternalApi.getAllEntitlementsForAccount(internalTenantContextWithValidAccountRecordId);
    } catch (final EntitlementApiException e) {
        throw new SubscriptionApiException(e);
    }
    // Build subscriptions
    final Map<UUID, List<Subscription>> subscriptionsPerBundle = buildSubscriptionsFromEntitlements(accountEntitlements);
    // Build subscription bundles
    final List<SubscriptionBundle> bundles = new LinkedList<SubscriptionBundle>();
    for (final UUID bundleId : subscriptionsPerBundle.keySet()) {
        final List<Subscription> subscriptionsForBundle = subscriptionsPerBundle.get(bundleId);
        final String externalKey = subscriptionsForBundle.get(0).getExternalKey();
        final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, accountEntitlements.getEntitlements().get(bundleId), internalTenantContextWithValidAccountRecordId);
        final SubscriptionBaseBundle baseBundle = accountEntitlements.getBundles().get(bundleId);
        final SubscriptionBundle subscriptionBundle = new DefaultSubscriptionBundle(bundleId, accountId, externalKey, subscriptionsForBundle, timeline, baseBundle.getOriginalCreatedDate(), baseBundle.getCreatedDate(), baseBundle.getUpdatedDate());
        bundles.add(subscriptionBundle);
    }
    // Sort the results for predictability
    return Ordering.<SubscriptionBundle>from(SUBSCRIPTION_BUNDLE_COMPARATOR).sortedCopy(bundles);
}
Also used : LinkedList(java.util.LinkedList) AccountEntitlements(org.killbill.billing.entitlement.AccountEntitlements) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID)

Example 2 with AccountEntitlements

use of org.killbill.billing.entitlement.AccountEntitlements in project killbill by killbill.

the class DefaultSubscriptionApi method getSubscriptionForEntitlementId.

@Override
public Subscription getSubscriptionForEntitlementId(final UUID entitlementId, final TenantContext tenantContext) throws SubscriptionApiException {
    // Retrieve entitlements
    final AccountEntitlements accountEntitlements;
    try {
        final UUID accountId = internalCallContextFactory.getAccountId(entitlementId, ObjectType.SUBSCRIPTION, tenantContext);
        final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
        accountEntitlements = entitlementInternalApi.getAllEntitlementsForAccount(internalTenantContextWithValidAccountRecordId);
    } catch (final EntitlementApiException e) {
        throw new SubscriptionApiException(e);
    }
    // Build subscriptions
    final Iterable<Subscription> accountSubscriptions = Iterables.<Subscription>concat(buildSubscriptionsFromEntitlements(accountEntitlements).values());
    return Iterables.<Subscription>find(accountSubscriptions, new Predicate<Subscription>() {

        @Override
        public boolean apply(final Subscription subscription) {
            return subscription.getId().equals(entitlementId);
        }
    });
}
Also used : AccountEntitlements(org.killbill.billing.entitlement.AccountEntitlements) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)2 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)2 AccountEntitlements (org.killbill.billing.entitlement.AccountEntitlements)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)1