From 3b341b29afe28e927732f7be89c885f0347fc49b Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Wed, 22 Aug 2012 11:15:50 +0100 Subject: [PATCH] from MIFOSX-38, tidy up serialisation around loan accounts and transactions. --- .../platform/api/LoansApiResource.java | 12 +- .../ApiDataConversionService.java | 11 -- .../ApiDataConversionServiceImpl.java | 109 ------------------ .../ApiJsonSerializerService.java | 9 ++ .../GoogleGsonApiJsonSerializerService.java | 40 +++++++ 5 files changed, 57 insertions(+), 124 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java index d4d6d4b7b..07845664a 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java @@ -36,6 +36,7 @@ import org.mifosng.platform.api.data.LoanSchedule; import org.mifosng.platform.api.data.LoanTransactionData; import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.infrastructure.ApiDataConversionService; +import org.mifosng.platform.api.infrastructure.ApiJsonSerializerService; import org.mifosng.platform.api.infrastructure.ApiParameterHelper; import org.mifosng.platform.exceptions.UnrecognizedQueryParamException; import org.mifosng.platform.loan.service.CalculationPlatformService; @@ -61,6 +62,9 @@ public class LoansApiResource { @Autowired private ApiDataConversionService apiDataConversionService; + + @Autowired + private ApiJsonSerializerService apiJsonSerializerService; @GET @Path("template") @@ -82,7 +86,7 @@ public class LoansApiResource { NewLoanData workflowData = this.loanReadPlatformService.retrieveClientAndProductDetails(clientId, productId); - return this.apiDataConversionService.convertNewLoanDataToJson(prettyPrint, responseParameters, workflowData); + return this.apiJsonSerializerService.serializeNewLoanDataToJson(prettyPrint, responseParameters, workflowData); } @GET @@ -134,7 +138,7 @@ public class LoansApiResource { LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, summary, repaymentSchedule, loanRepayments, permissions); - return this.apiDataConversionService.convertLoanAccountDataToJson(prettyPrint, responseParameters, loanAccount); + return this.apiJsonSerializerService.serialzieLoanAccountDataToJson(prettyPrint, responseParameters, loanAccount); } @POST @@ -291,7 +295,7 @@ public class LoansApiResource { throw new UnrecognizedQueryParamException("command", commandParam); } - return this.apiDataConversionService.convertLoanTransactionDataToJson(prettyPrint, responseParameters, transactionData); + return this.apiJsonSerializerService.serializeLoanTransactionDataToJson(prettyPrint, responseParameters, transactionData); } @GET @@ -314,7 +318,7 @@ public class LoansApiResource { LoanTransactionData transactionData = this.loanReadPlatformService.retrieveLoanTransactionDetails(loanId, transactionId); - return this.apiDataConversionService.convertLoanTransactionDataToJson(prettyPrint, responseParameters, transactionData); + return this.apiJsonSerializerService.serializeLoanTransactionDataToJson(prettyPrint, responseParameters, transactionData); } @POST diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java index d964b1fe6..fa275340d 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java @@ -21,20 +21,9 @@ import org.mifosng.platform.api.commands.SavingProductCommand; import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.data.ChargeData; -import org.mifosng.platform.api.data.LoanAccountData; -import org.mifosng.platform.api.data.LoanTransactionData; -import org.mifosng.platform.api.data.NewLoanData; public interface ApiDataConversionService { - // - String convertNewLoanDataToJson(boolean prettyPrint, Set responseParameters, NewLoanData newLoanData); - - String convertLoanAccountDataToJson(boolean prettyPrint, Set responseParameters, LoanAccountData loanAccount); - - String convertLoanTransactionDataToJson(boolean prettyPrint, Set responseParameters, LoanTransactionData... transactions); - // - ChargeCommand convertJsonToChargeCommand(Long resourceIdentifier, String json); String convertChargeDataToJson(boolean prettyPrint, Set responseParameters, ChargeData... charges); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index be8983589..60da43cd8 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -15,7 +15,6 @@ import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; -import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat; import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; @@ -38,9 +37,6 @@ import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.data.ApiParameterError; import org.mifosng.platform.api.data.ChargeData; -import org.mifosng.platform.api.data.LoanAccountData; -import org.mifosng.platform.api.data.LoanTransactionData; -import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.errorhandling.InvalidJsonException; import org.mifosng.platform.api.errorhandling.UnsupportedParameterException; import org.mifosng.platform.exceptions.PlatformApiDataValidationException; @@ -65,111 +61,6 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { public ApiDataConversionServiceImpl() { gsonConverter = new Gson(); } - - @Override - public String convertLoanTransactionDataToJson(final boolean prettyPrint, final Set responseParameters, final LoanTransactionData... transactions) { - final Set supportedParameters = new HashSet( - Arrays.asList("id", "transactionType", "date", "principal", "interest", "total", "totalWaived", "overpaid") - ); - - final Set parameterNamesToSkip = new HashSet(); - - if (!responseParameters.isEmpty()) { - if (!supportedParameters.containsAll(responseParameters)) { - throw new UnsupportedParameterException(new ArrayList(responseParameters)); - } - - parameterNamesToSkip.addAll(supportedParameters); - parameterNamesToSkip.removeAll(responseParameters); - } - - ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); - - GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); - builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); - builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); - if (prettyPrint) { - builder.setPrettyPrinting(); - } - Gson gsonDeserializer = builder.create(); - - String json = ""; - if (transactions != null && transactions.length == 1) { - json = gsonDeserializer.toJson(transactions[0]); - } else { - json = gsonDeserializer.toJson(transactions); - } - - return json; - } - - @Override - public String convertLoanAccountDataToJson(final boolean prettyPrint, final Set responseParameters, final LoanAccountData loanAccount) { - final Set supportedParameters = new HashSet( - Arrays.asList("id", "externalId", "fundId", "fundName", "loanProductId", "loanProductName", "principal", "inArrearsTolerance", "numberOfRepayments", - "repaymentEvery", "interestRatePerPeriod", "annualInterestRate", - "repaymentFrequencyType", "interestRateFrequencyType", "amortizationType", "interestType", "interestCalculationPeriodType", - "submittedOnDate", "approvedOnDate", "expectedDisbursementDate", "actualDisbursementDate", - "expectedFirstRepaymentOnDate", "interestChargedFromDate", "closedOnDate", "expectedMaturityDate", - "lifeCycleStatusId", "lifeCycleStatusText", "lifeCycleStatusDate", - "summary", "repaymentSchedule", "loanRepayments", "permissions", "convenienceData") - ); - - final Set parameterNamesToSkip = new HashSet(); - - if (!responseParameters.isEmpty()) { - if (!supportedParameters.containsAll(responseParameters)) { - throw new UnsupportedParameterException(new ArrayList(responseParameters)); - } - - parameterNamesToSkip.addAll(supportedParameters); - parameterNamesToSkip.removeAll(responseParameters); - } - - ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); - - GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); - builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); - builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); - if (prettyPrint) { - builder.setPrettyPrinting(); - } - Gson gsonDeserializer = builder.create(); - - return gsonDeserializer.toJson(loanAccount); - } - - @Override - public String convertNewLoanDataToJson(final boolean prettyPrint, final Set responseParameters, final NewLoanData newLoanData) { - - final Set supportedParameters = new HashSet( - Arrays.asList("clientId", "clientName", "productId", "productName", - "selectedProduct", "expectedDisbursementDate", "allowedProducts") - ); - - final Set parameterNamesToSkip = new HashSet(); - - if (!responseParameters.isEmpty()) { - if (!supportedParameters.containsAll(responseParameters)) { - throw new UnsupportedParameterException(new ArrayList(responseParameters)); - } - - parameterNamesToSkip.addAll(supportedParameters); - parameterNamesToSkip.removeAll(responseParameters); - } - - ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); - - GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); - builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); - builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); - if (prettyPrint) { - builder.setPrettyPrinting(); - } - Gson gsonDeserializer = builder.create(); - - return gsonDeserializer.toJson(newLoanData); - } @Override public ChargeCommand convertJsonToChargeCommand(Long resourceIdentifier, String json) { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJsonSerializerService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJsonSerializerService.java index 71750643a..f5450ebc0 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJsonSerializerService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJsonSerializerService.java @@ -14,7 +14,10 @@ import org.mifosng.platform.api.data.DepositProductData; import org.mifosng.platform.api.data.FundData; import org.mifosng.platform.api.data.GenericResultsetData; import org.mifosng.platform.api.data.GroupData; +import org.mifosng.platform.api.data.LoanAccountData; import org.mifosng.platform.api.data.LoanProductData; +import org.mifosng.platform.api.data.LoanTransactionData; +import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.data.NoteData; import org.mifosng.platform.api.data.OfficeData; import org.mifosng.platform.api.data.OfficeTransactionData; @@ -81,4 +84,10 @@ public interface ApiJsonSerializerService { String serializeNoteDataToJson(boolean prettyPrint, Set responseParameters, Collection notes); String serializeNoteDataToJson(boolean prettyPrint, Set responseParameters, NoteData note); + + String serializeNewLoanDataToJson(boolean prettyPrint, Set responseParameters, NewLoanData newLoan); + + String serialzieLoanAccountDataToJson(boolean prettyPrint, Set responseParameters, LoanAccountData loanAccount); + + String serializeLoanTransactionDataToJson(boolean prettyPrint, Set responseParameters, LoanTransactionData transaction); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/GoogleGsonApiJsonSerializerService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/GoogleGsonApiJsonSerializerService.java index dfcd35273..61ff3b1fb 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/GoogleGsonApiJsonSerializerService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/GoogleGsonApiJsonSerializerService.java @@ -16,7 +16,10 @@ import org.mifosng.platform.api.data.DepositProductData; import org.mifosng.platform.api.data.FundData; import org.mifosng.platform.api.data.GenericResultsetData; import org.mifosng.platform.api.data.GroupData; +import org.mifosng.platform.api.data.LoanAccountData; import org.mifosng.platform.api.data.LoanProductData; +import org.mifosng.platform.api.data.LoanTransactionData; +import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.data.NoteData; import org.mifosng.platform.api.data.OfficeData; import org.mifosng.platform.api.data.OfficeTransactionData; @@ -97,6 +100,25 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername", "createdOn", "updatedById", "updatedByUsername", "updatedOn") ); + + private static final Set NEW_LOAN_DATA_PARAMETERS = new HashSet( + Arrays.asList("clientId", "clientName", "productId", "productName", + "selectedProduct", "expectedDisbursementDate", "allowedProducts") + ); + + private static final Set LOAN_DATA_PARAMETERS = new HashSet( + Arrays.asList("id", "externalId", "fundId", "fundName", "loanProductId", "loanProductName", "principal", "inArrearsTolerance", "numberOfRepayments", + "repaymentEvery", "interestRatePerPeriod", "annualInterestRate", + "repaymentFrequencyType", "interestRateFrequencyType", "amortizationType", "interestType", "interestCalculationPeriodType", + "submittedOnDate", "approvedOnDate", "expectedDisbursementDate", "actualDisbursementDate", + "expectedFirstRepaymentOnDate", "interestChargedFromDate", "closedOnDate", "expectedMaturityDate", + "lifeCycleStatusId", "lifeCycleStatusText", "lifeCycleStatusDate", + "summary", "repaymentSchedule", "loanRepayments", "permissions", "convenienceData") + ); + + private static final Set LOAN_TRANSACTION_DATA_PARAMETERS = new HashSet( + Arrays.asList("id", "transactionType", "date", "principal", "interest", "total", "totalWaived", "overpaid") + ); private final GoogleGsonSerializerHelper helper; @@ -279,4 +301,22 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(NOTE_DATA_PARAMETERS, prettyPrint, responseParameters); return helper.serializedJsonFrom(gsonDeserializer, note); } + + @Override + public String serializeNewLoanDataToJson(final boolean prettyPrint, final Set responseParameters, final NewLoanData newLoan) { + final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(NEW_LOAN_DATA_PARAMETERS, prettyPrint, responseParameters); + return helper.serializedJsonFrom(gsonDeserializer, newLoan); + } + + @Override + public String serialzieLoanAccountDataToJson(final boolean prettyPrint, final Set responseParameters, final LoanAccountData loanAccount) { + final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(LOAN_DATA_PARAMETERS, prettyPrint, responseParameters); + return helper.serializedJsonFrom(gsonDeserializer, loanAccount); + } + + @Override + public String serializeLoanTransactionDataToJson(final boolean prettyPrint, final Set responseParameters, final LoanTransactionData transaction) { + final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(LOAN_TRANSACTION_DATA_PARAMETERS, prettyPrint, responseParameters); + return helper.serializedJsonFrom(gsonDeserializer, transaction); + } } \ No newline at end of file