Search in sources :

Example 11 with StandaloneCatalog

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

the class DefaultCatalogUserApi method uploadCatalog.

@Override
public void uploadCatalog(final String catalogXML, final CallContext callContext) throws CatalogApiException {
    final InternalTenantContext internalTenantContext = createInternalTenantContext(callContext);
    try {
        final VersionedCatalog versionedCatalog = (VersionedCatalog) catalogService.getFullCatalog(false, true, internalTenantContext);
        // Validation purpose:  Will throw if bad XML or catalog validation fails
        final InputStream stream = new ByteArrayInputStream(catalogXML.getBytes());
        final StaticCatalog newCatalogVersion = XMLLoader.getObjectFromStream(new URI("dummy"), stream, StandaloneCatalog.class);
        if (versionedCatalog != null) {
            // currentCatalog.getCatalogName() could be null if tenant was created with a default catalog
            if (versionedCatalog.getCatalogName() != null && !newCatalogVersion.getCatalogName().equals(versionedCatalog.getCatalogName())) {
                final ValidationErrors errors = new ValidationErrors();
                errors.add(String.format("Catalog name '%s' should match previous catalog name '%s'", newCatalogVersion.getCatalogName(), versionedCatalog.getCatalogName()), new URI("dummy"), StandaloneCatalog.class, "");
                // Bummer ValidationException CTOR is private to package...
                //final ValidationException validationException = new ValidationException(errors);
                //throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
                logger.info("Failed to load new catalog version: " + errors.toString());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
            }
            for (StandaloneCatalog c : versionedCatalog.getVersions()) {
                if (c.getEffectiveDate().compareTo(newCatalogVersion.getEffectiveDate()) == 0) {
                    final ValidationErrors errors = new ValidationErrors();
                    errors.add(String.format("Catalog version for effectiveDate '%s' already exists", newCatalogVersion.getEffectiveDate()), new URI("dummy"), StandaloneCatalog.class, "");
                    // Bummer ValidationException CTOR is private to package...
                    //final ValidationException validationException = new ValidationException(errors);
                    //throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
                    logger.info("Failed to load new catalog version: " + errors.toString());
                    throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
                }
            }
        }
        catalogCache.clearCatalog(internalTenantContext);
        tenantApi.addTenantKeyValue(TenantKey.CATALOG.toString(), catalogXML, callContext);
    } catch (final TenantApiException e) {
        throw new CatalogApiException(e);
    } catch (final ValidationException e) {
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
    } catch (final JAXBException e) {
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } catch (final TransformerException e) {
        throw new IllegalStateException(e);
    } catch (final URISyntaxException e) {
        throw new IllegalStateException(e);
    } catch (final SAXException e) {
        throw new IllegalStateException(e);
    } catch (final InvalidConfigException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ValidationException(org.killbill.xmlloader.ValidationException) ValidationErrors(org.killbill.xmlloader.ValidationErrors) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvalidConfigException(org.killbill.billing.catalog.api.InvalidConfigException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) URI(java.net.URI) SAXException(org.xml.sax.SAXException) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) ByteArrayInputStream(java.io.ByteArrayInputStream) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) TransformerException(javax.xml.transform.TransformerException)

Example 12 with StandaloneCatalog

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

the class DefaultCatalogUserApi method addSimplePlan.

@Override
public void addSimplePlan(final SimplePlanDescriptor descriptor, final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException {
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
        final StandaloneCatalog currentCatalog = getCurrentStandaloneCatalogForTenant(internalTenantContext);
        final CatalogUpdater catalogUpdater = (currentCatalog != null) ? new CatalogUpdater(currentCatalog) : new CatalogUpdater(BillingMode.IN_ADVANCE, effectiveDate, descriptor.getCurrency());
        catalogUpdater.addSimplePlanDescriptor(descriptor);
        catalogCache.clearCatalog(internalTenantContext);
        tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(), callContext);
    } catch (TenantApiException e) {
        throw new CatalogApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) CatalogUpdater(org.killbill.billing.catalog.CatalogUpdater)

Example 13 with StandaloneCatalog

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

the class EhCacheCatalogCache method getCatalog.

