from MIFOSX-38 - tidy up serialisation around funds.

This commit is contained in:
Keith Woodlock
2012-08-21 17:56:35 +01:00
parent f48e4de309
commit c7c5198206
7 changed files with 55 additions and 89 deletions
@@ -17,6 +17,7 @@ import javax.ws.rs.core.UriInfo;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.data.ConfigurationData;
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.configuration.service.ConfigurationReadPlatformService;
import org.mifosng.platform.configuration.service.ConfigurationWritePlatformService;
@@ -38,6 +39,9 @@ public class ConfigurationApiResource {
@Autowired
private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJsonSerializerService apiJsonSerializerService;
@GET
@Path("currency")
@Consumes({MediaType.APPLICATION_JSON})
@@ -55,7 +59,7 @@ public class ConfigurationApiResource {
ConfigurationData configurationData = this.configurationReadPlatformService.retrieveCurrencyConfiguration();
return this.apiDataConversionService.convertConfigurationDataToJson(prettyPrint, responseParameters, configurationData);
return this.apiJsonSerializerService.serializeConfigurationDataToJson(prettyPrint, responseParameters, configurationData);
}
@PUT
@@ -19,6 +19,7 @@ import org.mifosng.platform.api.commands.FundCommand;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.FundData;
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.fund.service.FundReadPlatformService;
import org.mifosng.platform.fund.service.FundWritePlatformService;
@@ -40,6 +41,9 @@ public class FundsApiResource {
@Autowired
private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJsonSerializerService apiJsonSerializerService;
@GET
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@@ -48,9 +52,9 @@ public class FundsApiResource {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<FundData> funds = this.readPlatformService.retrieveAllFunds();
final Collection<FundData> funds = this.readPlatformService.retrieveAllFunds();
return this.apiDataConversionService.convertFundDataToJson(prettyPrint, responseParameters, funds.toArray(new FundData[funds.size()]));
return this.apiJsonSerializerService.serializeFundDataToJson(prettyPrint, responseParameters, funds);
}
@POST
@@ -58,9 +62,9 @@ public class FundsApiResource {
@Produces({MediaType.APPLICATION_JSON})
public Response createFund(final String jsonRequestBody) {
FundCommand command = this.apiDataConversionService.convertJsonToFundCommand(null, jsonRequestBody);
final FundCommand command = this.apiDataConversionService.convertJsonToFundCommand(null, jsonRequestBody);
Long fundId = this.writePlatformService.createFund(command);
final Long fundId = this.writePlatformService.createFund(command);
return Response.ok().entity(new EntityIdentifier(fundId)).build();
}
@@ -71,12 +75,12 @@ public class FundsApiResource {
@Produces({MediaType.APPLICATION_JSON})
public String retreiveOffice(@PathParam("fundId") final Long fundId, @Context final UriInfo uriInfo) {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
final Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
FundData fund = this.readPlatformService.retrieveFund(fundId);
final FundData fund = this.readPlatformService.retrieveFund(fundId);
return this.apiDataConversionService.convertFundDataToJson(prettyPrint, responseParameters, fund);
return this.apiJsonSerializerService.serializeFundDataToJson(prettyPrint, responseParameters, fund);
}
@PUT
@@ -85,9 +89,9 @@ public class FundsApiResource {
@Produces({MediaType.APPLICATION_JSON})
public Response updateFund(@PathParam("fundId") final Long fundId, final String jsonRequestBody) {
FundCommand command = this.apiDataConversionService.convertJsonToFundCommand(fundId, jsonRequestBody);
final FundCommand command = this.apiDataConversionService.convertJsonToFundCommand(fundId, jsonRequestBody);
Long entityId = this.writePlatformService.updateFund(command);
final Long entityId = this.writePlatformService.updateFund(command);
return Response.ok().entity(new EntityIdentifier(entityId)).build();
}
@@ -2,6 +2,9 @@ package org.mifosng.platform.api.data;
import java.util.List;
/**
* Immutable data object for configuration.
*/
public class ConfigurationData {
private final List<CurrencyData> selectedCurrencyOptions;
@@ -25,10 +25,8 @@ import org.mifosng.platform.api.data.AdditionalFieldsSetData;
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.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;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.data.LoanAccountData;
@@ -41,8 +39,6 @@ import org.mifosng.platform.api.data.SavingProductData;
public interface ApiDataConversionService {
//
String convertConfigurationDataToJson(boolean prettyPrint, Set<String> responseParameters, ConfigurationData... configuration);
String convertClientDataToJson(boolean prettyPrint, Set<String> responseParameters, ClientData... clients);
String convertClientLoanAccountSummaryCollectionDataToJson(boolean prettyPrint, Set<String> responseParameters,
@@ -79,8 +75,6 @@ public interface ApiDataConversionService {
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
String convertFundDataToJson(boolean prettyPrint, Set<String> responseParameters, FundData... funds);
OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json);
RoleCommand convertJsonToRoleCommand(Long resourceIdentifier, String json);
@@ -42,10 +42,8 @@ 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.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;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.data.LoanAccountData;
@@ -405,74 +403,6 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
return json;
}
@Override
public String convertConfigurationDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final ConfigurationData... configuration) {
final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
String json = "";
if (configuration != null && configuration.length == 1) {
json = gsonDeserializer.toJson(configuration[0]);
} else {
json = gsonDeserializer.toJson(configuration);
}
return json;
}
@Override
public String convertFundDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final FundData... funds) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "externalId"));
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
String json = "";
if (funds != null && funds.length == 1) {
json = gsonDeserializer.toJson(funds[0]);
} else {
json = gsonDeserializer.toJson(funds);
}
return json;
}
@Override
public ChargeCommand convertJsonToChargeCommand(Long resourceIdentifier, String json) {
if (StringUtils.isBlank(json)) {
@@ -5,6 +5,8 @@ import java.util.Set;
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.FundData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.data.PermissionData;
@@ -28,6 +30,11 @@ public interface ApiJsonSerializerService {
String serializeOfficeDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeData office);
String serializeOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData officeTransactionData);
String serializeOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData officeTransaction);
String serializeConfigurationDataToJson(boolean prettyPrint, Set<String> responseParameters, ConfigurationData configuration);
String serializeFundDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<FundData> funds);
String serializeFundDataToJson(boolean prettyPrint, Set<String> responseParameters, FundData fund);
}
@@ -7,6 +7,8 @@ import java.util.Set;
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.FundData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.data.PermissionData;
@@ -33,6 +35,10 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ
);
private static final Set<String> OFFICE_TRANSACTIONS_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("transactionDate", "allowedOffices", "currencyOptions"));
private static final Set<String> CONFIGURATION_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
private static final Set<String> FUND_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "name", "externalId"));
private final GoogleGsonSerializerHelper helper;
@Autowired
@@ -93,4 +99,22 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(OFFICE_TRANSACTIONS_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, officeTransaction);
}
@Override
public String serializeConfigurationDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final ConfigurationData configuration) {
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(CONFIGURATION_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, configuration);
}
@Override
public String serializeFundDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final Collection<FundData> funds) {
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(FUND_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, funds.toArray(new FundData[funds.size()]));
}
@Override
public String serializeFundDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final FundData fund) {
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(FUND_DATA_PARAMETERS, prettyPrint, responseParameters);
return helper.serializedJsonFrom(gsonDeserializer, fund);
}
}