Search in sources :

Example 6 with EventsStream

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

the class DefaultEntitlementApiBase method getAllEntitlementsForBundle.

public List<Entitlement> getAllEntitlementsForBundle(final UUID bundleId, final InternalTenantContext tenantContext) throws EntitlementApiException {
    final List<EventsStream> eventsStreamForBundle = eventsStreamBuilder.buildForBundle(bundleId, tenantContext);
    final List<Entitlement> entitlements = new LinkedList<Entitlement>();
    for (final EventsStream eventsStream : eventsStreamForBundle) {
        final Entitlement entitlement = new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, tenantContext, internalCallContextFactory);
        entitlements.add(entitlement);
    }
    return entitlements;
}
Also used : EventsStream(org.killbill.billing.entitlement.EventsStream) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LinkedList(java.util.LinkedList)

Example 7 with EventsStream

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

the class DefaultEntitlementApiBase method getAllEntitlementsForAccount.

public AccountEntitlements getAllEntitlementsForAccount(final InternalTenantContext tenantContext) throws EntitlementApiException {
    final AccountEventsStreams accountEventsStreams = eventsStreamBuilder.buildForAccount(tenantContext);
    final Map<UUID, Collection<Entitlement>> entitlementsPerBundle = new HashMap<UUID, Collection<Entitlement>>();
    for (final UUID bundleId : accountEventsStreams.getEventsStreams().keySet()) {
        if (entitlementsPerBundle.get(bundleId) == null) {
            entitlementsPerBundle.put(bundleId, new LinkedList<Entitlement>());
        }
        for (final EventsStream eventsStream : accountEventsStreams.getEventsStreams().get(bundleId)) {
            final Entitlement entitlement = new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, tenantContext, internalCallContextFactory);
            entitlementsPerBundle.get(bundleId).add(entitlement);
        }
    }
    return new DefaultAccountEntitlements(accountEventsStreams, entitlementsPerBundle);
}
Also used : HashMap(java.util.HashMap) EventsStream(org.killbill.billing.entitlement.EventsStream) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Collection(java.util.Collection) AccountEventsStreams(org.killbill.billing.entitlement.AccountEventsStreams) UUID(java.util.UUID) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement)

Example 8 with EventsStream

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

the class DefaultEntitlementApi method getAllEntitlementsForAccountId.

@Override
public List<Entitlement> getAllEntitlementsForAccountId(final UUID accountId, final TenantContext tenantContext) throws EntitlementApiException {
    final EntitlementApi entitlementApi = this;
    final InternalTenantContext context = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
    final AccountEventsStreams accountEventsStreams = eventsStreamBuilder.buildForAccount(context);
    final List<EventsStream> eventsStreams = ImmutableList.<EventsStream>copyOf(Iterables.<EventsStream>concat(accountEventsStreams.getEventsStreams().values()));
    return Lists.<EventsStream, Entitlement>transform(eventsStreams, new Function<EventsStream, Entitlement>() {

        @Override
        public Entitlement apply(final EventsStream eventsStream) {
            return new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionBaseInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, context, internalCallContextFactory);
        }
    });
}
Also used : EventsStream(org.killbill.billing.entitlement.EventsStream) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) AccountEventsStreams(org.killbill.billing.entitlement.AccountEventsStreams) EntitlementLoggingHelper.logPauseResumeEntitlement(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logPauseResumeEntitlement) EntitlementLoggingHelper.logTransferEntitlement(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logTransferEntitlement)

Example 9 with EventsStream

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

the class DefaultEntitlementApi method createBaseEntitlementsWithAddOns.