@Override
public VersionedCatalog getCatalog(final boolean useDefaultCatalog, final boolean filterTemplateCatalog, final InternalTenantContext tenantContext) throws CatalogApiException {
    // STEPH TODO what are the possibilities for caching here ?
    final VersionedCatalog pluginVersionedCatalog = getCatalogFromPlugins(tenantContext);
    if (pluginVersionedCatalog != null) {
        return pluginVersionedCatalog;
    }
    if (tenantContext.getTenantRecordId() == InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID) {
        return useDefaultCatalog ? defaultCatalog : null;
    }
    // but to be on the safe side;;
    try {
        VersionedCatalog tenantCatalog = (VersionedCatalog) cacheController.get(tenantContext.getTenantRecordId(), filterTemplateCatalog ? cacheLoaderArgumentWithTemplateFiltering : cacheLoaderArgument);
        // for test purpose.
        if (useDefaultCatalog && tenantCatalog == null) {
            tenantCatalog = new VersionedCatalog(defaultCatalog.getClock());
            for (final StandaloneCatalog cur : defaultCatalog.getVersions()) {
                final StandaloneCatalogWithPriceOverride curWithOverride = new StandaloneCatalogWithPriceOverride(cur, priceOverride, tenantContext.getTenantRecordId(), internalCallContextFactory);
                tenantCatalog.add(curWithOverride);
            }
            cacheController.add(tenantContext.getTenantRecordId(), tenantCatalog);
        }
        return tenantCatalog;
    } catch (final IllegalStateException e) {
        throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, tenantContext.getTenantRecordId());
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PriceOverride(org.killbill.billing.catalog.override.PriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)

Example 14 with StandaloneCatalog

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

the class TestEhCacheCatalogCache method testDefaultCatalog.

//
// Verify CatalogCache returns default catalog when system property has been set (and CatalogCache has been initialized)
//
@Test(groups = "fast")
public void testDefaultCatalog() throws CatalogApiException {
    catalogCache.loadDefaultCatalog(Resources.getResource("SpyCarBasic.xml").toExternalForm());
    final VersionedCatalog result = catalogCache.getCatalog(true, true, internalCallContext);
    Assert.assertNotNull(result);
    final Collection<Product> products = result.getProducts(clock.getUTCNow());
    Assert.assertEquals(products.size(), 3);
    // Verify the lookup with other contexts
    final VersionedCatalog resultForMultiTenantContext = new VersionedCatalog(result.getClock());
    for (final StandaloneCatalog cur : result.getVersions()) {
        resultForMultiTenantContext.add(new StandaloneCatalogWithPriceOverride(cur, priceOverride, multiTenantContext.getTenantRecordId(), internalCallContextFactory));
    }
    Assert.assertEquals(catalogCache.getCatalog(true, true, multiTenantContext).getCatalogName(), resultForMultiTenantContext.getCatalogName());
    Assert.assertEquals(catalogCache.getCatalog(true, true, multiTenantContext).getVersions().size(), resultForMultiTenantContext.getVersions().size());
    for (int i = 0; i < catalogCache.getCatalog(true, true, multiTenantContext).getVersions().size(); i++) {
        Assert.assertEquals(((StandaloneCatalogWithPriceOverride) catalogCache.getCatalog(true, true, multiTenantContext).getVersions().get(i)).getTenantRecordId(), ((StandaloneCatalogWithPriceOverride) resultForMultiTenantContext.getVersions().get(i)).getTenantRecordId());
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) DefaultProduct(org.killbill.billing.catalog.DefaultProduct) Product(org.killbill.billing.catalog.api.Product) Test(org.testng.annotations.Test)

Example 15 with StandaloneCatalog

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

the class TestCatalogOverrideDao method testGetOverriddenPlanPhases.

@Test(groups = "slow")
public void testGetOverriddenPlanPhases() throws Exception {
    final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class);
    final Plan plan = catalog.findCurrentPlan("discount-standard-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[0].getName(), Currency.USD, BigDecimal.TEN, BigDecimal.ONE);
    resolvedOverrides[1] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[1].getName(), Currency.USD, BigDecimal.ONE, BigDecimal.TEN);
    resolvedOverrides[2] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, BigDecimal.ZERO, new BigDecimal("348.64"));
    final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan.getName(), new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
    final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
    assertEquals(phases.size(), 3);
    for (int i = 0; i < 3; i++) {
        final CatalogOverridePhaseDefinitionModelDao curPhase = phases.get(i);
        assertEquals(curPhase.getCurrency(), resolvedOverrides[i].getCurrency().name());
        assertEquals(curPhase.getFixedPrice().compareTo(resolvedOverrides[i].getFixedPrice()), 0);
        assertEquals(curPhase.getRecurringPrice().compareTo(resolvedOverrides[i].getRecurringPrice()), 0);
        assertEquals(curPhase.getParentPhaseName(), resolvedOverrides[i].getPhaseName());
    }
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Test(org.testng.annotations.Test)

Aggregations

StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)24 Test (org.testng.annotations.Test)15 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)11 BigDecimal (java.math.BigDecimal)8 VersionedCatalog (org.killbill.billing.catalog.VersionedCatalog)8 URI (java.net.URI)6 DateTime (org.joda.time.DateTime)5 StandaloneCatalogWithPriceOverride (org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)5 Usage (org.killbill.billing.catalog.api.Usage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Plan (org.killbill.billing.catalog.api.Plan)4 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 JAXBException (javax.xml.bind.JAXBException)3 TransformerException (javax.xml.transform.TransformerException)3 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)3 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)3 DefaultProduct (org.killbill.billing.catalog.DefaultProduct)3 InvalidConfigException (org.killbill.billing.catalog.api.InvalidConfigException)3 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)3