tidy up remaing commands for converstion from json.

This commit is contained in:
Keith Woodlock
2012-07-18 13:22:42 +01:00
parent 3f201d7ffa
commit e71e88a10b
13 changed files with 130 additions and 125 deletions
@@ -54,8 +54,8 @@ public class AdditionalFieldsApiResource {
private ApiJSONFormattingService jsonFormattingService; private ApiJSONFormattingService jsonFormattingService;
@GET @GET
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON }) @Produces({MediaType.APPLICATION_JSON})
public String datasets(@QueryParam("type") String type, public String datasets(@QueryParam("type") String type,
@Context UriInfo uriInfo) { @Context UriInfo uriInfo) {
@@ -69,8 +69,8 @@ public class AdditionalFieldsApiResource {
@GET @GET
@Path("{type}/{set}/{id}") @Path("{type}/{set}/{id}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON }) @Produces({MediaType.APPLICATION_JSON})
public String extraData(@PathParam("type") final String type, public String extraData(@PathParam("type") final String type,
@PathParam("set") final String set, @PathParam("id") final Long id, @PathParam("set") final String set, @PathParam("id") final Long id,
@Context UriInfo uriInfo) { @Context UriInfo uriInfo) {
@@ -98,8 +98,8 @@ public class AdditionalFieldsApiResource {
@POST @POST
@Path("{type}/{set}/{id}") @Path("{type}/{set}/{id}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON }) @Produces({MediaType.APPLICATION_JSON})
public Response saveExtraData(@PathParam("type") final String type, public Response saveExtraData(@PathParam("type") final String type,
@PathParam("set") final String set, @PathParam("id") final Long id, @PathParam("set") final String set, @PathParam("id") final Long id,
String reqbody) { String reqbody) {
@@ -38,10 +38,10 @@ import org.springframework.stereotype.Component;
@Path("/clients") @Path("/clients")
@Component @Component
@Scope("singleton") @Scope("singleton")
public class ClientApiResource { public class ClientsApiResource {
private final static Logger logger = LoggerFactory private final static Logger logger = LoggerFactory
.getLogger(ClientApiResource.class); .getLogger(ClientsApiResource.class);
private String defaultFieldList = "joinedDate"; private String defaultFieldList = "joinedDate";
private String allowedFieldList = "allowedOffices"; private String allowedFieldList = "allowedOffices";
@@ -219,12 +219,11 @@ public class ClientApiResource {
@Path("{clientId}/notes") @Path("{clientId}/notes")
@Consumes({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_JSON})
@Produces({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); NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(null, clientId, jsonRequestBody);
EntityIdentifier identifier = this.clientWritePlatformService EntityIdentifier identifier = this.clientWritePlatformService.addClientNote(command);
.addClientNote(command);
return Response.ok().entity(identifier).build(); return Response.ok().entity(identifier).build();
} }
@@ -237,8 +236,7 @@ public class ClientApiResource {
@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) {
NoteData note = this.clientReadPlatformService.retrieveClientNote( NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId);
clientId, noteId);
String selectedFields = ""; String selectedFields = "";
return this.jsonFormattingService.convertRequest(note, filterName, return this.jsonFormattingService.convertRequest(note, filterName,
@@ -251,11 +249,10 @@ public class ClientApiResource {
@Produces({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON})
public Response updateClientNote( public Response updateClientNote(
@PathParam("clientId") final Long clientId, @PathParam("clientId") final Long clientId,
@PathParam("noteId") final Long noteId, final NoteCommand command) { @PathParam("noteId") final Long noteId, final String jsonRequestBody) {
command.setClientId(clientId);
command.setId(noteId);
NoteCommand command = this.apiDataConversionService.convertJsonToNoteCommand(noteId, clientId, jsonRequestBody);
EntityIdentifier identifier = this.clientWritePlatformService EntityIdentifier identifier = this.clientWritePlatformService
.updateNote(command); .updateNote(command);
@@ -12,6 +12,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.ApiJSONFormattingService; import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
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;
@@ -33,14 +34,17 @@ public class ConfigurationApiResource {
@Autowired @Autowired
private ConfigurationWritePlatformService configurationWritePlatformService; private ConfigurationWritePlatformService configurationWritePlatformService;
@Autowired
private ApiDataConversionService apiDataConversionService;
@Autowired @Autowired
private ApiJSONFormattingService jsonFormattingService; private ApiJSONFormattingService jsonFormattingService;
@GET @GET
@Path("currency") @Path("currency")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON }) @Produces({MediaType.APPLICATION_JSON})
public String retrieveCurrencyDataForConfiguration(@Context UriInfo uriInfo) { public String retrieveCurrencyDataForConfiguration(@Context final UriInfo uriInfo) {
ConfigurationData configurationData = this.configurationReadPlatformService ConfigurationData configurationData = this.configurationReadPlatformService
.retrieveCurrencyConfiguration(); .retrieveCurrencyConfiguration();
@@ -53,13 +57,13 @@ public class ConfigurationApiResource {
@PUT @PUT
@Path("currency") @Path("currency")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON }) @Produces({MediaType.APPLICATION_JSON})
public Response updateAllowedCurrenciesForOrganisation( public Response updateAllowedCurrenciesForOrganisation(final String jsonRequestBody) {
OrganisationCurrencyCommand command) {
this.configurationWritePlatformService OrganisationCurrencyCommand command = this.apiDataConversionService.convertJsonToOrganisationCurrencyCommand(jsonRequestBody);
.updateOrganisationCurrencies(command);
this.configurationWritePlatformService.updateOrganisationCurrencies(command);
return Response.ok().entity(command).build(); return Response.ok().entity(command).build();
} }
@@ -1,10 +1,7 @@
package org.mifosng.platform.api.commands; package org.mifosng.platform.api.commands;
import javax.xml.bind.annotation.XmlRootElement;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
@XmlRootElement
public class ClientCommand { public class ClientCommand {
private final Long id; private final Long id;
@@ -1,60 +1,41 @@
package org.mifosng.platform.api.commands; 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 { public class NoteCommand {
private Long id; private final Long id;
private Long clientId; private final Long clientId;
private Long loanId; private final Long loanId;
private Long loanTransactionId; private final Long loanTransactionId;
private String note; 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() { public Long getId() {
return id; return id;
} }
public void setId(Long id) {
this.id = id;
}
public Long getClientId() { public Long getClientId() {
return clientId; return clientId;
} }
public void setClientId(Long clientId) {
this.clientId = clientId;
}
public Long getLoanId() { public Long getLoanId() {
return loanId; return loanId;
} }
public void setLoanId(Long loanId) {
this.loanId = loanId;
}
public Long getLoanTransactionId() { public Long getLoanTransactionId() {
return loanTransactionId; return loanTransactionId;
} }
public void setLoanTransactionId(Long loanTransactionId) {
this.loanTransactionId = loanTransactionId;
}
public String getNote() { public String getNote() {
return note; return note;
} }
public void setNote(String note) {
this.note = note;
}
} }
@@ -1,28 +1,17 @@
package org.mifosng.platform.api.commands; package org.mifosng.platform.api.commands;
import java.io.Serializable; /**
import java.util.List; * Immutable command for updating allowed currencies.
*/
import javax.xml.bind.annotation.XmlRootElement; public class OrganisationCurrencyCommand {
@XmlRootElement
public class OrganisationCurrencyCommand implements Serializable {
private String[] currencies; private String[] currencies;
public OrganisationCurrencyCommand() { public OrganisationCurrencyCommand(final String[] currencies) {
// this.currencies = currencies;
}
public OrganisationCurrencyCommand(final List<String> selectedCurrencyCodes) {
this.currencies = selectedCurrencyCodes.toArray(new String[selectedCurrencyCodes.size()]);
} }
public String[] getCurrencies() { public String[] getCurrencies() {
return currencies; return currencies;
} }
public void setCurrencies(String... currencies) {
this.currencies = currencies;
}
} }
@@ -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;
}
}
@@ -3,9 +3,6 @@ package org.mifosng.platform.api.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class AuthenticatedUserData { public class AuthenticatedUserData {
private String username; private String username;
@@ -3,11 +3,8 @@ package org.mifosng.platform.api.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.map.annotate.JsonFilter; import org.codehaus.jackson.map.annotate.JsonFilter;
@XmlRootElement
@JsonFilter("myFilter") @JsonFilter("myFilter")
public class ClientLoanAccountSummaryCollectionData { public class ClientLoanAccountSummaryCollectionData {
@@ -7,7 +7,9 @@ import org.mifosng.platform.api.commands.FundCommand;
import org.mifosng.platform.api.commands.LoanProductCommand; import org.mifosng.platform.api.commands.LoanProductCommand;
import org.mifosng.platform.api.commands.LoanStateTransitionCommand; import org.mifosng.platform.api.commands.LoanStateTransitionCommand;
import org.mifosng.platform.api.commands.LoanTransactionCommand; 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.OfficeCommand;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.commands.RoleCommand; 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;
@@ -36,4 +38,8 @@ public interface ApiDataConversionService {
AdjustLoanTransactionCommand convertJsonToAdjustLoanTransactionCommand( AdjustLoanTransactionCommand convertJsonToAdjustLoanTransactionCommand(
Long loanId, Long transactionId, String jsonRequestBody); Long loanId, Long transactionId, String jsonRequestBody);
OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(String jsonRequestBody);
NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String jsonRequestBody);
} }
@@ -24,7 +24,9 @@ import org.mifosng.platform.api.commands.FundCommand;
import org.mifosng.platform.api.commands.LoanProductCommand; import org.mifosng.platform.api.commands.LoanProductCommand;
import org.mifosng.platform.api.commands.LoanStateTransitionCommand; import org.mifosng.platform.api.commands.LoanStateTransitionCommand;
import org.mifosng.platform.api.commands.LoanTransactionCommand; 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.OfficeCommand;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.commands.RoleCommand; 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;
@@ -634,6 +636,66 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
return new AdjustLoanTransactionCommand(loanId, transactionId, transactionDate, note, transactionAmount); 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<Map<String, Object>>(){}.getType();
Map<String, Object> requestMap = gsonConverter.fromJson(json, typeOfMap);
Set<String> supportedParams = new HashSet<String>(
Arrays.asList("note")
);
checkForUnsupportedParameters(requestMap, supportedParams);
Set<String> modifiedParameters = new HashSet<String>();
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<Map<String, Object>>(){}.getType();
Map<String, Object> requestMap = gsonConverter.fromJson(json, typeOfMap);
Set<String> supportedParams = new HashSet<String>(
Arrays.asList("currencies")
);
checkForUnsupportedParameters(requestMap, supportedParams);
Set<String> modifiedParameters = new HashSet<String>();
// 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<array.size(); i++) {
currencies[i] = array.get(i).getAsString();
}
}
}
//
return new OrganisationCurrencyCommand(currencies);
}
private void checkForUnsupportedParameters(Map<String, ?> requestMap, Set<String> supportedParams) { private void checkForUnsupportedParameters(Map<String, ?> requestMap, Set<String> supportedParams) {
List<String> unsupportedParameterList = new ArrayList<String>(); List<String> unsupportedParameterList = new ArrayList<String>();
@@ -12,6 +12,7 @@ import org.mifosng.platform.currency.domain.ApplicationCurrency;
import org.mifosng.platform.currency.domain.ApplicationCurrencyRepository; import org.mifosng.platform.currency.domain.ApplicationCurrencyRepository;
import org.mifosng.platform.exceptions.PlatformApiDataValidationException; import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
import org.mifosng.platform.organisation.domain.OrganisationCurrency; import org.mifosng.platform.organisation.domain.OrganisationCurrency;
import org.mifosng.platform.organisation.domain.OrganisationCurrencyRepository;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -22,12 +23,14 @@ public class ConfigurationWritePlatformServiceJpaRepositoryImpl implements Confi
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final ApplicationCurrencyRepository applicationCurrencyRepository; private final ApplicationCurrencyRepository applicationCurrencyRepository;
private final OrganisationCurrencyRepository organisationCurrencyRepository;
@Autowired @Autowired
public ConfigurationWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, public ConfigurationWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
final ApplicationCurrencyRepository applicationCurrencyRepository) { final ApplicationCurrencyRepository applicationCurrencyRepository, final OrganisationCurrencyRepository organisationCurrencyRepository) {
this.context = context; this.context = context;
this.applicationCurrencyRepository = applicationCurrencyRepository; this.applicationCurrencyRepository = applicationCurrencyRepository;
this.organisationCurrencyRepository = organisationCurrencyRepository;
} }
@Transactional @Transactional
@@ -61,6 +64,9 @@ public class ConfigurationWritePlatformServiceJpaRepositoryImpl implements Confi
} }
} }
this.organisationCurrencyRepository.deleteAll();
this.organisationCurrencyRepository.save(allowedCurrencies);
if (!dataValidationErrors.isEmpty()) { if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
} }
@@ -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<OrganisationCurrency, Long>, JpaSpecificationExecutor<OrganisationCurrency> {
// no added behaviour
}