From e71e88a10bb4a4e1c034304cb3adbe68e62d3064 Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Wed, 18 Jul 2012 13:22:25 +0100 Subject: [PATCH] tidy up remaing commands for converstion from json. --- .../api/AdditionalFieldsApiResource.java | 12 ++-- ...iResource.java => ClientsApiResource.java} | 23 +++---- .../api/ConfigurationApiResource.java | 22 ++++--- .../platform/api/commands/ClientCommand.java | 3 - .../platform/api/commands/NoteCommand.java | 43 ++++--------- .../commands/OrganisationCurrencyCommand.java | 23 ++----- .../SubmitApproveDisburseLoanCommand.java | 39 ------------ .../api/data/AuthenticatedUserData.java | 3 - ...lientLoanAccountSummaryCollectionData.java | 3 - .../ApiDataConversionService.java | 6 ++ .../ApiDataConversionServiceImpl.java | 62 +++++++++++++++++++ ...WritePlatformServiceJpaRepositoryImpl.java | 8 ++- .../OrganisationCurrencyRepository.java | 8 +++ 13 files changed, 130 insertions(+), 125 deletions(-) rename mifosng-provider/src/main/java/org/mifosng/platform/api/{ClientApiResource.java => ClientsApiResource.java} (93%) delete mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/api/commands/SubmitApproveDisburseLoanCommand.java create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/organisation/domain/OrganisationCurrencyRepository.java diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/AdditionalFieldsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/AdditionalFieldsApiResource.java index ec39a4f02..ba9404b04 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/AdditionalFieldsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/AdditionalFieldsApiResource.java @@ -54,8 +54,8 @@ public class AdditionalFieldsApiResource { private ApiJSONFormattingService jsonFormattingService; @GET - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - @Produces({ MediaType.APPLICATION_JSON }) + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) public String datasets(@QueryParam("type") String type, @Context UriInfo uriInfo) { @@ -69,8 +69,8 @@ public class AdditionalFieldsApiResource { @GET @Path("{type}/{set}/{id}") - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - @Produces({ MediaType.APPLICATION_JSON }) + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) public String extraData(@PathParam("type") final String type, @PathParam("set") final String set, @PathParam("id") final Long id, @Context UriInfo uriInfo) { @@ -98,8 +98,8 @@ public class AdditionalFieldsApiResource { @POST @Path("{type}/{set}/{id}") - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - @Produces({ MediaType.APPLICATION_JSON }) + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) public Response saveExtraData(@PathParam("type") final String type, @PathParam("set") final String set, @PathParam("id") final Long id, String reqbody) { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java similarity index 93% rename from mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java rename to mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java index 62afc202f..a194364c5 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ClientsApiResource.java @@ -38,10 +38,10 @@ import org.springframework.stereotype.Component; @Path("/clients") @Component @Scope("singleton") -public class ClientApiResource { +public class ClientsApiResource { private final static Logger logger = LoggerFactory - .getLogger(ClientApiResource.class); + .getLogger(ClientsApiResource.class); private String defaultFieldList = "joinedDate"; private String allowedFieldList = "allowedOffices"; @@ -219,12 +219,11 @@ public class ClientApiResource { @Path("{clientId}/notes") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public Response addNewClientNote(@PathParam("clientId") final Long clientId, final NoteCommand command) { + public Response addNewClientNote(@PathParam("clientId") final Long clientId, final String jsonRequestBody) { - command.setClientId(clientId); - - EntityIdentifier identifier = this.clientWritePlatformService - .addClientNote(command); + NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(null, clientId, jsonRequestBody); + + EntityIdentifier identifier = this.clientWritePlatformService.addClientNote(command); return Response.ok().entity(identifier).build(); } @@ -237,8 +236,7 @@ public class ClientApiResource { @PathParam("clientId") final Long clientId, @PathParam("noteId") final Long noteId, @Context final UriInfo uriInfo) { - NoteData note = this.clientReadPlatformService.retrieveClientNote( - clientId, noteId); + NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId); String selectedFields = ""; return this.jsonFormattingService.convertRequest(note, filterName, @@ -251,11 +249,10 @@ public class ClientApiResource { @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); + @PathParam("noteId") final Long noteId, final String jsonRequestBody) { + NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(noteId, clientId, jsonRequestBody); + EntityIdentifier identifier = this.clientWritePlatformService .updateNote(command); 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 70c1ca8e0..3459610c9 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 @@ -12,6 +12,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.configuration.service.ConfigurationReadPlatformService; import org.mifosng.platform.configuration.service.ConfigurationWritePlatformService; @@ -33,14 +34,17 @@ public class ConfigurationApiResource { @Autowired private ConfigurationWritePlatformService configurationWritePlatformService; + @Autowired + private ApiDataConversionService apiDataConversionService; + @Autowired private ApiJSONFormattingService jsonFormattingService; @GET @Path("currency") - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - @Produces({ MediaType.APPLICATION_JSON }) - public String retrieveCurrencyDataForConfiguration(@Context UriInfo uriInfo) { + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public String retrieveCurrencyDataForConfiguration(@Context final UriInfo uriInfo) { ConfigurationData configurationData = this.configurationReadPlatformService .retrieveCurrencyConfiguration(); @@ -53,13 +57,13 @@ public class ConfigurationApiResource { @PUT @Path("currency") - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - @Produces({ MediaType.APPLICATION_JSON }) - public Response updateAllowedCurrenciesForOrganisation( - OrganisationCurrencyCommand command) { + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response updateAllowedCurrenciesForOrganisation(final String jsonRequestBody) { - this.configurationWritePlatformService - .updateOrganisationCurrencies(command); + OrganisationCurrencyCommand command = this.apiDataConversionService.convertJsonToOrganisationCurrencyCommand(jsonRequestBody); + + this.configurationWritePlatformService.updateOrganisationCurrencies(command); return Response.ok().entity(command).build(); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/ClientCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/ClientCommand.java index b3d51e686..a474fed2e 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/ClientCommand.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/ClientCommand.java @@ -1,10 +1,7 @@ package org.mifosng.platform.api.commands; -import javax.xml.bind.annotation.XmlRootElement; - import org.joda.time.LocalDate; -@XmlRootElement public class ClientCommand { private final Long id; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/NoteCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/NoteCommand.java index 0ad551cf5..be0a225da 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/NoteCommand.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/NoteCommand.java @@ -1,60 +1,41 @@ package org.mifosng.platform.api.commands; -import javax.xml.bind.annotation.XmlRootElement; - /** - * Command used for add or edit notes. + * Immutable command used for create or update of notes. */ -@XmlRootElement public class NoteCommand { - private Long id; - private Long clientId; - private Long loanId; - private Long loanTransactionId; - private String note; + private final Long id; + private final Long clientId; + private final Long loanId; + private final Long loanTransactionId; + private final String note; - public NoteCommand() { - // + public NoteCommand(final Long id, Long clientId, final String note) { + this.id = id; + this.clientId = clientId; + this.loanId = null; + this.loanTransactionId = null; + this.note = note; } public Long getId() { return id; } - public void setId(Long id) { - this.id = id; - } - public Long getClientId() { return clientId; } - public void setClientId(Long clientId) { - this.clientId = clientId; - } - public Long getLoanId() { return loanId; } - public void setLoanId(Long loanId) { - this.loanId = loanId; - } - public Long getLoanTransactionId() { return loanTransactionId; } - public void setLoanTransactionId(Long loanTransactionId) { - this.loanTransactionId = loanTransactionId; - } - public String getNote() { return note; } - - public void setNote(String note) { - this.note = note; - } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/OrganisationCurrencyCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/OrganisationCurrencyCommand.java index 0251c562e..6a3e341e4 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/OrganisationCurrencyCommand.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/OrganisationCurrencyCommand.java @@ -1,28 +1,17 @@ package org.mifosng.platform.api.commands; -import java.io.Serializable; -import java.util.List; - -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement -public class OrganisationCurrencyCommand implements Serializable { +/** + * Immutable command for updating allowed currencies. + */ +public class OrganisationCurrencyCommand { private String[] currencies; - public OrganisationCurrencyCommand() { - // - } - - public OrganisationCurrencyCommand(final List selectedCurrencyCodes) { - this.currencies = selectedCurrencyCodes.toArray(new String[selectedCurrencyCodes.size()]); + public OrganisationCurrencyCommand(final String[] currencies) { + this.currencies = currencies; } public String[] getCurrencies() { return currencies; } - - public void setCurrencies(String... currencies) { - this.currencies = currencies; - } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/SubmitApproveDisburseLoanCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/SubmitApproveDisburseLoanCommand.java deleted file mode 100644 index 68106c0b7..000000000 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/SubmitApproveDisburseLoanCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.mifosng.platform.api.commands; - -public class SubmitApproveDisburseLoanCommand { - - private SubmitLoanApplicationCommand submitLoanApplicationCommand; - private LoanStateTransitionCommand approveLoanCommand; - private LoanStateTransitionCommand disburseLoanCommand; - - public SubmitApproveDisburseLoanCommand() { - // - } - - public SubmitLoanApplicationCommand getSubmitLoanApplicationCommand() { - return submitLoanApplicationCommand; - } - - public void setSubmitLoanApplicationCommand( - SubmitLoanApplicationCommand submitLoanApplicationCommand) { - this.submitLoanApplicationCommand = submitLoanApplicationCommand; - } - - public LoanStateTransitionCommand getApproveLoanCommand() { - return approveLoanCommand; - } - - public void setApproveLoanCommand( - LoanStateTransitionCommand approveLoanCommand) { - this.approveLoanCommand = approveLoanCommand; - } - - public LoanStateTransitionCommand getDisburseLoanCommand() { - return disburseLoanCommand; - } - - public void setDisburseLoanCommand( - LoanStateTransitionCommand disburseLoanCommand) { - this.disburseLoanCommand = disburseLoanCommand; - } -} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AuthenticatedUserData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AuthenticatedUserData.java index e8cdc528a..82e8d3257 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AuthenticatedUserData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AuthenticatedUserData.java @@ -3,9 +3,6 @@ package org.mifosng.platform.api.data; import java.util.ArrayList; import java.util.Collection; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement public class AuthenticatedUserData { private String username; 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 d2059ebbc..0885cd87f 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,11 +3,8 @@ package org.mifosng.platform.api.data; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlRootElement; - import org.codehaus.jackson.map.annotate.JsonFilter; -@XmlRootElement @JsonFilter("myFilter") public class ClientLoanAccountSummaryCollectionData { 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 f1b4482af..3fc74b440 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 @@ -7,7 +7,9 @@ import org.mifosng.platform.api.commands.FundCommand; import org.mifosng.platform.api.commands.LoanProductCommand; import org.mifosng.platform.api.commands.LoanStateTransitionCommand; import org.mifosng.platform.api.commands.LoanTransactionCommand; +import org.mifosng.platform.api.commands.NoteCommand; import org.mifosng.platform.api.commands.OfficeCommand; +import org.mifosng.platform.api.commands.OrganisationCurrencyCommand; import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UserCommand; @@ -36,4 +38,8 @@ public interface ApiDataConversionService { AdjustLoanTransactionCommand convertJsonToAdjustLoanTransactionCommand( Long loanId, Long transactionId, String jsonRequestBody); + + OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(String jsonRequestBody); + + NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String jsonRequestBody); } \ No newline at end of file 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 96539c087..46a2af8bc 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 @@ -24,7 +24,9 @@ import org.mifosng.platform.api.commands.FundCommand; import org.mifosng.platform.api.commands.LoanProductCommand; import org.mifosng.platform.api.commands.LoanStateTransitionCommand; import org.mifosng.platform.api.commands.LoanTransactionCommand; +import org.mifosng.platform.api.commands.NoteCommand; import org.mifosng.platform.api.commands.OfficeCommand; +import org.mifosng.platform.api.commands.OrganisationCurrencyCommand; import org.mifosng.platform.api.commands.RoleCommand; import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UserCommand; @@ -634,6 +636,66 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { return new AdjustLoanTransactionCommand(loanId, transactionId, transactionDate, note, transactionAmount); } + + @Override + public NoteCommand convertJsonToNoteCommand(final Long noteId, final Long clientId, final String json) { + if (StringUtils.isBlank(json)) { + throw new InvalidJsonException(); + } + + Type typeOfMap = new TypeToken>(){}.getType(); + Map requestMap = gsonConverter.fromJson(json, typeOfMap); + + Set supportedParams = new HashSet( + Arrays.asList("note") + ); + + checkForUnsupportedParameters(requestMap, supportedParams); + + Set modifiedParameters = new HashSet(); + + String note = extractStringParameter("note", requestMap, modifiedParameters); + + return new NoteCommand(noteId, clientId, note); + } + + @Override + public OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(final String json) { + if (StringUtils.isBlank(json)) { + throw new InvalidJsonException(); + } + + Type typeOfMap = new TypeToken>(){}.getType(); + Map requestMap = gsonConverter.fromJson(json, typeOfMap); + + Set supportedParams = new HashSet( + Arrays.asList("currencies") + ); + + checkForUnsupportedParameters(requestMap, supportedParams); + + Set modifiedParameters = new HashSet(); + + // check array + JsonParser parser = new JsonParser(); + + String[] currencies = null; + JsonElement element = parser.parse(json); + if (element.isJsonObject()) { + JsonObject object = element.getAsJsonObject(); + if (object.has("currencies")) { + modifiedParameters.add("currencies"); + JsonArray array = object.get("currencies").getAsJsonArray(); + currencies = new String[array.size()]; + for (int i=0; i requestMap, Set supportedParams) { List unsupportedParameterList = new ArrayList(); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationWritePlatformServiceJpaRepositoryImpl.java index 592b8b291..d6af2398c 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/configuration/service/ConfigurationWritePlatformServiceJpaRepositoryImpl.java @@ -12,6 +12,7 @@ import org.mifosng.platform.currency.domain.ApplicationCurrency; import org.mifosng.platform.currency.domain.ApplicationCurrencyRepository; import org.mifosng.platform.exceptions.PlatformApiDataValidationException; import org.mifosng.platform.organisation.domain.OrganisationCurrency; +import org.mifosng.platform.organisation.domain.OrganisationCurrencyRepository; import org.mifosng.platform.security.PlatformSecurityContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,12 +23,14 @@ public class ConfigurationWritePlatformServiceJpaRepositoryImpl implements Confi private final PlatformSecurityContext context; private final ApplicationCurrencyRepository applicationCurrencyRepository; + private final OrganisationCurrencyRepository organisationCurrencyRepository; @Autowired public ConfigurationWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, - final ApplicationCurrencyRepository applicationCurrencyRepository) { + final ApplicationCurrencyRepository applicationCurrencyRepository, final OrganisationCurrencyRepository organisationCurrencyRepository) { this.context = context; this.applicationCurrencyRepository = applicationCurrencyRepository; + this.organisationCurrencyRepository = organisationCurrencyRepository; } @Transactional @@ -61,6 +64,9 @@ public class ConfigurationWritePlatformServiceJpaRepositoryImpl implements Confi } } + this.organisationCurrencyRepository.deleteAll(); + this.organisationCurrencyRepository.save(allowedCurrencies); + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/organisation/domain/OrganisationCurrencyRepository.java b/mifosng-provider/src/main/java/org/mifosng/platform/organisation/domain/OrganisationCurrencyRepository.java new file mode 100644 index 000000000..f1d03ffc9 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/organisation/domain/OrganisationCurrencyRepository.java @@ -0,0 +1,8 @@ +package org.mifosng.platform.organisation.domain; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface OrganisationCurrencyRepository extends JpaRepository, JpaSpecificationExecutor { + // no added behaviour +}