use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class VersionedCatalog method findCatalogPlanEntry.
private CatalogPlanEntry findCatalogPlanEntry(final PlanRequestWrapper wrapper, final DateTime requestedDate, final DateTime subscriptionStartDate) throws CatalogApiException {
final List<StandaloneCatalog> catalogs = versionsBeforeDate(requestedDate.toDate());
if (catalogs.isEmpty()) {
throw new CatalogApiException(ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE, requestedDate.toDate().toString());
}
for (int i = catalogs.size() - 1; i >= 0; i--) {
// Working backwards to find the latest applicable plan
final StandaloneCatalog c = catalogs.get(i);
final Plan plan;
try {
plan = wrapper.findPlan(c);
} catch (final CatalogApiException e) {
if (e.getCode() != ErrorCode.CAT_NO_SUCH_PLAN.getCode()) {
throw e;
} else {
// If we can't find an entry it probably means the plan has been retired so we keep looking...
continue;
}
}
final DateTime catalogEffectiveDate = CatalogDateHelper.toUTCDateTime(c.getEffectiveDate());
if (!subscriptionStartDate.isBefore(catalogEffectiveDate)) {
// Its a new subscription this plan always applies
return new CatalogPlanEntry(c, plan);
} else {
//Its an existing subscription
if (plan.getEffectiveDateForExistingSubscriptions() != null) {
//if it is null any change to this does not apply to existing subscriptions
final DateTime existingSubscriptionDate = CatalogDateHelper.toUTCDateTime(plan.getEffectiveDateForExistingSubscriptions());
if (requestedDate.isAfter(existingSubscriptionDate)) {
// this plan is now applicable to existing subs
return new CatalogPlanEntry(c, plan);
}
}
}
}
final PlanSpecifier spec = wrapper.getSpec();
throw new CatalogApiException(ErrorCode.CAT_PLAN_NOT_FOUND, spec.getPlanName() != null ? spec.getPlanName() : "undefined", spec.getProductName() != null ? spec.getProductName() : "undefined", spec.getBillingPeriod() != null ? spec.getBillingPeriod() : "undefined", spec.getPriceListName() != null ? spec.getPriceListName() : "undefined");
}
use of org.killbill.billing.catalog.api.CatalogApiException 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);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException 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);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException 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());
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class TestStandaloneCatalogWithPriceOverride method testCreatePlanNoProduct.
@Test(groups = "slow")
public void testCreatePlanNoProduct() throws Exception {
final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class);
final StaticCatalog standaloneCatalogWithPriceOverride = new StandaloneCatalogWithPriceOverride(catalog, priceOverride, internalCallContext.getTenantRecordId(), internalCallContextFactory);
try {
final PlanSpecifier specWithNullProduct = new PlanSpecifier(null, BillingPeriod.ANNUAL, "DEFAULT");
standaloneCatalogWithPriceOverride.createOrFindCurrentPlan(specWithNullProduct, null);
Assert.fail();
} catch (final CatalogApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.CAT_NULL_PRODUCT_NAME.getCode());
}
}
Aggregations