Search in sources :

Example 1 with DefaultPlan

use of org.killbill.billing.catalog.DefaultPlan in project killbill by killbill.

the class EhCacheOverriddenPlanCache method getOverriddenPlan.

@Override
public DefaultPlan getOverriddenPlan(final String planName, final StaticCatalog catalog, final InternalTenantContext context) {
    final ObjectType irrelevant = null;
    final Object[] args = new Object[2];
    args[0] = loaderCallback;
    args[1] = catalog;
    final CacheLoaderArgument argument = new CacheLoaderArgument(irrelevant, args, context);
    return (DefaultPlan) cacheController.get(planName, argument);
}
Also used : ObjectType(org.killbill.billing.ObjectType) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPriceOverride(org.killbill.billing.catalog.override.DefaultPriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)

Example 2 with DefaultPlan

use of org.killbill.billing.catalog.DefaultPlan in project killbill by killbill.

the class EhCacheOverriddenPlanCache method loadOverriddenPlan.

private DefaultPlan loadOverriddenPlan(final String planName, final StaticCatalog catalog, final InternalTenantContext context) throws CatalogApiException {
    final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName);
    if (!m.matches()) {
        throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, planName);
    }
    final String parentPlanName = m.group(1);
    final Long planDefRecordId = Long.parseLong(m.group(2));
    final List<CatalogOverridePhaseDefinitionModelDao> phaseDefs = overrideDao.getOverriddenPlanPhases(planDefRecordId, context);
    final DefaultPlan defaultPlan = (DefaultPlan) catalog.findCurrentPlan(parentPlanName);
    final PlanPhasePriceOverride[] overrides = createOverrides(defaultPlan, phaseDefs);
    final DefaultPlan result = new DefaultPlan(planName, defaultPlan, overrides);
    result.initialize((StandaloneCatalog) catalog, ((StandaloneCatalog) catalog).getCatalogURI());
    return result;
}
Also used : Matcher(java.util.regex.Matcher) CatalogOverridePhaseDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePhaseDefinitionModelDao) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)

Example 3 with DefaultPlan

use of org.killbill.billing.catalog.DefaultPlan in project killbill by killbill.

the class DefaultPriceOverride method getOrCreateOverriddenPlan.

@Override
public DefaultPlan getOrCreateOverriddenPlan(final StandaloneCatalog standaloneCatalog, final Plan parentPlan, final DateTime catalogEffectiveDate, final List<PlanPhasePriceOverride> overrides, @Nullable final InternalCallContext context) throws CatalogApiException {
    final PlanPhasePriceOverride[] resolvedOverride = new PlanPhasePriceOverride[parentPlan.getAllPhases().length];
    int index = 0;
    for (final PlanPhase curPhase : parentPlan.getAllPhases()) {
        final PlanPhasePriceOverride curOverride = Iterables.tryFind(overrides, new Predicate<PlanPhasePriceOverride>() {

            @Override
            public boolean apply(final PlanPhasePriceOverride input) {
                if (input.getPhaseName() != null) {
                    return input.getPhaseName().equals(curPhase.getName());
                }
                // If the phaseName was not passed, we infer by matching the phaseType. This obviously would not work in a case where
                // a plan is defined with multiple phases of the same type.
                final PlanPhaseSpecifier curPlanPhaseSpecifier = input.getPlanPhaseSpecifier();
                if (curPlanPhaseSpecifier.getPhaseType().equals(curPhase.getPhaseType())) {
                    return true;
                }
                return false;
            }
        }).orNull();
        resolvedOverride[index++] = curOverride != null ? new DefaultPlanPhasePriceOverride(curPhase.getName(), curOverride.getCurrency(), curOverride.getFixedPrice(), curOverride.getRecurringPrice()) : null;
    }
    for (int i = 0; i < resolvedOverride.length; i++) {
        final PlanPhasePriceOverride curOverride = resolvedOverride[i];
        if (curOverride != null) {
            final DefaultPlanPhase curPhase = (DefaultPlanPhase) parentPlan.getAllPhases()[i];
            if (curPhase.getFixed() == null && curOverride.getFixedPrice() != null) {
                final String error = String.format("There is no existing fixed price for the phase %s", curPhase.getName());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_INVALID_PRICE_OVERRIDE, parentPlan.getName(), error);
            }
            if (curPhase.getRecurring() == null && curOverride.getRecurringPrice() != null) {
                final String error = String.format("There is no existing recurring price for the phase %s", curPhase.getName());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_INVALID_PRICE_OVERRIDE, parentPlan.getName(), error);
            }
        }
    }
    final String planName;
    if (context != null) {
        final CatalogOverridePlanDefinitionModelDao overriddenPlan = overrideDao.getOrCreateOverridePlanDefinition(parentPlan.getName(), catalogEffectiveDate, resolvedOverride, context);
        planName = new StringBuffer(parentPlan.getName()).append("-").append(overriddenPlan.getRecordId()).toString();
    } else {
        planName = new StringBuffer(parentPlan.getName()).append("-dryrun-").append(DRY_RUN_PLAN_IDX.incrementAndGet()).toString();
    }
    final DefaultPlan result = new DefaultPlan(planName, (DefaultPlan) parentPlan, resolvedOverride);
    result.initialize(standaloneCatalog, standaloneCatalog.getCatalogURI());
    if (context == null) {
        overriddenPlanCache.addDryRunPlan(planName, result);
    }
    return result;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) Predicate(com.google.common.base.Predicate) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) CatalogOverridePlanDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePlanDefinitionModelDao) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)

