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 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 TestVersionedCatalog method testErrorOnDateTooEarly.
@Test(groups = "fast")
public void testErrorOnDateTooEarly() {
final DateTime dt0 = new DateTime("1977-01-01T00:00:00+00:00");
try {
vc.findPlan("foo", dt0);
Assert.fail("Date is too early an exception should have been thrown");
} catch (CatalogApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE.getCode());
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class VersionedCatalogLoader method load.
public VersionedCatalog load(final Iterable<String> catalogXMLs, final boolean filterTemplateCatalog, final Long tenantRecordId) throws CatalogApiException {
final VersionedCatalog result = new VersionedCatalog(clock);
final URI uri;
try {
uri = new URI("/tenantCatalog");
for (final String cur : catalogXMLs) {
final InputStream curCatalogStream = new ByteArrayInputStream(cur.getBytes());
final StandaloneCatalog catalog = XMLLoader.getObjectFromStream(uri, curCatalogStream, StandaloneCatalog.class);
if (!filterTemplateCatalog || !catalog.isTemplateCatalog()) {
result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, tenantRecordId, internalCallContextFactory));
}
}
// Perform initialization and validation for VersionedCatalog
XMLLoader.initializeAndValidate(uri, result);
return result;
} catch (final ValidationException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
} catch (final JAXBException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
} catch (final IOException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final TransformerException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final URISyntaxException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final SAXException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final InvalidConfigException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class StandaloneCatalog method findCurrentPhase.
@Override
public PlanPhase findCurrentPhase(final String name) throws CatalogApiException {
if (name == null || plans == null) {
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PHASE, name);
}
final String planName = DefaultPlanPhase.planName(name);
final Plan plan = findCurrentPlan(planName);
return plan.findPhase(name);
}
Aggregations