Search in sources :

Example 61 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultSubscriptionApi method getSubscriptionForEntitlementId.

@Override
public Subscription getSubscriptionForEntitlementId(final UUID entitlementId, final TenantContext tenantContext) throws SubscriptionApiException {
    try {
        final UUID accountId = internalCallContextFactory.getAccountId(entitlementId, ObjectType.SUBSCRIPTION, tenantContext);
        final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
        final DefaultEntitlement entitlement = (DefaultEntitlement) entitlementInternalApi.getEntitlementForId(entitlementId, internalTenantContextWithValidAccountRecordId);
        return new DefaultSubscription(entitlement);
    } catch (final EntitlementApiException e) {
        throw new SubscriptionApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) UUID(java.util.UUID)

Example 62 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultSubscriptionApi method getBlockingStates.

@Override
public Iterable<BlockingState> getBlockingStates(final UUID accountId, @Nullable final List<BlockingStateType> typeFilter, @Nullable final List<String> svcsFilter, final OrderingType orderingType, final int timeFilter, final TenantContext tenantContext) throws EntitlementApiException {
    try {
        final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
        final VersionedCatalog catalog = catalogInternalApi.getFullCatalog(true, true, internalTenantContextWithValidAccountRecordId);
        final List<BlockingState> allBlockingStates = blockingStateDao.getBlockingAllForAccountRecordId(catalog, internalTenantContextWithValidAccountRecordId);
        final ImmutableAccountData account = accountApi.getImmutableAccountDataById(accountId, internalTenantContextWithValidAccountRecordId);
        final Iterable<BlockingState> filteredByTypes = typeFilter != null && !typeFilter.isEmpty() ? Iterables.filter(allBlockingStates, new Predicate<BlockingState>() {

            @Override
            public boolean apply(final BlockingState input) {
                return typeFilter.contains(input.getType());
            }
        }) : allBlockingStates;
        final Iterable<BlockingState> filteredByTypesAndSvcs = svcsFilter != null && !svcsFilter.isEmpty() ? Iterables.filter(filteredByTypes, new Predicate<BlockingState>() {

            @Override
            public boolean apply(final BlockingState input) {
                return svcsFilter.contains(input.getService());
            }
        }) : filteredByTypes;
        final LocalDate localDateNowInAccountTimezone = internalTenantContextWithValidAccountRecordId.toLocalDate(clock.getUTCNow());
        final List<BlockingState> result = new ArrayList<BlockingState>();
        for (final BlockingState cur : filteredByTypesAndSvcs) {
            final LocalDate eventDate = internalTenantContextWithValidAccountRecordId.toLocalDate(cur.getEffectiveDate());
            final int comp = eventDate.compareTo(localDateNowInAccountTimezone);
            if ((comp <= 1 && ((timeFilter & SubscriptionApi.PAST_EVENTS) == SubscriptionApi.PAST_EVENTS)) || (comp == 0 && ((timeFilter & SubscriptionApi.PRESENT_EVENTS) == SubscriptionApi.PRESENT_EVENTS)) || (comp >= 1 && ((timeFilter & SubscriptionApi.FUTURE_EVENTS) == SubscriptionApi.FUTURE_EVENTS))) {
                result.add(cur);
            }
        }
        return orderingType == OrderingType.ASCENDING ? result : Lists.reverse(result);
    } catch (final AccountApiException e) {
        throw new EntitlementApiException(e);
    } catch (final CatalogApiException e) {
        throw new EntitlementApiException(e);
    }
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) ArrayList(java.util.ArrayList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) EntitlementLoggingHelper.logAddBlockingState(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logAddBlockingState) LocalDate(org.joda.time.LocalDate) Predicate(com.google.common.base.Predicate) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) AccountApiException(org.killbill.billing.account.api.AccountApiException)

Example 63 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultSubscriptionApi method getActiveSubscriptionBundleForExternalKey.

