From 46c315815d1bc3fefc7fb0e40285346c6599c8ad Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Fri, 21 Dec 2012 11:45:37 +0000 Subject: [PATCH] clean up remainder of serialization code for reports, datatables and groups.2 --- .../codes/api/CodesApiResource.java | 4 +- .../exception/CodeNotFoundException.java | 4 + .../service/CodeReadPlatformServiceImpl.java | 8 +- ...GsonPortfolioApiJsonSerializerService.java | 88 ------------ .../PortfolioApiDataConversionService.java | 3 - ...PortfolioApiDataConversionServiceImpl.java | 19 --- .../PortfolioApiJsonSerializerService.java | 27 ---- .../DefaultToApiJsonSerializer.java | 12 ++ .../serialization/ToApiJsonSerializer.java | 10 +- .../dataqueries/api/DataTableApiResource.java | 76 +++++------ .../dataqueries/api/ReportsApiResource.java | 26 ++-- .../api/DocumentManagementApiResource.java | 49 ++++--- .../api/AuthenticationApiResource.java | 16 ++- .../client/api/ClientsApiResource.java | 28 ++-- .../group/api/GroupsApiResource.java | 129 ++++++++---------- 15 files changed, 188 insertions(+), 311 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/api/CodesApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/api/CodesApiResource.java index 78aec868a..82f53edcd 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/api/CodesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/api/CodesApiResource.java @@ -85,7 +85,7 @@ public class CodesApiResource { @Path("{codeId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String retreiveOffice(@PathParam("codeId") final Long codeId, @Context final UriInfo uriInfo) { + public String retrieveCode(@PathParam("codeId") final Long codeId, @Context final UriInfo uriInfo) { final CodeData code = this.readPlatformService.retrieveCode(codeId); @@ -109,7 +109,7 @@ public class CodesApiResource { @Path("{codeId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String deleteCodeApplication(@PathParam("codeId") final Long codeId) { + public String deleteCode(@PathParam("codeId") final Long codeId) { final EntityIdentifier result = this.commandsSourceWritePlatformService.logCommandSource("DELETE", "CODE", "DELETE", "codes", codeId, "{}"); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java index 38c9634ad..612b889cc 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java @@ -10,4 +10,8 @@ public class CodeNotFoundException extends AbstractPlatformResourceNotFoundExcep public CodeNotFoundException(final String name) { super("error.msg.code.not.found", "Code with name `" + name + "` does not exist", name); } + + public CodeNotFoundException(final Long codeId) { + super("error.msg.code.identifier.not.found", "Code with identifier `" + codeId + "` does not exist", codeId); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java index 952a14c67..786bc1c53 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java @@ -5,9 +5,9 @@ import java.sql.SQLException; import java.util.Collection; import org.mifosplatform.infrastructure.codes.data.CodeData; +import org.mifosplatform.infrastructure.codes.exception.CodeNotFoundException; import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; -import org.mifosplatform.portfolio.fund.exception.FundNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -38,7 +38,7 @@ public class CodeReadPlatformServiceImpl implements CodeReadPlatformService { final Long id = rs.getLong("id"); final String code_name = rs.getString("code_name"); final boolean systemDefined = rs.getBoolean("systemDefined"); - + return new CodeData(id, code_name, systemDefined); } } @@ -54,7 +54,7 @@ public class CodeReadPlatformServiceImpl implements CodeReadPlatformService { } @Override - public CodeData retrieveCode(Long codeId) { + public CodeData retrieveCode(final Long codeId) { try { context.authenticatedUser(); @@ -63,7 +63,7 @@ public class CodeReadPlatformServiceImpl implements CodeReadPlatformService { return this.jdbcTemplate.queryForObject(sql, rm, new Object[] { codeId }); } catch (EmptyResultDataAccessException e) { - throw new FundNotFoundException(codeId); + throw new CodeNotFoundException(codeId); } } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/GoogleGsonPortfolioApiJsonSerializerService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/GoogleGsonPortfolioApiJsonSerializerService.java index 6c12cb2bb..9ae82027f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/GoogleGsonPortfolioApiJsonSerializerService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/GoogleGsonPortfolioApiJsonSerializerService.java @@ -6,13 +6,6 @@ import java.util.HashSet; import java.util.Set; import org.mifosplatform.infrastructure.core.serialization.GoogleGsonSerializerHelper; -import org.mifosplatform.infrastructure.dataqueries.data.DatatableData; -import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData; -import org.mifosplatform.infrastructure.documentmanagement.data.DocumentData; -import org.mifosplatform.infrastructure.security.data.AuthenticatedUserData; -import org.mifosplatform.portfolio.client.data.ClientAccountSummaryCollectionData; -import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; -import org.mifosplatform.portfolio.group.data.GroupData; import org.mifosplatform.portfolio.loanaccount.gaurantor.data.GuarantorData; import org.mifosplatform.portfolio.savingsaccount.data.SavingAccountData; import org.mifosplatform.portfolio.savingsaccount.data.SavingScheduleData; @@ -54,23 +47,6 @@ public class GoogleGsonPortfolioApiJsonSerializerService implements PortfolioApi "isLockinPeriodAllowed", "lockinPeriod", "lockinPeriodType", "printFDdetailsLocation", "availableInterest", "interestPostedAmount", "lastInterestPostedDate", "nextInterestPostedDate", "fatherName", "address", "imageKey")); - private static final Set DOCUMENT_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "parentEntityType", - "parentEntityId", "name", "fileName", "type", "size", "description")); - - private static final Set CLIENT_ACCOUNTS_DATA_PARAMETERS = new HashSet(Arrays.asList("pendingApprovalLoans", - "awaitingDisbursalLoans", "openLoans", "closedLoans", "anyLoanCount", "pendingApprovalLoanCount", "awaitingDisbursalLoanCount", - "activeLoanCount", "closedLoanCount", "pendingApprovalDespositAccountsCount", "pendingApprovalDespositAccounts", - "approvedDespositAccountsCount", "approvedDespositAccounts", "withdrawnByClientDespositAccountsCount", - "withdrawnByClientDespositAccounts", "closedDepositAccountsCount", "closedDepositAccounts", "rejectedDepositAccountsCount", - "rejectedDepositAccounts", "preclosedDepositAccountsCount", "preclosedDepositAccounts")); - - private static final Set GROUP_ACCOUNTS_DATA_PARAMETERS = new HashSet(Arrays.asList("pendingApprovalLoans", - "awaitingDisbursalLoans", "openLoans", "closedLoans", "anyLoanCount", "pendingApprovalLoanCount", "awaitingDisbursalLoanCount", - "activeLoanCount", "closedLoanCount")); - - private static final Set GROUP_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "officeId", "name", "externalId", - "clientMembers", "allowedClients", "allowedOffices")); - private static final Set SAVINGS_ACCOUNTS_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "status", "externalId", "clientId", "clientName", "productId", "productName", "productType", "currencyData", "savingsDepostiAmountPerPeriod", "savingsFrequencyType", "totalDepositAmount", "reccuringInterestRate", "savingInterestRate", "interestType", @@ -94,24 +70,6 @@ public class GoogleGsonPortfolioApiJsonSerializerService implements PortfolioApi this.helper = helper; } - @Override - public String serializeAuthenticatedUserDataToJson(final boolean prettyPrint, final AuthenticatedUserData authenticatedUserData) { - final Gson gsonDeserializer = helper.createGsonBuilder(prettyPrint); - return helper.serializedJsonFrom(gsonDeserializer, authenticatedUserData); - } - - @Override - public String serializeGenericResultsetDataToJson(final boolean prettyPrint, final GenericResultsetData resultsetData) { - final Gson gsonDeserializer = helper.createGsonBuilder(prettyPrint); - return helper.serializedJsonFrom(gsonDeserializer, resultsetData); - } - - @Override - public String serializeDatatableDataToJson(final boolean prettyPrint, final Collection datatables) { - final Gson gsonDeserializer = helper.createGsonBuilder(prettyPrint); - return helper.serializedJsonFrom(gsonDeserializer, datatables.toArray(new DatatableData[datatables.size()])); - } - @Override public String serializeSavingProductDataToJson(final boolean prettyPrint, final Set responseParameters, final Collection products) { @@ -160,51 +118,6 @@ public class GoogleGsonPortfolioApiJsonSerializerService implements PortfolioApi return helper.serializedJsonFrom(gsonDeserializer, account); } - @Override - public String serializeClientAccountSummaryCollectionDataToJson(final boolean prettyPrint, final Set responseParameters, - final ClientAccountSummaryCollectionData clientAccount) { - final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(CLIENT_ACCOUNTS_DATA_PARAMETERS, - prettyPrint, responseParameters); - return helper.serializedJsonFrom(gsonDeserializer, clientAccount); - } - - @Override - public String serializeGroupAccountSummaryCollectionDataToJson(final boolean prettyPrint, final Set responseParameters, - final GroupAccountSummaryCollectionData groupAccount) { - final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(GROUP_ACCOUNTS_DATA_PARAMETERS, - prettyPrint, responseParameters); - return helper.serializedJsonFrom(gsonDeserializer, groupAccount); - } - - @Override - public String serializeGroupDataToJson(final boolean prettyPrint, final Set responseParameters, - final Collection groups) { - final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(GROUP_DATA_PARAMETERS, - prettyPrint, responseParameters); - return helper.serializedJsonFrom(gsonDeserializer, groups.toArray(new GroupData[groups.size()])); - } - - @Override - public String serializeGroupDataToJson(final boolean prettyPrint, final Set responseParameters, final GroupData group) { - final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(GROUP_DATA_PARAMETERS, - prettyPrint, responseParameters); - return helper.serializedJsonFrom(gsonDeserializer, group); - } - - @Override - public String serializeDocumentDataToJson(boolean prettyPrint, Set responseParameters, Collection documentDatas) { - final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(DOCUMENT_DATA_PARAMETERS, - prettyPrint, responseParameters); - return helper.serializedJsonFrom(gsonDeserializer, documentDatas.toArray(new DocumentData[documentDatas.size()])); - } - - @Override - public String serializeDocumentDataToJson(boolean prettyPrint, Set responseParameters, DocumentData documentData) { - final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(DOCUMENT_DATA_PARAMETERS, - prettyPrint, responseParameters); - return helper.serializedJsonFrom(gsonDeserializer, documentData); - } - @Override public String serializeSavingAccountsDataToJson(boolean prettyPrint, Set responseParameters, SavingAccountData account) { final Gson gsonDeserializer = helper.createGsonBuilderWithParameterExclusionSerializationStrategy(SAVINGS_ACCOUNTS_DATA_PARAMETERS, @@ -234,5 +147,4 @@ public class GoogleGsonPortfolioApiJsonSerializerService implements PortfolioApi prettyPrint, responseParameters); return helper.serializedJsonFrom(gsonDeserializer, savingScheduleData); } - } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java index c3006ba42..9a0ff591c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionService.java @@ -1,6 +1,5 @@ package org.mifosplatform.infrastructure.core.api; -import org.mifosplatform.portfolio.client.command.NoteCommand; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.group.command.GroupCommand; import org.mifosplatform.portfolio.loanaccount.gaurantor.command.GuarantorCommand; @@ -23,8 +22,6 @@ public interface PortfolioApiDataConversionService { GroupCommand convertJsonToGroupCommand(Long resourceIdentifier, String json); - NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String json); - DepositAccountCommand convertJsonToDepositAccountCommand(Long resourceIdentifier, String json); DepositStateTransitionCommand convertJsonToDepositStateTransitionCommand(Long resourceIdentifier, String json); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java index 254973236..5ef034028 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiDataConversionServiceImpl.java @@ -22,7 +22,6 @@ import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.exception.UnsupportedParameterException; import org.mifosplatform.portfolio.client.command.ClientCommand; -import org.mifosplatform.portfolio.client.command.NoteCommand; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.client.serialization.ClientCommandFromApiJsonDeserializer; import org.mifosplatform.portfolio.group.command.GroupCommand; @@ -109,24 +108,6 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo return new GroupCommand(modifiedParameters, resourceIdentifier, externalId, name, officeId, clientMembers); } - @Override - public NoteCommand convertJsonToNoteCommand(final Long noteId, final Long clientId, final String json) { - if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } - - Type typeOfMap = new TypeToken>() {}.getType(); - Map requestMap = gsonConverter.fromJson(json, typeOfMap); - - Set supportedParams = new HashSet(Arrays.asList("note")); - - checkForUnsupportedParameters(requestMap, supportedParams); - - Set modifiedParameters = new HashSet(); - - String note = extractStringParameter("note", requestMap, modifiedParameters); - - return new NoteCommand(noteId, clientId, note); - } - private void checkForUnsupportedParameters(Map requestMap, Set supportedParams) { List unsupportedParameterList = new ArrayList(); for (String providedParameter : requestMap.keySet()) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiJsonSerializerService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiJsonSerializerService.java index 48d122a0c..28f603607 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiJsonSerializerService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/PortfolioApiJsonSerializerService.java @@ -3,13 +3,6 @@ package org.mifosplatform.infrastructure.core.api; import java.util.Collection; import java.util.Set; -import org.mifosplatform.infrastructure.dataqueries.data.DatatableData; -import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData; -import org.mifosplatform.infrastructure.documentmanagement.data.DocumentData; -import org.mifosplatform.infrastructure.security.data.AuthenticatedUserData; -import org.mifosplatform.portfolio.client.data.ClientAccountSummaryCollectionData; -import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; -import org.mifosplatform.portfolio.group.data.GroupData; import org.mifosplatform.portfolio.loanaccount.gaurantor.data.GuarantorData; import org.mifosplatform.portfolio.savingsaccount.data.SavingAccountData; import org.mifosplatform.portfolio.savingsaccount.data.SavingScheduleData; @@ -19,12 +12,6 @@ import org.mifosplatform.portfolio.savingsdepositproduct.data.DepositProductData public interface PortfolioApiJsonSerializerService { - String serializeAuthenticatedUserDataToJson(boolean prettyPrint, AuthenticatedUserData authenticatedUserData); - - String serializeGenericResultsetDataToJson(boolean prettyPrint, GenericResultsetData result); - - String serializeDatatableDataToJson(boolean prettyPrint, Collection result); - String serializeSavingProductDataToJson(boolean prettyPrint, Set responseParameters, Collection products); String serializeSavingProductDataToJson(boolean prettyPrint, Set responseParameters, SavingProductData savingProduct); @@ -37,20 +24,6 @@ public interface PortfolioApiJsonSerializerService { String serializeDepositAccountDataToJson(boolean prettyPrint, Set responseParameters, DepositAccountData account); - String serializeClientAccountSummaryCollectionDataToJson(boolean prettyPrint, Set responseParameters, - ClientAccountSummaryCollectionData clientAccount); - - String serializeGroupAccountSummaryCollectionDataToJson(boolean prettyPrint, Set responseParameters, - GroupAccountSummaryCollectionData groupAccount); - - String serializeGroupDataToJson(boolean prettyPrint, Set responseParameters, Collection groups); - - String serializeGroupDataToJson(boolean prettyPrint, Set responseParameters, GroupData group); - - String serializeDocumentDataToJson(boolean prettyPrint, Set responseParameters, Collection documentDatas); - - String serializeDocumentDataToJson(boolean prettyPrint, Set responseParameters, DocumentData documentData); - String serializeSavingAccountsDataToJson(boolean prettyPrint, Set responseParameters, SavingAccountData account); String serializeSavingAccountsDataToJson(boolean prettyPrint, Set responseParameters, Collection accounts); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/DefaultToApiJsonSerializer.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/DefaultToApiJsonSerializer.java index 6e242e9a6..127171e83 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/DefaultToApiJsonSerializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/DefaultToApiJsonSerializer.java @@ -41,6 +41,18 @@ public final class DefaultToApiJsonSerializer implements ToApiJsonSerializer< public String serialize(final Object object) { return this.excludeNothingWithPrettyPrintingOff.serialize(object); } + + @Override + public String serializePretty(boolean prettyOn, final Object object) { + String json = ""; + + if (prettyOn) { + json = this.excludeNothingWithPrettyPrintingOn.serialize(object); + } else { + json = serialize(object); + } + return json; + } @Override public String serialize(final ApiRequestJsonSerializationSettings settings, final Collection collection, diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/ToApiJsonSerializer.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/ToApiJsonSerializer.java index 73f89837b..cbfd8c757 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/ToApiJsonSerializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/serialization/ToApiJsonSerializer.java @@ -5,11 +5,13 @@ import java.util.Set; public interface ToApiJsonSerializer { - String serialize(final Object object); + String serialize(Object object); - String serializeResult(final Object object); + String serializePretty(boolean prettyOn, Object object); + + String serializeResult(Object object); - String serialize(final ApiRequestJsonSerializationSettings settings, Collection collection, Set supportedResponseParameters); + String serialize(ApiRequestJsonSerializationSettings settings, Collection collection, Set supportedResponseParameters); - String serialize(final ApiRequestJsonSerializationSettings settings, T single, Set supportedResponseParameters); + String serialize(ApiRequestJsonSerializationSettings settings, T single, Set supportedResponseParameters); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/DataTableApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/DataTableApiResource.java index de1e44232..f49e72c98 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/DataTableApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/DataTableApiResource.java @@ -24,8 +24,8 @@ import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.mifosplatform.infrastructure.core.api.ApiParameterHelper; -import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; +import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.dataqueries.data.DatatableData; import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData; import org.mifosplatform.infrastructure.dataqueries.service.GenericDataService; @@ -45,18 +45,20 @@ import org.springframework.stereotype.Component; public class DataTableApiResource { private final static Logger logger = LoggerFactory.getLogger(DataTableApiResource.class); + + private final PlatformSecurityContext context; + private final GenericDataService genericDataService; + private final ReadWriteNonCoreDataService readWriteNonCoreDataService; + private final ToApiJsonSerializer toApiJsonSerializer; @Autowired - private PlatformSecurityContext context; - - @Autowired - private GenericDataService genericDataService; - - @Autowired - private ReadWriteNonCoreDataService readWriteNonCoreDataService; - - @Autowired - private PortfolioApiJsonSerializerService apiJsonSerializerService; + public DataTableApiResource(final PlatformSecurityContext context, final GenericDataService genericDataService, + final ReadWriteNonCoreDataService readWriteNonCoreDataService, ToApiJsonSerializer toApiJsonSerializer) { + this.context = context; + this.genericDataService = genericDataService; + this.readWriteNonCoreDataService = readWriteNonCoreDataService; + this.toApiJsonSerializer = toApiJsonSerializer; + } @GET @Consumes({ MediaType.APPLICATION_JSON }) @@ -66,29 +68,29 @@ public class DataTableApiResource { final List result = this.readWriteNonCoreDataService.retrieveDatatableNames(apptable); final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - return this.apiJsonSerializerService.serializeDatatableDataToJson(prettyPrint, result); + return this.toApiJsonSerializer.serializePretty(prettyPrint, result); } @POST @Path("register/{datatable}/{apptable}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response registerDatatable(@PathParam("datatable") final String datatable, @PathParam("apptable") final String apptable) { + public String registerDatatable(@PathParam("datatable") final String datatable, @PathParam("apptable") final String apptable) { this.readWriteNonCoreDataService.registerDatatable(datatable, apptable); - return Response.ok().entity(EntityIdentifier.empty()).build(); + return this.toApiJsonSerializer.serialize(EntityIdentifier.empty()); } @POST @Path("deregister/{datatable}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response deregisterDatatable(@PathParam("datatable") final String datatable) { + public String deregisterDatatable(@PathParam("datatable") final String datatable) { this.readWriteNonCoreDataService.deregisterDatatable(datatable); - return Response.ok().entity(EntityIdentifier.empty()).build(); + return this.toApiJsonSerializer.serialize(EntityIdentifier.empty()); } @GET @@ -106,8 +108,8 @@ public class DataTableApiResource { String json = ""; final boolean genericResultSet = ApiParameterHelper.genericResultSet(uriInfo.getQueryParameters()); if (genericResultSet) { - boolean prettyPrints = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - json = this.apiJsonSerializerService.serializeGenericResultsetDataToJson(prettyPrints, results); + final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + json = this.toApiJsonSerializer.serializePretty(prettyPrint, results); } else { json = this.genericDataService.generateJsonFromGenericResultsetData(results); } @@ -119,7 +121,7 @@ public class DataTableApiResource { @Path("{datatable}/{apptableId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response newDatatableEntry(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId, + public String newDatatableEntry(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId, final String jsonRequestBody) { checkUserPermissionForDatatable(datatable, "CREATE"); @@ -127,17 +129,16 @@ public class DataTableApiResource { this.readWriteNonCoreDataService.newDatatableEntry(datatable, apptableId, queryParams); - EntityIdentifier entityIdentifier = new EntityIdentifier(Long.valueOf(apptableId)); - - return Response.ok().entity(entityIdentifier).build(); + EntityIdentifier entityIdentifier = EntityIdentifier.resourceResult(Long.valueOf(apptableId), null); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @PUT @Path("{datatable}/{apptableId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response updateDatatableEntryOnetoOne(@PathParam("datatable") final String datatable, + public String updateDatatableEntryOnetoOne(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId, final String jsonRequestBody) { /* * for updating one to one relationships (where foreign key is the @@ -148,17 +149,16 @@ public class DataTableApiResource { this.readWriteNonCoreDataService.updateDatatableEntryOnetoOne(datatable, apptableId, queryParams); - EntityIdentifier entityIdentifier = new EntityIdentifier(Long.valueOf(apptableId)); - - return Response.ok().entity(entityIdentifier).build(); + EntityIdentifier entityIdentifier = EntityIdentifier.resourceResult(Long.valueOf(apptableId), null); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @PUT @Path("{datatable}/{apptableId}/{datatableId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response updateDatatableEntryOnetoOne(@PathParam("datatable") final String datatable, + public String updateDatatableEntryOnetoOne(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId, @PathParam("datatableId") final Long datatableId, final String jsonRequestBody) { /* * for updating one to many relationships (where foreign key isn't the @@ -169,25 +169,24 @@ public class DataTableApiResource { this.readWriteNonCoreDataService.updateDatatableEntryOnetoMany(datatable, apptableId, datatableId, queryParams); - EntityIdentifier entityIdentifier = new EntityIdentifier(Long.valueOf(apptableId)); - - return Response.ok().entity(entityIdentifier).build(); + EntityIdentifier entityIdentifier = EntityIdentifier.resourceResult(Long.valueOf(apptableId), null); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @DELETE @Path("{datatable}/{apptableId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response deleteDatatableEntries(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId) { + public String deleteDatatableEntries(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId) { checkUserPermissionForDatatable(datatable, "DELETE"); this.readWriteNonCoreDataService.deleteDatatableEntries(datatable, apptableId); - EntityIdentifier entityIdentifier = new EntityIdentifier(Long.valueOf(apptableId)); + EntityIdentifier entityIdentifier = EntityIdentifier.resourceResult(Long.valueOf(apptableId), null); - return Response.ok().entity(entityIdentifier).build(); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @@ -195,20 +194,19 @@ public class DataTableApiResource { @Path("{datatable}/{apptableId}/{datatableId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response deleteDatatableEntries(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId, + public String deleteDatatableEntries(@PathParam("datatable") final String datatable, @PathParam("apptableId") final Long apptableId, @PathParam("datatableId") final Long datatableId) { checkUserPermissionForDatatable(datatable, "DELETE"); this.readWriteNonCoreDataService.deleteDatatableEntry(datatable, apptableId, datatableId); - EntityIdentifier entityIdentifier = new EntityIdentifier(Long.valueOf(apptableId)); - - return Response.ok().entity(entityIdentifier).build(); + EntityIdentifier entityIdentifier = EntityIdentifier.resourceResult(Long.valueOf(apptableId), null); + return this.toApiJsonSerializer.serialize(entityIdentifier); } - private Map getQueryParamsFromJsonRequestBody(String jsonRequestBody) { + private Map getQueryParamsFromJsonRequestBody(final String jsonRequestBody) { Map queryParams = new HashMap(); @@ -234,7 +232,7 @@ public class DataTableApiResource { } - private void checkUserPermissionForDatatable(String datatable, String accessType) { + private void checkUserPermissionForDatatable(final String datatable, final String accessType) { AppUser currentUser = context.authenticatedUser(); if (currentUser.hasNotPermissionForDatatable(datatable, accessType)) { throw new NoAuthorizationException("Not Authorised to " + accessType + " Data Table: " + datatable); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/ReportsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/ReportsApiResource.java index 9413b59fb..0b7ec480f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/ReportsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/api/ReportsApiResource.java @@ -19,7 +19,7 @@ import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.UriInfo; import org.mifosplatform.infrastructure.core.api.ApiParameterHelper; -import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerService; +import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData; import org.mifosplatform.infrastructure.dataqueries.service.ReadReportingService; import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException; @@ -34,14 +34,17 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class ReportsApiResource { - @Autowired - private PlatformSecurityContext context; + private final PlatformSecurityContext context; + private final ToApiJsonSerializer toApiJsonSerializer; + private final ReadReportingService readExtraDataAndReportingService; @Autowired - private ReadReportingService readExtraDataAndReportingService; - - @Autowired - private PortfolioApiJsonSerializerService apiJsonSerializerService; + public ReportsApiResource(final PlatformSecurityContext context, final ReadReportingService readExtraDataAndReportingService, + final ToApiJsonSerializer toApiJsonSerializer) { + this.context = context; + this.readExtraDataAndReportingService = readExtraDataAndReportingService; + this.toApiJsonSerializer = toApiJsonSerializer; + } @GET @Consumes({ MediaType.APPLICATION_JSON }) @@ -55,8 +58,7 @@ public class ReportsApiResource { boolean exportPdf = ApiParameterHelper.exportPdf(uriInfo.getQueryParameters()); - if (exportPdf) - { + if (exportPdf) { String result = this.readExtraDataAndReportingService.retrieveReportPDF(".", ".", extractedQueryParams); return Response.ok().entity(result).header("Content-Disposition", "attachment;filename=ReportList.pdf").build(); } @@ -64,7 +66,7 @@ public class ReportsApiResource { if (!exportCsv) { GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(".", ".", extractedQueryParams); - final String json = this.apiJsonSerializerService.serializeGenericResultsetDataToJson(prettyPrint, result); + final String json = this.toApiJsonSerializer.serializePretty(prettyPrint, result); return Response.ok().entity(json).build(); } @@ -123,7 +125,7 @@ public class ReportsApiResource { GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(reportName, parameterTypeValue, reportParams); - final String json = this.apiJsonSerializerService.serializeGenericResultsetDataToJson(prettyPrint, result); + final String json = this.toApiJsonSerializer.serializePretty(prettyPrint, result); return Response.ok().entity(json).type(MediaType.APPLICATION_JSON).build(); } @@ -136,7 +138,7 @@ public class ReportsApiResource { .header("Content-Disposition", "attachment;filename=" + reportName.replaceAll(" ", "") + ".csv").build(); } - private void checkUserPermissionForReport(String reportName, boolean parameterType) { + private void checkUserPermissionForReport(final String reportName, final boolean parameterType) { // Anyone can run a 'report' that is simply getting possible parameter // (dropdown listbox) values. diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/documentmanagement/api/DocumentManagementApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/documentmanagement/api/DocumentManagementApiResource.java index f07745e4f..194737d82 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/documentmanagement/api/DocumentManagementApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/documentmanagement/api/DocumentManagementApiResource.java @@ -2,6 +2,7 @@ package org.mifosplatform.infrastructure.documentmanagement.api; import java.io.File; import java.io.InputStream; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -22,9 +23,10 @@ import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.UriInfo; import org.mifosplatform.infrastructure.core.api.ApiConstants; -import org.mifosplatform.infrastructure.core.api.ApiParameterHelper; -import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerService; +import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; +import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; +import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.core.service.FileUtils; import org.mifosplatform.infrastructure.documentmanagement.command.DocumentCommand; import org.mifosplatform.infrastructure.documentmanagement.data.DocumentData; @@ -44,21 +46,26 @@ import com.sun.jersey.multipart.FormDataParam; @Scope("singleton") public class DocumentManagementApiResource { + private final Set RESPONSE_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "parentEntityType", "parentEntityId", + "name", "fileName", "type", "size", "description")); + private final String SystemEntityType = "DOCUMENT"; - + private final PlatformSecurityContext context; private final DocumentReadPlatformService documentReadPlatformService; private final DocumentWritePlatformService documentWritePlatformService; - private final PortfolioApiJsonSerializerService apiJsonSerializerService; + private final ApiRequestParameterHelper apiRequestParameterHelper; + private final ToApiJsonSerializer toApiJsonSerializer; @Autowired public DocumentManagementApiResource(final PlatformSecurityContext context, final DocumentReadPlatformService documentReadPlatformService, final DocumentWritePlatformService documentWritePlatformService, - final PortfolioApiJsonSerializerService apiJsonSerializerService) { + final ApiRequestParameterHelper apiRequestParameterHelper, final ToApiJsonSerializer toApiJsonSerializer) { this.context = context; this.documentReadPlatformService = documentReadPlatformService; this.documentWritePlatformService = documentWritePlatformService; - this.apiJsonSerializerService = apiJsonSerializerService; + this.apiRequestParameterHelper = apiRequestParameterHelper; + this.toApiJsonSerializer = toApiJsonSerializer; } @GET @@ -69,18 +76,16 @@ public class DocumentManagementApiResource { context.authenticatedUser().validateHasReadPermission(SystemEntityType); - final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - final Collection documentDatas = this.documentReadPlatformService.retrieveAllDocuments(entityType, entityId); - return this.apiJsonSerializerService.serializeDocumentDataToJson(prettyPrint, responseParameters, documentDatas); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.toApiJsonSerializer.serialize(settings, documentDatas, RESPONSE_DATA_PARAMETERS); } @POST @Consumes({ MediaType.MULTIPART_FORM_DATA }) @Produces({ MediaType.APPLICATION_JSON }) - public Response createDocument(@PathParam("entityType") String entityType, @PathParam("entityId") Long entityId, + public String createDocument(@PathParam("entityType") String entityType, @PathParam("entityId") Long entityId, @HeaderParam("Content-Length") Long fileSize, @FormDataParam("file") InputStream inputStream, @FormDataParam("file") FormDataContentDisposition fileDetails, @FormDataParam("file") FormDataBodyPart bodyPart, @FormDataParam("name") String name, @FormDataParam("description") String description) { @@ -101,14 +106,14 @@ public class DocumentManagementApiResource { Long documentId = this.documentWritePlatformService.createDocument(documentCommand, inputStream); - return Response.ok().entity(new EntityIdentifier(documentId)).build(); + return this.toApiJsonSerializer.serialize(EntityIdentifier.resourceResult(documentId, null)); } @PUT @Path("{documentId}") @Consumes({ MediaType.MULTIPART_FORM_DATA }) @Produces({ MediaType.APPLICATION_JSON }) - public Response updateDocument(@PathParam("entityType") String entityType, @PathParam("entityId") Long entityId, + public String updateDocument(@PathParam("entityType") String entityType, @PathParam("entityId") Long entityId, @PathParam("documentId") Long documentId, @HeaderParam("Content-Length") Long fileSize, @FormDataParam("file") InputStream inputStream, @FormDataParam("file") FormDataContentDisposition fileDetails, @FormDataParam("file") FormDataBodyPart bodyPart, @FormDataParam("name") String name, @@ -138,7 +143,7 @@ public class DocumentManagementApiResource { } EntityIdentifier identifier = this.documentWritePlatformService.updateDocument(documentCommand, inputStream); - return Response.ok().entity(identifier).build(); + return this.toApiJsonSerializer.serialize(identifier); } @GET @@ -150,12 +155,10 @@ public class DocumentManagementApiResource { context.authenticatedUser().validateHasReadPermission(SystemEntityType); - final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - final DocumentData documentData = this.documentReadPlatformService.retrieveDocument(entityType, entityId, documentId); - return this.apiJsonSerializerService.serializeDocumentDataToJson(prettyPrint, responseParameters, documentData); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.toApiJsonSerializer.serialize(settings, documentData, RESPONSE_DATA_PARAMETERS); } @GET @@ -172,6 +175,7 @@ public class DocumentManagementApiResource { ResponseBuilder response = Response.ok(file); response.header("Content-Disposition", "attachment; filename=\"" + documentData.fileName() + "\""); response.header("Content-Type", documentData.contentType()); + return response.build(); } @@ -179,13 +183,14 @@ public class DocumentManagementApiResource { @Path("{documentId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response deleteDocument(@PathParam("entityType") String entityType, @PathParam("entityId") Long entityId, + public String deleteDocument(@PathParam("entityType") String entityType, @PathParam("entityId") Long entityId, @PathParam("documentId") Long documentId) { - DocumentCommand documentCommand = new DocumentCommand(null, documentId, entityType, entityId, null, null, null, null, null, null); + final DocumentCommand documentCommand = new DocumentCommand(null, documentId, entityType, entityId, null, null, null, null, null, + null); - EntityIdentifier documentIdentifier = this.documentWritePlatformService.deleteDocument(documentCommand); + final EntityIdentifier documentIdentifier = this.documentWritePlatformService.deleteDocument(documentCommand); - return Response.ok(documentIdentifier).build(); + return this.toApiJsonSerializer.serialize(documentIdentifier); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/api/AuthenticationApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/api/AuthenticationApiResource.java index 66233383c..05828396d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/api/AuthenticationApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/api/AuthenticationApiResource.java @@ -10,7 +10,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerService; +import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.security.data.AuthenticatedUserData; import org.mifosplatform.useradministration.domain.AppUser; import org.springframework.beans.factory.annotation.Autowired; @@ -29,12 +29,16 @@ import com.sun.jersey.core.util.Base64; @Scope("singleton") public class AuthenticationApiResource { - @Qualifier("customAuthenticationProvider") - @Autowired - private DaoAuthenticationProvider customAuthenticationProvider; + private final DaoAuthenticationProvider customAuthenticationProvider; + private final ToApiJsonSerializer apiJsonSerializerService; @Autowired - private PortfolioApiJsonSerializerService apiJsonSerializerService; + public AuthenticationApiResource( + @Qualifier("customAuthenticationProvider") final DaoAuthenticationProvider customAuthenticationProvider, + final ToApiJsonSerializer apiJsonSerializerService) { + this.customAuthenticationProvider = customAuthenticationProvider; + this.apiJsonSerializerService = apiJsonSerializerService; + } @POST @Consumes({ MediaType.APPLICATION_JSON }) @@ -59,6 +63,6 @@ public class AuthenticationApiResource { base64EncodedAuthenticationKey)); } - return this.apiJsonSerializerService.serializeAuthenticatedUserDataToJson(false, authenticatedUserData); + return this.apiJsonSerializerService.serialize(authenticatedUserData); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java index 26ecb808b..acf67c803 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/api/ClientsApiResource.java @@ -27,10 +27,9 @@ import org.mifosplatform.commands.service.PortfolioCommandsReadPlatformService; import org.mifosplatform.infrastructure.core.api.ApiParameterHelper; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; import org.mifosplatform.infrastructure.core.api.PortfolioApiDataConversionService; -import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; -import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; +import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.office.data.OfficeLookup; import org.mifosplatform.organisation.office.service.OfficeReadPlatformService; @@ -52,27 +51,27 @@ public class ClientsApiResource { private final PlatformSecurityContext context; private final ClientReadPlatformService clientReadPlatformService; private final OfficeReadPlatformService officeReadPlatformService; - private final DefaultToApiJsonSerializer toApiJsonSerializer; + private final ToApiJsonSerializer toApiJsonSerializer; + private final ToApiJsonSerializer clientAccountSummaryToApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; private final PortfolioApiDataConversionService apiDataConversionService; - private final PortfolioApiJsonSerializerService apiJsonSerializerService; private final PortfolioCommandsReadPlatformService commandSourceReadPlatformService; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; @Autowired public ClientsApiResource(final PlatformSecurityContext context, final ClientReadPlatformService readPlatformService, - final OfficeReadPlatformService officeReadPlatformService, final DefaultToApiJsonSerializer toApiJsonSerializer, + final OfficeReadPlatformService officeReadPlatformService, final ToApiJsonSerializer toApiJsonSerializer, + final ToApiJsonSerializer clientAccountSummaryToApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioApiDataConversionService apiDataConversionService, - final PortfolioApiJsonSerializerService apiJsonSerializerService, final PortfolioCommandsReadPlatformService commandsReadPlatformService, final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { this.context = context; this.clientReadPlatformService = readPlatformService; this.officeReadPlatformService = officeReadPlatformService; this.toApiJsonSerializer = toApiJsonSerializer; + this.clientAccountSummaryToApiJsonSerializer = clientAccountSummaryToApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; this.apiDataConversionService = apiDataConversionService; - this.apiJsonSerializerService = apiJsonSerializerService; this.commandSourceReadPlatformService = commandsReadPlatformService; this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; } @@ -251,12 +250,17 @@ public class ClientsApiResource { context.authenticatedUser().validateHasReadPermission("CLIENT"); - final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - final ClientAccountSummaryCollectionData clientAccount = this.clientReadPlatformService.retrieveClientAccountDetails(clientId); - return this.apiJsonSerializerService.serializeClientAccountSummaryCollectionDataToJson(prettyPrint, responseParameters, - clientAccount); + final Set CLIENT_ACCOUNTS_DATA_PARAMETERS = new HashSet(Arrays.asList("pendingApprovalLoans", + "awaitingDisbursalLoans", "openLoans", "closedLoans", "anyLoanCount", "pendingApprovalLoanCount", + "awaitingDisbursalLoanCount", "activeLoanCount", "closedLoanCount", "pendingApprovalDespositAccountsCount", + "pendingApprovalDespositAccounts", "approvedDespositAccountsCount", "approvedDespositAccounts", + "withdrawnByClientDespositAccountsCount", "withdrawnByClientDespositAccounts", "closedDepositAccountsCount", + "closedDepositAccounts", "rejectedDepositAccountsCount", "rejectedDepositAccounts", "preclosedDepositAccountsCount", + "preclosedDepositAccounts")); + + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.clientAccountSummaryToApiJsonSerializer.serialize(settings, clientAccount, CLIENT_ACCOUNTS_DATA_PARAMETERS); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java index 96586d958..271b084bd 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java @@ -16,14 +16,15 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.apache.commons.lang.StringUtils; import org.mifosplatform.infrastructure.core.api.ApiParameterHelper; +import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; import org.mifosplatform.infrastructure.core.api.PortfolioApiDataConversionService; -import org.mifosplatform.infrastructure.core.api.PortfolioApiJsonSerializerService; import org.mifosplatform.infrastructure.core.data.EntityIdentifier; +import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; +import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.office.data.OfficeLookup; import org.mifosplatform.organisation.office.service.OfficeReadPlatformService; @@ -43,29 +44,40 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class GroupsApiResource { - @Autowired + private static final Set GROUP_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "officeId", "name", "externalId", + "clientMembers", "allowedClients", "allowedOffices")); + + private final PlatformSecurityContext context; private GroupReadPlatformService groupReadPlatformService; + private final GroupWritePlatformService groupWritePlatformService; + private final ClientReadPlatformService clientReadPlatformService; + private final OfficeReadPlatformService officeReadPlatformService; + private final ToApiJsonSerializer toApiJsonSerializer; + private final ToApiJsonSerializer groupSummaryToApiJsonSerializer; + private final ApiRequestParameterHelper apiRequestParameterHelper; + private final PortfolioApiDataConversionService apiDataConversionService; @Autowired - private GroupWritePlatformService groupWritePlatformService; - - @Autowired - private ClientReadPlatformService clientReadPlatformService; - - @Autowired - private PortfolioApiDataConversionService apiDataConversionService; - - @Autowired - private OfficeReadPlatformService officeReadPlatformService; - - @Autowired - private PortfolioApiJsonSerializerService apiJsonSerializerService; - - @Autowired - private PlatformSecurityContext context; - - private static final Set typicalResponseParameters = new HashSet(Arrays.asList("id", "officeId", "name", "externalId", - "clientMembers")); + public GroupsApiResource( + final PlatformSecurityContext context, + final GroupReadPlatformService groupReadPlatformService, + final GroupWritePlatformService groupWritePlatformService, + final ClientReadPlatformService clientReadPlatformService, + final OfficeReadPlatformService officeReadPlatformService, + final ToApiJsonSerializer toApiJsonSerializer, + final ToApiJsonSerializer groupSummaryToApiJsonSerializer, + final ApiRequestParameterHelper apiRequestParameterHelper, + final PortfolioApiDataConversionService apiDataConversionService) { + this.context = context; + this.groupReadPlatformService = groupReadPlatformService; + this.groupWritePlatformService = groupWritePlatformService; + this.clientReadPlatformService = clientReadPlatformService; + this.officeReadPlatformService = officeReadPlatformService; + this.toApiJsonSerializer = toApiJsonSerializer; + this.groupSummaryToApiJsonSerializer = groupSummaryToApiJsonSerializer; + this.apiRequestParameterHelper = apiRequestParameterHelper; + this.apiDataConversionService = apiDataConversionService; + } @GET @Consumes({ MediaType.APPLICATION_JSON }) @@ -77,17 +89,10 @@ public class GroupsApiResource { context.authenticatedUser().validateHasReadPermission("GROUP"); final String extraCriteria = getGroupExtraCriteria(sqlSearch, officeId, externalId, name, hierarchy); - - Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - if (responseParameters.isEmpty()) { - responseParameters.addAll(typicalResponseParameters); - } - - boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - Collection groups = this.groupReadPlatformService.retrieveAllGroups(extraCriteria); - return this.apiJsonSerializerService.serializeGroupDataToJson(prettyPrint, responseParameters, groups); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.toApiJsonSerializer.serialize(settings, groups, GROUP_DATA_PARAMETERS); } @GET @@ -99,13 +104,6 @@ public class GroupsApiResource { context.authenticatedUser().validateHasReadPermission("GROUP"); - Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - if (responseParameters.isEmpty()) { - responseParameters.addAll(typicalResponseParameters); - } - - boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - GroupData group = this.groupReadPlatformService.retrieveGroup(groupId); final Collection clientMembers = this.groupReadPlatformService.retrieveClientMembers(groupId); Collection availableClients = null; @@ -115,9 +113,6 @@ public class GroupsApiResource { boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); if (template) { - responseParameters.add("allowedClients"); - responseParameters.add("allowedOffices"); - if (officeId != null) { availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookupByOfficeId(officeId); } else { @@ -131,7 +126,8 @@ public class GroupsApiResource { group = new GroupData(group, group.clientMembers(), availableClients, allowedOffices); } - return this.apiJsonSerializerService.serializeGroupDataToJson(prettyPrint, responseParameters, group); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.toApiJsonSerializer.serialize(settings, group, GROUP_DATA_PARAMETERS); } @GET @@ -142,58 +138,51 @@ public class GroupsApiResource { context.authenticatedUser().validateHasReadPermission("GROUP"); - Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - if (responseParameters.isEmpty()) { - responseParameters.addAll(typicalResponseParameters); - } - boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - - responseParameters.addAll(Arrays.asList("officeId", "allowedClients", "allowedOffices")); - - GroupData groupData; + GroupData groupTemplateData; if (officeId != null) { - groupData = this.groupReadPlatformService.retrieveNewGroupDetails(officeId); + groupTemplateData = this.groupReadPlatformService.retrieveNewGroupDetails(officeId); } else { - groupData = this.groupReadPlatformService.retrieveNewGroupDetails(context.authenticatedUser().getOffice().getId()); + groupTemplateData = this.groupReadPlatformService.retrieveNewGroupDetails(context.authenticatedUser().getOffice().getId()); } - return this.apiJsonSerializerService.serializeGroupDataToJson(prettyPrint, responseParameters, groupData); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.toApiJsonSerializer.serialize(settings, groupTemplateData, GROUP_DATA_PARAMETERS); } @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response createGroup(final String jsonRequestBody) { + public String createGroup(final String jsonRequestBody) { final GroupCommand command = this.apiDataConversionService.convertJsonToGroupCommand(null, jsonRequestBody); EntityIdentifier entityIdentifier = this.groupWritePlatformService.createGroup(command); - return Response.ok().entity(entityIdentifier).build(); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @PUT @Path("{groupId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response updateGroup(@PathParam("groupId") final Long groupId, final String jsonRequestBody) { + public String updateGroup(@PathParam("groupId") final Long groupId, final String jsonRequestBody) { final GroupCommand command = this.apiDataConversionService.convertJsonToGroupCommand(groupId, jsonRequestBody); EntityIdentifier entityIdentifier = this.groupWritePlatformService.updateGroup(command); - return Response.ok().entity(entityIdentifier).build(); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @DELETE @Path("{groupId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public Response deleteGroup(@PathParam("groupId") final Long groupId) { + public String deleteGroup(@PathParam("groupId") final Long groupId) { EntityIdentifier entityIdentifier = this.groupWritePlatformService.deleteGroup(groupId); - return Response.ok().entity(entityIdentifier).build(); + return this.toApiJsonSerializer.serialize(entityIdentifier); } @GET @@ -204,20 +193,14 @@ public class GroupsApiResource { context.authenticatedUser().validateHasReadPermission("GROUP"); - Set typicalResponseParameters = new HashSet(Arrays.asList("pendingApprovalLoans", "awaitingDisbursalLoans", - "openLoans", "closedLoans", "anyLoanCount", "pendingApprovalLoanCount", "awaitingDisbursalLoanCount", "activeLoanCount", - "closedLoanCount")); + GroupAccountSummaryCollectionData groupAccount = this.groupReadPlatformService.retrieveGroupAccountDetails(groupId); + + final Set GROUP_ACCOUNTS_DATA_PARAMETERS = new HashSet(Arrays.asList("pendingApprovalLoans", + "awaitingDisbursalLoans", "openLoans", "closedLoans", "anyLoanCount", "pendingApprovalLoanCount", "awaitingDisbursalLoanCount", + "activeLoanCount", "closedLoanCount")); - Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - if (responseParameters.isEmpty()) { - responseParameters.addAll(typicalResponseParameters); - } - boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - - GroupAccountSummaryCollectionData clientAccount = this.groupReadPlatformService.retrieveGroupAccountDetails(groupId); - - return this.apiJsonSerializerService.serializeGroupAccountSummaryCollectionDataToJson(prettyPrint, responseParameters, - clientAccount); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.groupSummaryToApiJsonSerializer.serialize(settings, groupAccount, GROUP_ACCOUNTS_DATA_PARAMETERS); } // 'g.' preffix because of ERROR 1052 (23000): Column 'column_name' in where