use of org.killbill.billing.subscription.events.user.ApiEvent 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.user.ApiEvent in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method handleBasePlanEvent.
@Override
public int handleBasePlanEvent(final DefaultSubscriptionBase subscription, final SubscriptionBaseEvent event, final CallContext context) throws CatalogApiException {
final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
if (((ApiEvent) event).getApiEventType() == ApiEventType.CANCEL || ((ApiEvent) event).getApiEventType() == ApiEventType.CHANGE) {
final Product baseProduct = (subscription.getState() == EntitlementState.CANCELLED) ? null : subscription.getCurrentPlan().getProduct();
final List<SubscriptionBaseEvent> cancelEvents = new LinkedList<SubscriptionBaseEvent>();
final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = computeAddOnsToCancel(cancelEvents, baseProduct, subscription.getBundleId(), event.getEffectiveDate(), internalCallContext);
dao.cancelSubscriptionsOnBasePlanEvent(subscription, event, subscriptionsToBeCancelled, cancelEvents, internalCallContext);
return subscriptionsToBeCancelled.size();
} else {
dao.notifyOnBasePlanEvent(subscription, event, internalCallContext);
return 0;
}
}
use of org.killbill.billing.subscription.events.user.ApiEvent in project killbill by killbill.
the class TestSubscriptionHelper method checkNextPhaseChange.
public void checkNextPhaseChange(final DefaultSubscriptionBase subscription, final int expPendingEvents, final DateTime expPhaseChange) {
final List<SubscriptionBaseEvent> events = dao.getPendingEventsForSubscription(subscription.getId(), internalCallContext);
assertNotNull(events);
printEvents(events);
assertEquals(events.size(), expPendingEvents);
if (events.size() > 0 && expPhaseChange != null) {
boolean foundPhase = false;
boolean foundChange = false;
for (final SubscriptionBaseEvent cur : events) {
if (cur instanceof PhaseEvent) {
assertEquals(foundPhase, false);
foundPhase = true;
assertEquals(cur.getEffectiveDate(), expPhaseChange);
} else if (cur instanceof ApiEvent) {
final ApiEvent uEvent = (ApiEvent) cur;
assertEquals(ApiEventType.CHANGE, uEvent.getApiEventType());
assertEquals(foundChange, false);
foundChange = true;
} else {
assertFalse(true);
}
}
}
}
Aggregations