from MIFOSX-38, tidy up serialization of charges data.
This commit is contained in:
@@ -4,6 +4,7 @@ import org.mifosng.platform.api.commands.ChargeCommand;
|
||||
import org.mifosng.platform.api.data.ChargeData;
|
||||
import org.mifosng.platform.api.data.EntityIdentifier;
|
||||
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.charge.service.ChargeReadPlatformService;
|
||||
import org.mifosng.platform.charge.service.ChargeWritePlatformService;
|
||||
@@ -32,6 +33,9 @@ public class ChargesApiResource {
|
||||
|
||||
@Autowired
|
||||
private ApiDataConversionService apiDataConversionService;
|
||||
|
||||
@Autowired
|
||||
private ApiJsonSerializerService apiJsonSerializerService;
|
||||
|
||||
@GET
|
||||
@Consumes({MediaType.APPLICATION_JSON})
|
||||
@@ -43,8 +47,7 @@ public class ChargesApiResource {
|
||||
|
||||
Collection<ChargeData> charges = this.chargeReadPlatformService.retrieveAllCharges();
|
||||
|
||||
return this.apiDataConversionService.convertChargeDataToJson(prettyPrint,
|
||||
responseParameters, charges.toArray(new ChargeData[charges.size()]));
|
||||
return this.apiJsonSerializerService.serializeChargeDataToJson(prettyPrint, responseParameters, charges);
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -58,8 +61,7 @@ public class ChargesApiResource {
|
||||
|
||||
ChargeData charge = this.chargeReadPlatformService.retrieveCharge(chargeId);
|
||||
|
||||
return this.apiDataConversionService.convertChargeDataToJson(prettyPrint,
|
||||
responseParameters, charge);
|
||||
return this.apiJsonSerializerService.serializeChargeDataToJson(prettyPrint, responseParameters, charge);
|
||||
}
|
||||
|
||||
@POST
|
||||
|
||||
-5
@@ -1,7 +1,5 @@
|
||||
package org.mifosng.platform.api.infrastructure;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand;
|
||||
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
|
||||
import org.mifosng.platform.api.commands.ChargeCommand;
|
||||
@@ -20,14 +18,11 @@ import org.mifosng.platform.api.commands.RoleCommand;
|
||||
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;
|
||||
|
||||
public interface ApiDataConversionService {
|
||||
|
||||
ChargeCommand convertJsonToChargeCommand(Long resourceIdentifier, String json);
|
||||
|
||||
String convertChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, ChargeData... charges);
|
||||
|
||||
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
|
||||
|
||||
OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json);
|
||||
|
||||
-36
@@ -36,7 +36,6 @@ 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.ApiParameterError;
|
||||
import org.mifosng.platform.api.data.ChargeData;
|
||||
import org.mifosng.platform.api.errorhandling.InvalidJsonException;
|
||||
import org.mifosng.platform.api.errorhandling.UnsupportedParameterException;
|
||||
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
|
||||
@@ -44,9 +43,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.format.number.NumberFormatter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.google.gson.ExclusionStrategy;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
@@ -86,39 +83,6 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
|
||||
return new ChargeCommand(modifiedParameters, resourceIdentifier, name, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, ChargeData... charges) {
|
||||
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "amount"));
|
||||
|
||||
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 (charges != null && charges.length == 1) {
|
||||
json = gsonDeserializer.toJson(charges[0]);
|
||||
} else {
|
||||
json = gsonDeserializer.toJson(charges);
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FundCommand convertJsonToFundCommand(final Long resourceIdentifier, final String json) {
|
||||
|
||||
|
||||
+5
@@ -6,6 +6,7 @@ import java.util.Set;
|
||||
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.ChargeData;
|
||||
import org.mifosng.platform.api.data.ClientData;
|
||||
import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData;
|
||||
import org.mifosng.platform.api.data.ConfigurationData;
|
||||
@@ -90,4 +91,8 @@ public interface ApiJsonSerializerService {
|
||||
String serialzieLoanAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanAccountData loanAccount);
|
||||
|
||||
String serializeLoanTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanTransactionData transaction);
|
||||
|
||||
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ChargeData> charges);
|
||||
|
||||
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, ChargeData charge);
|
||||
}
|
||||
+15
@@ -8,6 +8,7 @@ import java.util.Set;
|
||||
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.ChargeData;
|
||||
import org.mifosng.platform.api.data.ClientData;
|
||||
import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData;
|
||||
import org.mifosng.platform.api.data.ConfigurationData;
|
||||
@@ -119,6 +120,8 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ
|
||||
private static final Set<String> LOAN_TRANSACTION_DATA_PARAMETERS = new HashSet<String>(
|
||||
Arrays.asList("id", "transactionType", "date", "principal", "interest", "total", "totalWaived", "overpaid")
|
||||
);
|
||||
|
||||
private static final Set<String> CHARGES_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "name", "amount"));
|
||||
|
||||
private final GoogleGsonSerializerHelper helper;
|
||||
|
||||
@@ -319,4 +322,16 @@ public class GoogleGsonApiJsonSerializerService implements ApiJsonSerializerServ
|
||||
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(LOAN_TRANSACTION_DATA_PARAMETERS, prettyPrint, responseParameters);
|
||||
return helper.serializedJsonFrom(gsonDeserializer, transaction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serializeChargeDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final Collection<ChargeData> charges) {
|
||||
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(CHARGES_DATA_PARAMETERS, prettyPrint, responseParameters);
|
||||
return helper.serializedJsonFrom(gsonDeserializer, charges.toArray(new ChargeData[charges.size()]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serializeChargeDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final ChargeData charge) {
|
||||
final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(CHARGES_DATA_PARAMETERS, prettyPrint, responseParameters);
|
||||
return helper.serializedJsonFrom(gsonDeserializer, charge);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user