@Override
 public void migrate(final AccountMigration toBeMigrated, final CallContext context)
     throws SubscriptionBaseMigrationApiException {
   final AccountMigrationData accountMigrationData =
       createAccountMigrationData(toBeMigrated, context);
   dao.migrate(
       toBeMigrated.getAccountKey(),
       accountMigrationData,
       internalCallContextFactory.createInternalCallContext(
           toBeMigrated.getAccountKey(), context));
 }
  private AccountMigrationData createAccountMigrationData(
      final AccountMigration toBeMigrated, final CallContext context)
      throws SubscriptionBaseMigrationApiException {
    final UUID accountId = toBeMigrated.getAccountKey();
    final DateTime now = clock.getUTCNow();

    final List<BundleMigrationData> accountBundleData = new LinkedList<BundleMigrationData>();

    for (final BundleMigration curBundle : toBeMigrated.getBundles()) {

      final DefaultSubscriptionBaseBundle bundleData =
          new DefaultSubscriptionBaseBundle(
              curBundle.getBundleKey(), accountId, now, now, now, now);
      final List<SubscriptionMigrationData> bundleSubscriptionData =
          new LinkedList<AccountMigrationData.SubscriptionMigrationData>();

      final List<SubscriptionMigration> sortedSubscriptions =
          Lists.newArrayList(curBundle.getSubscriptions());
      // Make sure we have first BASE or STANDALONE, then ADDON and for each category order by CED
      Collections.sort(
          sortedSubscriptions,
          new 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(),
                  now,
                  curSub.getChargedThroughDate(),
                  context);
          bundleStartDate = data.getInitialEvents().get(0).getEffectiveDate();
        } else {
          data =
              createSubscriptionMigrationDataWithBundleDate(
                  bundleData.getId(),
                  curSub.getCategory(),
                  curSub.getSubscriptionCases(),
                  now,
                  bundleStartDate,
                  curSub.getChargedThroughDate(),
                  context);
        }
        if (data != null) {
          bundleSubscriptionData.add(data);
        }
      }
      final BundleMigrationData bundleMigrationData =
          new BundleMigrationData(bundleData, bundleSubscriptionData);
      accountBundleData.add(bundleMigrationData);
    }

    return new AccountMigrationData(accountBundleData);
  }