tidy up return of json for clients api functionality.

This commit is contained in:
Keith Woodlock
2012-07-22 00:42:41 +01:00
parent 676cb33d37
commit b3996a4a1c
12 changed files with 421 additions and 222 deletions
@@ -1,7 +1,10 @@
package org.mifosng.platform.api; package org.mifosng.platform.api;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; 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.NoteData;
import org.mifosng.platform.api.data.OfficeLookup; import org.mifosng.platform.api.data.OfficeLookup;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService; 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.ClientReadPlatformService;
import org.mifosng.platform.client.service.ClientWritePlatformService; import org.mifosng.platform.client.service.ClientWritePlatformService;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService; import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
@@ -40,12 +43,7 @@ import org.springframework.stereotype.Component;
@Scope("singleton") @Scope("singleton")
public class ClientsApiResource { public class ClientsApiResource {
private final static Logger logger = LoggerFactory private final static Logger logger = LoggerFactory.getLogger(ClientsApiResource.class);
.getLogger(ClientsApiResource.class);
private String defaultFieldList = "joinedDate";
private String allowedFieldList = "allowedOffices";
private String filterName = "myFilter";
@Autowired @Autowired
private ClientReadPlatformService clientReadPlatformService; private ClientReadPlatformService clientReadPlatformService;
@@ -59,9 +57,6 @@ public class ClientsApiResource {
@Autowired @Autowired
private OfficeReadPlatformService officeReadPlatformService; private OfficeReadPlatformService officeReadPlatformService;
@Autowired
private ApiJSONFormattingService jsonFormattingService;
@GET @GET
@Consumes({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
@@ -73,15 +68,21 @@ public class ClientsApiResource {
@QueryParam("firstName") final String firstName, @QueryParam("firstName") final String firstName,
@QueryParam("lastName") final String lastName) { @QueryParam("lastName") final String lastName) {
String extraCriteria = getClientCriteria(sqlSearch, officeId, final String extraCriteria = getClientCriteria(sqlSearch, officeId, externalId, displayName, firstName, lastName);
externalId, displayName, firstName, lastName);
Collection<ClientData> clients = this.clientReadPlatformService Set<String> typicalResponseParameters = new HashSet<String>(
.retrieveAllIndividualClients(extraCriteria); Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName")
);
String selectedFields = ""; Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
return this.jsonFormattingService.convertRequest(clients, filterName, if (responseParameters.isEmpty()) {
allowedFieldList, selectedFields, uriInfo.getQueryParameters()); responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<ClientData> clients = this.clientReadPlatformService.retrieveAllIndividualClients(extraCriteria);
return this.apiDataConversionService.convertClientDataToJson(prettyPrint, responseParameters, clients.toArray(new ClientData[clients.size()]));
} }
private String getClientCriteria(String sqlSearch, Integer officeId, private String getClientCriteria(String sqlSearch, Integer officeId,
@@ -129,20 +130,26 @@ public class ClientsApiResource {
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public String retrieveClientData( public String retrieveClientData(
@PathParam("clientId") final Long clientId, @PathParam("clientId") final Long clientId,
@QueryParam("template") final String template, @Context final UriInfo uriInfo) { @Context final UriInfo uriInfo) {
ClientData clientData = this.clientReadPlatformService Set<String> typicalResponseParameters = new HashSet<String>(
.retrieveIndividualClient(clientId); Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName")
);
if (template != null && template.equalsIgnoreCase("true")) { Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
clientData.setAllowedOffices(new ArrayList<OfficeLookup>( if (responseParameters.isEmpty()) {
officeReadPlatformService.retrieveAllOfficesForLookup())); 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<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup()));
responseParameters.add("allowedOffices");
} }
String selectedFields = ""; return this.apiDataConversionService.convertClientDataToJson(prettyPrint, responseParameters, clientData);
return this.jsonFormattingService.convertRequest(clientData,
filterName, allowedFieldList, selectedFields,
uriInfo.getQueryParameters());
} }
@GET @GET
@@ -151,13 +158,19 @@ public class ClientsApiResource {
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public String newClientDetails(@Context UriInfo uriInfo) { public String newClientDetails(@Context UriInfo uriInfo) {
ClientData clientData = this.clientReadPlatformService Set<String> typicalResponseParameters = new HashSet<String>(
.retrieveNewClientDetails(); Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName", "allowedOffices")
);
String selectedFields = defaultFieldList + "," + allowedFieldList; Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
return this.jsonFormattingService.convertRequest(clientData, if (responseParameters.isEmpty()) {
filterName, allowedFieldList, selectedFields, responseParameters.addAll(typicalResponseParameters);
uriInfo.getQueryParameters()); }
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
ClientData clientData = this.clientReadPlatformService.retrieveNewClientDetails();
return this.apiDataConversionService.convertClientDataToJson(prettyPrint, responseParameters, clientData);
} }
@POST @POST
@@ -189,16 +202,22 @@ public class ClientsApiResource {
@Path("{clientId}/loans") @Path("{clientId}/loans")
@Consumes({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public String retrieveClientAccount( public String retrieveClientAccount(@PathParam("clientId") final Long clientId,
@PathParam("clientId") final Long clientId, @Context final UriInfo uriInfo) { @Context final UriInfo uriInfo) {
ClientLoanAccountSummaryCollectionData clientAccount = this.clientReadPlatformService Set<String> typicalResponseParameters = new HashSet<String>(
.retrieveClientAccountDetails(clientId); Arrays.asList("pendingApprovalLoans", "awaitingDisbursalLoans", "openLoans", "closedLoans")
);
String selectedFields = ""; Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
return this.jsonFormattingService.convertRequest(clientAccount, if (responseParameters.isEmpty()) {
filterName, allowedFieldList, selectedFields, responseParameters.addAll(typicalResponseParameters);
uriInfo.getQueryParameters()); }
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
ClientLoanAccountSummaryCollectionData clientAccount = this.clientReadPlatformService.retrieveClientAccountDetails(clientId);
return this.apiDataConversionService.convertClientLoanAccountSummaryCollectionDataToJson(prettyPrint, responseParameters, clientAccount);
} }
@GET @GET
@@ -207,12 +226,20 @@ public class ClientsApiResource {
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public String retrieveAllClientNotes(@PathParam("clientId") final Long clientId, @Context final UriInfo uriInfo) { public String retrieveAllClientNotes(@PathParam("clientId") final Long clientId, @Context final UriInfo uriInfo) {
Collection<NoteData> notes = this.clientReadPlatformService Set<String> typicalResponseParameters = new HashSet<String>(
.retrieveAllClientNotes(clientId); Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername",
"createdOn", "updatedById", "updatedByUsername", "updatedOn")
);
String selectedFields = ""; Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
return this.jsonFormattingService.convertRequest(notes, filterName, if (responseParameters.isEmpty()) {
allowedFieldList, selectedFields, uriInfo.getQueryParameters()); responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<NoteData> notes = this.clientReadPlatformService.retrieveAllClientNotes(clientId);
return this.apiDataConversionService.convertNoteDataToJson(prettyPrint, responseParameters, notes.toArray(new NoteData[notes.size()]));
} }
@POST @POST
@@ -234,13 +261,23 @@ public class ClientsApiResource {
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public String retrieveClientNote( public String retrieveClientNote(
@PathParam("clientId") final Long clientId, @PathParam("clientId") final Long clientId,
@PathParam("noteId") final Long noteId, @Context final UriInfo uriInfo) { @PathParam("noteId") final Long noteId,
@Context final UriInfo uriInfo) {
Set<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername",
"createdOn", "updatedById", "updatedByUsername", "updatedOn")
);
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId); NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId);
String selectedFields = ""; return this.apiDataConversionService.convertNoteDataToJson(prettyPrint, responseParameters, note);
return this.jsonFormattingService.convertRequest(note, filterName,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
} }
@PUT @PUT
@@ -253,8 +290,7 @@ public class ClientsApiResource {
NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(noteId, clientId, jsonRequestBody); NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(noteId, clientId, jsonRequestBody);
EntityIdentifier identifier = this.clientWritePlatformService EntityIdentifier identifier = this.clientWritePlatformService.updateNote(command);
.updateNote(command);
return Response.ok().entity(identifier).build(); return Response.ok().entity(identifier).build();
} }
@@ -1,5 +1,9 @@
package org.mifosng.platform.api; 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.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.PUT; 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.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.data.ConfigurationData; import org.mifosng.platform.api.data.ConfigurationData;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService; 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.ConfigurationReadPlatformService;
import org.mifosng.platform.configuration.service.ConfigurationWritePlatformService; import org.mifosng.platform.configuration.service.ConfigurationWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -25,9 +29,6 @@ import org.springframework.stereotype.Component;
@Scope("singleton") @Scope("singleton")
public class ConfigurationApiResource { public class ConfigurationApiResource {
private String allowedFieldList = "";
private String filterName = "myFilter";
@Autowired @Autowired
private ConfigurationReadPlatformService configurationReadPlatformService; private ConfigurationReadPlatformService configurationReadPlatformService;
@@ -37,22 +38,24 @@ public class ConfigurationApiResource {
@Autowired @Autowired
private ApiDataConversionService apiDataConversionService; private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJSONFormattingService jsonFormattingService;
@GET @GET
@Path("currency") @Path("currency")
@Consumes({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public String retrieveCurrencyDataForConfiguration(@Context final UriInfo uriInfo) { public String retrieveCurrencyDataForConfiguration(@Context final UriInfo uriInfo) {
ConfigurationData configurationData = this.configurationReadPlatformService Set<String> typicalResponseParameters = new HashSet<String>(
.retrieveCurrencyConfiguration(); Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
String selectedFields = ""; Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
return this.jsonFormattingService.convertRequest(configurationData, if (responseParameters.isEmpty()) {
filterName, allowedFieldList, selectedFields, responseParameters.addAll(typicalResponseParameters);
uriInfo.getQueryParameters()); }
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
ConfigurationData configurationData = this.configurationReadPlatformService.retrieveCurrencyConfiguration();
return this.apiDataConversionService.convertConfigurationDataToJson(prettyPrint, responseParameters, configurationData);
} }
@PUT @PUT
@@ -1,31 +1,24 @@
package org.mifosng.platform.api.data; package org.mifosng.platform.api.data;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.annotate.JsonFilter;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
@JsonFilter("myFilter") public class ClientData {
public class ClientData implements Serializable {
private Long officeId; private final Long officeId;
private String officeName; private final String officeName;
private Long id; private final Long id;
private String firstname; private final String firstname;
private String lastname; private final String lastname;
private String displayName; private final String displayName;
private String externalId; private final String externalId;
private LocalDate joinedDate; private final LocalDate joinedDate;
private List<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>(); private List<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>();
public ClientData() {
//
}
public ClientData(final Long officeId, final String officeName, public ClientData(final Long officeId, final String officeName,
final Long id, final String firstname, final String lastname, final Long id, final String firstname, final String lastname,
final String externalId, final LocalDate joinedDate) { final String externalId, final LocalDate joinedDate) {
@@ -46,68 +39,48 @@ public class ClientData implements Serializable {
this.joinedDate = joinedDate; this.joinedDate = joinedDate;
} }
public Long getId() { public ClientData(final Long officeId, final LocalDate joinedDate, final List<OfficeLookup> offices) {
return this.id; this.id = null;
} this.officeId = officeId;
this.officeName = null;
public String getFirstname() { this.firstname = null;
return this.firstname; this.lastname = null;
} this.displayName = null;
this.externalId = null;
public String getLastname() { this.joinedDate = joinedDate;
return this.lastname; this.allowedOffices = offices;
}
public String getExternalId() {
return this.externalId;
} }
public Long getOfficeId() { public Long getOfficeId() {
return this.officeId; return officeId;
} }
public String getOfficeName() { public String getOfficeName() {
return this.officeName; return officeName;
} }
public LocalDate getJoinedDate() { public Long getId() {
return this.joinedDate; return id;
} }
public void setOfficeId(final Long officeId) { public String getFirstname() {
this.officeId = officeId; return firstname;
} }
public void setOfficeName(final String officeName) { public String getLastname() {
this.officeName = officeName; return lastname;
}
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 getDisplayName() { public String getDisplayName() {
return this.displayName; return displayName;
} }
public void setDisplayName(final String displayName) { public String getExternalId() {
this.displayName = displayName; return externalId;
}
public LocalDate getJoinedDate() {
return joinedDate;
} }
public List<OfficeLookup> getAllowedOffices() { public List<OfficeLookup> getAllowedOffices() {
@@ -3,19 +3,17 @@ package org.mifosng.platform.api.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.codehaus.jackson.map.annotate.JsonFilter;
@JsonFilter("myFilter")
public class ClientLoanAccountSummaryCollectionData { public class ClientLoanAccountSummaryCollectionData {
private List<ClientLoanAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientLoanAccountSummaryData>(); private List<ClientLoanAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientLoanAccountSummaryData>();
private List<ClientLoanAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientLoanAccountSummaryData>(); private List<ClientLoanAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientLoanAccountSummaryData>();
private List<ClientLoanAccountSummaryData> openLoans = new ArrayList<ClientLoanAccountSummaryData>(); private List<ClientLoanAccountSummaryData> openLoans = new ArrayList<ClientLoanAccountSummaryData>();
private List<ClientLoanAccountSummaryData> closedLoans = new ArrayList<ClientLoanAccountSummaryData>(); private List<ClientLoanAccountSummaryData> closedLoans = new ArrayList<ClientLoanAccountSummaryData>();
private int anyLoanCount;
public ClientLoanAccountSummaryCollectionData() { private int pendingApprovalLoanCount;
// private int awaitingDisbursalLoanCount;
} private int activeLoanCount;
private int closedLoanCount;
public ClientLoanAccountSummaryCollectionData( public ClientLoanAccountSummaryCollectionData(
final List<ClientLoanAccountSummaryData> pendingApprovalLoans, final List<ClientLoanAccountSummaryData> pendingApprovalLoans,
@@ -26,60 +24,46 @@ public class ClientLoanAccountSummaryCollectionData {
this.awaitingDisbursalLoans = awaitingDisbursalLoans; this.awaitingDisbursalLoans = awaitingDisbursalLoans;
this.openLoans = openLoans; this.openLoans = openLoans;
this.closedLoans = closedLoans; this.closedLoans = closedLoans;
} this.pendingApprovalLoanCount = this.pendingApprovalLoans.size();
this.awaitingDisbursalLoanCount = this.awaitingDisbursalLoans.size();
public Integer getAnyLoanCount() { this.activeLoanCount = this.openLoans.size();
return getPendingApprovalLoanCount() + getAwaitingDisbursalLoanCount() this.closedLoanCount = this.closedLoans.size();
+ getActiveLoanCount() + getClosedLoanCount(); this.anyLoanCount = this.pendingApprovalLoanCount + this.awaitingDisbursalLoanCount + this.activeLoanCount + this.closedLoanCount;
}
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();
} }
public List<ClientLoanAccountSummaryData> getPendingApprovalLoans() { public List<ClientLoanAccountSummaryData> getPendingApprovalLoans() {
return pendingApprovalLoans; return pendingApprovalLoans;
} }
public void setPendingApprovalLoans(
List<ClientLoanAccountSummaryData> pendingApprovalLoans) {
this.pendingApprovalLoans = pendingApprovalLoans;
}
public List<ClientLoanAccountSummaryData> getAwaitingDisbursalLoans() { public List<ClientLoanAccountSummaryData> getAwaitingDisbursalLoans() {
return awaitingDisbursalLoans; return awaitingDisbursalLoans;
} }
public void setAwaitingDisbursalLoans(
List<ClientLoanAccountSummaryData> awaitingDisbursalLoans) {
this.awaitingDisbursalLoans = awaitingDisbursalLoans;
}
public List<ClientLoanAccountSummaryData> getOpenLoans() { public List<ClientLoanAccountSummaryData> getOpenLoans() {
return openLoans; return openLoans;
} }
public void setOpenLoans(List<ClientLoanAccountSummaryData> openLoans) {
this.openLoans = openLoans;
}
public List<ClientLoanAccountSummaryData> getClosedLoans() { public List<ClientLoanAccountSummaryData> getClosedLoans() {
return closedLoans; return closedLoans;
} }
public void setClosedLoans(List<ClientLoanAccountSummaryData> closedLoans) { public int getAnyLoanCount() {
this.closedLoans = closedLoans; return anyLoanCount;
}
public int getPendingApprovalLoanCount() {
return pendingApprovalLoanCount;
}
public int getAwaitingDisbursalLoanCount() {
return awaitingDisbursalLoanCount;
}
public int getActiveLoanCount() {
return activeLoanCount;
}
public int getClosedLoanCount() {
return closedLoanCount;
} }
} }
@@ -1,35 +1,22 @@
package org.mifosng.platform.api.data; package org.mifosng.platform.api.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.codehaus.jackson.map.annotate.JsonFilter; public class ConfigurationData {
@JsonFilter("myFilter") private final List<CurrencyData> selectedCurrencyOptions;
public class ConfigurationData implements Serializable { private final List<CurrencyData> currencyOptions;
private List<CurrencyData> selectedCurrencyOptions = new ArrayList<CurrencyData>(); public ConfigurationData(final List<CurrencyData> currencyOptions, final List<CurrencyData> selectedCurrencyOptions) {
private List<CurrencyData> currencyOptions = new ArrayList<CurrencyData>(); this.currencyOptions = currencyOptions;
this.selectedCurrencyOptions = selectedCurrencyOptions;
public ConfigurationData() {
//
} }
public List<CurrencyData> getSelectedCurrencyOptions() { public List<CurrencyData> getSelectedCurrencyOptions() {
return selectedCurrencyOptions; return selectedCurrencyOptions;
} }
public void setSelectedCurrencyOptions(
List<CurrencyData> selectedCurrencyOptions) {
this.selectedCurrencyOptions = selectedCurrencyOptions;
}
public List<CurrencyData> getCurrencyOptions() { public List<CurrencyData> getCurrencyOptions() {
return currencyOptions; return currencyOptions;
} }
public void setCurrencyOptions(List<CurrencyData> currencyOptions) {
this.currencyOptions = currencyOptions;
}
} }
@@ -1,9 +1,7 @@
package org.mifosng.platform.api.data; package org.mifosng.platform.api.data;
import org.codehaus.jackson.map.annotate.JsonFilter;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@JsonFilter("myFilter")
public class NoteData { public class NoteData {
private Long id; private Long id;
@@ -16,7 +16,11 @@ import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.data.AppUserData; 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.FundData;
import org.mifosng.platform.api.data.NoteData;
import org.mifosng.platform.api.data.OfficeData; import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.data.PermissionData; import org.mifosng.platform.api.data.PermissionData;
@@ -32,6 +36,15 @@ public interface ApiDataConversionService {
String convertAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, AppUserData... users); String convertAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, AppUserData... users);
String convertOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData... officeTransactions); String convertOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData... officeTransactions);
String convertConfigurationDataToJson(boolean prettyPrint, Set<String> responseParameters, ConfigurationData... configuration);
String convertClientDataToJson(boolean prettyPrint, Set<String> responseParameters, ClientData... clients);
String convertClientLoanAccountSummaryCollectionDataToJson(boolean prettyPrint, Set<String> responseParameters,
ClientLoanAccountSummaryCollectionData clientAccountsData);
String convertNoteDataToJson(boolean prettyPrint, Set<String> responseParameters, NoteData... notes);
// //
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json); FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
@@ -15,6 +15,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand; 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.commands.UserCommand;
import org.mifosng.platform.api.data.ApiParameterError; import org.mifosng.platform.api.data.ApiParameterError;
import org.mifosng.platform.api.data.AppUserData; 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.FundData;
import org.mifosng.platform.api.data.NoteData;
import org.mifosng.platform.api.data.OfficeData; import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.data.PermissionData; import org.mifosng.platform.api.data.PermissionData;
@@ -62,6 +67,144 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
gsonConverter = new Gson(); gsonConverter = new Gson();
} }
@Override
public String convertNoteDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final NoteData... notes) {
Set<String> supportedParameters = new HashSet<String>(
Arrays.asList("id", "clientId", "loanId", "loanTransactionId", "noteType", "note", "createdById", "createdByUsername",
"createdOn", "updatedById", "updatedByUsername", "updatedOn")
);
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
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<String> responseParameters,
final ClientLoanAccountSummaryCollectionData clientAccountsData) {
Set<String> supportedParameters = new HashSet<String>(
Arrays.asList("pendingApprovalLoans", "awaitingDisbursalLoans", "openLoans", "closedLoans")
);
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
return gsonDeserializer.toJson(clientAccountsData);
}
@Override
public String convertClientDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final ClientData... clients) {
Set<String> supportedParameters = new HashSet<String>(
Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName", "allowedOffices")
);
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
String json = "";
if (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<String> responseParameters, final ConfigurationData... configuration) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
String json = "";
if (configuration != null && configuration.length == 1) {
json = gsonDeserializer.toJson(configuration[0]);
} else {
json = gsonDeserializer.toJson(configuration);
}
return json;
}
@Override @Override
public String convertOfficeTransactionDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final OfficeTransactionData... officeTransactions) { public String convertOfficeTransactionDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final OfficeTransactionData... officeTransactions) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("transactionDate", "currencyOptions", "allowedOffices")); Set<String> supportedParameters = new HashSet<String>(Arrays.asList("transactionDate", "currencyOptions", "allowedOffices"));
@@ -86,7 +229,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
} }
Gson gsonDeserializer = builder.create(); 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 @Override
@@ -115,7 +265,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
} }
Gson gsonDeserializer = builder.create(); 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 @Override
@@ -142,7 +299,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
} }
Gson gsonDeserializer = builder.create(); 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 @Override
@@ -170,7 +334,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
} }
Gson gsonDeserializer = builder.create(); 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 @Override
@@ -199,7 +370,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
} }
Gson gsonDeserializer = builder.create(); 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 @Override
@@ -226,7 +404,14 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
} }
Gson gsonDeserializer = builder.create(); 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 @Override
@@ -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<DateTime> {
@Override
public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
JsonElement element = null;
if (src != null) {
element = new JsonPrimitive(src.getMillis());
}
return element;
}
}
@@ -14,8 +14,7 @@ public interface ClientReadPlatformService {
ClientData retrieveNewClientDetails(); ClientData retrieveNewClientDetails();
ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails( ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails(Long clientId);
Long clientId);
Collection<NoteData> retrieveAllClientNotes(Long clientId); Collection<NoteData> retrieveAllClientNotes(Long clientId);
@@ -65,8 +65,7 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
} }
@Override @Override
public Collection<ClientData> retrieveAllIndividualClients( public Collection<ClientData> retrieveAllIndividualClients(final String extraCriteria) {
String extraCriteria) {
this.context.authenticatedUser(); this.context.authenticatedUser();
@@ -112,13 +111,8 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
List<OfficeLookup> offices = new ArrayList<OfficeLookup>( List<OfficeLookup> offices = new ArrayList<OfficeLookup>(
officeReadPlatformService.retrieveAllOfficesForLookup()); officeReadPlatformService.retrieveAllOfficesForLookup());
ClientData clientData = new ClientData(); final Long officeId = currentUser.getOffice().getId();
clientData.setOfficeId(currentUser.getOffice().getId()); return new ClientData(officeId, new LocalDate(), offices);
clientData.setAllowedOffices(offices);
clientData.setJoinedDate(new LocalDate());
return clientData;
} }
private static final class ClientMapper implements RowMapper<ClientData> { private static final class ClientMapper implements RowMapper<ClientData> {
@@ -21,15 +21,14 @@ public class ConfigurationReadPlatformServiceImpl implements ConfigurationReadPl
@Override @Override
public ConfigurationData retrieveCurrencyConfiguration() { public ConfigurationData retrieveCurrencyConfiguration() {
List<CurrencyData> selectedCurrencyOptions = new ArrayList<CurrencyData>(this.currencyReadPlatformService.retrieveAllowedCurrencies()); List<CurrencyData> selectedCurrencyOptions = new ArrayList<CurrencyData>(this.currencyReadPlatformService.retrieveAllowedCurrencies());
List<CurrencyData> currencyOptions = new ArrayList<CurrencyData>(this.currencyReadPlatformService.retrieveAllPlatformCurrencies()); List<CurrencyData> currencyOptions = new ArrayList<CurrencyData>(this.currencyReadPlatformService.retrieveAllPlatformCurrencies());
// remove selected currency options // remove selected currency options
currencyOptions.removeAll(selectedCurrencyOptions); currencyOptions.removeAll(selectedCurrencyOptions);
ConfigurationData configurationData = new ConfigurationData(); ConfigurationData configurationData = new ConfigurationData(currencyOptions, selectedCurrencyOptions);
configurationData.setCurrencyOptions(currencyOptions);
configurationData.setSelectedCurrencyOptions(selectedCurrencyOptions);
return configurationData; return configurationData;
} }