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.alignment;
 
 
 
 
 public class MigrationPlanAligner extends BaseAligner {
 
     private final CatalogService catalogService;
 
     @Inject
     public MigrationPlanAligner(final CatalogService catalogService) {
         this. = catalogService;
     }
 
 
     public TimedMigration[] getEventsMigration(final SubscriptionMigrationCase[] inputfinal DateTime now)
             throws SubscriptionBaseMigrationApiException {
 
         try {
             TimedMigration[] events;
             final Plan plan0 = .getFullCatalog().findPlan(input[0].getPlanPhaseSpecifier().getProductName(),
                                                                         input[0].getPlanPhaseSpecifier().getBillingPeriod(), input[0].getPlanPhaseSpecifier().getPriceListName(), now);
 
             final Plan plan1 = (input.length > 1) ? .getFullCatalog().findPlan(input[1].getPlanPhaseSpecifier().getProductName(),
                                                                                              input[1].getPlanPhaseSpecifier().getBillingPeriod(), input[1].getPlanPhaseSpecifier().getPriceListName(), now) :
                                null;
 
             DateTime migrationStartDate = input[0].getEffectiveDate();
 
             if (isRegularMigratedSubscription(input)) {
 
                 events = getEventsOnRegularMigration(plan0,
                                                      getPlanPhase(plan0input[0].getPlanPhaseSpecifier().getPhaseType()),
                                                      input[0].getPlanPhaseSpecifier().getPriceListName(),
                                                      migrationStartDate);
 
             } else if (isRegularFutureCancelledMigratedSubscription(input)) {
 
                 events = getEventsOnFuturePlanCancelMigration(plan0,
                                                               getPlanPhase(plan0input[0].getPlanPhaseSpecifier().getPhaseType()),
                                                               input[0].getPlanPhaseSpecifier().getPriceListName(),
                                                               migrationStartDate,
                                                               input[0].getCancelledDate());
 
             } else if (isPhaseChangeMigratedSubscription(input)) {
 
                 final PhaseType curPhaseType = input[0].getPlanPhaseSpecifier().getPhaseType();
                 Duration curPhaseDuration = null;
                 for (final PlanPhase cur : plan0.getAllPhases()) {
                     if (cur.getPhaseType() == curPhaseType) {
                         curPhaseDuration = cur.getDuration();
                         break;
                     }
                 }
                 if (curPhaseDuration == null) {
                     throw new SubscriptionBaseMigrationApiException(String.format("Failed to compute current phase duration for plan %s and phase %s",
                                                                              plan0.getName(), curPhaseType));
                 }
 
                 migrationStartDate = removeDuration(input[1].getEffectiveDate(), curPhaseDuration);
                 events = getEventsOnFuturePhaseChangeMigration(plan0,
                                                                getPlanPhase(plan0input[0].getPlanPhaseSpecifier().getPhaseType()),
                                                                input[0].getPlanPhaseSpecifier().getPriceListName(),
                                                                migrationStartDate,
                                                                input[1].getEffectiveDate());
 
             } else if (isPlanChangeMigratedSubscription(input)) {
 
                 events = getEventsOnFuturePlanChangeMigration(plan0,
                                                               getPlanPhase(plan0input[0].getPlanPhaseSpecifier().getPhaseType()),
                                                              plan1,
                                                              getPlanPhase(plan1input[1].getPlanPhaseSpecifier().getPhaseType()),
                                                              input[0].getPlanPhaseSpecifier().getPriceListName(),
                                                              migrationStartDate,
                                                              input[1].getEffectiveDate());
            } else {
                throw new SubscriptionBaseMigrationApiException("Unknown migration type");
            }
            return events;
        } catch (CatalogApiException e) {
            throw new SubscriptionBaseMigrationApiException(e);
        }
    }
    private TimedMigration[] getEventsOnRegularMigration(final Plan planfinal PlanPhase initialPhasefinal String priceListfinal DateTime effectiveDate) {
        final TimedMigration[] result = new TimedMigration[1];
        result[0] = new TimedMigration(effectiveDate..planinitialPhasepriceList);
        return result;
    }
    private TimedMigration[] getEventsOnFuturePhaseChangeMigration(final Plan planfinal PlanPhase initialPhasefinal String priceListfinal DateTime effectiveDatefinal DateTime effectiveDateForNextPhase)
            throws SubscriptionBaseMigrationApiException {
        final TimedMigration[] result = new TimedMigration[2];
        result[0] = new TimedMigration(effectiveDate..planinitialPhasepriceList);
        boolean foundCurrent = false;
        PlanPhase nextPhase = null;
        for (final PlanPhase cur : plan.getAllPhases()) {
            if (cur == initialPhase) {
                foundCurrent = true;
                continue;
            }
            if (foundCurrent) {
                nextPhase = cur;
            }
        }
        if (nextPhase == null) {
            throw new SubscriptionBaseMigrationApiException(String.format("Cannot find next phase for Plan %s and current Phase %s",
                                                                     plan.getName(), initialPhase.getName()));
        }
        result[1] = new TimedMigration(effectiveDateForNextPhase.nullplannextPhasepriceList);
        return result;
    }
    private TimedMigration[] getEventsOnFuturePlanChangeMigration(final Plan currentPlanfinal PlanPhase currentPhasefinal Plan newPlanfinal PlanPhase newPhasefinal String priceListfinal DateTime effectiveDatefinal DateTime effectiveDateForChangePlan) {
        final TimedMigration[] result = new TimedMigration[2];
        result[0] = new TimedMigration(effectiveDate..currentPlancurrentPhasepriceList);
        result[1] = new TimedMigration(effectiveDateForChangePlan..newPlannewPhasepriceList);
        return result;
    }
    private TimedMigration[] getEventsOnFuturePlanCancelMigration(final Plan planfinal PlanPhase initialPhasefinal String priceListfinal DateTime effectiveDatefinal DateTime effectiveDateForCancellation) {
        final TimedMigration[] result = new TimedMigration[2];
        result[0] = new TimedMigration(effectiveDate..planinitialPhasepriceList);
        result[1] = new TimedMigration(effectiveDateForCancellation..nullnullnull);
        return result;
    }
    // STEPH should be in catalog
    private PlanPhase getPlanPhase(final Plan planfinal PhaseType phaseTypethrows SubscriptionBaseMigrationApiException {
        for (final PlanPhase cur : plan.getAllPhases()) {
            if (cur.getPhaseType() == phaseType) {
                return cur;
            }
        }
        throw new SubscriptionBaseMigrationApiException(String.format("Cannot find PlanPhase from Plan %s and type %s"plan.getName(), phaseType));
    }
    private boolean isRegularMigratedSubscription(final SubscriptionMigrationCase[] input) {
        return (input.length == 1 && input[0].getCancelledDate() == null);
    }
        return (input.length == 1 && input[0].getCancelledDate() != null);
    }
    private boolean isPhaseChangeMigratedSubscription(final SubscriptionMigrationCase[] input) {
        if (input.length != 2) {
            return false;
        }
        return (isSamePlan(input[0].getPlanPhaseSpecifier(), input[1].getPlanPhaseSpecifier()) &&
                !isSamePhase(input[0].getPlanPhaseSpecifier(), input[1].getPlanPhaseSpecifier()));
    }
    private boolean isPlanChangeMigratedSubscription(final SubscriptionMigrationCase[] input) {
        if (input.length != 2) {
            return false;
        }
        return !isSamePlan(input[0].getPlanPhaseSpecifier(), input[1].getPlanPhaseSpecifier());
    }
    private boolean isSamePlan(final PlanPhaseSpecifier plan0final PlanPhaseSpecifier plan1) {
        if (plan0.getPriceListName().equals(plan1.getPriceListName()) &&
            plan0.getProductName().equals(plan1.getProductName()) &&
            plan0.getBillingPeriod() == plan1.getBillingPeriod()) {
            return true;
        }
        return false;
    }
    private boolean isSamePhase(final PlanPhaseSpecifier plan0final PlanPhaseSpecifier plan1) {
        if (plan0.getPhaseType() == plan1.getPhaseType()) {
            return true;
        }
        return false;
    }
New to GrepCode? Check out our FAQ X