Example 4 with DefaultPlan

use of org.killbill.billing.catalog.DefaultPlan in project killbill by killbill.

the class StandaloneCatalogMapper method toDefaultPlan.

private Plan toDefaultPlan(final Plan input) {
    if (tmpDefaultPlans != null) {
        final Plan existingPlan = tmpDefaultPlans.get(input.getName());
        if (existingPlan == null)
            throw new IllegalStateException("Unknown plan " + input.getName());
        return existingPlan;
    }
    final DefaultPlan result = new DefaultPlan();
    result.setName(input.getName());
    result.setEffectiveDateForExistingSubscriptions(input.getEffectiveDateForExistingSubscriptions());
    result.setFinalPhase(toDefaultPlanPhase(input.getFinalPhase()));
    result.setInitialPhases(toDefaultPlanPhases(ImmutableList.copyOf(input.getInitialPhases())));
    result.setPlansAllowedInBundle(input.getPlansAllowedInBundle());
    result.setProduct(toDefaultProduct(input.getProduct()));
    result.setPriceListName(input.getPriceListName());
    return result;
}
Also used : DefaultPlan(org.killbill.billing.catalog.DefaultPlan) Plan(org.killbill.billing.catalog.api.Plan) DefaultPlan(org.killbill.billing.catalog.DefaultPlan)

Example 5 with DefaultPlan

use of org.killbill.billing.catalog.DefaultPlan in project killbill by killbill.

the class TestXMLWriter method testAddPlan.

