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;
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<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName")
);
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<ClientData> clients = this.clientReadPlatformService
.retrieveAllIndividualClients(extraCriteria);
String selectedFields = "";
return this.jsonFormattingService.convertRequest(clients, filterName,
allowedFieldList, selectedFields, 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,
@@ -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<OfficeLookup>(
officeReadPlatformService.retrieveAllOfficesForLookup()));
@Context final UriInfo uriInfo) {
Set<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName")
);
Set<String> 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<OfficeLookup>(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<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "officeId", "officeName", "externalId", "firstname", "lastname", "joinedDate", "displayName", "allowedOffices")
);
Set<String> 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<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("pendingApprovalLoans", "awaitingDisbursalLoans", "openLoans", "closedLoans")
);
Set<String> 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<NoteData> notes = this.clientReadPlatformService
.retrieveAllClientNotes(clientId);
String selectedFields = "";
return this.jsonFormattingService.convertRequest(notes, filterName,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
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());
Collection<NoteData> 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<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);
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();
}
@@ -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<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
Set<String> 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
@@ -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<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>();
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<OfficeLookup> 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<OfficeLookup> getAllowedOffices() {
@@ -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<ClientLoanAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientLoanAccountSummaryData>();
private List<ClientLoanAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientLoanAccountSummaryData>();
private List<ClientLoanAccountSummaryData> openLoans = new ArrayList<ClientLoanAccountSummaryData>();
private List<ClientLoanAccountSummaryData> closedLoans = new ArrayList<ClientLoanAccountSummaryData>();
public ClientLoanAccountSummaryCollectionData() {
//
}
private int anyLoanCount;
private int pendingApprovalLoanCount;
private int awaitingDisbursalLoanCount;
private int activeLoanCount;
private int closedLoanCount;
public ClientLoanAccountSummaryCollectionData(
final List<ClientLoanAccountSummaryData> pendingApprovalLoans,
final List<ClientLoanAccountSummaryData> 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<ClientLoanAccountSummaryData> getPendingApprovalLoans() {
return pendingApprovalLoans;
}
public void setPendingApprovalLoans(
List<ClientLoanAccountSummaryData> pendingApprovalLoans) {
this.pendingApprovalLoans = pendingApprovalLoans;
}
public List<ClientLoanAccountSummaryData> getAwaitingDisbursalLoans() {
return awaitingDisbursalLoans;
}
public void setAwaitingDisbursalLoans(
List<ClientLoanAccountSummaryData> awaitingDisbursalLoans) {
this.awaitingDisbursalLoans = awaitingDisbursalLoans;
}
public List<ClientLoanAccountSummaryData> getOpenLoans() {
return openLoans;
}
public void setOpenLoans(List<ClientLoanAccountSummaryData> openLoans) {
this.openLoans = openLoans;
}
public List<ClientLoanAccountSummaryData> getClosedLoans() {
return closedLoans;
}
public void setClosedLoans(List<ClientLoanAccountSummaryData> 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;
}
}
@@ -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<CurrencyData> selectedCurrencyOptions;
private final List<CurrencyData> currencyOptions;
private List<CurrencyData> selectedCurrencyOptions = new ArrayList<CurrencyData>();
private List<CurrencyData> currencyOptions = new ArrayList<CurrencyData>();
public ConfigurationData() {
//
public ConfigurationData(final List<CurrencyData> currencyOptions, final List<CurrencyData> selectedCurrencyOptions) {
this.currencyOptions = currencyOptions;
this.selectedCurrencyOptions = selectedCurrencyOptions;
}
public List<CurrencyData> getSelectedCurrencyOptions() {
return selectedCurrencyOptions;
}
public void setSelectedCurrencyOptions(
List<CurrencyData> selectedCurrencyOptions) {
this.selectedCurrencyOptions = selectedCurrencyOptions;
}
public List<CurrencyData> getCurrencyOptions() {
return currencyOptions;
}
public void setCurrencyOptions(List<CurrencyData> currencyOptions) {
this.currencyOptions = currencyOptions;
}
}
@@ -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;
@@ -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<String> responseParameters, AppUserData... users);
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);
@@ -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<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
public String convertOfficeTransactionDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final OfficeTransactionData... officeTransactions) {
Set<String> supportedParameters = new HashSet<String>(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
@@ -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();
ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails(
Long clientId);
ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails(Long clientId);
Collection<NoteData> retrieveAllClientNotes(Long clientId);
@@ -65,8 +65,7 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
}
@Override
public Collection<ClientData> retrieveAllIndividualClients(
String extraCriteria) {
public Collection<ClientData> retrieveAllIndividualClients(final String extraCriteria) {
this.context.authenticatedUser();
@@ -112,13 +111,8 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(
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<ClientData> {
@@ -21,15 +21,14 @@ public class ConfigurationReadPlatformServiceImpl implements ConfigurationReadPl
@Override
public ConfigurationData retrieveCurrencyConfiguration() {
List<CurrencyData> selectedCurrencyOptions = new ArrayList<CurrencyData>(this.currencyReadPlatformService.retrieveAllowedCurrencies());
List<CurrencyData> currencyOptions = new ArrayList<CurrencyData>(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;
}