MIFOSX-748: update api documentation, resolve null pointers when creating charge with empty request.

This commit is contained in:
Keith Woodlock
2013-10-19 21:48:27 +01:00
parent 961e581f63
commit 0f2685c63c
6 changed files with 514 additions and 452 deletions
@@ -5,10 +5,12 @@
*/
package org.mifosplatform.portfolio.charge.domain;
public enum ChargeAppliesTo {
INVALID(0, "chargeAppliesTo.invalid"), //
LOAN(1, "chargeAppliesTo.loan"), SAVINGS(2, "chargeAppliesTo.savings");
LOAN(1, "chargeAppliesTo.loan"), //
SAVINGS(2, "chargeAppliesTo.savings");
private final Integer value;
private final String code;
@@ -26,27 +28,23 @@ public enum ChargeAppliesTo {
return this.code;
}
public static int minValue() {
return 1;
}
public static int maxValue() {
return 2;// modify whenever a new value added
}
public static ChargeAppliesTo fromInt(final Integer chargeAppliesTo) {
ChargeAppliesTo chargeAppliesToType = ChargeAppliesTo.INVALID;
switch (chargeAppliesTo) {
case 1:
chargeAppliesToType = LOAN;
break;
case 2:
chargeAppliesToType = SAVINGS;
break;
default:
chargeAppliesToType = INVALID;
break;
if (chargeAppliesTo != null) {
switch (chargeAppliesTo) {
case 1:
chargeAppliesToType = LOAN;
break;
case 2:
chargeAppliesToType = SAVINGS;
break;
default:
chargeAppliesToType = INVALID;
break;
}
}
return chargeAppliesToType;
}
@@ -57,4 +55,8 @@ public enum ChargeAppliesTo {
public boolean isSavingsCharge() {
return this.value.equals(ChargeAppliesTo.SAVINGS.getValue());
}
public static Object[] validValues() {
return new Object[] {ChargeAppliesTo.LOAN.getValue(), ChargeAppliesTo.SAVINGS.getValue()};
}
}
@@ -29,6 +29,10 @@ public enum ChargeCalculationType {
return this.code;
}
public static Object[] validValues() {
return new Integer[] { ChargeCalculationType.FLAT.getValue(), ChargeCalculationType.PERCENT_OF_AMOUNT.getValue() };
}
public static ChargeCalculationType fromInt(final Integer chargeCalculation) {
ChargeCalculationType chargeCalculationType = ChargeCalculationType.INVALID;
switch (chargeCalculation) {
@@ -21,6 +21,10 @@ public enum ChargePaymentMode {
return this.code;
}
public static Object[] validValues() {
return new Integer[] { ChargePaymentMode.REGULAR.getValue(), ChargePaymentMode.ACCOUNT_TRANSFER.getValue() };
}
public static ChargePaymentMode fromInt(final Integer paymentMode) {
ChargePaymentMode chargeAppliesToType = ChargePaymentMode.REGULAR;
switch (paymentMode) {
@@ -8,9 +8,11 @@ package org.mifosplatform.portfolio.charge.domain;
public enum ChargeTimeType {
INVALID(0, "chargeTimeType.invalid"), //
DISBURSEMENT(1, "chargeTimeType.disbursement"), //only for loan charges
SPECIFIED_DUE_DATE(2, "chargeTimeType.specifiedDueDate"), // for loan and savings charges
SAVINGS_ACTIVATION(3, "chargeTimeType.savingsActivation"), // only for savings
DISBURSEMENT(1, "chargeTimeType.disbursement"), // only for loan charges
SPECIFIED_DUE_DATE(2, "chargeTimeType.specifiedDueDate"), // for loan and
// savings charges
SAVINGS_ACTIVATION(3, "chargeTimeType.savingsActivation"), // only for
// savings
SAVINGS_CLOSURE(4, "chargeTimeType.savingsClosure"), // only for savings
WITHDRAWAL_FEE(5, "chargeTimeType.withdrawalFee"), // only for savings
ANNUAL_FEE(6, "chargeTimeType.annualFee"), // only for savings
@@ -32,45 +34,45 @@ public enum ChargeTimeType {
return this.code;
}
public static int minValue() {
return 1;
public static Object[] validLoanValues() {
return new Integer[] { ChargeTimeType.DISBURSEMENT.getValue(), ChargeTimeType.SPECIFIED_DUE_DATE.getValue() };
}
public static int maxValue() {
int max = 1;// Default it to one (1)
for (ChargeTimeType chargeTimeType : ChargeTimeType.values()) {
max = (chargeTimeType.getValue() > max) ? chargeTimeType.getValue() : max;
}
return max;
public static Object[] validSavingsValues() {
return new Integer[] { ChargeTimeType.SPECIFIED_DUE_DATE.getValue(), ChargeTimeType.SAVINGS_ACTIVATION.getValue(),
ChargeTimeType.SAVINGS_CLOSURE.getValue(), ChargeTimeType.WITHDRAWAL_FEE.getValue(), ChargeTimeType.ANNUAL_FEE.getValue(),
ChargeTimeType.MONTHLY_FEE.getValue() };
}
public static ChargeTimeType fromInt(final Integer chargeTime) {
ChargeTimeType chargeTimeType = ChargeTimeType.INVALID;
switch (chargeTime) {
case 1:
chargeTimeType = DISBURSEMENT;
break;
case 2:
chargeTimeType = SPECIFIED_DUE_DATE;
break;
case 3:
chargeTimeType = SAVINGS_ACTIVATION;
break;
case 4:
chargeTimeType = SAVINGS_CLOSURE;
break;
case 5:
chargeTimeType = WITHDRAWAL_FEE;
break;
case 6:
chargeTimeType = ANNUAL_FEE;
break;
case 7:
chargeTimeType = MONTHLY_FEE;
break;
default:
chargeTimeType = INVALID;
break;
if (chargeTime != null) {
switch (chargeTime) {
case 1:
chargeTimeType = DISBURSEMENT;
break;
case 2:
chargeTimeType = SPECIFIED_DUE_DATE;
break;
case 3:
chargeTimeType = SAVINGS_ACTIVATION;
break;
case 4:
chargeTimeType = SAVINGS_CLOSURE;
break;
case 5:
chargeTimeType = WITHDRAWAL_FEE;
break;
case 6:
chargeTimeType = ANNUAL_FEE;
break;
case 7:
chargeTimeType = MONTHLY_FEE;
break;
default:
chargeTimeType = INVALID;
break;
}
}
return chargeTimeType;
}
@@ -90,11 +92,11 @@ public enum ChargeTimeType {
public boolean isSavingsClosure() {
return this.value.equals(ChargeTimeType.SAVINGS_CLOSURE.getValue());
}
public boolean isWithdrawalFee() {
return this.value.equals(ChargeTimeType.WITHDRAWAL_FEE.getValue());
}
public boolean isAnnualFee() {
return this.value.equals(ChargeTimeType.ANNUAL_FEE.getValue());
}
@@ -102,7 +104,7 @@ public enum ChargeTimeType {
public boolean isMonthlyFee() {
return this.value.equals(ChargeTimeType.MONTHLY_FEE.getValue());
}
public boolean isAllowedLoanChargeTime() {
return isTimeOfDisbursement() || isOnSpecifiedDueDate();
}
@@ -23,6 +23,8 @@ 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.charge.domain.ChargeAppliesTo;
import org.mifosplatform.portfolio.charge.domain.ChargeCalculationType;
import org.mifosplatform.portfolio.charge.domain.ChargePaymentMode;
import org.mifosplatform.portfolio.charge.domain.ChargeTimeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -38,7 +40,7 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
*/
private final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("name", "amount", "locale", "currencyCode",
"currencyOptions", "chargeAppliesTo", "chargeTimeType", "chargeCalculationType", "chargeCalculationTypeOptions", "penalty",
"active", "chargePaymentMode", "feeOnMonthDay", "feeInterval", "monthDayFormat","minCap","maxCap"));
"active", "chargePaymentMode", "feeOnMonthDay", "feeInterval", "monthDayFormat", "minCap", "maxCap"));
private final FromJsonHelper fromApiJsonHelper;
@@ -58,6 +60,50 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
final JsonElement element = this.fromApiJsonHelper.parse(json);
final Integer chargeAppliesTo = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargeAppliesTo", element);
baseDataValidator.reset().parameter("chargeAppliesTo").value(chargeAppliesTo).notNull();
if (chargeAppliesTo != null) {
baseDataValidator.reset().parameter("chargeAppliesTo").value(chargeAppliesTo).isOneOfTheseValues(ChargeAppliesTo.validValues());
}
final ChargeAppliesTo appliesTo = ChargeAppliesTo.fromInt(chargeAppliesTo);
if (appliesTo.isLoanCharge()) {
// loan applicable validation
final Integer chargeTimeType = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargeTimeType", element);
baseDataValidator.reset().parameter("chargeTimeType").value(chargeTimeType).notNull();
if (chargeTimeType != null) {
baseDataValidator.reset().parameter("chargeTimeType").value(chargeTimeType).isOneOfTheseValues(ChargeTimeType.validLoanValues());
}
final Integer chargePaymentMode = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargePaymentMode", element);
baseDataValidator.reset().parameter("chargePaymentMode").value(chargePaymentMode).notNull().isOneOfTheseValues(ChargePaymentMode.validValues());
if (chargePaymentMode != null) {
baseDataValidator.reset().parameter("chargePaymentMode").value(chargePaymentMode).isOneOfTheseValues(ChargePaymentMode.validValues());
}
} else if (appliesTo.isSavingsCharge()) {
// savings applicable validation
final Integer chargeTimeType = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargeTimeType", element);
baseDataValidator.reset().parameter("chargeTimeType").value(chargeTimeType).notNull();
if (chargeTimeType != null) {
baseDataValidator.reset().parameter("chargeTimeType").value(chargeTimeType).isOneOfTheseValues(ChargeTimeType.validSavingsValues());
}
final ChargeTimeType ctt = ChargeTimeType.fromInt(chargeTimeType);
if (ctt.isMonthlyFee()) {
final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed("feeOnMonthDay", element);
baseDataValidator.reset().parameter("feeOnMonthDay").value(monthDay).notNull();
final Integer feeInterval = this.fromApiJsonHelper.extractIntegerNamed("feeInterval", element, Locale.getDefault());
baseDataValidator.reset().parameter("feeInterval").value(feeInterval).notNull().inMinMaxRange(1, 12);
}
if (ctt.isAnnualFee()) {
final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed("feeOnMonthDay", element);
baseDataValidator.reset().parameter("feeOnMonthDay").value(monthDay).notNull();
}
}
final String name = this.fromApiJsonHelper.extractStringNamed("name", element);
baseDataValidator.reset().parameter("name").value(name).notBlank().notExceedingLengthOf(100);
@@ -67,24 +113,6 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
final BigDecimal amount = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("amount", element.getAsJsonObject());
baseDataValidator.reset().parameter("amount").value(amount).notNull().positiveAmount();
final Integer chargeAppliesTo = this.fromApiJsonHelper.extractIntegerNamed("chargeAppliesTo", element, Locale.getDefault());
baseDataValidator.reset().parameter("chargeAppliesTo").value(chargeAppliesTo).notNull()
.inMinMaxRange(ChargeAppliesTo.minValue(), ChargeAppliesTo.maxValue());
final Integer chargeTimeType = this.fromApiJsonHelper.extractIntegerNamed("chargeTimeType", element, Locale.getDefault());
baseDataValidator.reset().parameter("chargeTimeType").value(chargeTimeType).notNull()
.inMinMaxRange(ChargeTimeType.minValue(), ChargeTimeType.maxValue());
final Integer chargeCalculationType = this.fromApiJsonHelper.extractIntegerNamed("chargeCalculationType", element,
Locale.getDefault());
baseDataValidator.reset().parameter("chargeCalculationType").value(chargeCalculationType).notNull().inMinMaxRange(1, 4);
ChargeAppliesTo appliesTo = ChargeAppliesTo.fromInt(chargeAppliesTo);
if (appliesTo.isLoanCharge()) {
final Integer chargePaymentMode = this.fromApiJsonHelper.extractIntegerNamed("chargePaymentMode", element, Locale.getDefault());
baseDataValidator.reset().parameter("chargePaymentMode").value(chargePaymentMode).notNull().inMinMaxRange(0, 1);
}
if (this.fromApiJsonHelper.parameterExists("penalty", element)) {
final Boolean penalty = this.fromApiJsonHelper.extractBooleanNamed("penalty", element);
baseDataValidator.reset().parameter("penalty").value(penalty).notNull();
@@ -94,26 +122,18 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
final Boolean active = this.fromApiJsonHelper.extractBooleanNamed("active", element);
baseDataValidator.reset().parameter("active").value(active).notNull();
}
final ChargeTimeType ctt = ChargeTimeType.fromInt(chargeTimeType);
if(ctt.isMonthlyFee()){
final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed("feeOnMonthDay", element);
baseDataValidator.reset().parameter("feeOnMonthDay").value(monthDay).notNull();
final Integer feeInterval = this.fromApiJsonHelper.extractIntegerNamed("feeInterval", element, Locale.getDefault());
baseDataValidator.reset().parameter("feeInterval").value(feeInterval).notNull().inMinMaxRange(1, 12);
}
if(ctt.isAnnualFee()){
final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed("feeOnMonthDay", element);
baseDataValidator.reset().parameter("feeOnMonthDay").value(monthDay).notNull();
final Integer chargeCalculationType = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargeCalculationType", element);
baseDataValidator.reset().parameter("chargeCalculationType").value(chargeCalculationType).notNull();
if (chargeCalculationType != null) {
baseDataValidator.reset().parameter("chargeCalculationType").value(chargeCalculationType).isOneOfTheseValues(ChargeCalculationType.validValues());
}
if(this.fromApiJsonHelper.parameterExists("minCap",element)){
if (this.fromApiJsonHelper.parameterExists("minCap", element)) {
final BigDecimal minCap = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("minCap", element.getAsJsonObject());
baseDataValidator.reset().parameter("minCap").value(minCap).notNull().positiveAmount();
}
if(this.fromApiJsonHelper.parameterExists("maxCap",element)){
if (this.fromApiJsonHelper.parameterExists("maxCap", element)) {
final BigDecimal maxCap = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("maxCap", element.getAsJsonObject());
baseDataValidator.reset().parameter("maxCap").value(maxCap).notNull().positiveAmount();
}
@@ -131,6 +151,7 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("charge");
final JsonElement element = this.fromApiJsonHelper.parse(json);
if (this.fromApiJsonHelper.parameterExists("name", element)) {
final String name = this.fromApiJsonHelper.extractStringNamed("name", element);
baseDataValidator.reset().parameter("name").value(name).notBlank().notExceedingLengthOf(100);
@@ -146,33 +167,34 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
baseDataValidator.reset().parameter("amount").value(amount).notNull().positiveAmount();
}
if(this.fromApiJsonHelper.parameterExists("minCap",element)){
if (this.fromApiJsonHelper.parameterExists("minCap", element)) {
final BigDecimal minCap = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("minCap", element.getAsJsonObject());
baseDataValidator.reset().parameter("minCap").value(minCap).notNull().positiveAmount();
}
if(this.fromApiJsonHelper.parameterExists("maxCap",element)){
if (this.fromApiJsonHelper.parameterExists("maxCap", element)) {
final BigDecimal maxCap = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("maxCap", element.getAsJsonObject());
baseDataValidator.reset().parameter("maxCap").value(maxCap).notNull().positiveAmount();
}
if (this.fromApiJsonHelper.parameterExists("chargeAppliesTo", element)) {
final Integer chargeAppliesTo = this.fromApiJsonHelper.extractIntegerNamed("chargeAppliesTo", element, Locale.getDefault());
final Integer chargeAppliesTo = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargeAppliesTo", element);
baseDataValidator.reset().parameter("chargeAppliesTo").value(chargeAppliesTo).notNull()
.inMinMaxRange(ChargeAppliesTo.minValue(), ChargeAppliesTo.maxValue());
.isOneOfTheseValues(ChargeAppliesTo.validValues());
}
if (this.fromApiJsonHelper.parameterExists("chargeTimeType", element)) {
final Integer chargeTimeType = this.fromApiJsonHelper.extractIntegerNamed("chargeTimeType", element, Locale.getDefault());
final Integer chargeTimeType = this.fromApiJsonHelper.extractIntegerSansLocaleNamed("chargeTimeType", element);
baseDataValidator.reset().parameter("chargeTimeType").value(chargeTimeType).notNull()
.inMinMaxRange(ChargeTimeType.minValue(), ChargeTimeType.maxValue());
.isOneOfTheseValues(ChargeTimeType.validLoanValues(), ChargeTimeType.validSavingsValues());
}
if (this.fromApiJsonHelper.parameterExists("feeOnMonthDay", element)) {
final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed("feeOnMonthDay", element);
final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed("feeOnMonthDay", element);
baseDataValidator.reset().parameter("feeOnMonthDay").value(monthDay).notNull();
}
if (this.fromApiJsonHelper.parameterExists("feeInterval", element)) {
final Integer feeInterval = this.fromApiJsonHelper.extractIntegerNamed("feeInterval", element, Locale.getDefault());
baseDataValidator.reset().parameter("feeInterval").value(feeInterval).notNull().inMinMaxRange(1, 12);
@@ -198,11 +220,11 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
final Boolean active = this.fromApiJsonHelper.extractBooleanNamed("active", element);
baseDataValidator.reset().parameter("active").value(active).notNull();
}
if(this.fromApiJsonHelper.parameterExists("minCap",element)){
if (this.fromApiJsonHelper.parameterExists("minCap", element)) {
final BigDecimal minCap = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("minCap", element.getAsJsonObject());
baseDataValidator.reset().parameter("minCap").value(minCap).notNull().positiveAmount();
}
if(this.fromApiJsonHelper.parameterExists("maxCap",element)){
if (this.fromApiJsonHelper.parameterExists("maxCap", element)) {
final BigDecimal maxCap = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed("maxCap", element.getAsJsonObject());
baseDataValidator.reset().parameter("maxCap").value(maxCap).notNull().positiveAmount();
}
@@ -211,7 +233,6 @@ public final class ChargeDefinitionCommandFromApiJsonDeserializer {
}
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
}
}