示例#1
0
  @Override
  public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) {
    // Validation: check for nulls

    if (plan == null) {
      errors.add(
          new ValidationError(
              String.format("Invalid plan for recurring section"),
              catalog.getCatalogURI(),
              DefaultRecurring.class,
              ""));
    }

    if (phase == null) {
      errors.add(
          new ValidationError(
              String.format("Invalid phase for recurring section"),
              catalog.getCatalogURI(),
              DefaultPlan.class,
              plan.getName().toString()));
    }

    if (billingPeriod == null) {
      errors.add(
          new ValidationError(
              String.format(
                  "Recurring section of Phase %s of plan %s has a recurring price but no billing period",
                  phase.getPhaseType().toString(), plan.getName()),
              catalog.getCatalogURI(),
              DefaultPlanPhase.class,
              phase.getPhaseType().toString()));
    }

    // Validation: if there is a recurring price there must be a billing period
    if ((recurringPrice != null)
        && (billingPeriod == null || billingPeriod == BillingPeriod.NO_BILLING_PERIOD)) {
      errors.add(
          new ValidationError(
              String.format(
                  "Recurring section of Phase %s of plan %s has a recurring price but no billing period",
                  phase.getPhaseType().toString(), plan.getName()),
              catalog.getCatalogURI(),
              DefaultPlanPhase.class,
              phase.getPhaseType().toString()));
    }

    // Validation: if there is no recurring price there should be no billing period
    if ((recurringPrice == null) && billingPeriod != BillingPeriod.NO_BILLING_PERIOD) {
      errors.add(
          new ValidationError(
              String.format(
                  "Recurring section of Phase %s of plan %s has no recurring price but does have a billing period. The billing period should be set to '%s'",
                  phase.getPhaseType().toString(), plan.getName(), BillingPeriod.NO_BILLING_PERIOD),
              catalog.getCatalogURI(),
              DefaultPlanPhase.class,
              phase.getPhaseType().toString()));
    }
    return errors;
  }
  private synchronized DefaultPrice[] getZeroPrice(StandaloneCatalog root) {
    Currency[] currencies = root.getCurrentSupportedCurrencies();
    DefaultPrice[] zeroPrice = new DefaultPrice[currencies.length];
    for (int i = 0; i < currencies.length; i++) {
      zeroPrice[i] = new DefaultPrice();
      zeroPrice[i].setCurrency(currencies[i]);
      zeroPrice[i].setValue(new BigDecimal(0));
    }

    return zeroPrice;
  }
 @Override
 public ValidationErrors validate(StandaloneCatalog catalog, ValidationErrors errors) {
   Currency[] supportedCurrencies = catalog.getCurrentSupportedCurrencies();
   for (Price p : prices) {
     Currency currency = p.getCurrency();
     if (!currencyIsSupported(currency, supportedCurrencies)) {
       errors.add(
           "Unsupported currency: " + currency, catalog.getCatalogURI(), this.getClass(), "");
     }
     try {
       if (p.getValue().doubleValue() < 0.0) {
         errors.add(
             "Negative value for price in currency: " + currency,
             catalog.getCatalogURI(),
             this.getClass(),
             "");
       }
     } catch (CurrencyValueNull e) {
       // No currency => nothing to check, ignore exception
     }
   }
   return errors;
 }