Search in sources :

Example 1 with ValidationError

use of org.killbill.xmlloader.ValidationError in project killbill by killbill.

the class DefaultPlanPhase method validate.

@Override
public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) {
    if (planName == null) {
        errors.add(new ValidationError(String.format("Invalid plan for phase '%s'", type), DefaultPlanPhase.class, ""));
    }
    if (fixed == null && recurring == null && usages.length == 0) {
        errors.add(new ValidationError(String.format("Phase %s of plan %s need to define at least either a fixed or recurrring or usage section.", type.toString(), planName), DefaultPlanPhase.class, type.toString()));
    }
    if (fixed != null) {
        fixed.validate(catalog, errors);
    }
    if (recurring != null) {
        recurring.validate(catalog, errors);
    }
    duration.validate(catalog, errors);
    validateCollection(catalog, errors, usages);
    return errors;
}
Also used : ValidationError(org.killbill.xmlloader.ValidationError) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride)

Example 2 with ValidationError

use of org.killbill.xmlloader.ValidationError in project killbill by killbill.

the class DefaultPlanRules method validate.

@Override
public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) {
    // 
    // Validate that there is a default policy for change AND cancel rules and check unicity of rules
    // 
    final HashSet<DefaultCaseChangePlanPolicy> caseChangePlanPoliciesSet = new HashSet<DefaultCaseChangePlanPolicy>();
    boolean foundDefaultCase = false;
    for (final DefaultCaseChangePlanPolicy cur : changeCase) {
        if (caseChangePlanPoliciesSet.contains(cur)) {
            errors.add(new ValidationError(String.format("Duplicate rule for change plan %s", cur.toString()), DefaultPlanRules.class, ""));
        } else {
            caseChangePlanPoliciesSet.add(cur);
        }
        if (cur.getPhaseType() == null && cur.getFromProduct() == null && cur.getFromProductCategory() == null && cur.getFromBillingPeriod() == null && cur.getFromPriceList() == null && cur.getToProduct() == null && cur.getToProductCategory() == null && cur.getToBillingPeriod() == null && cur.getToPriceList() == null) {
            foundDefaultCase = true;
        }
        cur.validate(catalog, errors);
    }
    if (!foundDefaultCase) {
        errors.add(new ValidationError("Missing default rule case for plan change", DefaultPlanRules.class, ""));
    }
    final HashSet<DefaultCaseCancelPolicy> defaultCaseCancelPoliciesSet = new HashSet<DefaultCaseCancelPolicy>();
    foundDefaultCase = false;
    for (final DefaultCaseCancelPolicy cur : cancelCase) {
        if (defaultCaseCancelPoliciesSet.contains(cur)) {
            errors.add(new ValidationError(String.format("Duplicate rule for plan cancellation %s", cur.toString()), DefaultPlanRules.class, ""));
        } else {
            defaultCaseCancelPoliciesSet.add(cur);
        }
        if (cur.getPhaseType() == null && cur.getProduct() == null && cur.getProductCategory() == null && cur.getBillingPeriod() == null && cur.getPriceList() == null) {
            foundDefaultCase = true;
        }
        cur.validate(catalog, errors);
    }
    if (!foundDefaultCase) {
        errors.add(new ValidationError("Missing default rule case for plan cancellation", DefaultPlanRules.class, ""));
    }
    final HashSet<DefaultCaseChangePlanAlignment> caseChangePlanAlignmentsSet = new HashSet<DefaultCaseChangePlanAlignment>();
    for (final DefaultCaseChangePlanAlignment cur : changeAlignmentCase) {
        if (caseChangePlanAlignmentsSet.contains(cur)) {
            errors.add(new ValidationError(String.format("Duplicate rule for plan change alignment %s", cur.toString()), DefaultPlanRules.class, ""));
        } else {
            caseChangePlanAlignmentsSet.add(cur);
        }
        cur.validate(catalog, errors);
    }
    final HashSet<DefaultCaseCreateAlignment> caseCreateAlignmentsSet = new HashSet<DefaultCaseCreateAlignment>();
    for (final DefaultCaseCreateAlignment cur : createAlignmentCase) {
        if (caseCreateAlignmentsSet.contains(cur)) {
            errors.add(new ValidationError(String.format("Duplicate rule for create plan alignment %s", cur.toString()), DefaultPlanRules.class, ""));
        } else {
            caseCreateAlignmentsSet.add(cur);
        }
        cur.validate(catalog, errors);
    }
    final HashSet<DefaultCaseBillingAlignment> caseBillingAlignmentsSet = new HashSet<DefaultCaseBillingAlignment>();
    for (final DefaultCaseBillingAlignment cur : billingAlignmentCase) {
        if (caseBillingAlignmentsSet.contains(cur)) {
            errors.add(new ValidationError(String.format("Duplicate rule for billing alignment %s", cur.toString()), DefaultPlanRules.class, ""));
        } else {
            caseBillingAlignmentsSet.add(cur);
        }
        cur.validate(catalog, errors);
    }
    final HashSet<DefaultCasePriceList> casePriceListsSet = new HashSet<DefaultCasePriceList>();
    for (final DefaultCasePriceList cur : priceListCase) {
        if (casePriceListsSet.contains(cur)) {
            errors.add(new ValidationError(String.format("Duplicate rule for price list transition %s", cur.toString()), DefaultPlanRules.class, ""));
        } else {
            casePriceListsSet.add(cur);
        }
        cur.validate(catalog, errors);
    }
    return errors;
}
Also used : ValidationError(org.killbill.xmlloader.ValidationError) HashSet(java.util.HashSet)

