use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.
the class MockSubscriptionDaoMemory method createSubscriptionsWithAddOns.
@Override
public void createSubscriptionsWithAddOns(final List<SubscriptionBaseWithAddOns> subscriptions, final Map<UUID, List<SubscriptionBaseEvent>> initialEventsMap, final InternalCallContext context) {
synchronized (events) {
for (final SubscriptionBaseWithAddOns subscription : subscriptions) {
for (final SubscriptionBase subscriptionBase : subscription.getSubscriptionBaseList()) {
final List<SubscriptionBaseEvent> initialEvents = initialEventsMap.get(subscriptionBase.getId());
events.addAll(initialEvents);
for (final SubscriptionBaseEvent cur : initialEvents) {
recordFutureNotificationFromTransaction(null, cur.getEffectiveDate(), new SubscriptionNotificationKey(cur.getId()), context);
}
final SubscriptionBase updatedSubscription = buildSubscription((DefaultSubscriptionBase) subscriptionBase, context);
this.subscriptions.add(updatedSubscription);
mockNonEntityDao.addTenantRecordIdMapping(updatedSubscription.getId(), context);
}
}
}
}
use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.
the class DefaultSubscriptionBase method rebuildTransitions.
public void rebuildTransitions(final List<SubscriptionBaseEvent> inputEvents, final Catalog catalog) throws CatalogApiException {
if (inputEvents == null) {
return;
}
this.events = inputEvents;
filterOutDuplicateCancelEvents(events);
UUID nextUserToken = null;
UUID nextEventId = null;
DateTime nextCreatedDate = null;
EntitlementState nextState = null;
String nextPlanName = null;
String nextPhaseName = null;
Integer nextBillingCycleDayLocal = null;
UUID prevEventId = null;
DateTime prevCreatedDate = null;
EntitlementState previousState = null;
PriceList previousPriceList = null;
Plan previousPlan = null;
PlanPhase previousPhase = null;
Integer previousBillingCycleDayLocal = null;
transitions = new LinkedList<SubscriptionBaseTransition>();
for (final SubscriptionBaseEvent cur : inputEvents) {
if (!cur.isActive()) {
continue;
}
ApiEventType apiEventType = null;
boolean isFromDisk = true;
nextEventId = cur.getId();
nextCreatedDate = cur.getCreatedDate();
switch(cur.getType()) {
case PHASE:
final PhaseEvent phaseEV = (PhaseEvent) cur;
nextPhaseName = phaseEV.getPhase();
break;
case BCD_UPDATE:
final BCDEvent bcdEvent = (BCDEvent) cur;
nextBillingCycleDayLocal = bcdEvent.getBillCycleDayLocal();
break;
case API_USER:
final ApiEvent userEV = (ApiEvent) cur;
apiEventType = userEV.getApiEventType();
isFromDisk = userEV.isFromDisk();
switch(apiEventType) {
case TRANSFER:
case CREATE:
prevEventId = null;
prevCreatedDate = null;
previousState = null;
previousPlan = null;
previousPhase = null;
previousPriceList = null;
nextState = EntitlementState.ACTIVE;
nextPlanName = userEV.getEventPlan();
nextPhaseName = userEV.getEventPlanPhase();
break;
case CHANGE:
nextPlanName = userEV.getEventPlan();
nextPhaseName = userEV.getEventPlanPhase();
break;
case CANCEL:
nextState = EntitlementState.CANCELLED;
nextPlanName = null;
nextPhaseName = null;
break;
case UNCANCEL:
default:
throw new SubscriptionBaseError(String.format("Unexpected UserEvent type = %s", userEV.getApiEventType().toString()));
}
break;
default:
throw new SubscriptionBaseError(String.format("Unexpected Event type = %s", cur.getType()));
}
Plan nextPlan = null;
PlanPhase nextPhase = null;
PriceList nextPriceList = null;
nextPlan = (nextPlanName != null) ? catalog.findPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseName, cur.getEffectiveDate(), getAlignStartDate()) : null;
nextPriceList = (nextPlan != null) ? catalog.findPriceListForPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
final SubscriptionBaseTransitionData transition = new SubscriptionBaseTransitionData(cur.getId(), id, bundleId, bundleExternalKey, cur.getType(), apiEventType, cur.getEffectiveDate(), prevEventId, prevCreatedDate, previousState, previousPlan, previousPhase, previousPriceList, previousBillingCycleDayLocal, nextEventId, nextCreatedDate, nextState, nextPlan, nextPhase, nextPriceList, nextBillingCycleDayLocal, cur.getTotalOrdering(), cur.getCreatedDate(), nextUserToken, isFromDisk);
transitions.add(transition);
previousState = nextState;
previousPlan = nextPlan;
previousPhase = nextPhase;
previousPriceList = nextPriceList;
prevEventId = nextEventId;
prevCreatedDate = nextCreatedDate;
previousBillingCycleDayLocal = nextBillingCycleDayLocal;
}
}
use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method createPlansWithAddOns.
@Override
public List<SubscriptionBaseWithAddOns> createPlansWithAddOns(final UUID accountId, final Iterable<SubscriptionAndAddOnsSpecifier> subscriptionsAndAddOns, final CallContext context) throws SubscriptionBaseApiException {
final Map<UUID, List<SubscriptionBaseEvent>> eventsMap = new HashMap<UUID, List<SubscriptionBaseEvent>>();
final Collection<List<SubscriptionBase>> subscriptionBaseAndAddOnsList = new ArrayList<List<SubscriptionBase>>();
final List<SubscriptionBaseWithAddOns> allSubscriptions = new ArrayList<SubscriptionBaseWithAddOns>();
for (final SubscriptionAndAddOnsSpecifier subscriptionAndAddOns : subscriptionsAndAddOns) {
final List<SubscriptionBase> subscriptionBaseList = new ArrayList<SubscriptionBase>();
createEvents(subscriptionAndAddOns.getSubscriptionSpecifiers(), context, eventsMap, subscriptionBaseList);
subscriptionBaseAndAddOnsList.add(subscriptionBaseList);
final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = new DefaultSubscriptionBaseWithAddOns(subscriptionAndAddOns.getBundleId(), subscriptionBaseList, subscriptionAndAddOns.getEffectiveDate());
allSubscriptions.add(subscriptionBaseWithAddOns);
}
final InternalCallContext internalCallContext = createCallContextFromAccountId(accountId, context);
dao.createSubscriptionsWithAddOns(allSubscriptions, eventsMap, internalCallContext);
for (final List<SubscriptionBase> subscriptions : subscriptionBaseAndAddOnsList) {
final SubscriptionBase baseSubscription = findBaseSubscription(subscriptions);
rebuildTransitions(internalCallContext, subscriptions, baseSubscription);
}
return allSubscriptions;
}
use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.
the class DefaultSubscriptionDao method cancelSubscriptionsFromTransaction.
private void cancelSubscriptionsFromTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final List<DefaultSubscriptionBase> subscriptions, final List<SubscriptionBaseEvent> cancelEvents, final InternalCallContext context) throws EntityPersistenceException {
for (int i = 0; i < subscriptions.size(); i++) {
final DefaultSubscriptionBase subscription = subscriptions.get(i);
final SubscriptionBaseEvent cancelEvent = cancelEvents.get(i);
cancelSubscriptionFromTransaction(subscription, cancelEvent, entitySqlDaoWrapperFactory, context, subscriptions.size() - i - 1);
}
}
use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method doCancelPlan.
private boolean doCancelPlan(final Map<DefaultSubscriptionBase, DateTime> subscriptions, final DateTime now, final InternalCallContext internalCallContext) throws SubscriptionBaseApiException {
final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = new LinkedList<DefaultSubscriptionBase>();
final List<SubscriptionBaseEvent> cancelEvents = new LinkedList<SubscriptionBaseEvent>();
try {
for (final DefaultSubscriptionBase subscription : subscriptions.keySet()) {
final DateTime effectiveDate = subscriptions.get(subscription);
validateEffectiveDate(subscription, effectiveDate);
subscriptionsToBeCancelled.add(subscription);
cancelEvents.addAll(getEventsOnCancelPlan(subscription, effectiveDate, now, false, internalCallContext));
if (subscription.getCategory() == ProductCategory.BASE) {
subscriptionsToBeCancelled.addAll(computeAddOnsToCancel(cancelEvents, null, subscription.getBundleId(), effectiveDate, internalCallContext));
}
}
dao.cancelSubscriptions(subscriptionsToBeCancelled, cancelEvents, internalCallContext);
boolean allSubscriptionsCancelled = true;
for (final DefaultSubscriptionBase subscription : subscriptions.keySet()) {
final Catalog fullCatalog = catalogService.getFullCatalog(true, true, internalCallContext);
subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), fullCatalog);
allSubscriptionsCancelled = allSubscriptionsCancelled && (subscription.getState() == EntitlementState.CANCELLED);
}
return allSubscriptionsCancelled;
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
}
Aggregations