@Override
public SubscriptionBundle getActiveSubscriptionBundleForExternalKey(final String externalKey, final TenantContext context) throws SubscriptionApiException {
    final InternalTenantContext internalContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context);
    try {
        final UUID activeSubscriptionIdForKey = entitlementUtils.getFirstActiveSubscriptionIdForKeyOrNull(externalKey, internalContext);
        if (activeSubscriptionIdForKey == null) {
            throw new SubscriptionApiException(new SubscriptionBaseApiException(ErrorCode.SUB_GET_INVALID_BUNDLE_KEY, externalKey));
        }
        final InternalTenantContext internalContextWithAccountRecordId = internalCallContextFactory.createInternalTenantContext(activeSubscriptionIdForKey, ObjectType.SUBSCRIPTION, context);
        final UUID bundleId = subscriptionBaseInternalApi.getBundleIdFromSubscriptionId(activeSubscriptionIdForKey, internalContextWithAccountRecordId);
        return getSubscriptionBundle(bundleId, context);
    } catch (final SubscriptionBaseApiException e) {
        throw new SubscriptionApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 64 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultSubscriptionApi method getSubscriptionBundle.

@Override
public SubscriptionBundle getSubscriptionBundle(final UUID bundleId, final TenantContext tenantContext) throws SubscriptionApiException {
    final UUID accountId = internalCallContextFactory.getAccountId(bundleId, ObjectType.BUNDLE, tenantContext);
    final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
    final SubscriptionBaseBundle baseBundle;
    try {
        baseBundle = subscriptionBaseInternalApi.getBundleFromId(bundleId, internalTenantContextWithValidAccountRecordId);
    } catch (final SubscriptionBaseApiException e) {
        throw new SubscriptionApiException(e);
    }
    final List<Entitlement> bundleEntitlements;
    try {
        bundleEntitlements = entitlementInternalApi.getAllEntitlementsForBundle(bundleId, internalTenantContextWithValidAccountRecordId);
    } catch (final EntitlementApiException e) {
        throw new SubscriptionApiException(e);
    }
    // Build subscriptions
    final List<Subscription> bundleSubscriptions = new LinkedList<Subscription>();
    for (final Entitlement entitlement : bundleEntitlements) {
        if (entitlement instanceof DefaultEntitlement) {
            bundleSubscriptions.add(new DefaultSubscription((DefaultEntitlement) entitlement));
        } else {
            throw new ShouldntHappenException("Entitlement should be a DefaultEntitlement instance");
        }
    }
    final String bundleExternalKey = bundleSubscriptions.get(0).getBundleExternalKey();
    final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, bundleExternalKey, bundleEntitlements, internalTenantContextWithValidAccountRecordId);
    return new DefaultSubscriptionBundle(bundleId, accountId, bundleExternalKey, bundleSubscriptions, timeline, baseBundle.getOriginalCreatedDate(), baseBundle.getCreatedDate(), baseBundle.getUpdatedDate());
}
Also used : LinkedList(java.util.LinkedList) ShouldntHappenException(org.killbill.billing.util.customfield.ShouldntHappenException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 65 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultEntitlement method getLocalDateFromEntitlementPolicy.

private LocalDate getLocalDateFromEntitlementPolicy(final EntitlementActionPolicy entitlementPolicy, final CallContext callContext) {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(getAccountId(), callContext);
    final LocalDate cancellationDate;
    switch(entitlementPolicy) {
        case IMMEDIATE:
            cancellationDate = internalTenantContext.toLocalDate(callContext.getCreatedDate());
            break;
        case END_OF_TERM:
            if (getSubscriptionBase().getChargedThroughDate() != null) {
                cancellationDate = internalTenantContext.toLocalDate(getSubscriptionBase().getChargedThroughDate());
            } else {
                cancellationDate = internalTenantContext.toLocalDate(callContext.getCreatedDate());
            }
            break;
        default:
            throw new RuntimeException("Unsupported policy " + entitlementPolicy);
    }
    return (cancellationDate.compareTo(getEffectiveStartDate()) < 0) ? getEffectiveStartDate() : cancellationDate;
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) LocalDate(org.joda.time.LocalDate)

Aggregations

InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)92 UUID (java.util.UUID)15 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)13 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)11 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)10 ArrayList (java.util.ArrayList)9 ObjectType (org.killbill.billing.ObjectType)9 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)9 TenantContext (org.killbill.billing.util.callcontext.TenantContext)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 DateTime (org.joda.time.DateTime)7 LocalDate (org.joda.time.LocalDate)7 Predicate (com.google.common.base.Predicate)6 AccountApiException (org.killbill.billing.account.api.AccountApiException)6 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)6 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)6