@Test(groups = "fast")
public void testAddPlan() throws Exception {
    final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarBasic.xml").toExternalForm(), StandaloneCatalog.class);
    final MutableStaticCatalog mutableCatalog = new DefaultMutableStaticCatalog(catalog);
    final DefaultProduct newProduct = new DefaultProduct();
    newProduct.setName("Dynamic");
    newProduct.setCatagory(ProductCategory.BASE);
    newProduct.initialize((StandaloneCatalog) mutableCatalog, new URI("dummy"));
    mutableCatalog.addProduct(newProduct);
    final DefaultPlanPhase trialPhase = new DefaultPlanPhase();
    trialPhase.setPhaseType(PhaseType.TRIAL);
    trialPhase.setDuration(new DefaultDuration().setUnit(TimeUnit.DAYS).setNumber(14));
    trialPhase.setFixed(new DefaultFixed().setFixedPrice(new DefaultInternationalPrice().setPrices(new DefaultPrice[] { new DefaultPrice().setCurrency(Currency.USD).setValue(BigDecimal.ZERO) })));
    final DefaultPlanPhase evergreenPhase = new DefaultPlanPhase();
    evergreenPhase.setPhaseType(PhaseType.EVERGREEN);
    evergreenPhase.setDuration(new DefaultDuration().setUnit(TimeUnit.MONTHS).setNumber(1));
    evergreenPhase.setRecurring(new DefaultRecurring().setBillingPeriod(BillingPeriod.MONTHLY).setRecurringPrice(new DefaultInternationalPrice().setPrices(new DefaultPrice[] { new DefaultPrice().setCurrency(Currency.USD).setValue(BigDecimal.TEN) })));
    final DefaultPlan newPlan = new DefaultPlan();
    newPlan.setName("dynamic-monthly");
    newPlan.setPriceListName(DefaultPriceListSet.DEFAULT_PRICELIST_NAME);
    newPlan.setProduct(newProduct);
    newPlan.setInitialPhases(new DefaultPlanPhase[] { trialPhase });
    newPlan.setFinalPhase(evergreenPhase);
    // TODO Ordering breaks
    mutableCatalog.addPlan(newPlan);
    newPlan.initialize((StandaloneCatalog) mutableCatalog, new URI("dummy"));
    final String newCatalogStr = XMLWriter.writeXML((StandaloneCatalog) mutableCatalog, StandaloneCatalog.class);
    final StandaloneCatalog newCatalog = XMLLoader.getObjectFromStream(new URI("dummy"), new ByteArrayInputStream(newCatalogStr.getBytes(Charset.forName("UTF-8"))), StandaloneCatalog.class);
    assertEquals(newCatalog.getCurrentPlans().size(), catalog.getCurrentPlans().size() + 1);
    final Plan plan = newCatalog.findCurrentPlan("dynamic-monthly");
    assertEquals(plan.getName(), "dynamic-monthly");
    assertEquals(plan.getPriceListName(), DefaultPriceListSet.DEFAULT_PRICELIST_NAME);
    assertEquals(plan.getProduct().getName(), "Dynamic");
    assertEquals(plan.getProduct().getCategory(), ProductCategory.BASE);
    assertEquals(plan.getInitialPhases().length, 1);
    assertEquals(plan.getInitialPhases()[0].getName(), "dynamic-monthly-trial");
    assertEquals(plan.getInitialPhases()[0].getPhaseType(), PhaseType.TRIAL);
    assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices().length, 1);
    assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getCurrency(), Currency.USD);
    assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getValue(), BigDecimal.ZERO);
    assertEquals(plan.getFinalPhase().getName(), "dynamic-monthly-evergreen");
    assertEquals(plan.getFinalPhase().getPhaseType(), PhaseType.EVERGREEN);
    assertEquals(plan.getFinalPhase().getRecurring().getBillingPeriod(), BillingPeriod.MONTHLY);
    assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices().length, 1);
    assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getCurrency(), Currency.USD);
    assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getValue(), BigDecimal.TEN);
}
Also used : DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) DefaultProduct(org.killbill.billing.catalog.DefaultProduct) DefaultRecurring(org.killbill.billing.catalog.DefaultRecurring) DefaultInternationalPrice(org.killbill.billing.catalog.DefaultInternationalPrice) Plan(org.killbill.billing.catalog.api.Plan) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) URI(java.net.URI) DefaultMutableStaticCatalog(org.killbill.billing.catalog.DefaultMutableStaticCatalog) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) MutableStaticCatalog(org.killbill.billing.catalog.api.MutableStaticCatalog) DefaultMutableStaticCatalog(org.killbill.billing.catalog.DefaultMutableStaticCatalog) DefaultFixed(org.killbill.billing.catalog.DefaultFixed) DefaultDuration(org.killbill.billing.catalog.DefaultDuration) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Aggregations

DefaultPlan (org.killbill.billing.catalog.DefaultPlan)5 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)3 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)3 DefaultPlanPhase (org.killbill.billing.catalog.DefaultPlanPhase)2 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)2 Plan (org.killbill.billing.catalog.api.Plan)2 Predicate (com.google.common.base.Predicate)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URI (java.net.URI)1 Matcher (java.util.regex.Matcher)1 ObjectType (org.killbill.billing.ObjectType)1 DefaultDuration (org.killbill.billing.catalog.DefaultDuration)1 DefaultFixed (org.killbill.billing.catalog.DefaultFixed)1 DefaultInternationalPrice (org.killbill.billing.catalog.DefaultInternationalPrice)1 DefaultMutableStaticCatalog (org.killbill.billing.catalog.DefaultMutableStaticCatalog)1 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)1 DefaultProduct (org.killbill.billing.catalog.DefaultProduct)1 DefaultRecurring (org.killbill.billing.catalog.DefaultRecurring)1 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)1 StandaloneCatalogWithPriceOverride (org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)1