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.svcs;
 
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
 import  javax.annotation.Nullable;
 
 
 
 
 
     private final Logger log = LoggerFactory.getLogger(DefaultSubscriptionInternalApi.class);
 
     private final AddonUtils addonUtils;
     private final NonEntityDao nonEntityDao;
 
     @Inject
     public DefaultSubscriptionInternalApi(final SubscriptionDao dao,
                                           final DefaultSubscriptionBaseApiService apiService,
                                           final Clock clock,
                                           final CatalogService catalogService,
                                           final AddonUtils addonUtils,
                                           final NonEntityDao nonEntityDao) {
         super(daoapiServiceclockcatalogService);
         this. = addonUtils;
         this. = nonEntityDao;
     }
 
     @Override
     public SubscriptionBase createSubscription(final UUID bundleIdfinal PlanPhaseSpecifier specfinal DateTime requestedDateWithMsfinal InternalCallContext contextthrows SubscriptionBaseApiException {
         try {
             final String realPriceList = (spec.getPriceListName() == null) ? . : spec.getPriceListName();
             final DateTime now = .getUTCNow();
             final DateTime requestedDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : now;
             if (requestedDate.isAfter(now)) {
                 throw new SubscriptionBaseApiException(.now.toString(), requestedDate.toString());
             }
            final DateTime effectiveDate = requestedDate;
            final Catalog catalog = .getFullCatalog();
            final Plan plan = catalog.findPlan(spec.getProductName(), spec.getBillingPeriod(), realPriceListrequestedDate);
            final PlanPhase phase = plan.getAllPhases()[0];
            if (phase == null) {
                throw new SubscriptionBaseError(String.format("No initial PlanPhase for Product %s, term %s and set %s does not exist in the catalog",
                                                              spec.getProductName(), spec.getBillingPeriod().toString(), realPriceList));
            }
            final SubscriptionBaseBundle bundle = .getSubscriptionBundleFromId(bundleIdcontext);
            if (bundle == null) {
                throw new SubscriptionBaseApiException(.bundleId);
            }
            DateTime bundleStartDate = null;
            final DefaultSubscriptionBase baseSubscription = (DefaultSubscriptionBase.getBaseSubscription(bundleIdcontext);
            switch (plan.getProduct().getCategory()) {
                case :
                    if (baseSubscription != null) {
                        if (baseSubscription.getState() == .) {
                            throw new SubscriptionBaseApiException(.bundleId);
                        }
                    }
                    bundleStartDate = requestedDate;
                    break;
                case :
                    if (baseSubscription == null) {
                        throw new SubscriptionBaseApiException(.bundleId);
                    }
                    if (effectiveDate.isBefore(baseSubscription.getStartDate())) {
                        throw new SubscriptionBaseApiException(.effectiveDate.toString(), baseSubscription.getStartDate().toString());
                    }
                    .checkAddonCreationRights(baseSubscriptionplan);
                    bundleStartDate = baseSubscription.getStartDate();
                    break;
                case :
                    if (baseSubscription != null) {
                        throw new SubscriptionBaseApiException(.bundleId);
                    }
                    // Not really but we don't care, there is no alignment for STANDALONE subscriptions
                    bundleStartDate = requestedDate;
                    break;
                default:
                    throw new SubscriptionBaseError(String.format("Can't create subscription of type %s",
                                                                  plan.getProduct().getCategory().toString()));
            }
            final UUID tenantId = .retrieveIdFromObject(context.getTenantRecordId(), .);
            return .createPlan(new SubscriptionBuilder()
                                                 .setId(UUID.randomUUID())
                                                 .setBundleId(bundleId)
                                                 .setCategory(plan.getProduct().getCategory())
                                                 .setBundleStartDate(bundleStartDate)
                                                 .setAlignStartDate(effectiveDate),
                                         planspec.getPhaseType(), realPriceListrequestedDateeffectiveDatenowcontext.toCallContext(tenantId));
        } catch (CatalogApiException e) {
            throw new SubscriptionBaseApiException(e);
        }
    }
    @Override
    public SubscriptionBaseBundle createBundleForAccount(final UUID accountIdfinal String bundleKeyfinal InternalCallContext contextthrows SubscriptionBaseApiException {
        final List<SubscriptionBaseBundleexistingBundles = .getSubscriptionBundlesForKey(bundleKeycontext);
        final DateTime now = .getUTCNow();
        final DateTime originalCreatedDate = existingBundles.size() > 0 ? existingBundles.get(0).getCreatedDate() : now;
        final DefaultSubscriptionBaseBundle bundle = new DefaultSubscriptionBaseBundle(bundleKeyaccountIdnoworiginalCreatedDatenownow);
        return .createSubscriptionBundle(bundlecontext);
    }
    @Override
    public List<SubscriptionBaseBundlegetBundlesForAccountAndKey(final UUID accountIdfinal String bundleKeyfinal InternalTenantContext contextthrows SubscriptionBaseApiException {
        final List<SubscriptionBaseBundlebundlesForAccountAndKey = .getSubscriptionBundlesForAccountAndKey(accountIdbundleKeycontext);
        return bundlesForAccountAndKey;
    }
    @Override
    public List<SubscriptionBaseBundlegetBundlesForAccount(final UUID accountIdfinal InternalTenantContext context) {
        return .getSubscriptionBundleForAccount(accountIdcontext);
    }
    @Override
    public List<SubscriptionBaseBundlegetBundlesForKey(final String bundleKeyfinal InternalTenantContext context) {
        final List<SubscriptionBaseBundleresult = .getSubscriptionBundlesForKey(bundleKeycontext);
        return result;
    }
    @Override
    public Iterable<UUIDgetNonAOSubscriptionIdsForKey(final String bundleKeyfinal InternalTenantContext context) {
        return .getNonAOSubscriptionIdsForKey(bundleKeycontext);
    }
    public static SubscriptionBaseBundle getActiveBundleForKeyNotException(final List<SubscriptionBaseBundleexistingBundlesfinal SubscriptionDao daofinal Clock clockfinal InternalTenantContext context)  {
        for (SubscriptionBaseBundle cur : existingBundles) {
            final List<SubscriptionBasesubscriptions = dao.getSubscriptions(cur.getId(), context);
            for (SubscriptionBase s : subscriptions) {
                if (s.getCategory() == .) {
                    continue;
                }
                if (s.getEndDate() == null || s.getEndDate().compareTo(clock.getUTCNow()) > 0) {
                    return cur;
                }
            }
        }
        return null;
    }
    @Override
                                                            InternalTenantContext context) {
        final List<SubscriptionBaseinternalSubscriptions = .getSubscriptions(bundleIdcontext);
        return createSubscriptionsForApiUse(internalSubscriptions);
    }
    @Override
        final Map<UUIDList<SubscriptionBase>> internalSubscriptions = .getSubscriptionsForAccount(context);
        final Map<UUIDList<SubscriptionBase>> result = new HashMap<UUIDList<SubscriptionBase>>();
        for (final UUID bundleId : internalSubscriptions.keySet()) {
            result.put(bundleIdcreateSubscriptionsForApiUse(internalSubscriptions.get(bundleId)));
        }
        return result;
    }
    @Override
    public SubscriptionBase getBaseSubscription(UUID bundleId,
                                                InternalTenantContext contextthrows SubscriptionBaseApiException {
        final SubscriptionBase result = .getBaseSubscription(bundleIdcontext);
        if (result == null) {
            throw new SubscriptionBaseApiException(.bundleId);
        }
        return createSubscriptionForApiUse(result);
    }
    @Override
                                                  InternalTenantContext contextthrows SubscriptionBaseApiException {
        final SubscriptionBase result = .getSubscriptionFromId(idcontext);
        if (result == null) {
            throw new SubscriptionBaseApiException(.id);
        }
        return createSubscriptionForApiUse(result);
    }
    @Override
    public SubscriptionBaseBundle getBundleFromId(final UUID idfinal InternalTenantContext contextthrows SubscriptionBaseApiException {
        final SubscriptionBaseBundle result = .getSubscriptionBundleFromId(idcontext);
        if (result == null) {
        }
        return result;
    }
    @Override
    public UUID getAccountIdFromSubscriptionId(final UUID subscriptionIdfinal InternalTenantContext contextthrows SubscriptionBaseApiException {
        return .getAccountIdFromSubscriptionId(subscriptionIdcontext);
    }
    @Override
    public void setChargedThroughDate(UUID subscriptionId,
                                      DateTime chargedThruDateInternalCallContext context) {
        final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase.getSubscriptionFromId(subscriptionIdcontext);
        final SubscriptionBuilder builder = new SubscriptionBuilder(subscription)
                .setChargedThroughDate(chargedThruDate);
        .updateChargedThroughDate(new DefaultSubscriptionBase(builder), context);
    }
    @Override
    public List<EffectiveSubscriptionInternalEventgetAllTransitions(final SubscriptionBase subscriptionfinal InternalTenantContext context) {
        final List<SubscriptionBaseTransitiontransitions = ((DefaultSubscriptionBasesubscription).getAllTransitions();
        return convertEffectiveSubscriptionInternalEventFromSubscriptionTransitions(subscriptioncontexttransitions);
    }
    @Override
        final List<SubscriptionBaseTransitiontransitions = ((DefaultSubscriptionBasesubscription).getBillingTransitions();
        return convertEffectiveSubscriptionInternalEventFromSubscriptionTransitions(subscriptioncontexttransitions);
    }
    @Override
    public DateTime getNextBillingDate(final UUID accountIdfinal InternalTenantContext context) {
        final List<SubscriptionBaseBundlebundles = getBundlesForAccount(accountIdcontext);
        DateTime result = null;
        for (final SubscriptionBaseBundle bundle : bundles) {
            final List<SubscriptionBasesubscriptions = getSubscriptionsForBundle(bundle.getId(), context);
            for (final SubscriptionBase subscription : subscriptions) {
                final DateTime chargedThruDate = subscription.getChargedThroughDate();
                if (result == null ||
                    (chargedThruDate != null && chargedThruDate.isBefore(result))) {
                    result = subscription.getChargedThroughDate();
                }
            }
        }
        return result;
    }
    @Override
    public List<EntitlementAOStatusDryRungetDryRunChangePlanStatus(final UUID subscriptionId, @Nullable final String baseProductNamefinal DateTime requestedDatefinal InternalTenantContext contextthrows SubscriptionBaseApiException {
        final SubscriptionBase subscription = .getSubscriptionFromId(subscriptionIdcontext);
        if (subscription == null) {
            throw new SubscriptionBaseApiException(.subscriptionId);
        }
        if (subscription.getCategory() != .) {
        }
        final List<EntitlementAOStatusDryRunresult = new LinkedList<EntitlementAOStatusDryRun>();
        final List<SubscriptionBasebundleSubscriptions = .getSubscriptions(subscription.getBundleId(), context);
        for (final SubscriptionBase cur : bundleSubscriptions) {
            if (cur.getId().equals(subscriptionId)) {
                continue;
            }
            // If ADDON is cancelled, skip
            if (cur.getState() == .) {
                continue;
            }
            final DryRunChangeReason reason;
            // If baseProductName is null, it's a cancellation dry-run. In this case, return all addons, so they are cancelled
            if (baseProductName != null && .isAddonIncludedFromProdName(baseProductNamerequestedDatecur.getCurrentPlan())) {
                reason = .;
            } else if (baseProductName != null && .isAddonAvailableFromProdName(baseProductNamerequestedDatecur.getCurrentPlan())) {
                reason = .;
            } else {
                reason = .;
            }
            final EntitlementAOStatusDryRun status = new DefaultSubscriptionStatusDryRun(cur.getId(),
                                                                                        cur.getCurrentPlan().getProduct().getName(),
                                                                                        cur.getCurrentPhase().getPhaseType(),
                                                                                        cur.getCurrentPlan().getBillingPeriod(),
                                                                                        cur.getCurrentPriceList().getName(), reason);
            result.add(status);
        }
        return result;
    }
    @Override
    public void updateExternalKey(final UUID bundleIdfinal String newExternalKeyfinal InternalCallContext context) {
        .updateBundleExternalKey(bundleIdnewExternalKeycontext);
    }
                                                                                                                          final InternalTenantContext contextfinal List<SubscriptionBaseTransitiontransitions) {
        return ImmutableList.<EffectiveSubscriptionInternalEvent>copyOf(Collections2.transform(transitionsnew Function<SubscriptionBaseTransitionEffectiveSubscriptionInternalEvent>() {
            @Override
            @Nullable
            public EffectiveSubscriptionInternalEvent apply(@Nullable SubscriptionBaseTransition input) {
                return new DefaultEffectiveSubscriptionEvent((SubscriptionBaseTransitionDatainput, ((DefaultSubscriptionBasesubscription).getAlignStartDate(), nullcontext.getAccountRecordId(), context.getTenantRecordId());
            }
        }));
    }
New to GrepCode? Check out our FAQ X