From 3a6923d71e9e8afcad1eb64b5708cfbcaf3d27a4 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Sat, 26 May 2012 17:30:05 +1000 Subject: [PATCH] filtering clients (part of) --- .../java/org/mifosng/data/ClientData.java | 4 + .../java/org/mifosng/data/ClientList.java | 28 --- .../platform/api/ClientApiResource.java | 181 +++++++++++------- .../ClientReadPlatformServiceImpl.java | 2 +- 4 files changed, 118 insertions(+), 97 deletions(-) delete mode 100644 mifosng-data/src/main/java/org/mifosng/data/ClientList.java diff --git a/mifosng-data/src/main/java/org/mifosng/data/ClientData.java b/mifosng-data/src/main/java/org/mifosng/data/ClientData.java index 301df6b14..2f126f784 100644 --- a/mifosng-data/src/main/java/org/mifosng/data/ClientData.java +++ b/mifosng-data/src/main/java/org/mifosng/data/ClientData.java @@ -6,11 +6,15 @@ import java.util.List; import javax.xml.bind.annotation.XmlRootElement; +import org.codehaus.jackson.annotate.JsonIgnoreProperties; +import org.codehaus.jackson.map.annotate.JsonFilter; import org.joda.time.DateTime; import org.joda.time.Days; import org.joda.time.LocalDate; @XmlRootElement(name = "client") +@JsonIgnoreProperties({ "organisationId","organisationName" }) +@JsonFilter("myFilter") public class ClientData implements Serializable { private Long organisationId; diff --git a/mifosng-data/src/main/java/org/mifosng/data/ClientList.java b/mifosng-data/src/main/java/org/mifosng/data/ClientList.java deleted file mode 100644 index f408a9c7c..000000000 --- a/mifosng-data/src/main/java/org/mifosng/data/ClientList.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.mifosng.data; - -import java.util.ArrayList; -import java.util.Collection; - -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement -public class ClientList { - - private Collection clients = new ArrayList(); - - protected ClientList() { - // - } - - public ClientList(final Collection clients) { - this.clients = clients; - } - - public Collection getClients() { - return this.clients; - } - - public void setClients(final Collection clients) { - this.clients = clients; - } -} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java index d4359a572..b0c098f1a 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java @@ -1,5 +1,6 @@ package org.mifosng.platform.api; +import java.util.ArrayList; import java.util.Collection; import javax.ws.rs.Consumes; @@ -9,22 +10,26 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; 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.joda.time.LocalDate; import org.mifosng.data.ClientData; -import org.mifosng.data.ClientList; import org.mifosng.data.ClientLoanAccountSummaryCollectionData; import org.mifosng.data.EntityIdentifier; import org.mifosng.data.NoteData; import org.mifosng.data.NoteDataList; +import org.mifosng.data.OfficeLookup; import org.mifosng.data.command.ClientCommand; import org.mifosng.data.command.NoteCommand; import org.mifosng.platform.api.infrastructure.ApiDataConversionService; import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; import org.mifosng.platform.client.service.ClientReadPlatformService; import org.mifosng.platform.client.service.ClientWritePlatformService; +import org.mifosng.platform.organisation.service.OfficeReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -34,138 +39,178 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class ClientApiResource { - private String defaultFieldList = "openingDate"; - private String allowedFieldList = "allowedParents"; + private String defaultFieldList = "joinedDate"; + private String allowedFieldList = "allowedOffices"; private String filterName = "myFilter"; - - @Autowired - private ClientReadPlatformService clientReadPlatformService; - - @Autowired - private ClientWritePlatformService clientWritePlatformService; - - @Autowired - private ApiDataConversionService apiDataConversionService; + + @Autowired + private ClientReadPlatformService clientReadPlatformService; + + @Autowired + private ClientWritePlatformService clientWritePlatformService; + + @Autowired + private ApiDataConversionService apiDataConversionService; + + @Autowired + private OfficeReadPlatformService officeReadPlatformService; @Autowired private ApiJSONFormattingService jsonFormattingService; - + @GET - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON}) - public Response retrieveAllIndividualClients() { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public String retrieveAllIndividualClients(@Context UriInfo uriInfo) { - Collection clients = this.clientReadPlatformService.retrieveAllIndividualClients(); + Collection clients = this.clientReadPlatformService + .retrieveAllIndividualClients(); - return Response.ok().entity(new ClientList(clients)).build(); + String selectedFields = ""; + return this.jsonFormattingService.convertRequest(clients, filterName, + allowedFieldList, selectedFields, uriInfo.getQueryParameters()); } - + @GET @Path("{clientId}") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON}) - public Response retrieveClientData(@PathParam("clientId") final Long clientId) { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public String retrieveClientData( + @PathParam("clientId") final Long clientId, + @QueryParam("template") String template, @Context UriInfo uriInfo) { - ClientData clientData = this.clientReadPlatformService.retrieveIndividualClient(clientId); + ClientData clientData = this.clientReadPlatformService + .retrieveIndividualClient(clientId); - return Response.ok().entity(clientData).build(); + if (template != null && template.equalsIgnoreCase("true")) { + clientData.setAllowedOffices(new ArrayList( + officeReadPlatformService.retrieveAllOfficesForLookup())); + } + + String selectedFields = ""; + return this.jsonFormattingService.convertRequest(clientData, + filterName, allowedFieldList, selectedFields, + uriInfo.getQueryParameters()); } - + @GET @Path("template") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON}) - public Response newClientDetails() { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public String newClientDetails(@Context UriInfo uriInfo) { - ClientData clientData = this.clientReadPlatformService.retrieveNewClientDetails(); + ClientData clientData = this.clientReadPlatformService + .retrieveNewClientDetails(); - return Response.ok().entity(clientData).build(); + String selectedFields = defaultFieldList + "," + allowedFieldList; + return this.jsonFormattingService.convertRequest(clientData, + filterName, allowedFieldList, selectedFields, + uriInfo.getQueryParameters()); } @POST - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON }) + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) public Response enrollClient(final ClientCommand command) { - - LocalDate joiningDate = apiDataConversionService.convertFrom(command.getJoiningDateFormatted(), "joiningDateFormatted", command.getDateFormat()); + + LocalDate joiningDate = apiDataConversionService.convertFrom( + command.getJoiningDateFormatted(), "joiningDateFormatted", + command.getDateFormat()); command.setJoiningDate(joiningDate); - + Long clientId = this.clientWritePlatformService.enrollClient(command); return Response.ok().entity(new EntityIdentifier(clientId)).build(); } - + @PUT @Path("{clientId}") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON }) - public Response updateClient(@PathParam("clientId") final Long clientId, final ClientCommand command) { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response updateClient(@PathParam("clientId") final Long clientId, + final ClientCommand command) { - LocalDate joiningDate = apiDataConversionService.convertFrom(command.getJoiningDateFormatted(), "joiningDateFormatted", command.getDateFormat()); + LocalDate joiningDate = apiDataConversionService.convertFrom( + command.getJoiningDateFormatted(), "joiningDateFormatted", + command.getDateFormat()); command.setJoiningDate(joiningDate); command.setId(clientId); - - EntityIdentifier identifier = this.clientWritePlatformService.updateClientDetails(command); + + EntityIdentifier identifier = this.clientWritePlatformService + .updateClientDetails(command); return Response.ok().entity(identifier).build(); } @GET @Path("{clientId}/loans") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON }) - public Response retrieveClientAccount(@PathParam("clientId") final Long clientId) { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response retrieveClientAccount( + @PathParam("clientId") final Long clientId) { - ClientLoanAccountSummaryCollectionData clientAccount = this.clientReadPlatformService.retrieveClientAccountDetails(clientId); + ClientLoanAccountSummaryCollectionData clientAccount = this.clientReadPlatformService + .retrieveClientAccountDetails(clientId); return Response.ok().entity(clientAccount).build(); } @GET @Path("{clientId}/notes") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON}) - public Response retrieveAllClientNotes(@PathParam("clientId") final Long clientId) { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response retrieveAllClientNotes( + @PathParam("clientId") final Long clientId) { - Collection notes = this.clientReadPlatformService.retrieveAllClientNotes(clientId); + Collection notes = this.clientReadPlatformService + .retrieveAllClientNotes(clientId); return Response.ok().entity(new NoteDataList(notes)).build(); } - + @POST @Path("{clientId}/notes") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON}) - public Response addNewClientNote(@PathParam("clientId") final Long clientId, final NoteCommand command) { - + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response addNewClientNote( + @PathParam("clientId") final Long clientId, + final NoteCommand command) { + command.setClientId(clientId); - - EntityIdentifier identifier = this.clientWritePlatformService.addClientNote(command); + + EntityIdentifier identifier = this.clientWritePlatformService + .addClientNote(command); return Response.ok().entity(identifier).build(); } - + @GET @Path("{clientId}/notes/{noteId}") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON }) - public Response retrieveClientNote(@PathParam("clientId") final Long clientId, @PathParam("noteId") final Long noteId) { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response retrieveClientNote( + @PathParam("clientId") final Long clientId, + @PathParam("noteId") final Long noteId) { - NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId); + NoteData note = this.clientReadPlatformService.retrieveClientNote( + clientId, noteId); return Response.ok().entity(note).build(); } - + @PUT @Path("{clientId}/notes/{noteId}") - @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - @Produces({MediaType.APPLICATION_JSON }) - public Response updateClientNote(@PathParam("clientId") final Long clientId, @PathParam("noteId") final Long noteId, final NoteCommand command) { + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON }) + public Response updateClientNote( + @PathParam("clientId") final Long clientId, + @PathParam("noteId") final Long noteId, final NoteCommand command) { command.setClientId(clientId); command.setId(noteId); - - 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/client/service/ClientReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientReadPlatformServiceImpl.java index a6765aa4c..3d2e972aa 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 @@ -83,7 +83,7 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService try { AppUser currentUser = this.context.authenticatedUser(); - +//TODO - JW include office name in query rather than get the lot for office name List offices = new ArrayList(officeReadPlatformService.retrieveAllOffices()); ClientMapper rm = new ClientMapper(offices, currentUser.getOrganisation());