from MIFOSX-38, tidy up serialization of deposit accounts.

This commit is contained in:
Keith Woodlock
2012-08-21 21:47:07 +01:00
parent bd37a66247
commit 75ba809761
5 changed files with 35 additions and 59 deletions
@@ -26,6 +26,7 @@ import org.mifosng.platform.api.data.DepositProductLookup;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.EnumOptionData;
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.loan.domain.PeriodFrequencyType;
import org.mifosng.platform.saving.service.DepositAccountReadPlatformService;
@@ -53,6 +54,9 @@ public class DepositAccountsApiResource {
@Autowired
private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJsonSerializerService apiJsonSerializerService;
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@@ -99,7 +103,7 @@ public class DepositAccountsApiResource {
Collection<DepositAccountData> accounts = this.depositAccountReadPlatformService.retrieveAllDepositAccounts();
return this.apiDataConversionService.convertDepositAccountDataToJson(prettyPrint, responseParameters, accounts.toArray(new DepositAccountData[accounts.size()]));
return this.apiJsonSerializerService.serializeDepositAccountDataToJson(prettyPrint, responseParameters, accounts);
}
@GET
@@ -129,7 +133,7 @@ public class DepositAccountsApiResource {
account = handleTemplateRelatedData(responseParameters, account);
}
return this.apiDataConversionService.convertDepositAccountDataToJson(prettyPrint, responseParameters, account);
return this.apiJsonSerializerService.serializeDepositAccountDataToJson(prettyPrint, responseParameters, account);
}
@GET
@@ -160,7 +164,7 @@ public class DepositAccountsApiResource {
account = handleTemplateRelatedData(responseParameters, account);
return this.apiDataConversionService.convertDepositAccountDataToJson(prettyPrint, responseParameters, account);
return this.apiJsonSerializerService.serializeDepositAccountDataToJson(prettyPrint, responseParameters, account);
}
@DELETE
@@ -24,7 +24,6 @@ import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.data.ChargeData;
import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData;
import org.mifosng.platform.api.data.DepositAccountData;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.data.LoanAccountData;
import org.mifosng.platform.api.data.LoanTransactionData;
@@ -45,8 +44,6 @@ public interface ApiDataConversionService {
String convertNoteDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<NoteData> notes);
String convertDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositAccountData... accounts);
String convertNewLoanDataToJson(boolean prettyPrint, Set<String> responseParameters, NewLoanData newLoanData);
String convertLoanAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanAccountData loanAccount);
@@ -41,7 +41,6 @@ import org.mifosng.platform.api.data.ApiParameterError;
import org.mifosng.platform.api.data.ChargeData;
import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData;
import org.mifosng.platform.api.data.DepositAccountData;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.data.LoanAccountData;
import org.mifosng.platform.api.data.LoanTransactionData;
@@ -1296,56 +1295,4 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
interestCompoundedEvery, interestCompoundedEveryPeriodType,
canRenew, canPreClose, preClosureInterestRate);
}
@Override
public String convertDepositAccountDataToJson(
final boolean prettyPrint,
final Set<String> responseParameters,
final DepositAccountData... accounts) {
Set<String> supportedParameters = new HashSet<String>(
Arrays.asList("productOptions", "interestCompoundedEveryPeriodTypeOptions",
"createdOn", "lastModifedOn",
"id", "externalId", "clientId", "clientName", "productId", "productName",
"currency", "deposit", "maturityInterestRate", "tenureInMonths",
"interestCompoundedEvery", "interestCompoundedEveryPeriodType",
"renewalAllowed","preClosureAllowed","preClosureInterestRate"
)
);
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
// strip out all know support params from expected response to see if unsupported parameters requested for response.
Set<String> differentParametersSet = new HashSet<String>(responseParameters);
differentParametersSet.removeAll(supportedParameters);
if (!differentParametersSet.isEmpty()) {
throw new UnsupportedParameterException(new ArrayList<String>(differentParametersSet));
}
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 (accounts != null && accounts.length == 1) {
json = gsonDeserializer.toJson(accounts[0]);
} else {
json = gsonDeserializer.toJson(accounts);
}
return json;
}
}
@@ -7,6 +7,7 @@ import org.mifosng.platform.api.data.AdditionalFieldsSetData;
import org.mifosng.platform.api.data.AppUserData;
import org.mifosng.platform.api.data.AuthenticatedUserData;
import org.mifosng.platform.api.data.ConfigurationData;
import org.mifosng.platform.api.data.DepositAccountData;
import org.mifosng.platform.api.data.DepositProductData;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.data.GenericResultsetData;
@@ -58,4 +59,8 @@ public interface ApiJsonSerializerService {
String serializeDepositProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<DepositProductData> products);
String serializeDepositProductDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositProductData depositProduct);
String serializeDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<DepositAccountData> accounts);
String serializeDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositAccountData account);
}
@@ -9,6 +9,7 @@ import org.mifosng.platform.api.data.AdditionalFieldsSetData;
import org.mifosng.platform.api.data.AppUserData;
import org.mifosng.platform.api.data.AuthenticatedUserData;
import org.mifosng.platform.api.data.ConfigurationData;
import org.mifosng.platform.api.data.DepositAccountData;
import org.mifosng.platform.api.data.DepositProductData;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.data.GenericResultsetData;
@@ -64,6 +65,16 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ
"maturityMaxInterestRate", "interestCompoundedEvery", "interestCompoundedEveryPeriodType",
"renewalAllowed","preClosureAllowed","preClosureInterestRate")
);
private static final Set<String> SAVINGS_DEPOSIT_ACCOUNT_DATA_PARAMETERS = new HashSet<String>(
Arrays.asList("productOptions", "interestCompoundedEveryPeriodTypeOptions",
"createdOn", "lastModifedOn",
"id", "externalId", "clientId", "clientName", "productId", "productName",
"currency", "deposit", "maturityInterestRate", "tenureInMonths",
"interestCompoundedEvery", "interestCompoundedEveryPeriodType",
"renewalAllowed","preClosureAllowed","preClosureInterestRate"
)
);
private final GoogleGsonSerializerHelper helper;
@@ -191,4 +202,16 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(SAVINGS_DEPOSIT_PRODUCT_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, depositProduct);
}
@Override
public String serializeDepositAccountDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final Collection<DepositAccountData> accounts) {
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(SAVINGS_DEPOSIT_ACCOUNT_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, accounts.toArray(new DepositAccountData[accounts.size()]));
}
@Override
public String serializeDepositAccountDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final DepositAccountData account) {
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(SAVINGS_DEPOSIT_ACCOUNT_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, account);
}
}