Search in sources :

Example 1 with ApiEventUncancel

use of org.killbill.billing.subscription.events.user.ApiEventUncancel in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method uncancel.

@Override
public boolean uncancel(final DefaultSubscriptionBase subscription, final CallContext context) throws SubscriptionBaseApiException {
    if (!subscription.isSubscriptionFutureCancelled()) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_UNCANCEL_BAD_STATE, subscription.getId().toString());
    }
    final DateTime now = clock.getUTCNow();
    final SubscriptionBaseEvent uncancelEvent = new ApiEventUncancel(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(now).setFromDisk(true));
    final List<SubscriptionBaseEvent> uncancelEvents = new ArrayList<SubscriptionBaseEvent>();
    uncancelEvents.add(uncancelEvent);
    final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
    //
    // Used to compute effective for next phase (which was set unactive during cancellation).
    // In case of a pending subscription we don't want to pass an effective date prior the CREATE event as we would end up with the wrong
    // transition in PlanAligner (next transition would be CREATE instead of potential next PHASE)
    //
    final DateTime planAlignerEffectiveDate = subscription.getState() == EntitlementState.PENDING ? subscription.getStartDate() : now;
    final TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, planAlignerEffectiveDate, internalCallContext);
    final PhaseEvent nextPhaseEvent = (nextTimedPhase != null) ? PhaseEventData.createNextPhaseEvent(subscription.getId(), nextTimedPhase.getPhase().getName(), nextTimedPhase.getStartPhase()) : null;
    if (nextPhaseEvent != null) {
        uncancelEvents.add(nextPhaseEvent);
    }
    dao.uncancelSubscription(subscription, uncancelEvents, internalCallContext);
    try {
        final Catalog fullCatalog = catalogService.getFullCatalog(true, true, internalCallContext);
        subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), fullCatalog);
        return true;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) ArrayList(java.util.ArrayList) TimedPhase(org.killbill.billing.subscription.alignment.TimedPhase) ApiEventUncancel(org.killbill.billing.subscription.events.user.ApiEventUncancel) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime) Catalog(org.killbill.billing.catalog.api.Catalog) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Aggregations

ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 Catalog (org.killbill.billing.catalog.api.Catalog)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1 TimedPhase (org.killbill.billing.subscription.alignment.TimedPhase)1 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)1 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)1 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)1 ApiEventUncancel (org.killbill.billing.subscription.events.user.ApiEventUncancel)1