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, @Nullable 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(getSafeFirstCatalogEffectiveDate(effectiveDate, callContext), descriptor.getCurrency());
catalogUpdater.addSimplePlanDescriptor(descriptor);
tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(internalTenantContext), callContext);
catalogCache.clearCatalog(internalTenantContext);
} catch (TenantApiException e) {
throw new CatalogApiException(e);
}
}
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 {
VersionedCatalog versionedCatalog = catalogService.getFullCatalog(false, true, internalTenantContext);
if (versionedCatalog == null) {
// If this is the first version
versionedCatalog = new DefaultVersionedCatalog();
}
// Validation purpose: Will throw if bad XML or catalog validation fails
final InputStream stream = new ByteArrayInputStream(catalogXML.getBytes());
final StaticCatalog newCatalogVersion = XMLLoader.getObjectFromStream(stream, StandaloneCatalog.class);
final ValidationErrors errors = new ValidationErrors();
// Fix for https://github.com/killbill/killbill/issues/1481
((DefaultVersionedCatalog) versionedCatalog).add((StandaloneCatalog) newCatalogVersion);
((DefaultVersionedCatalog) versionedCatalog).validate(null, errors);
if (!errors.isEmpty()) {
// 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());
}
tenantApi.addTenantKeyValue(TenantKey.CATALOG.toString(), catalogXML, callContext);
catalogCache.clearCatalog(internalTenantContext);
} 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 SAXException e) {
throw new IllegalStateException(e);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class DefaultCatalogCache method getCatalog.
@Override
public VersionedCatalog getCatalog(final boolean useDefaultCatalog, final boolean filterTemplateCatalog, final boolean internalUse, final InternalTenantContext tenantContext) throws CatalogApiException {
//
if (internalUse) {
Preconditions.checkState(tenantContext.getAccountRecordId() != null, "Unexpected null accountRecordId in context issued from internal Kill Bill service");
}
final VersionedCatalog pluginVersionedCatalog = getCatalogFromPlugins(tenantContext);
if (pluginVersionedCatalog != null) {
return pluginVersionedCatalog;
}
if (InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID.equals(tenantContext.getTenantRecordId())) {
return useDefaultCatalog ? defaultCatalog : null;
}
// but to be on the safe side;;
try {
DefaultVersionedCatalog tenantCatalog = cacheController.get(tenantContext.getTenantRecordId(), filterTemplateCatalog ? cacheLoaderArgumentWithTemplateFiltering : cacheLoaderArgument);
// for test purpose.
if (useDefaultCatalog && tenantCatalog == null) {
tenantCatalog = new DefaultVersionedCatalog();
for (final StaticCatalog cur : defaultCatalog.getVersions()) {
final StandaloneCatalogWithPriceOverride curWithOverride = new StandaloneCatalogWithPriceOverride(cur, priceOverride, tenantContext.getTenantRecordId(), internalCallContextFactory);
tenantCatalog.add(curWithOverride);
}
initializeCatalog(tenantCatalog);
cacheController.putIfAbsent(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 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();
if (curOverride != null) {
List<UsagePriceOverride> resolvedUsageOverrides = getResolvedUsageOverrides(curPhase.getUsages(), curOverride.getUsagePriceOverrides());
resolvedOverride[index++] = new DefaultPlanPhasePriceOverride(curPhase.getName(), curOverride.getCurrency(), curOverride.getFixedPrice(), curOverride.getRecurringPrice(), resolvedUsageOverrides);
} else {
resolvedOverride[index++] = 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, 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);
if (context == null) {
overriddenPlanCache.addDryRunPlan(planName, result);
}
return result;
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class PriceOverridePattern method getPlanParts.
public String[] getPlanParts(final String planName) throws CatalogApiException {
final Matcher m = pattern.matcher(planName);
if (!m.matches()) {
throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, planName);
}
final String[] res = new String[2];
res[0] = m.group(1);
res[1] = m.group(2);
return res;
}
Aggregations