From 12abc17a8807e658a8b29e94d62d3599fd4527e9 Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Wed, 17 Jul 2013 14:11:52 +0100 Subject: [PATCH] tidy up validation of savings accounts. --- .../SavingsCompoundingInterestPeriodType.java | 4 +- ...ingsInterestCalculationDaysInYearType.java | 14 +++++++ .../SavingsInterestCalculationType.java | 14 +++++++ .../SavingsPostingInterestPeriodType.java | 14 +++++++ .../savings/SavingsWithdrawalFeesType.java | 14 +++++++ .../data/SavingsAccountDataValidator.java | 37 ++++++++++++++----- .../data/SavingsProductDataValidator.java | 13 +++++-- 7 files changed, 95 insertions(+), 15 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsCompoundingInterestPeriodType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsCompoundingInterestPeriodType.java index 42843f5a8..5bfafea3a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsCompoundingInterestPeriodType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsCompoundingInterestPeriodType.java @@ -46,7 +46,9 @@ public enum SavingsCompoundingInterestPeriodType { public static Object[] integerValues() { List values = new ArrayList(); for (SavingsCompoundingInterestPeriodType enumType : values()) { - values.add(enumType.getValue()); + if (enumType.getValue() > 0) { + values.add(enumType.getValue()); + } } return values.toArray(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationDaysInYearType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationDaysInYearType.java index 7e54f07bb..23ba5a0c2 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationDaysInYearType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationDaysInYearType.java @@ -5,6 +5,9 @@ */ package org.mifosplatform.portfolio.savings; +import java.util.ArrayList; +import java.util.List; + /** *
    * People typically use either of the following settings when calculating there @@ -35,6 +38,17 @@ public enum SavingsInterestCalculationDaysInYearType { return code; } + public static Object[] integerValues() { + List values = new ArrayList(); + for (SavingsInterestCalculationDaysInYearType enumType : values()) { + if (enumType.getValue() > 0) { + values.add(enumType.getValue()); + } + } + + return values.toArray(); + } + public static SavingsInterestCalculationDaysInYearType fromInt(final Integer type) { SavingsInterestCalculationDaysInYearType repaymentFrequencyType = SavingsInterestCalculationDaysInYearType.INVALID; if (type != null) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationType.java index 20e337784..03d810f05 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsInterestCalculationType.java @@ -5,6 +5,9 @@ */ package org.mifosplatform.portfolio.savings; +import java.util.ArrayList; +import java.util.List; + /** *
      * There are two methods to calculate the interest on a savings account: @@ -49,6 +52,17 @@ public enum SavingsInterestCalculationType { return code; } + public static Object[] integerValues() { + List values = new ArrayList(); + for (SavingsInterestCalculationType enumType : values()) { + if (enumType.getValue() > 0) { + values.add(enumType.getValue()); + } + } + + return values.toArray(); + } + public static SavingsInterestCalculationType fromInt(final Integer type) { SavingsInterestCalculationType repaymentFrequencyType = SavingsInterestCalculationType.INVALID; if (type != null) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsPostingInterestPeriodType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsPostingInterestPeriodType.java index fc48d091c..e0f8a8ad6 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsPostingInterestPeriodType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsPostingInterestPeriodType.java @@ -5,6 +5,9 @@ */ package org.mifosplatform.portfolio.savings; +import java.util.ArrayList; +import java.util.List; + /** * The interest posting period is the span of time at the end of which savings * earned but not yet credited/posted in a client's account is credited/posted. @@ -32,6 +35,17 @@ public enum SavingsPostingInterestPeriodType { return code; } + public static Object[] integerValues() { + List values = new ArrayList(); + for (SavingsPostingInterestPeriodType enumType : values()) { + if (enumType.getValue() > 0) { + values.add(enumType.getValue()); + } + } + + return values.toArray(); + } + public static SavingsPostingInterestPeriodType fromInt(final Integer type) { SavingsPostingInterestPeriodType repaymentFrequencyType = SavingsPostingInterestPeriodType.INVALID; if (type != null) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsWithdrawalFeesType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsWithdrawalFeesType.java index 1a585f2a3..43b3ded1d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsWithdrawalFeesType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/SavingsWithdrawalFeesType.java @@ -5,6 +5,9 @@ */ package org.mifosplatform.portfolio.savings; +import java.util.ArrayList; +import java.util.List; + public enum SavingsWithdrawalFeesType { INVALID(0, "savingsWithdrawalFeesType.invalid"), // @@ -27,6 +30,17 @@ public enum SavingsWithdrawalFeesType { return code; } + public static Object[] integerValues() { + List values = new ArrayList(); + for (SavingsWithdrawalFeesType enumType : values()) { + if (enumType.getValue() > 0) { + values.add(enumType.getValue()); + } + } + + return values.toArray(); + } + public static SavingsWithdrawalFeesType fromInt(final Integer type) { SavingsWithdrawalFeesType withdrawalFeeType = SavingsWithdrawalFeesType.INVALID; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsAccountDataValidator.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsAccountDataValidator.java index 45f5842d1..ca1ac947c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsAccountDataValidator.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsAccountDataValidator.java @@ -10,6 +10,7 @@ import static org.mifosplatform.portfolio.savings.SavingsApiConstants.annualFeeA import static org.mifosplatform.portfolio.savings.SavingsApiConstants.annualFeeOnMonthDayParamName; import static org.mifosplatform.portfolio.savings.SavingsApiConstants.clientIdParamName; import static org.mifosplatform.portfolio.savings.SavingsApiConstants.externalIdParamName; +import static org.mifosplatform.portfolio.savings.SavingsApiConstants.fieldOfficerIdParamName; import static org.mifosplatform.portfolio.savings.SavingsApiConstants.groupIdParamName; import static org.mifosplatform.portfolio.savings.SavingsApiConstants.interestCalculationDaysInYearTypeParamName; import static org.mifosplatform.portfolio.savings.SavingsApiConstants.interestCalculationTypeParamName; @@ -39,6 +40,11 @@ import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; import org.mifosplatform.portfolio.savings.SavingsApiConstants; +import org.mifosplatform.portfolio.savings.SavingsCompoundingInterestPeriodType; +import org.mifosplatform.portfolio.savings.SavingsInterestCalculationDaysInYearType; +import org.mifosplatform.portfolio.savings.SavingsInterestCalculationType; +import org.mifosplatform.portfolio.savings.SavingsPostingInterestPeriodType; +import org.mifosplatform.portfolio.savings.SavingsWithdrawalFeesType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -84,6 +90,11 @@ public class SavingsAccountDataValidator { final Long productId = fromApiJsonHelper.extractLongNamed(productIdParamName, element); baseDataValidator.reset().parameter(productIdParamName).value(productId).notNull().integerGreaterThanZero(); + if (fromApiJsonHelper.parameterExists(fieldOfficerIdParamName, element)) { + final Long fieldOfficerId = fromApiJsonHelper.extractLongNamed(fieldOfficerIdParamName, element); + baseDataValidator.reset().parameter(fieldOfficerIdParamName).value(fieldOfficerId).notNull().integerGreaterThanZero(); + } + final LocalDate submittedOnDate = fromApiJsonHelper.extractLocalDateNamed(submittedOnDateParamName, element); baseDataValidator.reset().parameter(submittedOnDateParamName).value(submittedOnDate).notNull(); @@ -106,28 +117,28 @@ public class SavingsAccountDataValidator { final Integer interestCompoundingPeriodType = fromApiJsonHelper.extractIntegerSansLocaleNamed( interestCompoundingPeriodTypeParamName, element); baseDataValidator.reset().parameter(interestCompoundingPeriodTypeParamName).value(interestCompoundingPeriodType).notNull() - .isOneOfTheseValues(new Object[] { 1, 2, 3, 4, 5, 6, 7, 8 }); + .isOneOfTheseValues(SavingsCompoundingInterestPeriodType.integerValues()); } if (fromApiJsonHelper.parameterExists(interestPostingPeriodTypeParamName, element)) { final Integer interestPostingPeriodType = fromApiJsonHelper.extractIntegerSansLocaleNamed(interestPostingPeriodTypeParamName, element); baseDataValidator.reset().parameter(interestPostingPeriodTypeParamName).value(interestPostingPeriodType).notNull() - .isOneOfTheseValues(new Object[] { 4, 5, 6, 7 }); + .isOneOfTheseValues(SavingsPostingInterestPeriodType.integerValues()); } if (fromApiJsonHelper.parameterExists(interestCalculationTypeParamName, element)) { final Integer interestCalculationType = fromApiJsonHelper.extractIntegerSansLocaleNamed(interestCalculationTypeParamName, element); baseDataValidator.reset().parameter(interestCalculationTypeParamName).value(interestCalculationType).notNull() - .isOneOfTheseValues(new Object[] { 1, 2 }); + .isOneOfTheseValues(SavingsInterestCalculationType.integerValues()); } if (fromApiJsonHelper.parameterExists(interestCalculationDaysInYearTypeParamName, element)) { final Integer interestCalculationDaysInYearType = fromApiJsonHelper.extractIntegerSansLocaleNamed( interestCalculationDaysInYearTypeParamName, element); baseDataValidator.reset().parameter(interestCalculationDaysInYearTypeParamName).value(interestCalculationDaysInYearType) - .notNull().isOneOfTheseValues(360, 365); + .notNull().isOneOfTheseValues(SavingsInterestCalculationDaysInYearType.integerValues()); } if (this.fromApiJsonHelper.parameterExists(minRequiredOpeningBalanceParamName, element)) { @@ -170,7 +181,8 @@ public class SavingsAccountDataValidator { if (withdrawalFeeAmount != null) { final Integer withdrawalFeeType = fromApiJsonHelper.extractIntegerSansLocaleNamed(withdrawalFeeTypeParamName, element); - baseDataValidator.reset().parameter(withdrawalFeeTypeParamName).value(withdrawalFeeType).isOneOfTheseValues(1, 2); + baseDataValidator.reset().parameter(withdrawalFeeTypeParamName).value(withdrawalFeeType) + .isOneOfTheseValues(SavingsWithdrawalFeesType.integerValues()); } } @@ -262,6 +274,11 @@ public class SavingsAccountDataValidator { baseDataValidator.reset().parameter(productIdParamName).value(productId).notNull().integerGreaterThanZero(); } + if (fromApiJsonHelper.parameterExists(fieldOfficerIdParamName, element)) { + final Long fieldOfficerId = fromApiJsonHelper.extractLongNamed(fieldOfficerIdParamName, element); + baseDataValidator.reset().parameter(fieldOfficerIdParamName).value(fieldOfficerId).notNull().integerGreaterThanZero(); + } + if (fromApiJsonHelper.parameterExists(submittedOnDateParamName, element)) { final LocalDate submittedOnDate = fromApiJsonHelper.extractLocalDateNamed(submittedOnDateParamName, element); baseDataValidator.reset().parameter(submittedOnDateParamName).value(submittedOnDate).notNull(); @@ -286,28 +303,28 @@ public class SavingsAccountDataValidator { final Integer interestCompoundingPeriodType = fromApiJsonHelper.extractIntegerSansLocaleNamed( interestCompoundingPeriodTypeParamName, element); baseDataValidator.reset().parameter(interestCompoundingPeriodTypeParamName).value(interestCompoundingPeriodType).notNull() - .isOneOfTheseValues(new Object[] { 1, 2, 3, 4, 5, 6, 7, 8 }); + .isOneOfTheseValues(SavingsCompoundingInterestPeriodType.integerValues()); } if (this.fromApiJsonHelper.parameterExists(interestPostingPeriodTypeParamName, element)) { final Integer interestPostingPeriodType = fromApiJsonHelper.extractIntegerSansLocaleNamed(interestPostingPeriodTypeParamName, element); baseDataValidator.reset().parameter(interestPostingPeriodTypeParamName).value(interestPostingPeriodType).notNull() - .isOneOfTheseValues(new Object[] { 4, 5, 6, 7 }); + .isOneOfTheseValues(SavingsPostingInterestPeriodType.integerValues()); } if (this.fromApiJsonHelper.parameterExists(interestCalculationTypeParamName, element)) { final Integer interestCalculationType = fromApiJsonHelper.extractIntegerSansLocaleNamed(interestCalculationTypeParamName, element); baseDataValidator.reset().parameter(interestCalculationTypeParamName).value(interestCalculationType).notNull() - .isOneOfTheseValues(new Object[] { 1, 2 }); + .isOneOfTheseValues(SavingsInterestCalculationType.integerValues()); } if (this.fromApiJsonHelper.parameterExists(interestCalculationDaysInYearTypeParamName, element)) { final Integer interestCalculationDaysInYearType = fromApiJsonHelper.extractIntegerSansLocaleNamed( interestCalculationDaysInYearTypeParamName, element); baseDataValidator.reset().parameter(interestCalculationDaysInYearTypeParamName).value(interestCalculationDaysInYearType) - .notNull().isOneOfTheseValues(360, 365); + .notNull().isOneOfTheseValues(SavingsInterestCalculationDaysInYearType.integerValues()); } if (this.fromApiJsonHelper.parameterExists(minRequiredOpeningBalanceParamName, element)) { @@ -339,7 +356,7 @@ public class SavingsAccountDataValidator { if (this.fromApiJsonHelper.parameterExists(withdrawalFeeTypeParamName, element)) { final Integer withdrawalFeeType = fromApiJsonHelper.extractIntegerSansLocaleNamed(withdrawalFeeTypeParamName, element); baseDataValidator.reset().parameter(withdrawalFeeTypeParamName).value(withdrawalFeeType).ignoreIfNull() - .isOneOfTheseValues(1, 2); + .isOneOfTheseValues(SavingsWithdrawalFeesType.integerValues()); } if (this.fromApiJsonHelper.parameterExists(annualFeeAmountParamName, element)) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsProductDataValidator.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsProductDataValidator.java index 586722a67..f900d1474 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsProductDataValidator.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/data/SavingsProductDataValidator.java @@ -41,6 +41,10 @@ import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; import org.mifosplatform.portfolio.savings.SavingsCompoundingInterestPeriodType; +import org.mifosplatform.portfolio.savings.SavingsInterestCalculationDaysInYearType; +import org.mifosplatform.portfolio.savings.SavingsInterestCalculationType; +import org.mifosplatform.portfolio.savings.SavingsPostingInterestPeriodType; +import org.mifosplatform.portfolio.savings.SavingsWithdrawalFeesType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -96,16 +100,16 @@ public class SavingsProductDataValidator { final Integer interestPostingPeriodType = fromApiJsonHelper.extractIntegerSansLocaleNamed(interestPostingPeriodTypeParamName, element); baseDataValidator.reset().parameter(interestPostingPeriodTypeParamName).value(interestPostingPeriodType).notNull() - .isOneOfTheseValues(new Object[] { 4, 5, 6, 7 }); + .isOneOfTheseValues(SavingsPostingInterestPeriodType.integerValues()); final Integer interestCalculationType = fromApiJsonHelper.extractIntegerSansLocaleNamed(interestCalculationTypeParamName, element); baseDataValidator.reset().parameter(interestCalculationTypeParamName).value(interestCalculationType).notNull() - .isOneOfTheseValues(new Object[] { 1, 2 }); + .isOneOfTheseValues(SavingsInterestCalculationType.integerValues()); final Integer interestCalculationDaysInYearType = fromApiJsonHelper.extractIntegerSansLocaleNamed( interestCalculationDaysInYearTypeParamName, element); baseDataValidator.reset().parameter(interestCalculationDaysInYearTypeParamName).value(interestCalculationDaysInYearType).notNull() - .isOneOfTheseValues(360, 365); + .isOneOfTheseValues(SavingsInterestCalculationDaysInYearType.integerValues()); if (this.fromApiJsonHelper.parameterExists(minRequiredOpeningBalanceParamName, element)) { final BigDecimal minOpeningBalance = fromApiJsonHelper.extractBigDecimalWithLocaleNamed(minRequiredOpeningBalanceParamName, @@ -147,7 +151,8 @@ public class SavingsProductDataValidator { if (withdrawalFeeAmount != null) { final Integer withdrawalFeeType = fromApiJsonHelper.extractIntegerSansLocaleNamed(withdrawalFeeTypeParamName, element); - baseDataValidator.reset().parameter(withdrawalFeeTypeParamName).value(withdrawalFeeType).isOneOfTheseValues(1, 2); + baseDataValidator.reset().parameter(withdrawalFeeTypeParamName).value(withdrawalFeeType) + .isOneOfTheseValues(SavingsWithdrawalFeesType.integerValues()); } }