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.user;
 
 import java.util.List;
 import java.util.UUID;
 
 import  javax.annotation.Nullable;
 
 
 
 public class DefaultSubscriptionBase extends EntityBase implements SubscriptionBase {
 
     private static final Logger log = LoggerFactory.getLogger(DefaultSubscriptionBase.class);
 
     private final Clock clock;
     private final SubscriptionBaseApiService apiService;
 
     //
     // Final subscription fields
     //
     private final UUID bundleId;
     private final DateTime alignStartDate;
     private final DateTime bundleStartDate;
     private final ProductCategory category;
 
     //
     // Those can be modified through non User APIs, and a new SubscriptionBase
     // object would be created
     //
     private final long activeVersion;
     private final DateTime chargedThroughDate;
 
     //
     // User APIs (create, change, cancelWithRequestedDate,...) will recompute those each time,
     // so the user holding that subscription object get the correct state when
     // the call completes
     //
 
     // Low level events are ONLY used for Repair APIs
     protected List<SubscriptionBaseEventevents;
 
 
     public List<SubscriptionBaseEventgetEvents() {
         return ;
     }
 
     // Transient object never returned at the API
     public DefaultSubscriptionBase(final SubscriptionBuilder builder) {
         this(buildernullnull);
    }
    public DefaultSubscriptionBase(final SubscriptionBuilder builder, @Nullable final SubscriptionBaseApiService apiService, @Nullable final Clock clock) {
        super(builder.getId(), builder.getCreatedDate(), builder.getUpdatedDate());
        this. = apiService;
        this. = clock;
        this. = builder.getBundleId();
        this. = builder.getAlignStartDate();
        this. = builder.getBundleStartDate();
        this. = builder.getCategory();
        this. = builder.getActiveVersion();
        this. = builder.getChargedThroughDate();
    }
    // Used for API to make sure we have a clock and an apiService set before we return the object
    public DefaultSubscriptionBase(final DefaultSubscriptionBase internalSubscriptionfinal SubscriptionBaseApiService apiServicefinal Clock clock) {
        super(internalSubscription.getId(), internalSubscription.getCreatedDate(), internalSubscription.getUpdatedDate());
        this. = apiService;
        this. = clock;
        this. = internalSubscription.getBundleId();
        this. = internalSubscription.getAlignStartDate();
        this. = internalSubscription.getBundleStartDate();
        this. = internalSubscription.getCategory();
        this. = internalSubscription.getActiveVersion();
        this. = internalSubscription.getChargedThroughDate();
        this. = new LinkedList<SubscriptionBaseTransition>(internalSubscription.getAllTransitions());
        this. = internalSubscription.getEvents();
    }
    @Override
    public UUID getBundleId() {
        return ;
    }
    @Override
    public DateTime getStartDate() {
        return .get(0).getEffectiveTransitionTime();
    }
    @Override
    public EntitlementState getState() {
        return (getPreviousTransition() == null) ? null
                                                 : getPreviousTransition().getNextState();
    }
    @Override
        if ( == null) {
            return null;
        }
        final SubscriptionBaseTransitionData initialTransition = (SubscriptionBaseTransitionData.get(0);
        switch (initialTransition.getApiEventType()) {
            case :
            case :
                return .;
            case :
                return .;
            default:
                return .;
        }
    }
    @Override
    public PlanPhase getCurrentPhase() {
        return (getPreviousTransition() == null) ? null
                                                 : getPreviousTransition().getNextPhase();
    }
    @Override
    public Plan getCurrentPlan() {
        return (getPreviousTransition() == null) ? null
                                                 : getPreviousTransition().getNextPlan();
    }
    @Override
    public PriceList getCurrentPriceList() {
        return (getPreviousTransition() == null) ? null :
               getPreviousTransition().getNextPriceList();
    }
    @Override
    public DateTime getEndDate() {
        final SubscriptionBaseTransition latestTransition = getPreviousTransition();
        if (latestTransition.getNextState() == .) {
            return latestTransition.getEffectiveTransitionTime();
        }
        return null;
    }
    @Override
    public DateTime getFutureEndDate() {
        if ( == null) {
            return null;
        }
                ..,
                ..);
        while (it.hasNext()) {
            final SubscriptionBaseTransition cur = it.next();
            if (cur.getTransitionType() == .) {
                return cur.getEffectiveTransitionTime();
            }
        }
        return null;
    }
    public boolean recreate(final PlanPhaseSpecifier specfinal DateTime requestedDate,
                            final CallContext contextthrows SubscriptionBaseApiException {
        return .recreatePlan(thisspecrequestedDatecontext);
    }
    @Override
    public boolean cancel(final CallContext contextthrows SubscriptionBaseApiException {
        return .cancel(thiscontext);
    }
    @Override
    public boolean cancelWithDate(final DateTime requestedDatefinal CallContext contextthrows SubscriptionBaseApiException {
        return .cancelWithRequestedDate(thisrequestedDatecontext);
    }
    @Override
    public boolean cancelWithPolicy(final BillingActionPolicy policyfinal CallContext contextthrows SubscriptionBaseApiException {
        return .cancelWithPolicy(thispolicycontext);
    }
    @Override
    public boolean uncancel(final CallContext context)
            throws SubscriptionBaseApiException {
        return .uncancel(thiscontext);
    }
    @Override
    public DateTime changePlan(final String productNamefinal BillingPeriod termfinal String priceList,
                               final CallContext contextthrows SubscriptionBaseApiException {
        return .changePlan(thisproductNametermpriceListcontext);
    }
    @Override
    public DateTime changePlanWithDate(final String productNamefinal BillingPeriod termfinal String priceList,
                                       final DateTime requestedDatefinal CallContext contextthrows SubscriptionBaseApiException {
        return .changePlanWithRequestedDate(thisproductNametermpriceListrequestedDatecontext);
    }
    @Override
    public DateTime changePlanWithPolicy(final String productNamefinal BillingPeriod termfinal String priceList,
                                         final BillingActionPolicy policyfinal CallContext contextthrows SubscriptionBaseApiException {
        return .changePlanWithPolicy(thisproductNametermpriceListpolicycontext);
    }
    @Override
        if ( == null) {
            return null;
        }
                ..,
                ..);
        return it.hasNext() ? it.next() : null;
    }
    @Override
    public Product getLastActiveProduct() {
        if (getState() == .) {
            final SubscriptionBaseTransition data = getPreviousTransition();
            return data.getPreviousPlan().getProduct();
        } else {
            return getCurrentPlan().getProduct();
        }
    }
    @Override
    public PriceList getLastActivePriceList() {
        if (getState() == .) {
            final SubscriptionBaseTransition data = getPreviousTransition();
            return data.getPreviousPriceList();
        } else {
            return getCurrentPriceList();
        }
    }
    @Override
        if (getState() == .) {
            final SubscriptionBaseTransition data = getPreviousTransition();
            return data.getPreviousPlan().getProduct().getCategory();
        } else {
            return getCurrentPlan().getProduct().getCategory();
        }
    }
    @Override
    public Plan getLastActivePlan() {
        if (getState() == .) {
            final SubscriptionBaseTransition data = getPreviousTransition();
            return data.getPreviousPlan();
        } else {
            return getCurrentPlan();
        }
    }
    @Override
    public PlanPhase getLastActivePhase() {
        if (getState() == .) {
            final SubscriptionBaseTransition data = getPreviousTransition();
            return data.getPreviousPhase();
        } else {
            return getCurrentPhase();
        }
    }
    @Override
        if (getState() == .) {
            final SubscriptionBaseTransition data = getPreviousTransition();
            return data.getPreviousPlan().getBillingPeriod();
        } else {
            return getCurrentPlan().getBillingPeriod();
        }
    }
    @Override
        if ( == null) {
            return null;
        }
                ..,
                ..);
        return it.hasNext() ? it.next() : null;
    }
    @Override
    public ProductCategory getCategory() {
        return ;
    }
    public DateTime getBundleStartDate() {
        return ;
    }
    @Override
    public DateTime getChargedThroughDate() {
        return ;
    }
    @Override
        if ( == null) {
            return Collections.emptyList();
        }
        final List<SubscriptionBaseTransitionresult = new ArrayList<SubscriptionBaseTransition>();
        while (it.hasNext()) {
            result.add(it.next());
        }
        return result;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                 + (( == null) ? 0 : .hashCode());
        return result;
    }
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DefaultSubscriptionBase other = (DefaultSubscriptionBaseobj;
        if ( == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!.equals(other.id)) {
            return false;
        }
        return true;
    }
    public SubscriptionBaseTransitionData getTransitionFromEvent(final SubscriptionBaseEvent eventfinal int seqId) {
        if ( == null || event == null) {
            return null;
        }
        SubscriptionBaseTransitionData prev = null;
        for (final SubscriptionBaseTransition cur : ) {
            final SubscriptionBaseTransitionData curData = (SubscriptionBaseTransitionDatacur;
            if (curData.getId().equals(event.getId())) {
                final SubscriptionBaseTransitionData withSeq = new SubscriptionBaseTransitionData(curDataseqId);
                return withSeq;
            }
            if (curData.getTotalOrdering() < event.getTotalOrdering()) {
                prev = curData;
            }
        }
        // Since UNCANCEL are not part of the transitions, we compute a new 'UNCANCEL' transition based on the event right before that UNCANCEL
        // This is used to be able to send a bus event for uncancellation
        if (prev != null && event.getType() == . && ((ApiEventevent).getEventType() == .) {
            return withSeq;
        }
        return null;
    }
    public DateTime getAlignStartDate() {
        return ;
    }
    public long getLastEventOrderedId() {
                ..,
                ..);
        return it.hasNext() ? ((SubscriptionBaseTransitionDatait.next()).getTotalOrdering() : -1L;
    }
    public long getActiveVersion() {
        return ;
    }
        if ( == null) {
            return Collections.emptyList();
        }
        final List<SubscriptionBaseTransitionresult = new ArrayList<SubscriptionBaseTransition>();
                ..,
                ..);
        // Remove anything prior to first CREATE or MIGRATE_BILLING
        boolean foundInitialEvent = false;
        while (it.hasNext()) {
            final SubscriptionBaseTransitionData curTransition = (SubscriptionBaseTransitionDatait.next();
            if (!foundInitialEvent) {
                foundInitialEvent = curTransition.getEventType() == . &&
                                    (curTransition.getApiEventType() == . ||
                                     curTransition.getApiEventType() == . ||
                                     curTransition.getApiEventType() == .);
            }
            if (foundInitialEvent) {
                result.add(curTransition);
            }
        }
        return result;
    }
        if ( == null) {
            throw new SubscriptionBaseError(String.format("No transitions for subscription %s"getId()));
        }
                                                                                                     ,
                                                                                                     .,
                                                                                                     .,
                                                                                                     .,
                                                                                                     .);
        while (it.hasNext()) {
            final SubscriptionBaseTransitionData cur = (SubscriptionBaseTransitionDatait.next();
            if (cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .) {
                return cur;
            }
        }
        throw new SubscriptionBaseError(String.format("Failed to find InitialTransitionForCurrentPlan id = %s"getId()));
    }
    public boolean isSubscriptionFutureCancelled() {
        return getFutureEndDate() != null;
    }
    public DateTime getPlanChangeEffectiveDate(final BillingActionPolicy policy) {
        switch (policy) {
            case :
                return .getUTCNow();
            case :
                //
                // If we have a chargedThroughDate that is 'up to date' we use it, if not default to now
                // chargedThroughDate could exist and be less than now if:
                // 1. account is not being invoiced, for e.g AUTO_INVOICING_OFF nis set
                // 2. In the case if FIXED item CTD is set using startDate of the service period
                //
                return ( != null && .isAfter(.getUTCNow())) ?  : .getUTCNow();
            default:
                throw new SubscriptionBaseError(String.format(
                        "Unexpected policy type %s"policy.toString()));
        }
    }
    public DateTime getCurrentPhaseStart() {
        if ( == null) {
            throw new SubscriptionBaseError(String.format(
                    "No transitions for subscription %s"getId()));
        }
                ..,
                ..);
        while (it.hasNext()) {
            final SubscriptionBaseTransitionData cur = (SubscriptionBaseTransitionDatait.next();
            if (cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .
                || cur.getTransitionType() == .) {
                return cur.getEffectiveTransitionTime();
            }
        }
        throw new SubscriptionBaseError(String.format(
                "Failed to find CurrentPhaseStart id = %s"getId().toString()));
    }
    public void rebuildTransitions(final List<SubscriptionBaseEventinputEventsfinal Catalog catalog) {
        if (inputEvents == null) {
            return;
        }
        this. = inputEvents;
        UUID nextUserToken = null;
        UUID nextEventId = null;
        DateTime nextCreatedDate = null;
        EntitlementState nextState = null;
        String nextPlanName = null;
        String nextPhaseName = null;
        String nextPriceListName = null;
        UUID prevEventId = null;
        DateTime prevCreatedDate = null;
        EntitlementState previousState = null;
        PriceList previousPriceList = null;
        Plan previousPlan = null;
        PlanPhase previousPhase = null;
        for (final SubscriptionBaseEvent cur : inputEvents) {
            if (!cur.isActive() || cur.getActiveVersion() < ) {
                continue;
            }
            ApiEventType apiEventType = null;
            boolean isFromDisk = true;
            nextEventId = cur.getId();
            nextCreatedDate = cur.getCreatedDate();
            switch (cur.getType()) {
                case :
                    final PhaseEvent phaseEV = (PhaseEventcur;
                    nextPhaseName = phaseEV.getPhase();
                    break;
                case :
                    final ApiEvent userEV = (ApiEventcur;
                    apiEventType = userEV.getEventType();
                    isFromDisk = userEV.isFromDisk();
                    switch (apiEventType) {
                        case :
                        case :
                        case :
                        case :
                        case :
                            prevEventId = null;
                            prevCreatedDate = null;
                            previousState = null;
                            previousPlan = null;
                            previousPhase = null;
                            previousPriceList = null;
                            nextState = .;
                            nextPlanName = userEV.getEventPlan();
                            nextPhaseName = userEV.getEventPlanPhase();
                            nextPriceListName = userEV.getPriceList();
                            break;
                        case :
                            nextPlanName = userEV.getEventPlan();
                            nextPhaseName = userEV.getEventPlanPhase();
                            nextPriceListName = userEV.getPriceList();
                            break;
                        case :
                            nextState = .;
                            nextPlanName = null;
                            nextPhaseName = null;
                            break;
                        case :
                        default:
                            throw new SubscriptionBaseError(String.format(
                                    "Unexpected UserEvent type = %s"userEV
                                    .getEventType().toString()));
                    }
                    break;
                default:
                    throw new SubscriptionBaseError(String.format(
                            "Unexpected Event type = %s"cur.getType()));
            }
            Plan nextPlan = null;
            PlanPhase nextPhase = null;
            PriceList nextPriceList = null;
            try {
                nextPlan = (nextPlanName != null) ? catalog.findPlan(nextPlanNamecur.getRequestedDate(), getAlignStartDate()) : null;
                nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseNamecur.getRequestedDate(), getAlignStartDate()) : null;
                nextPriceList = (nextPriceListName != null) ? catalog.findPriceList(nextPriceListNamecur.getRequestedDate()) : null;
            } catch (CatalogApiException e) {
                .error(String.format("Failed to build transition for subscription %s"), e);
            }
            final SubscriptionBaseTransitionData transition = new SubscriptionBaseTransitionData(
                    cur.getId(), cur.getType(), apiEventType,
                    cur.getRequestedDate(), cur.getEffectiveDate(),
                    prevEventIdprevCreatedDate,
                    previousStatepreviousPlanpreviousPhase,
                    previousPriceList,
                    nextEventIdnextCreatedDate,
                    nextStatenextPlannextPhase,
                    nextPriceListcur.getTotalOrdering(),
                    cur.getCreatedDate(),
                    nextUserToken,
                    isFromDisk);
            .add(transition);
            previousState = nextState;
            previousPlan = nextPlan;
            previousPhase = nextPhase;
            previousPriceList = nextPriceList;
            prevEventId = nextEventId;
            prevCreatedDate = nextCreatedDate;
        }
    }
New to GrepCode? Check out our FAQ X