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.migration;
 
 import java.util.List;
 import java.util.UUID;
 
 
 
 
 
     private final MigrationPlanAligner migrationAligner;
 
     @Inject
     public DefaultSubscriptionBaseMigrationApi(final MigrationPlanAligner migrationAligner,
                                                final SubscriptionBaseApiService apiService,
                                                final CatalogService catalogService,
                                                final SubscriptionDao dao,
                                                final Clock clock,
                                                final InternalCallContextFactory internalCallContextFactory) {
         super(daoapiServiceclockcatalogService);
         this. = migrationAligner;
         this. = internalCallContextFactory;
     }
 
     @Override
     public void migrate(final AccountMigration toBeMigratedfinal CallContext context)
             throws SubscriptionBaseMigrationApiException {
         final AccountMigrationData accountMigrationData = createAccountMigrationData(toBeMigratedcontext);
         .migrate(toBeMigrated.getAccountKey(), accountMigrationData.createInternalCallContext(toBeMigrated.getAccountKey(), context));
     }
 
     private AccountMigrationData createAccountMigrationData(final AccountMigration toBeMigratedfinal CallContext context)
             throws SubscriptionBaseMigrationApiException {
         final UUID accountId = toBeMigrated.getAccountKey();
         final DateTime now = .getUTCNow();
 
         final List<BundleMigrationDataaccountBundleData = new LinkedList<BundleMigrationData>();
 
         for (final BundleMigration curBundle : toBeMigrated.getBundles()) {
 
             final DefaultSubscriptionBaseBundle bundleData = new DefaultSubscriptionBaseBundle(curBundle.getBundleKey(), accountIdnownownownow);
             final List<SubscriptionMigrationDatabundleSubscriptionData = new LinkedList<AccountMigrationData.SubscriptionMigrationData>();
 
             final List<SubscriptionMigrationsortedSubscriptions = Lists.newArrayList(curBundle.getSubscriptions());
             // Make sure we have first BASE or STANDALONE, then ADDON and for each category order by CED
             Collections.sort(sortedSubscriptionsnew Comparator<SubscriptionMigration>() {
                 @Override
                 public int compare(final SubscriptionMigration o1,
                                   final SubscriptionMigration o2) {
                    if (o1.getCategory().equals(o2.getCategory())) {
                        return o1.getSubscriptionCases()[0].getEffectiveDate().compareTo(o2.getSubscriptionCases()[0].getEffectiveDate());
                    } else {
                        if (!o1.getCategory().name().equalsIgnoreCase("ADD_ON")) {
                            return -1;
                        } else if (o1.getCategory().name().equalsIgnoreCase("ADD_ON")) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                }
            });
            DateTime bundleStartDate = null;
            for (final SubscriptionMigration curSub : sortedSubscriptions) {
                SubscriptionMigrationData data = null;
                if (bundleStartDate == null) {
                    data = createInitialSubscription(bundleData.getId(), curSub.getCategory(), curSub.getSubscriptionCases(), nowcurSub.getChargedThroughDate(), context);
                    bundleStartDate = data.getInitialEvents().get(0).getEffectiveDate();
                } else {
                    data = createSubscriptionMigrationDataWithBundleDate(bundleData.getId(), curSub.getCategory(), curSub.getSubscriptionCases(), now,
                                                                         bundleStartDatecurSub.getChargedThroughDate(), context);
                }
                if (data != null) {
                    bundleSubscriptionData.add(data);
                }
            }
            final BundleMigrationData bundleMigrationData = new BundleMigrationData(bundleDatabundleSubscriptionData);
            accountBundleData.add(bundleMigrationData);
        }
        return new AccountMigrationData(accountBundleData);
    }
    private SubscriptionMigrationData createInitialSubscription(final UUID bundleIdfinal ProductCategory productCategory,
                                                                final SubscriptionMigrationCase[] inputfinal DateTime nowfinal DateTime ctdfinal CallContext context)
            throws SubscriptionBaseMigrationApiException {
        final TimedMigration[] events = .getEventsMigration(inputnow);
        final DateTime migrationStartDate = events[0].getEventTime();
        final List<SubscriptionBaseEventemptyEvents = Collections.emptyList();
        final DefaultSubscriptionBase defaultSubscriptionBase = createSubscriptionForApiUse(new SubscriptionBuilder()
                                                                                      .setId(UUID.randomUUID())
                                                                                      .setBundleId(bundleId)
                                                                                      .setCategory(productCategory)
                                                                                      .setBundleStartDate(migrationStartDate)
                                                                                      .setAlignStartDate(migrationStartDate),
                                                                              emptyEvents);
        return new SubscriptionMigrationData(defaultSubscriptionBasetoEvents(defaultSubscriptionBasenowctdeventscontext), ctd);
    }
    private SubscriptionMigrationData createSubscriptionMigrationDataWithBundleDate(final UUID bundleIdfinal ProductCategory productCategory,
                                                                                    final SubscriptionMigrationCase[] inputfinal DateTime nowfinal DateTime bundleStartDatefinal DateTime ctdfinal CallContext context)
            throws SubscriptionBaseMigrationApiException {
        final TimedMigration[] events = .getEventsMigration(inputnow);
        final DateTime migrationStartDate = events[0].getEventTime();
        final List<SubscriptionBaseEventemptyEvents = Collections.emptyList();
        final DefaultSubscriptionBase defaultSubscriptionBase = createSubscriptionForApiUse(new SubscriptionBuilder()
                                                                                      .setId(UUID.randomUUID())
                                                                                      .setBundleId(bundleId)
                                                                                      .setCategory(productCategory)
                                                                                      .setBundleStartDate(bundleStartDate)
                                                                                      .setAlignStartDate(migrationStartDate),
                                                                              emptyEvents);
        return new SubscriptionMigrationData(defaultSubscriptionBasetoEvents(defaultSubscriptionBasenowctdeventscontext), ctd);
    }
    private List<SubscriptionBaseEventtoEvents(final DefaultSubscriptionBase defaultSubscriptionBasefinal DateTime nowfinal DateTime ctdfinal TimedMigration[] migrationEventsfinal CallContext context) {
        if (ctd == null) {
            throw new SubscriptionBaseError(String.format("Could not create migration billing event ctd = %s"ctd));
        }
        final List<SubscriptionBaseEventevents = new ArrayList<SubscriptionBaseEvent>(migrationEvents.length);
        ApiEventMigrateBilling apiEventMigrateBilling = null;
        // The first event date after the MIGRATE_ENTITLEMENT event
        DateTime nextEventDate = null;
        boolean isCancelledSubscriptionPriorOrAtCTD = false;
        for (final TimedMigration cur : migrationEvents) {
            final ApiEventBuilder builder = new ApiEventBuilder()
                    .setSubscriptionId(defaultSubscriptionBase.getId())
                    .setEventPlan((cur.getPlan() != null) ? cur.getPlan().getName() : null)
                    .setEventPlanPhase((cur.getPhase() != null) ? cur.getPhase().getName() : null)
                    .setEventPriceList(cur.getPriceList())
                    .setActiveVersion(defaultSubscriptionBase.getActiveVersion())
                    .setEffectiveDate(cur.getEventTime())
                    .setProcessedDate(now)
                    .setRequestedDate(now)
                    .setFromDisk(true);
            if (cur.getEventType() == .) {
                nextEventDate = nextEventDate != null && nextEventDate.compareTo(cur.getEventTime()) < 0 ? nextEventDate : cur.getEventTime();
                final PhaseEvent nextPhaseEvent = PhaseEventData.createNextPhaseEvent(cur.getPhase().getName(), defaultSubscriptionBasenowcur.getEventTime());
                events.add(nextPhaseEvent);
            } else if (cur.getEventType() == .) {
                switch (cur.getApiEventType()) {
                    case :
                        ApiEventMigrateSubscription creationEvent = new ApiEventMigrateSubscription(builder);
                        events.add(creationEvent);
                        break;
                    case :
                        nextEventDate = nextEventDate != null && nextEventDate.compareTo(cur.getEventTime()) < 0 ? nextEventDate : cur.getEventTime();
                        events.add(new ApiEventChange(builder));
                        break;
                    case :
                        isCancelledSubscriptionPriorOrAtCTD = !cur.getEventTime().isAfter(ctd);
                        nextEventDate = nextEventDate != null && nextEventDate.compareTo(cur.getEventTime()) < 0 ? nextEventDate : cur.getEventTime();
                        events.add(new ApiEventCancel(builder));
                        break;
                    default:
                        throw new SubscriptionBaseError(String.format("Unexpected type of api migration event %s"cur.getApiEventType()));
                }
            } else {
                throw new SubscriptionBaseError(String.format("Unexpected type of migration event %s"cur.getEventType()));
            }
            // create the MIGRATE_BILLING based on the current state of the last event.
            if (!cur.getEventTime().isAfter(ctd)) {
                builder.setEffectiveDate(ctd);
                builder.setUuid(UUID.randomUUID());
                apiEventMigrateBilling = new ApiEventMigrateBilling(builder);
            }
        }
        // Always ADD MIGRATE BILLING which is constructed from latest state seen in the stream prior to CTD
        if (apiEventMigrateBilling != null && !isCancelledSubscriptionPriorOrAtCTD) {
            events.add(apiEventMigrateBilling);
        }
        Collections.sort(eventsnew Comparator<SubscriptionBaseEvent>() {
            int compForApiType(final SubscriptionBaseEvent o1final SubscriptionBaseEvent o2final ApiEventType type) {
                ApiEventType apiO1 = null;
                if (o1.getType() == .) {
                    apiO1 = ((ApiEvento1).getEventType();
                }
                ApiEventType apiO2 = null;
                if (o2.getType() == .) {
                    apiO2 = ((ApiEvento2).getEventType();
                }
                if (apiO1 != null && apiO1.equals(type)) {
                    return -1;
                } else if (apiO2 != null && apiO2.equals(type)) {
                    return 1;
                } else {
                    return 0;
                }
            }
            @Override
            public int compare(final SubscriptionBaseEvent o1final SubscriptionBaseEvent o2) {
                int comp = o1.getEffectiveDate().compareTo(o2.getEffectiveDate());
                if (comp == 0) {
                    comp = compForApiType(o1o2.);
                }
                if (comp == 0) {
                    comp = compForApiType(o1o2.);
                }
                return comp;
            }
        });
        return events;
    }
New to GrepCode? Check out our FAQ X