Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2013 Ning, Inc.
   *
   * Ning licenses this file to you under the Apache License, version 2.0
   * (the "License"); you may not use this file except in compliance with the
   * License.  You may obtain a copy of the License at:
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
 
 package com.ning.billing.subscription.api.transfer;
 
 import java.util.List;
 import java.util.UUID;
 
 
 
 
 
     private final CatalogService catalogService;
     private final SubscriptionBaseTimelineApi timelineApi;
 
     @Inject
     public DefaultSubscriptionBaseTransferApi(final Clock clockfinal SubscriptionDao daofinal SubscriptionBaseTimelineApi timelineApifinal CatalogService catalogService,
                                               final SubscriptionBaseApiService apiServicefinal InternalCallContextFactory internalCallContextFactory) {
         super(daoapiServiceclockcatalogService);
         this. = catalogService;
         this. = timelineApi;
         this. = internalCallContextFactory;
     }
 
     private SubscriptionBaseEvent createEvent(final boolean firstEventfinal ExistingEvent existingEventfinal DefaultSubscriptionBase subscriptionfinal DateTime transferDatefinal CallContext context)
             throws CatalogApiException {
 
         SubscriptionBaseEvent newEvent = null;
 
         final Catalog catalog = .getFullCatalog();
 
         final DateTime effectiveDate = existingEvent.getEffectiveDate().isBefore(transferDate) ? transferDate : existingEvent.getEffectiveDate();
 
         final PlanPhaseSpecifier spec = existingEvent.getPlanPhaseSpecifier();
         final PlanPhase currentPhase = existingEvent.getPlanPhaseName() != null ? catalog.findPhase(existingEvent.getPlanPhaseName(), effectiveDatesubscription.getAlignStartDate()) : null;
 
         if (spec == null || currentPhase == null) {
             // Ignore cancellations - we assume that transferred subscriptions should always be active
             return null;
         }
         final ApiEventBuilder apiBuilder = new ApiEventBuilder()
                 .setSubscriptionId(subscription.getId())
                 .setEventPlan(currentPhase.getPlan().getName())
                 .setEventPlanPhase(currentPhase.getName())
                 .setEventPriceList(spec.getPriceListName())
                .setActiveVersion(subscription.getActiveVersion())
                .setProcessedDate(.getUTCNow())
                .setEffectiveDate(effectiveDate)
                .setRequestedDate(effectiveDate)
                .setFromDisk(true);
        switch (existingEvent.getSubscriptionTransitionType()) {
            case :
            case :
            case :
            case :
                newEvent = new ApiEventTransfer(apiBuilder);
                break;
            // Should we even keep future change events; product question really
            case :
                newEvent = firstEvent ? new ApiEventTransfer(apiBuilder) : new ApiEventChange(apiBuilder);
                break;
            case :
                newEvent = firstEvent ? new ApiEventTransfer(apiBuilder) :
                           PhaseEventData.createNextPhaseEvent(currentPhase.getName(), subscription.getUTCNow(), effectiveDate);
                break;
            // Ignore these events except if it's the first event for the new subscription
            case :
                if (firstEvent) {
                    newEvent = new ApiEventTransfer(apiBuilder);
                }
                break;
            case :
                break;
            default:
                throw new SubscriptionBaseError(String.format("Unexpected transitionType %s"existingEvent.getSubscriptionTransitionType()));
        }
        return newEvent;
    }
    List<SubscriptionBaseEventtoEvents(final List<ExistingEventexistingEventsfinal DefaultSubscriptionBase subscription,
                                    final DateTime transferDatefinal CallContext contextthrows SubscriptionBaseTransferApiException {
        try {
            final List<SubscriptionBaseEventresult = new LinkedList<SubscriptionBaseEvent>();
            SubscriptionBaseEvent event = null;
            ExistingEvent prevEvent = null;
            boolean firstEvent = true;
            for (ExistingEvent cur : existingEvents) {
                // Skip all events prior to the transferDate
                if (cur.getEffectiveDate().isBefore(transferDate)) {
                    prevEvent = cur;
                    continue;
                }
                // Add previous event the first time if needed
                if (prevEvent != null) {
                    event = createEvent(firstEventprevEventsubscriptiontransferDatecontext);
                    if (event != null) {
                        result.add(event);
                        firstEvent = false;
                    }
                    prevEvent = null;
                }
                event = createEvent(firstEventcursubscriptiontransferDatecontext);
                if (event != null) {
                    result.add(event);
                    firstEvent = false;
                }
            }
            // Previous loop did not get anything because transferDate is greater than effectiveDate of last event
            if (prevEvent != null) {
                event = createEvent(firstEventprevEventsubscriptiontransferDatecontext);
                if (event != null) {
                    result.add(event);
                }
                prevEvent = null;
            }
            return result;
        } catch (CatalogApiException e) {
            throw new SubscriptionBaseTransferApiException(e);
        }
    }
    @Override
    public SubscriptionBaseBundle transferBundle(final UUID sourceAccountIdfinal UUID destAccountId,
                                             final String bundleKeyfinal DateTime transferDatefinal boolean transferAddOn,
                                             final boolean cancelImmediatelyfinal CallContext contextthrows SubscriptionBaseTransferApiException {
        final InternalCallContext fromInternalCallContext = .createInternalCallContext(sourceAccountIdcontext);
        final InternalCallContext toInternalCallContext = .createInternalCallContext(destAccountIdcontext);
        try {
            final DateTime effectiveTransferDate = transferDate == null ? .getUTCNow() : transferDate;
            if (effectiveTransferDate.isAfter(.getUTCNow())) {
                // The transfer event for the migrated bundle will be the first one, which cannot be in the future
                // (subscription always expects the first event to be in the past)
                throw new SubscriptionBaseTransferApiException(.effectiveTransferDate);
            }
            final List<SubscriptionBaseBundlebundlesForAccountAndKey = .getSubscriptionBundlesForAccountAndKey(sourceAccountIdbundleKeyfromInternalCallContext);
            final SubscriptionBaseBundle bundle = DefaultSubscriptionInternalApi.getActiveBundleForKeyNotException(bundlesForAccountAndKeyfromInternalCallContext);
            if (bundle == null) {
                throw new SubscriptionBaseTransferApiException(.bundleKey);
            }
            // Get the bundle timeline for the old account
            final BundleBaseTimeline bundleBaseTimeline = .getBundleTimeline(bundlecontext);
            final DefaultSubscriptionBaseBundle subscriptionBundleData = new DefaultSubscriptionBaseBundle(bundleKeydestAccountIdeffectiveTransferDate,
                                                                                                           bundle.getOriginalCreatedDate(), .getUTCNow(), .getUTCNow());
            final List<SubscriptionMigrationDatasubscriptionMigrationDataList = new LinkedList<SubscriptionMigrationData>();
            final List<TransferCancelDatatransferCancelDataList = new LinkedList<TransferCancelData>();
            DateTime bundleStartdate = null;
            for (final SubscriptionBaseTimeline cur : bundleBaseTimeline.getSubscriptions()) {
                final DefaultSubscriptionBase oldSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(cur.getId(), fromInternalCallContext);
                // Skip already cancelled subscriptions
                if (oldSubscription.getState() == .) {
                    continue;
                }
                final List<ExistingEventexistingEvents = cur.getExistingEvents();
                final ProductCategory productCategory = existingEvents.get(0).getPlanPhaseSpecifier().getProductCategory();
                // For future add-on cancellations, don't add a cancellation on disk right away (mirror the behavior
                // on base plan cancellations, even though we don't support un-transfer today)
                if (productCategory != . || cancelImmediately) {
                    // Create the cancelWithRequestedDate event on effectiveCancelDate
                    final DateTime effectiveCancelDate = !cancelImmediately && oldSubscription.getChargedThroughDate() != null &&
                                                         effectiveTransferDate.isBefore(oldSubscription.getChargedThroughDate()) ?
                                                         oldSubscription.getChargedThroughDate() : effectiveTransferDate;
                    final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder()
                                                                                         .setSubscriptionId(cur.getId())
                                                                                         .setActiveVersion(cur.getActiveVersion())
                                                                                         .setProcessedDate(.getUTCNow())
                                                                                         .setEffectiveDate(effectiveCancelDate)
                                                                                         .setRequestedDate(effectiveTransferDate)
                                                                                         .setFromDisk(true));
                    TransferCancelData cancelData = new TransferCancelData(oldSubscriptioncancelEvent);
                    transferCancelDataList.add(cancelData);
                }
                if (productCategory == . && !transferAddOn) {
                    continue;
                }
                // We Align with the original subscription
                final DateTime subscriptionAlignStartDate = oldSubscription.getAlignStartDate();
                if (bundleStartdate == null) {
                    bundleStartdate = oldSubscription.getStartDate();
                }
                // Create the new subscription for the new bundle on the new account
                final DefaultSubscriptionBase defaultSubscriptionBase = createSubscriptionForApiUse(new SubscriptionBuilder()
                                                                                                            .setId(UUID.randomUUID())
                                                                                                            .setBundleId(subscriptionBundleData.getId())
                                                                                                            .setCategory(productCategory)
                                                                                                            .setBundleStartDate(effectiveTransferDate)
                                                                                                            .setAlignStartDate(subscriptionAlignStartDate),
                                                                                                    ImmutableList.<SubscriptionBaseEvent>of());
                final List<SubscriptionBaseEventevents = toEvents(existingEventsdefaultSubscriptionBaseeffectiveTransferDatecontext);
                final SubscriptionMigrationData curData = new SubscriptionMigrationData(defaultSubscriptionBaseeventsnull);
                subscriptionMigrationDataList.add(curData);
            }
            BundleMigrationData bundleMigrationData = new BundleMigrationData(subscriptionBundleDatasubscriptionMigrationDataList);
            // Atomically cancelWithRequestedDate all subscription on old account and create new bundle, subscriptions, events for new account
            .transfer(sourceAccountIddestAccountIdbundleMigrationDatatransferCancelDataListfromInternalCallContexttoInternalCallContext);
            return bundleMigrationData.getData();
        } catch (SubscriptionBaseRepairException e) {
            throw new SubscriptionBaseTransferApiException(e);
        }
    }
New to GrepCode? Check out our FAQ X