private List<UUID> createBaseEntitlementsWithAddOns(final OperationType operationType, final UUID accountId, final Iterable<BaseEntitlementWithAddOnsSpecifier> originalBaseEntitlementWithAddOnsSpecifiers, final boolean renameCancelledBundleIfExist, final Iterable<PluginProperty> properties, final CallContext callContext) throws EntitlementApiException {
    logCreateEntitlementsWithAOs(log, originalBaseEntitlementWithAddOnsSpecifiers);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(operationType, accountId, null, originalBaseEntitlementWithAddOnsSpecifiers, null, properties, callContext);
    final WithEntitlementPlugin<List<UUID>> createBaseEntitlementsWithAddOns = new WithEntitlementPlugin<List<UUID>>() {

        @Override
        public List<UUID> doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
            final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(accountId, callContext);
            final VersionedCatalog catalog;
            try {
                catalog = catalogInternalApi.getFullCatalog(true, true, contextWithValidAccountRecordId);
            } catch (final CatalogApiException e) {
                throw new EntitlementApiException(e);
            }
            final Map<UUID, Optional<EventsStream>> eventsStreamForBaseSubscriptionPerBundle = new HashMap<UUID, Optional<EventsStream>>();
            final Map<String, Optional<UUID>> bundleKeyToIdMapping = new HashMap<String, Optional<UUID>>();
            final Iterable<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifiersAfterPlugins = updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers();
            final Collection<SubscriptionBaseWithAddOnsSpecifier> subscriptionBaseWithAddOnsSpecifiers = new LinkedList<SubscriptionBaseWithAddOnsSpecifier>();
            DateTime upTo = null;
            for (final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier : baseEntitlementWithAddOnsSpecifiersAfterPlugins) {
                // Entitlement
                final DateTime entitlementRequestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getEntitlementEffectiveDate(), updatedPluginContext.getCreatedDate(), contextWithValidAccountRecordId);
                upTo = upTo == null || upTo.compareTo(entitlementRequestedDate) < 0 ? entitlementRequestedDate : upTo;
                final UUID bundleId = populateCaches(baseEntitlementWithAddOnsSpecifier, eventsStreamForBaseSubscriptionPerBundle, bundleKeyToIdMapping, catalog, callContext, contextWithValidAccountRecordId);
                if (bundleId != null) {
                    final Optional<EventsStream> eventsStreamForBaseSubscription = eventsStreamForBaseSubscriptionPerBundle.get(bundleId);
                    if (eventsStreamForBaseSubscription.isPresent()) {
                        // Verify if the operation is valid for that bundle
                        preCheckAddEntitlement(bundleId, entitlementRequestedDate, baseEntitlementWithAddOnsSpecifier, eventsStreamForBaseSubscription.get());
                    }
                }
                final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifier.getBundleId(), baseEntitlementWithAddOnsSpecifier.getBundleExternalKey(), baseEntitlementWithAddOnsSpecifier.getEntitlementSpecifier(), baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate(), baseEntitlementWithAddOnsSpecifier.isMigrated());
                subscriptionBaseWithAddOnsSpecifiers.add(subscriptionBaseWithAddOnsSpecifier);
            }
            // Verify if operation is allowed by looking for is_block_change on Account
            // Note that to fully check for block_change we should also look for BlockingState at the BUNDLE/SUBSCRIPTION level in case some of the input contain a BP that already exists.
            checkForAccountBlockingChange(accountId, upTo, contextWithValidAccountRecordId);
            final List<SubscriptionBaseWithAddOns> subscriptionsWithAddOns;
            try {
                subscriptionsWithAddOns = subscriptionBaseInternalApi.createBaseSubscriptionsWithAddOns(catalog, subscriptionBaseWithAddOnsSpecifiers, renameCancelledBundleIfExist, contextWithValidAccountRecordId);
            } catch (final SubscriptionBaseApiException e) {
                throw new EntitlementApiException(e);
            }
            // Update the context for plugins (assume underlying ordering is respected)
            for (int i = 0; i < subscriptionsWithAddOns.size(); i++) {
                final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = subscriptionsWithAddOns.get(i);
                updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers(i).setBundleId(subscriptionBaseWithAddOns.getBundle().getId());
                updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers(i).setBundleExternalKey(subscriptionBaseWithAddOns.getBundle().getExternalKey());
            }
            return createEntitlementEvents(baseEntitlementWithAddOnsSpecifiersAfterPlugins, subscriptionsWithAddOns, updatedPluginContext, contextWithValidAccountRecordId);
        }
    };
    return pluginExecution.executeWithPlugin(createBaseEntitlementsWithAddOns, pluginContext);
}
Also used : SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) SubscriptionBaseWithAddOnsSpecifier(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier) WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) HashMap(java.util.HashMap) EventsStream(org.killbill.billing.entitlement.EventsStream) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DateTime(org.joda.time.DateTime) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) Optional(com.google.common.base.Optional) LinkedList(java.util.LinkedList) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException)

Example 10 with EventsStream

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

the class DefaultEntitlementApi method populateEventsStreamForBaseSubscriptionPerBundleCache.

private void populateEventsStreamForBaseSubscriptionPerBundleCache(final UUID bundleId, final Map<UUID, Optional<EventsStream>> eventsStreamForBaseSubscriptionPerBundle, final TenantContext callContext, final InternalCallContext contextWithValidAccountRecordId) throws EntitlementApiException {
    if (eventsStreamForBaseSubscriptionPerBundle.get(bundleId) == null) {
        final List<SubscriptionBase> subscriptionsByBundle;
        try {
            subscriptionsByBundle = subscriptionBaseInternalApi.getSubscriptionsForBundle(bundleId, null, contextWithValidAccountRecordId);
            if (subscriptionsByBundle == null || subscriptionsByBundle.isEmpty()) {
                throw new EntitlementApiException(ErrorCode.SUB_NO_ACTIVE_SUBSCRIPTIONS, bundleId);
            }
        } catch (final SubscriptionBaseApiException e) {
            throw new EntitlementApiException(e);
        }
        final boolean isStandalone = Iterables.any(subscriptionsByBundle, new Predicate<SubscriptionBase>() {

            @Override
            public boolean apply(final SubscriptionBase input) {
                return ProductCategory.STANDALONE.equals(input.getCategory());
            }
        });
        if (!isStandalone) {
            final EventsStream eventsStreamForBaseSubscription = eventsStreamBuilder.buildForBaseSubscription(bundleId, callContext);
            eventsStreamForBaseSubscriptionPerBundle.put(bundleId, Optional.<EventsStream>of(eventsStreamForBaseSubscription));
        } else {
            eventsStreamForBaseSubscriptionPerBundle.put(bundleId, Optional.<EventsStream>absent());
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) EventsStream(org.killbill.billing.entitlement.EventsStream) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Aggregations

EventsStream (org.killbill.billing.entitlement.EventsStream)13 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)7 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 UUID (java.util.UUID)5 DateTime (org.joda.time.DateTime)4 AccountEventsStreams (org.killbill.billing.entitlement.AccountEventsStreams)4 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)4 ImmutableList (com.google.common.collect.ImmutableList)3 Collection (java.util.Collection)3 List (java.util.List)3 BlockingState (org.killbill.billing.entitlement.api.BlockingState)3 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)3 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 AccountApiException (org.killbill.billing.account.api.AccountApiException)2 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)2