Example 3 with ValidationError

use of org.killbill.xmlloader.ValidationError in project killbill by killbill.

the class VersionedCatalogLoader method load.

public VersionedCatalog load(final Collection<String> catalogXMLs, final boolean filterTemplateCatalog, final Long tenantRecordId) throws CatalogApiException {
    try {
        final Collection<Future<StandaloneCatalog>> catalogs = new ArrayList<Future<StandaloneCatalog>>(catalogXMLs.size());
        for (final String cur : catalogXMLs) {
            catalogs.add(executorService.submit(new Callable<StandaloneCatalog>() {

                @Override
                public StandaloneCatalog call() throws Exception {
                    final InputStream curCatalogStream = new ByteArrayInputStream(cur.getBytes());
                    final StandaloneCatalog catalog = XMLLoader.getObjectFromStream(curCatalogStream, StandaloneCatalog.class);
                    if (!filterTemplateCatalog || !catalog.isTemplateCatalog()) {
                        return new StandaloneCatalogWithPriceOverride(catalog, priceOverride, tenantRecordId, internalCallContextFactory);
                    }
                    return null;
                }
            }));
        }
        final DefaultVersionedCatalog result = new DefaultVersionedCatalog();
        for (final Future<StandaloneCatalog> standaloneCatalogFuture : catalogs) {
            final StandaloneCatalog catalog = standaloneCatalogFuture.get();
            if (catalog != null) {
                result.add(catalog);
            }
        }
        XMLLoader.initializeAndValidate(result);
        return result;
    } catch (final ValidationException e) {
        logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
        for (final ValidationError ve : e.getErrors()) {
            logger.warn(ve.toString());
        }
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
    } catch (final InterruptedException e) {
        logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
    } catch (final ExecutionException e) {
        logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) ValidationException(org.killbill.xmlloader.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) Callable(java.util.concurrent.Callable) ByteArrayInputStream(java.io.ByteArrayInputStream) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Future(java.util.concurrent.Future) ValidationError(org.killbill.xmlloader.ValidationError) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ValidationError

use of org.killbill.xmlloader.ValidationError in project killbill by killbill.

the class DefaultVersionedCatalog method validatePlanShape.

private void validatePlanShape(final Plan plan, final Plan targetPlan, final ValidationErrors errors) {
    if (plan.getAllPhases().length != targetPlan.getAllPhases().length) {
        errors.add(new ValidationError(String.format("Number of phases for plan '%s' differs between version '%s' and '%s'", plan.getName(), plan.getCatalog().getEffectiveDate(), targetPlan.getCatalog().getEffectiveDate()), DefaultVersionedCatalog.class, ""));
        // In this case we don't bother checking each phase -- the code below assumes the # are equal
        return;
    }
    for (int i = 0; i < plan.getAllPhases().length; i++) {
        final PlanPhase cur = plan.getAllPhases()[i];
        final PlanPhase target = targetPlan.getAllPhases()[i];
        if (!cur.getName().equals(target.getName())) {
            errors.add(new ValidationError(String.format("Phase '%s'for plan '%s' in version '%s' does not exist in version '%s'", cur.getName(), plan.getName(), plan.getCatalog().getEffectiveDate(), targetPlan.getCatalog().getEffectiveDate()), DefaultVersionedCatalog.class, ""));
        }
    }
}
Also used : PlanPhase(org.killbill.billing.catalog.api.PlanPhase) ValidationError(org.killbill.xmlloader.ValidationError)

Example 5 with ValidationError

use of org.killbill.xmlloader.ValidationError in project killbill by killbill.

the class DefaultVersionedCatalog method validate.

@Override
public ValidationErrors validate(final DefaultVersionedCatalog catalog, final ValidationErrors errors) {
    final Set<Date> effectiveDates = new TreeSet<Date>();
    for (final StaticCatalog c : versions) {
        if (effectiveDates.contains(c.getEffectiveDate())) {
            errors.add(new ValidationError(String.format("Catalog effective date '%s' already exists for a previous version", c.getEffectiveDate()), DefaultVersionedCatalog.class, ""));
        } else {
            effectiveDates.add(c.getEffectiveDate());
        }
        if (!c.getCatalogName().equals(catalogName)) {
            errors.add(new ValidationError(String.format("Catalog name '%s' is not consistent across versions ", c.getCatalogName()), DefaultVersionedCatalog.class, ""));
        }
        errors.addAll(((StandaloneCatalog) c).validate((StandaloneCatalog) c, errors));
    }
    validateUniformPlanShapeAcrossVersions(errors);
    return errors;
}
Also used : TreeSet(java.util.TreeSet) ValidationError(org.killbill.xmlloader.ValidationError) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) Date(java.util.Date)

Aggregations

ValidationError (org.killbill.xmlloader.ValidationError)6 UsagePriceOverride (org.killbill.billing.catalog.api.UsagePriceOverride)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 DefaultVersionedCatalog (org.killbill.billing.catalog.DefaultVersionedCatalog)1 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)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 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)1 TierPriceOverride (org.killbill.billing.catalog.api.TierPriceOverride)1 TieredBlockPriceOverride (org.killbill.billing.catalog.api.TieredBlockPriceOverride)1