add remaining state transitions and loan transactions to api format.

This commit is contained in:
Keith Woodlock
2012-05-17 02:07:41 +01:00
parent 8bc0b7461f
commit 7bb8b9359e
28 changed files with 397 additions and 1462 deletions
@@ -15,7 +15,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mifosng.ui.JaxRs311ErrorResponseHandler;
import org.springframework.security.oauth.common.OAuthProviderParameter;
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
import org.springframework.security.oauth.consumer.OAuthRequestFailedException;
@@ -27,11 +26,9 @@ import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
public class MifosNgOauthConsumerContextFilter extends
OAuthConsumerContextFilter {
public class MifosNgOauthConsumerContextFilter extends OAuthConsumerContextFilter {
private static final Log LOG = LogFactory
.getLog(MifosNgOauthConsumerContextFilter.class);
private static final Log LOG = LogFactory.getLog(MifosNgOauthConsumerContextFilter.class);
private RestTemplate noAuthRestTemplate;
@@ -41,10 +38,8 @@ public class MifosNgOauthConsumerContextFilter extends
throws IOException, ServletException {
ResponseErrorHandler defaultResponseErrorHandler = new DefaultResponseErrorHandler();
ResponseErrorHandler jaxrs311ErrorResponseHandler = new JaxRs311ErrorResponseHandler(
defaultResponseErrorHandler);
this.noAuthRestTemplate = new RestTemplate();
this.noAuthRestTemplate.setErrorHandler(jaxrs311ErrorResponseHandler);
this.noAuthRestTemplate.setErrorHandler(defaultResponseErrorHandler);
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
@@ -1,11 +1,5 @@
package org.mifosng.ui;
import org.mifosng.data.EntityIdentifier;
import org.mifosng.data.LoanRepaymentData;
import org.mifosng.data.command.AdjustLoanTransactionCommand;
import org.mifosng.data.command.LoanStateTransitionCommand;
import org.mifosng.data.command.LoanTransactionCommand;
import org.mifosng.data.command.UndoLoanDisbursalCommand;
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
public interface CommonRestOperations {
@@ -13,20 +7,4 @@ public interface CommonRestOperations {
void logout(String accessToken);
void updateProtectedResource(ProtectedResourceDetails loadProtectedResourceDetailsById);
EntityIdentifier disburseLoan(LoanStateTransitionCommand command);
EntityIdentifier undloLoanDisbursal(UndoLoanDisbursalCommand command);
LoanRepaymentData retrieveNewLoanRepaymentDetails(Long loanId);
LoanRepaymentData retrieveLoanRepaymentDetails(Long loanId, Long repaymentId);
EntityIdentifier makeLoanRepayment(LoanTransactionCommand command);
EntityIdentifier adjustLoanRepayment(AdjustLoanTransactionCommand command);
LoanRepaymentData retrieveNewLoanWaiverDetails(Long loanId);
EntityIdentifier waiveLoanAmount(LoanTransactionCommand command);
}
@@ -1,34 +1,15 @@
package org.mifosng.ui;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.mifosng.configuration.ApplicationConfigurationService;
import org.mifosng.data.ClientList;
import org.mifosng.data.EntityIdentifier;
import org.mifosng.data.ErrorResponse;
import org.mifosng.data.ErrorResponseList;
import org.mifosng.data.LoanRepaymentData;
import org.mifosng.data.command.AdjustLoanTransactionCommand;
import org.mifosng.data.command.LoanStateTransitionCommand;
import org.mifosng.data.command.LoanTransactionCommand;
import org.mifosng.data.command.UndoLoanApprovalCommand;
import org.mifosng.data.command.UndoLoanDisbursalCommand;
import org.mifosng.ui.loanproduct.ClientValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import com.thoughtworks.xstream.XStream;
@Service(value = "commonRestOperations")
public class CommonRestOperationsImpl implements CommonRestOperations {
@@ -65,231 +46,12 @@ public class CommonRestOperationsImpl implements CommonRestOperations {
this.oauthRestServiceTemplate = new OAuthRestTemplate(resource);
}
private HttpEntity<ClientList> emptyRequest() {
private HttpEntity<Object> emptyRequest() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
HttpEntity<ClientList> requestEntity = new HttpEntity<ClientList>(
requestHeaders);
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestHeaders);
return requestEntity;
}
private ErrorResponseList parseErrors(HttpStatusCodeException e) {
XStream xstream = new XStream();
xstream.alias("errorResponseList", ErrorResponseList.class);
xstream.alias("errorResponse", ErrorResponse.class);
ErrorResponseList errorList = new ErrorResponseList();
if (HttpStatus.BAD_REQUEST.equals(e.getStatusCode())) {
errorList = (ErrorResponseList) xstream.fromXML(e
.getResponseBodyAsString());
} else if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
List<ErrorResponse> errors = new ArrayList<ErrorResponse>();
errors.add(new ErrorResponse(e.getMessage(), "error", e
.getMessage()));
errorList = new ErrorResponseList(errors);
} else if (HttpStatus.UNAUTHORIZED.equals(e.getStatusCode())) {
throw new AccessDeniedException(e.getMessage());
} else {
throw e;
}
return errorList;
}
@Override
public EntityIdentifier disburseLoan(
final LoanStateTransitionCommand command) {
try {
URI restUri = URI.create(getBaseServerUrl().concat(
"api/protected/loan/disburse"));
ResponseEntity<EntityIdentifier> s = this.oauthRestServiceTemplate
.postForEntity(restUri,
loanStateTransitionApplicationRequest(command),
EntityIdentifier.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public EntityIdentifier undloLoanDisbursal(
final UndoLoanDisbursalCommand command) {
try {
URI restUri = URI.create(getBaseServerUrl().concat(
"api/protected/loan/undodisbursal"));
ResponseEntity<EntityIdentifier> s = this.oauthRestServiceTemplate
.postForEntity(restUri, undoLoanDisbursalRequest(command),
EntityIdentifier.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public LoanRepaymentData retrieveNewLoanRepaymentDetails(Long loanId) {
try {
URI restUri = URI.create(getBaseServerUrl()
.concat("api/protected/loan/").concat(loanId.toString())
.concat("/repayment/"));
ResponseEntity<LoanRepaymentData> s = this.oauthRestServiceTemplate
.exchange(restUri, HttpMethod.GET, emptyRequest(),
LoanRepaymentData.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public LoanRepaymentData retrieveLoanRepaymentDetails(Long loanId,
Long repaymentId) {
try {
URI restUri = URI.create(getBaseServerUrl()
.concat("api/protected/loan/").concat(loanId.toString())
.concat("/repayment/").concat(repaymentId.toString()));
ResponseEntity<LoanRepaymentData> s = this.oauthRestServiceTemplate
.exchange(restUri, HttpMethod.GET, emptyRequest(),
LoanRepaymentData.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public EntityIdentifier makeLoanRepayment(
final LoanTransactionCommand command) {
try {
URI restUri = URI.create(getBaseServerUrl().concat(
"api/protected/loan/repayment"));
ResponseEntity<EntityIdentifier> s = this.oauthRestServiceTemplate
.postForEntity(restUri, loanTransactionRequest(command),
EntityIdentifier.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public EntityIdentifier adjustLoanRepayment(
AdjustLoanTransactionCommand command) {
try {
URI restUri = URI.create(getBaseServerUrl().concat(
"api/protected/loan/repayment/adjust"));
ResponseEntity<EntityIdentifier> s = this.oauthRestServiceTemplate
.postForEntity(restUri,
adjustLoanRepaymentRequest(command),
EntityIdentifier.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public LoanRepaymentData retrieveNewLoanWaiverDetails(Long loanId) {
try {
URI restUri = URI.create(getBaseServerUrl()
.concat("api/protected/loan/").concat(loanId.toString())
.concat("/waive/"));
ResponseEntity<LoanRepaymentData> s = this.oauthRestServiceTemplate
.exchange(restUri, HttpMethod.GET, emptyRequest(),
LoanRepaymentData.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
@Override
public EntityIdentifier waiveLoanAmount(LoanTransactionCommand command) {
try {
URI restUri = URI.create(getBaseServerUrl().concat(
"api/protected/loan/waive"));
ResponseEntity<EntityIdentifier> s = this.oauthRestServiceTemplate
.postForEntity(restUri, loanTransactionRequest(command),
EntityIdentifier.class);
return s.getBody();
} catch (HttpStatusCodeException e) {
ErrorResponseList errorList = parseErrors(e);
throw new ClientValidationException(errorList.getErrors());
}
}
private HttpEntity<LoanTransactionCommand> loanTransactionRequest(
final LoanTransactionCommand command) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
return new HttpEntity<LoanTransactionCommand>(command, requestHeaders);
}
private HttpEntity<AdjustLoanTransactionCommand> adjustLoanRepaymentRequest(
final AdjustLoanTransactionCommand command) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
return new HttpEntity<AdjustLoanTransactionCommand>(command,
requestHeaders);
}
private HttpEntity<UndoLoanDisbursalCommand> undoLoanDisbursalRequest(
final UndoLoanDisbursalCommand command) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
return new HttpEntity<UndoLoanDisbursalCommand>(command, requestHeaders);
}
private HttpEntity<UndoLoanApprovalCommand> undoLoanApprovalRequest(
final UndoLoanApprovalCommand command) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
return new HttpEntity<UndoLoanApprovalCommand>(command, requestHeaders);
}
private HttpEntity<LoanStateTransitionCommand> loanStateTransitionApplicationRequest(
final LoanStateTransitionCommand command) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
return new HttpEntity<LoanStateTransitionCommand>(command,
requestHeaders);
}
}
@@ -1,34 +0,0 @@
package org.mifosng.ui;
import org.mifosng.data.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClientException;
public class Jax2bRestClientException extends RestClientException {
private final HttpStatus statusCode;
private final MediaType contentType;
private final ErrorResponse errorResponse;
public Jax2bRestClientException(final HttpStatus statusCode,
final MediaType contentType, final ErrorResponse errorResponse) {
super(statusCode.name() + ": on client http rest request.");
this.statusCode = statusCode;
this.contentType = contentType;
this.errorResponse = errorResponse;
}
public HttpStatus getStatusCode() {
return this.statusCode;
}
public MediaType getContentType() {
return this.contentType;
}
public ErrorResponse getErrorResponse() {
return this.errorResponse;
}
}
@@ -1,34 +0,0 @@
package org.mifosng.ui;
import org.mifosng.data.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClientException;
public class Jax2bRestServerException extends RestClientException {
private final HttpStatus statusCode;
private final MediaType contentType;
private final ErrorResponse errorResponse;
public Jax2bRestServerException(final HttpStatus statusCode,
final MediaType contentType, final ErrorResponse errorResponse) {
super(statusCode.name() + ": on client http rest request.");
this.statusCode = statusCode;
this.contentType = contentType;
this.errorResponse = errorResponse;
}
public HttpStatus getStatusCode() {
return this.statusCode;
}
public MediaType getContentType() {
return this.contentType;
}
public ErrorResponse getErrorResponse() {
return this.errorResponse;
}
}
@@ -1,49 +0,0 @@
package org.mifosng.ui;
import java.io.IOException;
import org.mifosng.data.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestClientException;
public class JaxRs311ErrorResponseHandler implements ResponseErrorHandler {
private final ResponseErrorHandler delegateResponseErrorHandler;
public JaxRs311ErrorResponseHandler(
final ResponseErrorHandler delegateResponseErrorHandler) {
this.delegateResponseErrorHandler = delegateResponseErrorHandler;
}
@Override
public boolean hasError(final ClientHttpResponse response) throws IOException {
return this.delegateResponseErrorHandler.hasError(response);
}
@Override
public void handleError(final ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
MediaType contentType = response.getHeaders().getContentType();
Jaxb2RootElementHttpMessageConverter converter = new Jaxb2RootElementHttpMessageConverter();
ErrorResponse errorResponse = (ErrorResponse) converter.read(
ErrorResponse.class, response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new Jax2bRestClientException(statusCode, contentType, errorResponse);
case SERVER_ERROR:
throw new Jax2bRestServerException(statusCode, contentType,
errorResponse);
default:
throw new RestClientException("Unknown status code [" + statusCode
+ "]");
}
}
}
@@ -1,15 +1,9 @@
package org.mifosng.ui.configuration;
import java.util.Collection;
import javax.servlet.http.HttpServletResponse;
import org.mifosng.configuration.ApplicationConfigurationService;
import org.mifosng.configuration.OAuthProviderDetails;
import org.mifosng.data.ErrorResponse;
import org.mifosng.oauth.RefreshableProtectedResourceDetailsService;
import org.mifosng.ui.CommonRestOperations;
import org.mifosng.ui.loanproduct.ClientValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth.consumer.ProtectedResourceDetailsService;
@@ -18,7 +12,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
@@ -44,17 +37,6 @@ public class ConfigurationController {
return "unAuthorizedAction";
}
@ExceptionHandler(ClientValidationException.class)
public @ResponseBody
Collection<ErrorResponse> validationException(ClientValidationException ex,
HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType("application/json");
return ex.getValidationErrors();
}
@ModelAttribute("oauthSettingsFormBean")
@RequestMapping(value = "oauth/configuration/edit", method = RequestMethod.GET)
public ModelAndView showOAuthConfiguration() {
@@ -1,82 +0,0 @@
package org.mifosng.ui.loan;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.mifosng.data.CurrencyData;
import org.mifosng.data.MoneyData;
import org.mifosng.ui.CommonRestOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @deprecated - some initial work around cashflow for CreoCore that can be deleted later.
*/
@Deprecated
@Controller
public class CashflowController {
// private final CommonRestOperations commonRestOperations;
@Autowired
public CashflowController(final CommonRestOperations commonRestOperations) {
// this.commonRestOperations = commonRestOperations;
}
@RequestMapping(value = "/portfolio/client/{clientId}/cashflow/new", method = RequestMethod.GET)
public String submitNewCashflowAnalysis(@PathVariable("clientId") final Long clientId) {
return "redirect:/createCashflowAnalysis?loanId=0&clientId={clientId}";
}
// called from webflow
public void populateLoanDetailsForCashflow(final CashflowFormBean formBean, Long loanId, Long clientId) {
formBean.setBusinessName("Fixed Example Business (Sole Trader)");
formBean.setClientName("Fixed Example Client");
CurrencyData currency = new CurrencyData("XOF", "", 0, "CFA", "currency.XOF");
MoneyData amount = MoneyData.of(currency, BigDecimal.valueOf(0));
formBean.setExpectedLoanTerm(12);
List<MultiTermExpense> investments = new ArrayList<MultiTermExpense>();
investments.add(new MultiTermExpense("equipment purchase", Integer.valueOf(0), amount.getAmount(), formBean.getExpectedLoanTerm()));
investments.add(new MultiTermExpense("Option two", Integer.valueOf(0), amount.getAmount(), formBean.getExpectedLoanTerm()));
investments.add(new MultiTermExpense("Option three", Integer.valueOf(0), amount.getAmount(), formBean.getExpectedLoanTerm()));
formBean.setInvestments(investments);
List<OneOffExpense> fixedExpenses = new ArrayList<OneOffExpense>();
fixedExpenses.add(new OneOffExpense("Fixed one", amount.getAmount()));
fixedExpenses.add(new OneOffExpense("Fixed two", amount.getAmount()));
fixedExpenses.add(new OneOffExpense("Fixed three", amount.getAmount()));
formBean.setFixedExpenses(fixedExpenses);
List<UnitInformationOfProduct> products = new ArrayList<UnitInformationOfProduct>();
products.add(new UnitInformationOfProduct("First Product", Integer.valueOf(10), BigDecimal.valueOf(Double.valueOf("12.00")), BigDecimal.valueOf(Double.valueOf("23.00"))));
products.add(new UnitInformationOfProduct("Second Product", Integer.valueOf(500), BigDecimal.valueOf(Double.valueOf("10.00")), BigDecimal.valueOf(Double.valueOf("15.00"))));
formBean.setProducts(products);
// List<ClientReadModel> individualClients = new ArrayList<ClientReadModel>(this.commonRestOperations.retrieveLoan);
// List<LoanProductReadModel> loanProducts = new ArrayList<LoanProductReadModel>(
// this.commonRestOperations.retrieveAllLoanProducts());
//
// loanFormBean.setApplicantOptions(individualClients);
// if (individualClients.size() == 1) {
// loanFormBean.setSelectedApplicantOption(individualClients.get(0)
// .getId());
// }
//
// loanFormBean.setLoanProductOptions(loanProducts);
// if (loanProducts.size() == 1) {
// loanFormBean.setSelectedLoanProductOption(loanProducts.get(0)
// .getId());
// }
}
}
@@ -1,76 +0,0 @@
package org.mifosng.ui.loan;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class CashflowFormBean implements Serializable {
private String clientName;
private String businessName;
private Integer expectedLoanTerm = Integer.valueOf(12);
private List<MultiTermExpense> investments = new ArrayList<MultiTermExpense>();
private List<OneOffExpense> fixedExpenses = new ArrayList<OneOffExpense>();
private List<UnitInformationOfProduct> products = new ArrayList<UnitInformationOfProduct>();
private Number entrepreneurInput = Double.valueOf("0");
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public List<OneOffExpense> getFixedExpenses() {
return fixedExpenses;
}
public void setFixedExpenses(List<OneOffExpense> fixedExpenses) {
this.fixedExpenses = fixedExpenses;
}
public List<UnitInformationOfProduct> getProducts() {
return products;
}
public void setProducts(List<UnitInformationOfProduct> products) {
this.products = products;
}
public Integer getExpectedLoanTerm() {
return expectedLoanTerm;
}
public void setExpectedLoanTerm(Integer expectedLoanTerm) {
this.expectedLoanTerm = expectedLoanTerm;
}
public List<MultiTermExpense> getInvestments() {
return investments;
}
public void setInvestments(List<MultiTermExpense> investments) {
this.investments = investments;
}
public Number getEntrepreneurInput() {
return entrepreneurInput;
}
public void setEntrepreneurInput(Number entrepreneurInput) {
this.entrepreneurInput = entrepreneurInput;
}
}
@@ -1,191 +0,0 @@
package org.mifosng.ui.loan;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.mifosng.data.EntityIdentifier;
import org.mifosng.data.ErrorResponse;
import org.mifosng.data.LoanRepaymentData;
import org.mifosng.data.command.AdjustLoanTransactionCommand;
import org.mifosng.data.command.LoanStateTransitionCommand;
import org.mifosng.data.command.LoanTransactionCommand;
import org.mifosng.data.command.UndoLoanDisbursalCommand;
import org.mifosng.ui.CommonRestOperations;
import org.mifosng.ui.loanproduct.ClientValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@Controller
public class LoanTransactionController {
private final CommonRestOperations commonRestOperations;
@Autowired
public LoanTransactionController(final CommonRestOperations commonRestOperations) {
this.commonRestOperations = commonRestOperations;
}
@ExceptionHandler(AccessDeniedException.class)
public String accessDeniedException() {
return "unAuthorizedAction";
}
@ExceptionHandler(ClientValidationException.class)
public @ResponseBody Collection<ErrorResponse> validationException(ClientValidationException ex, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType("application/json");
return ex.getValidationErrors();
}
private LocalDate parseStringToLocalDate(String eventDate, String dateFieldIdentifier) {
LocalDate eventLocalDate = null;
if (StringUtils.isNotBlank(eventDate)) {
try {
Locale locale = LocaleContextHolder.getLocale();
eventLocalDate = DateTimeFormat.forPattern("dd MMMM yyyy").withLocale(locale).parseLocalDate(eventDate.toLowerCase(locale));
} catch (IllegalArgumentException e) {
List<ErrorResponse> validationErrors = new ArrayList<ErrorResponse>();
validationErrors.add(new ErrorResponse("validation.msg.invalid.date.format", dateFieldIdentifier, eventDate));
throw new ClientValidationException(validationErrors);
}
}
return eventLocalDate;
}
private BigDecimal parseStringToBigDecimal(String source) {
try {
BigDecimal number = null;
if (StringUtils.isNotBlank(source)) {
String sourceWithoutSpaces = source.replaceAll(" ", "");
Locale locale = LocaleContextHolder.getLocale();
NumberFormat format = NumberFormat.getNumberInstance(locale);
Number parsedNumber = format.parse(sourceWithoutSpaces);
number = BigDecimal.valueOf(Double.valueOf(parsedNumber
.doubleValue()));
}
return number;
} catch (ParseException e) {
List<ErrorResponse> validationErrors = new ArrayList<ErrorResponse>();
validationErrors.add(new ErrorResponse("validation.msg.invalid.number.format", "amount", source));
throw new ClientValidationException(validationErrors);
}
}
@RequestMapping(consumes="application/x-www-form-urlencoded", produces="application/json", value = "/portfolio/loan/{loanId}/disburse", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody EntityIdentifier disburseLoan(
@PathVariable("loanId") final Long loanId,
@RequestParam(value="eventDate", required=false) String eventDate,
@RequestParam(value="comment", required=false) String comment) {
LocalDate disbursalDate = parseStringToLocalDate(eventDate, "eventDate");
LoanStateTransitionCommand command = new LoanStateTransitionCommand(loanId, disbursalDate, comment);
return this.commonRestOperations.disburseLoan(command);
}
@RequestMapping(value = "/portfolio/loan/{loanId}/undodisbursal", method = RequestMethod.POST)
public String undoLoanDisbursal(@PathVariable final Long loanId) {
UndoLoanDisbursalCommand command = new UndoLoanDisbursalCommand(loanId);
EntityIdentifier clientAccountId = this.commonRestOperations
.undloLoanDisbursal(command);
return "redirect:/portfolio/client/" + clientAccountId.getEntityId();
}
@RequestMapping(consumes="application/json", produces="application/json", value ="/portfolio/loan/{loanId}/repayment", method = RequestMethod.GET)
public @ResponseBody LoanRepaymentData retrieveNewLoanRepaymentDetail(@PathVariable("loanId") final Long loanId) {
return this.commonRestOperations.retrieveNewLoanRepaymentDetails(loanId);
}
@RequestMapping(consumes="application/x-www-form-urlencoded", produces="application/json", value = "/portfolio/loan/{loanId}/repayment", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody EntityIdentifier enterRepaymentOnLoan(
@PathVariable("loanId") final Long loanId,
@RequestParam(value="transactionDate", required=false) String transactionDate,
@RequestParam(value="transactionComment", required=false) String transactionComment,
@RequestParam(value="transactionAmount", required=false) String transactionAmount) {
LocalDate repaymentDate = parseStringToLocalDate(transactionDate, "transactionDate");
BigDecimal repaymentAmount = parseStringToBigDecimal(transactionAmount);
LoanTransactionCommand command = new LoanTransactionCommand(loanId,
repaymentDate, transactionComment, repaymentAmount);
return this.commonRestOperations.makeLoanRepayment(command);
}
@RequestMapping(consumes="application/json", produces="application/json", value = "/portfolio/loan/{loanId}/repayment/{repaymentId}/adjust", method = RequestMethod.GET)
public @ResponseBody LoanRepaymentData retrieveLoanRepayment(@PathVariable("loanId") final Long loanId, @PathVariable("repaymentId") final Long repaymentId) {
return this.commonRestOperations.retrieveLoanRepaymentDetails(loanId, repaymentId);
}
@RequestMapping(consumes="application/x-www-form-urlencoded", produces="application/json", value = "/portfolio/loan/{loanId}/repayment/{repaymentId}/adjust", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody EntityIdentifier adjustPreviousRepaymentOnLoan(
@PathVariable("loanId") final Long loanId, @PathVariable("repaymentId") final Long repaymentId,
@RequestParam(value="transactionDate", required=false) String transactionDate,
@RequestParam(value="transactionComment", required=false) String transactionComment,
@RequestParam(value="transactionAmount", required=false) String transactionAmount) {
LocalDate repaymentDate = parseStringToLocalDate(transactionDate, "transactionDate");
BigDecimal repaymentAmount = parseStringToBigDecimal(transactionAmount);
AdjustLoanTransactionCommand command = new AdjustLoanTransactionCommand(loanId, repaymentId, repaymentDate, transactionComment, repaymentAmount);
return this.commonRestOperations.adjustLoanRepayment(command);
}
@RequestMapping(consumes="application/json", produces="application/json", value = "/portfolio/loan/{loanId}/waive", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody LoanRepaymentData retrievePossibleWaiveDetails(@PathVariable("loanId") final Long loanId) {
return this.commonRestOperations.retrieveNewLoanWaiverDetails(loanId);
}
@RequestMapping(consumes="application/x-www-form-urlencoded", produces="application/json", value = "/portfolio/loan/{loanId}/waive", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody EntityIdentifier waiveLoanAmount(
@PathVariable("loanId") final Long loanId,
@RequestParam(value="transactionDate", required=false) String transactionDate,
@RequestParam(value="transactionComment", required=false) String transactionComment,
@RequestParam(value="transactionAmount", required=false) String transactionAmount) {
LocalDate waiveDate = parseStringToLocalDate(transactionDate, "transactionDate");
BigDecimal waiveAmount = parseStringToBigDecimal(transactionAmount);
LoanTransactionCommand command = new LoanTransactionCommand(loanId,
waiveDate, transactionComment, waiveAmount);
return this.commonRestOperations.waiveLoanAmount(command);
}
}
@@ -1,79 +0,0 @@
package org.mifosng.ui.loan;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class MultiTermExpense implements Serializable {
private String name;
private String description;
private Integer fullTermInMonths;
private Number fullAmount;
private Integer applicableTermInMonths;
private Number applicableAmount;
public MultiTermExpense() {
//
}
public MultiTermExpense(String name, Integer fullTermInMonths, BigDecimal fullAmount, Integer applicableTermInMonths) {
this.name = name;
this.fullTermInMonths = fullTermInMonths;
this.fullAmount = fullAmount;
this.applicableTermInMonths = applicableTermInMonths;
this.applicableAmount = fullAmount;
BigDecimal fullAmountAsDecimal = BigDecimal.valueOf(this.fullAmount.longValue());
if (!fullAmountAsDecimal.equals(BigDecimal.ZERO)) {
this.applicableAmount = fullAmountAsDecimal.divide(BigDecimal.valueOf(applicableTermInMonths.longValue()), RoundingMode.HALF_EVEN);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getFullTermInMonths() {
return fullTermInMonths;
}
public void setFullTermInMonths(Integer fullTermInMonths) {
this.fullTermInMonths = fullTermInMonths;
}
public Number getFullAmount() {
return fullAmount;
}
public void setFullAmount(Number fullAmount) {
this.fullAmount = fullAmount;
}
public Integer getApplicableTermInMonths() {
return applicableTermInMonths;
}
public void setApplicableTermInMonths(Integer applicableTermInMonths) {
this.applicableTermInMonths = applicableTermInMonths;
}
public Number getApplicableAmount() {
return applicableAmount;
}
public void setApplicableAmount(Number applicableAmount) {
this.applicableAmount = applicableAmount;
}
}
@@ -1,45 +0,0 @@
package org.mifosng.ui.loan;
import java.io.Serializable;
import java.math.BigDecimal;
public class OneOffExpense implements Serializable {
private String name;
private String description;
private Number amount;
public OneOffExpense() {
//
}
public OneOffExpense(String name, BigDecimal amount) {
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Number getAmount() {
return amount;
}
public void setAmount(Number amount) {
this.amount = amount;
}
}
@@ -1,57 +0,0 @@
package org.mifosng.ui.loan;
import java.io.Serializable;
import java.math.BigDecimal;
public class UnitInformationOfProduct implements Serializable {
private String name;
private Number unitsSold = Integer.valueOf(0);
private Number costPerUnit = BigDecimal.ZERO;
private Number pricePerUnit = BigDecimal.ZERO;
public UnitInformationOfProduct() {
//
}
public UnitInformationOfProduct(String name, Integer unitsSold,
BigDecimal costPerUnit, BigDecimal pricePerUnit) {
this.name = name;
this.unitsSold = unitsSold;
this.costPerUnit = costPerUnit;
this.pricePerUnit = pricePerUnit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Number getUnitsSold() {
return unitsSold;
}
public void setUnitsSold(Number unitsSold) {
this.unitsSold = unitsSold;
}
public Number getCostPerUnit() {
return costPerUnit;
}
public void setCostPerUnit(Number costPerUnit) {
this.costPerUnit = costPerUnit;
}
public Number getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(Number pricePerUnit) {
this.pricePerUnit = pricePerUnit;
}
}
@@ -1,19 +0,0 @@
package org.mifosng.ui.loanproduct;
import java.util.List;
import org.mifosng.data.ErrorResponse;
public class ClientValidationException extends RuntimeException {
private final List<ErrorResponse> validationErrors;
public ClientValidationException(List<ErrorResponse> validationErrors) {
super("Validation errors exist");
this.validationErrors = validationErrors;
}
public List<ErrorResponse> getValidationErrors() {
return validationErrors;
}
}