diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java index a194364c5..1bd4ffd9f 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java @@ -1,7 +1,10 @@ package org.mifosng.platform.api; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -25,7 +28,7 @@ import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.NoteData; import org.mifosng.platform.api.data.OfficeLookup; import org.mifosng.platform.api.infrastructure.ApiDataConversionService; -import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; +import org.mifosng.platform.api.infrastructure.ApiParameterHelper; import org.mifosng.platform.client.service.ClientReadPlatformService; import org.mifosng.platform.client.service.ClientWritePlatformService; import org.mifosng.platform.organisation.service.OfficeReadPlatformService; @@ -40,12 +43,7 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class ClientsApiResource { - private final static Logger logger = LoggerFactory - .getLogger(ClientsApiResource.class); - - private String defaultFieldList = "joinedDate"; - private String allowedFieldList = "allowedOffices"; - private String filterName = "myFilter"; + private final static Logger logger = LoggerFactory.getLogger(ClientsApiResource.class); @Autowired private ClientReadPlatformService clientReadPlatformService; @@ -59,9 +57,6 @@ public class ClientsApiResource { @Autowired private OfficeReadPlatformService officeReadPlatformService; - @Autowired - private ApiJSONFormattingService jsonFormattingService; - @GET @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) @@ -73,15 +68,21 @@ public class ClientsApiResource { @QueryParam("firstName") final String firstName, @QueryParam("lastName") final String lastName) { - String extraCriteria = getClientCriteria(sqlSearch, officeId, - externalId, displayName, firstName, lastName); + final String extraCriteria = getClientCriteria(sqlSearch, officeId, externalId, displayName, firstName, lastName); + + Set typicalResponseParameters = new HashSet( + Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName") + ); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - Collection clients = this.clientReadPlatformService - .retrieveAllIndividualClients(extraCriteria); - - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(clients, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + Collection clients = this.clientReadPlatformService.retrieveAllIndividualClients(extraCriteria); + + return this.apiDataConversionService.convertClientDataToJson(prettyPrint, responseParameters, clients.toArray(new ClientData[clients.size()])); } private String getClientCriteria(String sqlSearch, Integer officeId, @@ -129,20 +130,26 @@ public class ClientsApiResource { @Produces({MediaType.APPLICATION_JSON}) public String retrieveClientData( @PathParam("clientId") final Long clientId, - @QueryParam("template") final String template, @Context final UriInfo uriInfo) { - - ClientData clientData = this.clientReadPlatformService - .retrieveIndividualClient(clientId); - - if (template != null && template.equalsIgnoreCase("true")) { - clientData.setAllowedOffices(new ArrayList( - officeReadPlatformService.retrieveAllOfficesForLookup())); + @Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet( + Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName") + ); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); + + ClientData clientData = this.clientReadPlatformService.retrieveIndividualClient(clientId); + if (template) { + clientData.setAllowedOffices(new ArrayList(officeReadPlatformService.retrieveAllOfficesForLookup())); + responseParameters.add("allowedOffices"); } - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(clientData, - filterName, allowedFieldList, selectedFields, - uriInfo.getQueryParameters()); + return this.apiDataConversionService.convertClientDataToJson(prettyPrint, responseParameters, clientData); } @GET @@ -150,14 +157,20 @@ public class ClientsApiResource { @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public String newClientDetails(@Context UriInfo uriInfo) { - - ClientData clientData = this.clientReadPlatformService - .retrieveNewClientDetails(); - - String selectedFields = defaultFieldList + "," + allowedFieldList; - return this.jsonFormattingService.convertRequest(clientData, - filterName, allowedFieldList, selectedFields, - uriInfo.getQueryParameters()); + + Set typicalResponseParameters = new HashSet( + Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName", "allowedOffices") + ); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + ClientData clientData = this.clientReadPlatformService.retrieveNewClientDetails(); + + return this.apiDataConversionService.convertClientDataToJson(prettyPrint, responseParameters, clientData); } @POST @@ -189,16 +202,22 @@ public class ClientsApiResource { @Path("{clientId}/loans") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public String retrieveClientAccount( - @PathParam("clientId") final Long clientId, @Context final UriInfo uriInfo) { + public String retrieveClientAccount(@PathParam("clientId") final Long clientId, + @Context final UriInfo uriInfo) { - ClientLoanAccountSummaryCollectionData clientAccount = this.clientReadPlatformService - .retrieveClientAccountDetails(clientId); - - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(clientAccount, - filterName, allowedFieldList, selectedFields, - uriInfo.getQueryParameters()); + Set typicalResponseParameters = new HashSet( + Arrays.asList("pendingApprovalLoans", "awaitingDisbursalLoans", "openLoans", "closedLoans") + ); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + ClientLoanAccountSummaryCollectionData clientAccount = this.clientReadPlatformService.retrieveClientAccountDetails(clientId); + + return this.apiDataConversionService.convertClientLoanAccountSummaryCollectionDataToJson(prettyPrint, responseParameters, clientAccount); } @GET @@ -207,12 +226,20 @@ public class ClientsApiResource { @Produces({MediaType.APPLICATION_JSON}) public String retrieveAllClientNotes(@PathParam("clientId") final Long clientId, @Context final UriInfo uriInfo) { - Collection notes = this.clientReadPlatformService - .retrieveAllClientNotes(clientId); - - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(notes, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + Set typicalResponseParameters = new HashSet( + Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername", + "createdOn", "updatedById", "updatedByUsername", "updatedOn") + ); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + Collection notes = this.clientReadPlatformService.retrieveAllClientNotes(clientId); + + return this.apiDataConversionService.convertNoteDataToJson(prettyPrint, responseParameters, notes.toArray(new NoteData[notes.size()])); } @POST @@ -234,13 +261,23 @@ public class ClientsApiResource { @Produces({MediaType.APPLICATION_JSON}) public String retrieveClientNote( @PathParam("clientId") final Long clientId, - @PathParam("noteId") final Long noteId, @Context final UriInfo uriInfo) { - + @PathParam("noteId") final Long noteId, + @Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet( + Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername", + "createdOn", "updatedById", "updatedByUsername", "updatedOn") + ); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId); - - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(note, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + + return this.apiDataConversionService.convertNoteDataToJson(prettyPrint, responseParameters, note); } @PUT @@ -253,8 +290,7 @@ public class ClientsApiResource { NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(noteId, clientId, jsonRequestBody); - EntityIdentifier identifier = this.clientWritePlatformService - .updateNote(command); + EntityIdentifier identifier = this.clientWritePlatformService.updateNote(command); return Response.ok().entity(identifier).build(); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ConfigurationApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ConfigurationApiResource.java index 3459610c9..2618d214c 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/ConfigurationApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ConfigurationApiResource.java @@ -1,5 +1,9 @@ package org.mifosng.platform.api; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.PUT; @@ -13,7 +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.ApiJSONFormattingService; +import org.mifosng.platform.api.infrastructure.ApiParameterHelper; import org.mifosng.platform.configuration.service.ConfigurationReadPlatformService; import org.mifosng.platform.configuration.service.ConfigurationWritePlatformService; import org.springframework.beans.factory.annotation.Autowired; @@ -25,9 +29,6 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class ConfigurationApiResource { - private String allowedFieldList = ""; - private String filterName = "myFilter"; - @Autowired private ConfigurationReadPlatformService configurationReadPlatformService; @@ -37,22 +38,24 @@ public class ConfigurationApiResource { @Autowired private ApiDataConversionService apiDataConversionService; - @Autowired - private ApiJSONFormattingService jsonFormattingService; - @GET @Path("currency") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public String retrieveCurrencyDataForConfiguration(@Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet( + Arrays.asList("selectedCurrencyOptions", "currencyOptions")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - ConfigurationData configurationData = this.configurationReadPlatformService - .retrieveCurrencyConfiguration(); - - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(configurationData, - filterName, allowedFieldList, selectedFields, - uriInfo.getQueryParameters()); + ConfigurationData configurationData = this.configurationReadPlatformService.retrieveCurrencyConfiguration(); + + return this.apiDataConversionService.convertConfigurationDataToJson(prettyPrint, responseParameters, configurationData); } @PUT diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientData.java index b284fe597..6242e3530 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientData.java @@ -1,31 +1,24 @@ package org.mifosng.platform.api.data; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; -import org.codehaus.jackson.map.annotate.JsonFilter; import org.joda.time.LocalDate; -@JsonFilter("myFilter") -public class ClientData implements Serializable { +public class ClientData { - private Long officeId; - private String officeName; - private Long id; - private String firstname; - private String lastname; - private String displayName; - private String externalId; - private LocalDate joinedDate; + private final Long officeId; + private final String officeName; + private final Long id; + private final String firstname; + private final String lastname; + private final String displayName; + private final String externalId; + private final LocalDate joinedDate; private List allowedOffices = new ArrayList(); - public ClientData() { - // - } - public ClientData(final Long officeId, final String officeName, final Long id, final String firstname, final String lastname, final String externalId, final LocalDate joinedDate) { @@ -46,68 +39,48 @@ public class ClientData implements Serializable { this.joinedDate = joinedDate; } - public Long getId() { - return this.id; - } - - public String getFirstname() { - return this.firstname; - } - - public String getLastname() { - return this.lastname; - } - - public String getExternalId() { - return this.externalId; + public ClientData(final Long officeId, final LocalDate joinedDate, final List offices) { + this.id = null; + this.officeId = officeId; + this.officeName = null; + this.firstname = null; + this.lastname = null; + this.displayName = null; + this.externalId = null; + this.joinedDate = joinedDate; + this.allowedOffices = offices; } public Long getOfficeId() { - return this.officeId; + return officeId; } public String getOfficeName() { - return this.officeName; + return officeName; } - public LocalDate getJoinedDate() { - return this.joinedDate; + public Long getId() { + return id; } - public void setOfficeId(final Long officeId) { - this.officeId = officeId; + public String getFirstname() { + return firstname; } - public void setOfficeName(final String officeName) { - this.officeName = officeName; - } - - public void setId(final Long id) { - this.id = id; - } - - public void setFirstname(final String firstname) { - this.firstname = firstname; - } - - public void setLastname(final String lastname) { - this.lastname = lastname; - } - - public void setExternalId(final String externalId) { - this.externalId = externalId; - } - - public void setJoinedDate(final LocalDate joinedDate) { - this.joinedDate = joinedDate; + public String getLastname() { + return lastname; } public String getDisplayName() { - return this.displayName; + return displayName; } - public void setDisplayName(final String displayName) { - this.displayName = displayName; + public String getExternalId() { + return externalId; + } + + public LocalDate getJoinedDate() { + return joinedDate; } public List getAllowedOffices() { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientLoanAccountSummaryCollectionData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientLoanAccountSummaryCollectionData.java index 0885cd87f..33678252b 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientLoanAccountSummaryCollectionData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ClientLoanAccountSummaryCollectionData.java @@ -3,20 +3,18 @@ package org.mifosng.platform.api.data; import java.util.ArrayList; import java.util.List; -import org.codehaus.jackson.map.annotate.JsonFilter; - -@JsonFilter("myFilter") public class ClientLoanAccountSummaryCollectionData { private List pendingApprovalLoans = new ArrayList(); private List awaitingDisbursalLoans = new ArrayList(); private List openLoans = new ArrayList(); private List closedLoans = new ArrayList(); - - public ClientLoanAccountSummaryCollectionData() { - // - } - + private int anyLoanCount; + private int pendingApprovalLoanCount; + private int awaitingDisbursalLoanCount; + private int activeLoanCount; + private int closedLoanCount; + public ClientLoanAccountSummaryCollectionData( final List pendingApprovalLoans, final List awaitingDisbursalLoans, @@ -26,60 +24,46 @@ public class ClientLoanAccountSummaryCollectionData { this.awaitingDisbursalLoans = awaitingDisbursalLoans; this.openLoans = openLoans; this.closedLoans = closedLoans; - } - - public Integer getAnyLoanCount() { - return getPendingApprovalLoanCount() + getAwaitingDisbursalLoanCount() - + getActiveLoanCount() + getClosedLoanCount(); - } - - public Integer getPendingApprovalLoanCount() { - return this.pendingApprovalLoans.size(); - } - - public Integer getAwaitingDisbursalLoanCount() { - return this.awaitingDisbursalLoans.size(); - } - - public Integer getActiveLoanCount() { - return this.openLoans.size(); - } - - public Integer getClosedLoanCount() { - return this.closedLoans.size(); + this.pendingApprovalLoanCount = this.pendingApprovalLoans.size(); + this.awaitingDisbursalLoanCount = this.awaitingDisbursalLoans.size(); + this.activeLoanCount = this.openLoans.size(); + this.closedLoanCount = this.closedLoans.size(); + this.anyLoanCount = this.pendingApprovalLoanCount + this.awaitingDisbursalLoanCount + this.activeLoanCount + this.closedLoanCount; } public List getPendingApprovalLoans() { return pendingApprovalLoans; } - public void setPendingApprovalLoans( - List pendingApprovalLoans) { - this.pendingApprovalLoans = pendingApprovalLoans; - } - public List getAwaitingDisbursalLoans() { return awaitingDisbursalLoans; } - public void setAwaitingDisbursalLoans( - List awaitingDisbursalLoans) { - this.awaitingDisbursalLoans = awaitingDisbursalLoans; - } - public List getOpenLoans() { return openLoans; } - public void setOpenLoans(List openLoans) { - this.openLoans = openLoans; - } - public List getClosedLoans() { return closedLoans; } - public void setClosedLoans(List closedLoans) { - this.closedLoans = closedLoans; + public int getAnyLoanCount() { + return anyLoanCount; + } + + public int getPendingApprovalLoanCount() { + return pendingApprovalLoanCount; + } + + public int getAwaitingDisbursalLoanCount() { + return awaitingDisbursalLoanCount; + } + + public int getActiveLoanCount() { + return activeLoanCount; + } + + public int getClosedLoanCount() { + return closedLoanCount; } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ConfigurationData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ConfigurationData.java index 9e922029a..f6a88a142 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ConfigurationData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/ConfigurationData.java @@ -1,35 +1,22 @@ package org.mifosng.platform.api.data; -import java.io.Serializable; -import java.util.ArrayList; import java.util.List; -import org.codehaus.jackson.map.annotate.JsonFilter; +public class ConfigurationData { -@JsonFilter("myFilter") -public class ConfigurationData implements Serializable { + private final List selectedCurrencyOptions; + private final List currencyOptions; - private List selectedCurrencyOptions = new ArrayList(); - private List currencyOptions = new ArrayList(); - - public ConfigurationData() { - // + public ConfigurationData(final List currencyOptions, final List selectedCurrencyOptions) { + this.currencyOptions = currencyOptions; + this.selectedCurrencyOptions = selectedCurrencyOptions; } public List getSelectedCurrencyOptions() { return selectedCurrencyOptions; } - public void setSelectedCurrencyOptions( - List selectedCurrencyOptions) { - this.selectedCurrencyOptions = selectedCurrencyOptions; - } - public List getCurrencyOptions() { return currencyOptions; } - - public void setCurrencyOptions(List currencyOptions) { - this.currencyOptions = currencyOptions; - } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/NoteData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/NoteData.java index e5f52dbe2..6cbedab18 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/NoteData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/NoteData.java @@ -1,9 +1,7 @@ package org.mifosng.platform.api.data; -import org.codehaus.jackson.map.annotate.JsonFilter; import org.joda.time.DateTime; -@JsonFilter("myFilter") public class NoteData { private Long id; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java index e4209e8ad..c139ddb07 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java @@ -16,7 +16,11 @@ import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.data.AppUserData; +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.FundData; +import org.mifosng.platform.api.data.NoteData; import org.mifosng.platform.api.data.OfficeData; import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.PermissionData; @@ -32,6 +36,15 @@ public interface ApiDataConversionService { String convertAppUserDataToJson(boolean prettyPrint, Set responseParameters, AppUserData... users); String convertOfficeTransactionDataToJson(boolean prettyPrint, Set responseParameters, OfficeTransactionData... officeTransactions); + + String convertConfigurationDataToJson(boolean prettyPrint, Set responseParameters, ConfigurationData... configuration); + + String convertClientDataToJson(boolean prettyPrint, Set responseParameters, ClientData... clients); + + String convertClientLoanAccountSummaryCollectionDataToJson(boolean prettyPrint, Set responseParameters, + ClientLoanAccountSummaryCollectionData clientAccountsData); + + String convertNoteDataToJson(boolean prettyPrint, Set responseParameters, NoteData... notes); // FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index 2874ff6cc..87a8efb21 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -15,6 +15,7 @@ import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; +import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat; import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; @@ -32,7 +33,11 @@ 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.AppUserData; +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.FundData; +import org.mifosng.platform.api.data.NoteData; import org.mifosng.platform.api.data.OfficeData; import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.PermissionData; @@ -62,6 +67,144 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { gsonConverter = new Gson(); } + @Override + public String convertNoteDataToJson(final boolean prettyPrint, final Set responseParameters, final NoteData... notes) { + Set supportedParameters = new HashSet( + Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername", + "createdOn", "updatedById", "updatedByUsername", "updatedOn") + ); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + if (!supportedParameters.containsAll(responseParameters)) { + throw new UnsupportedParameterException(new ArrayList(responseParameters)); + } + + parameterNamesToSkip.addAll(supportedParameters); + parameterNamesToSkip.removeAll(responseParameters); + } + + ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); + + GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); + builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); + builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); + if (prettyPrint) { + builder.setPrettyPrinting(); + } + Gson gsonDeserializer = builder.create(); + + String json = ""; + if (notes != null && notes.length == 1) { + json = gsonDeserializer.toJson(notes[0]); + } else { + json = gsonDeserializer.toJson(notes); + } + + return json; + } + + @Override + public String convertClientLoanAccountSummaryCollectionDataToJson(final boolean prettyPrint, final Set responseParameters, + final ClientLoanAccountSummaryCollectionData clientAccountsData) { + Set supportedParameters = new HashSet( + Arrays.asList("pendingApprovalLoans", "awaitingDisbursalLoans", "openLoans", "closedLoans") + ); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + if (!supportedParameters.containsAll(responseParameters)) { + throw new UnsupportedParameterException(new ArrayList(responseParameters)); + } + + parameterNamesToSkip.addAll(supportedParameters); + parameterNamesToSkip.removeAll(responseParameters); + } + + ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); + + GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); + builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); + if (prettyPrint) { + builder.setPrettyPrinting(); + } + Gson gsonDeserializer = builder.create(); + + return gsonDeserializer.toJson(clientAccountsData); + } + + @Override + public String convertClientDataToJson(final boolean prettyPrint, final Set responseParameters, final ClientData... clients) { + Set supportedParameters = new HashSet( + Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName", "allowedOffices") + ); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + if (!supportedParameters.containsAll(responseParameters)) { + throw new UnsupportedParameterException(new ArrayList(responseParameters)); + } + + parameterNamesToSkip.addAll(supportedParameters); + parameterNamesToSkip.removeAll(responseParameters); + } + + ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); + + GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); + builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); + if (prettyPrint) { + builder.setPrettyPrinting(); + } + Gson gsonDeserializer = builder.create(); + + String json = ""; + if (clients != null && clients.length == 1) { + json = gsonDeserializer.toJson(clients[0]); + } else { + json = gsonDeserializer.toJson(clients); + } + + return json; + } + + @Override + public String convertConfigurationDataToJson(final boolean prettyPrint, final Set responseParameters, final ConfigurationData... configuration) { + Set supportedParameters = new HashSet(Arrays.asList("selectedCurrencyOptions", "currencyOptions")); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + if (!supportedParameters.containsAll(responseParameters)) { + throw new UnsupportedParameterException(new ArrayList(responseParameters)); + } + + parameterNamesToSkip.addAll(supportedParameters); + parameterNamesToSkip.removeAll(responseParameters); + } + + ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); + + GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); + builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); + 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 convertOfficeTransactionDataToJson(final boolean prettyPrint, final Set responseParameters, final OfficeTransactionData... officeTransactions) { Set supportedParameters = new HashSet(Arrays.asList("transactionDate", "currencyOptions", "allowedOffices")); @@ -86,7 +229,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } Gson gsonDeserializer = builder.create(); - return gsonDeserializer.toJson(officeTransactions); + String json = ""; + if (officeTransactions != null && officeTransactions.length == 1) { + json = gsonDeserializer.toJson(officeTransactions[0]); + } else { + json = gsonDeserializer.toJson(officeTransactions); + } + + return json; } @Override @@ -115,7 +265,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } Gson gsonDeserializer = builder.create(); - return gsonDeserializer.toJson(users); + String json = ""; + if (users != null && users.length == 1) { + json = gsonDeserializer.toJson(users[0]); + } else { + json = gsonDeserializer.toJson(users); + } + + return json; } @Override @@ -142,7 +299,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } Gson gsonDeserializer = builder.create(); - return gsonDeserializer.toJson(roles); + String json = ""; + if (roles != null && roles.length == 1) { + json = gsonDeserializer.toJson(roles[0]); + } else { + json = gsonDeserializer.toJson(roles); + } + + return json; } @Override @@ -170,7 +334,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } Gson gsonDeserializer = builder.create(); - return gsonDeserializer.toJson(permissions); + String json = ""; + if (permissions != null && permissions.length == 1) { + json = gsonDeserializer.toJson(permissions[0]); + } else { + json = gsonDeserializer.toJson(permissions); + } + + return json; } @Override @@ -199,7 +370,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } Gson gsonDeserializer = builder.create(); - return gsonDeserializer.toJson(offices); + String json = ""; + if (offices != null && offices.length == 1) { + json = gsonDeserializer.toJson(offices[0]); + } else { + json = gsonDeserializer.toJson(offices); + } + + return json; } @Override @@ -226,7 +404,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { } Gson gsonDeserializer = builder.create(); - return gsonDeserializer.toJson(funds); + String json = ""; + if (funds != null && funds.length == 1) { + json = gsonDeserializer.toJson(funds[0]); + } else { + json = gsonDeserializer.toJson(funds); + } + + return json; } @Override diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/JodaDateTimeAdapter.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/JodaDateTimeAdapter.java new file mode 100644 index 000000000..a7c070305 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/JodaDateTimeAdapter.java @@ -0,0 +1,28 @@ +package org.mifosng.platform.api.infrastructure; + +import java.lang.reflect.Type; + +import org.joda.time.DateTime; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +/** + * Serializer for joda time {@link DateTime} that returns date as long to match previous functionality. + */ +public class JodaDateTimeAdapter implements JsonSerializer { + + @Override + public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) { + + JsonElement element = null; + if (src != null) { + element = new JsonPrimitive(src.getMillis()); + } + + return element; + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformService.java index feda62d09..ab4d29b76 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformService.java @@ -14,8 +14,7 @@ public interface ClientReadPlatformService { ClientData retrieveNewClientDetails(); - ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails( - Long clientId); + ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails(Long clientId); Collection retrieveAllClientNotes(Long clientId); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformServiceImpl.java index f6f4dc3b9..2bbcfe46d 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformServiceImpl.java @@ -65,8 +65,7 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService } @Override - public Collection retrieveAllIndividualClients( - String extraCriteria) { + public Collection retrieveAllIndividualClients(final String extraCriteria) { this.context.authenticatedUser(); @@ -112,13 +111,8 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService List offices = new ArrayList( officeReadPlatformService.retrieveAllOfficesForLookup()); - ClientData clientData = new ClientData(); - clientData.setOfficeId(currentUser.getOffice().getId()); - clientData.setAllowedOffices(offices); - - clientData.setJoinedDate(new LocalDate()); - - return clientData; + final Long officeId = currentUser.getOffice().getId(); + return new ClientData(officeId, new LocalDate(), offices); } private static final class ClientMapper implements RowMapper { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationReadPlatformServiceImpl.java index 9d702606d..c12be07cc 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationReadPlatformServiceImpl.java @@ -21,15 +21,14 @@ public class ConfigurationReadPlatformServiceImpl implements ConfigurationReadPl @Override public ConfigurationData retrieveCurrencyConfiguration() { + List selectedCurrencyOptions = new ArrayList(this.currencyReadPlatformService.retrieveAllowedCurrencies()); List currencyOptions = new ArrayList(this.currencyReadPlatformService.retrieveAllPlatformCurrencies()); // remove selected currency options currencyOptions.removeAll(selectedCurrencyOptions); - ConfigurationData configurationData = new ConfigurationData(); - configurationData.setCurrencyOptions(currencyOptions); - configurationData.setSelectedCurrencyOptions(selectedCurrencyOptions); + ConfigurationData configurationData = new ConfigurationData(currencyOptions, selectedCurrencyOptions); return configurationData; }