Search in sources :

Example 1 with CatalogOverridePlanDefinitionModelDao

use of org.killbill.billing.catalog.dao.CatalogOverridePlanDefinitionModelDao 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();
        resolvedOverride[index++] = curOverride != null ? new DefaultPlanPhasePriceOverride(curPhase.getName(), curOverride.getCurrency(), curOverride.getFixedPrice(), curOverride.getRecurringPrice()) : 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.getName(), 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, standaloneCatalog.getCatalogURI());
    if (context == null) {
        overriddenPlanCache.addDryRunPlan(planName, result);
    }
    return result;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) Predicate(com.google.common.base.Predicate) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) CatalogOverridePlanDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePlanDefinitionModelDao) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)

Aggregations

Predicate (com.google.common.base.Predicate)1 DefaultPlan (org.killbill.billing.catalog.DefaultPlan)1 DefaultPlanPhase (org.killbill.billing.catalog.DefaultPlanPhase)1 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)1 StandaloneCatalogWithPriceOverride (org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)1 CatalogOverridePlanDefinitionModelDao (org.killbill.billing.catalog.dao.CatalogOverridePlanDefinitionModelDao)1