@Override public DefaultPlan findCurrentPlan(final String name) throws CatalogApiException { if (name == null || plans == null) { throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, name); } final DefaultPlan result = (DefaultPlan) plans.findByName(name); if (result != null) { return result; } throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, name); }
@Override public Product findCurrentProduct(final String name) throws CatalogApiException { if (name == null || products == null) { throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PRODUCT, name); } final Product result = products.findByName(name); if (result != null) { return result; } throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PRODUCT, name); }
@Override public PriceList findCurrentPricelist(final String name) throws CatalogApiException { if (name == null || priceLists == null) { throw new CatalogApiException(ErrorCode.CAT_PRICE_LIST_NOT_FOUND, name); } return priceLists.findPriceListFrom(name); }
@Override public Double getMinTopUpCredit() throws CatalogApiException { if (!CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE.equals(minTopUpCredit) && type != BlockType.TOP_UP) { throw new CatalogApiException(ErrorCode.CAT_NOT_TOP_UP_BLOCK, phase.getName()); } return minTopUpCredit; }
public DefaultPriceList findPriceListFrom(final String priceListName) throws CatalogApiException { if (priceListName == null) { throw new CatalogApiException(ErrorCode.CAT_NULL_PRICE_LIST_NAME); } if (defaultPricelist.getName().equals(priceListName)) { return defaultPricelist; } for (final DefaultPriceList pl : childPriceLists) { if (pl.getName().equals(priceListName)) { return pl; } } throw new CatalogApiException(ErrorCode.CAT_PRICE_LIST_NOT_FOUND, priceListName); }
private int indexOfVersionForDate(final Date date) throws CatalogApiException { for (int i = versions.size() - 1; i >= 0; i--) { final StandaloneCatalog c = versions.get(i); if (c.getEffectiveDate().getTime() <= date.getTime()) { return i; } } // If the only version we have are after the input date, we return the first version // This is not strictly correct from an api point of view, but there is no real good use case // where the system would ask for the catalog for a date prior any catalog was uploaded and // yet time manipulation could end of inn that state -- see https://github.com/killbill/killbill/issues/760 if (!versions.isEmpty()) { return 0; } throw new CatalogApiException(ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE, date.toString()); }
public static String planName(final String phaseName) throws CatalogApiException { for (final PhaseType type : PhaseType.values()) { if (phaseName.endsWith(type.toString().toLowerCase())) { return phaseName.substring(0, phaseName.length() - type.toString().length() - 1); } } throw new CatalogApiException(ErrorCode.CAT_BAD_PHASE_NAME, phaseName); }
@Override public PlanPhase findPhase(final String name) throws CatalogApiException { for (final PlanPhase pp : getAllPhases()) { if (pp.getName().equals(name)) { return pp; } } throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PHASE, name); }
@Override public BigDecimal getPrice(final Currency currency) throws CatalogApiException { if (prices.length == 0) { return BigDecimal.ZERO; } for (final Price p : prices) { if (p.getCurrency() == currency) { return p.getValue(); } } throw new CatalogApiException(ErrorCode.CAT_NO_PRICE_FOR_CURRENCY, currency); }
private DefaultPlan loadOverriddenPlan(final String planName, final StandaloneCatalog 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 = catalog.findCurrentPlan(parentPlanName); final PlanPhasePriceOverride[] overrides = createOverrides(defaultPlan, phaseDefs, context); final DefaultPlan result = new DefaultPlan(catalog, planName, defaultPlan, overrides); result.initialize(catalog); return result; }
public Plan getPlanFrom(final Product product, final BillingPeriod period, final String priceListName) throws CatalogApiException { Collection<Plan> plans = null; final DefaultPriceList pl = findPriceListFrom(priceListName); if (pl != null) { plans = pl.findPlans(product, period); } if (plans.size() == 0) { plans = defaultPricelist.findPlans(product, period); } switch (plans.size()) { case 0: return null; case 1: return plans.iterator().next(); default: throw new CatalogApiException(ErrorCode.CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST, priceListName, product.getName(), period); } }
@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); }
} catch (final ValidationException e) { logger.warn("Failed to load default catalog", e); throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString); } catch (final JAXBException e) { logger.warn("Failed to load default catalog", e); throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString); } catch (IllegalArgumentException e) { logger.warn("Failed to load default catalog", e); throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString); } catch (Exception e) { logger.warn("Failed to load default catalog", e);
@Override public DateTime addToDateTime(final DateTime dateTime) throws CatalogApiException { if ((number == null) && (unit != TimeUnit.UNLIMITED)) { return dateTime; } switch (unit) { case DAYS: return dateTime.plusDays(number); case WEEKS: return dateTime.plusWeeks(number); case MONTHS: return dateTime.plusMonths(number); case YEARS: return dateTime.plusYears(number); case UNLIMITED: default: throw new CatalogApiException(ErrorCode.CAT_UNDEFINED_DURATION, unit); } }
private void validateNewPlanDescriptor(final SimplePlanDescriptor desc) throws CatalogApiException { final boolean invalidPlan = desc.getPlanId() == null && (desc.getProductCategory() == null || desc.getBillingPeriod() == null); final boolean invalidPrice = (desc.getAmount() == null || desc.getAmount().compareTo(BigDecimal.ZERO) < 0) || desc.getCurrency() == null; if (invalidPlan || invalidPrice) { throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc); } if (desc.getProductCategory() == ProductCategory.ADD_ON) { if (desc.getAvailableBaseProducts() == null || desc.getAvailableBaseProducts().isEmpty()) { throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc); } for (final String cur : desc.getAvailableBaseProducts()) { if (getExistingProduct(cur) == null) { throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc); } } } }
@Override public LocalDate addToLocalDate(final LocalDate localDate) throws CatalogApiException { if ((number == null) && (unit != TimeUnit.UNLIMITED)) { return localDate; } switch (unit) { case DAYS: return localDate.plusDays(number); case WEEKS: return localDate.plusWeeks(number); case MONTHS: return localDate.plusMonths(number); case YEARS: return localDate.plusYears(number); case UNLIMITED: default: throw new CatalogApiException(ErrorCode.CAT_UNDEFINED_DURATION, unit); } }
@Override public void deleteCatalog(final CallContext callContext) throws CatalogApiException { final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext); try { tenantApi.deleteTenantKey(TenantKey.CATALOG.toString(), callContext); catalogCache.clearCatalog(internalTenantContext); createDefaultEmptyCatalog(callContext.getCreatedDate(), callContext); } catch (final TenantApiException e) { throw new CatalogApiException(e); } }
@Override public void createDefaultEmptyCatalog(@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), null); tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(), callContext); catalogCache.clearCatalog(internalTenantContext); } catch (TenantApiException e) { throw new CatalogApiException(e); } }
@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(), callContext); catalogCache.clearCatalog(internalTenantContext); } catch (TenantApiException e) { throw new CatalogApiException(e); } }
@Override public Plan createOrFindCurrentPlan(final PlanSpecifier spec, final PlanPhasePriceOverridesWithCallContext unused) throws CatalogApiException { final Plan result; if (spec.getPlanName() != null) { result = findCurrentPlan(spec.getPlanName()); } else { if (spec.getProductName() == null) { throw new CatalogApiException(ErrorCode.CAT_NULL_PRODUCT_NAME); } if (spec.getBillingPeriod() == null) { throw new CatalogApiException(ErrorCode.CAT_NULL_BILLING_PERIOD); } final String inputOrDefaultPricelist = (spec.getPriceListName() == null) ? PriceListSet.DEFAULT_PRICELIST_NAME : spec.getPriceListName(); final Product product = findCurrentProduct(spec.getProductName()); result = priceLists.getPlanFrom(product, spec.getBillingPeriod(), inputOrDefaultPricelist); } if (result == null) { 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"); } return result; }