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;
}
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);
}
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);
}
});
}
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);
}
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());
}
}
}
Aggregations