added ApplySavings command

This commit is contained in:
rishy
2014-06-26 17:51:36 +05:30
parent 45a372944c
commit ef43647a10
10 changed files with 161 additions and 20 deletions
@@ -13,6 +13,8 @@ import javax.ws.rs.core.MediaType;
import org.mifosplatform.batch.domain.BatchRequest;
import org.mifosplatform.batch.domain.BatchResponse;
import org.mifosplatform.batch.exception.ErrorHandler;
import org.mifosplatform.batch.exception.ErrorInfo;
import org.mifosplatform.batch.serialization.BatchRequestJsonHelper;
import org.mifosplatform.batch.service.BatchApiService;
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
@@ -96,9 +98,11 @@ public class BatchApiResource {
BatchResponse response = new BatchResponse();
response.setStatusCode(400);
response.setBody("One or more of your requests couldn't be completed because of errors" +
" - Rolling back the whole transaction\n\n" + e.toString());
//Gets an object of type ErrorInfo, containing information about raised exception
ErrorInfo ex = ErrorHandler.handler(e);
response.setStatusCode(ex.getStatusCode());
response.setBody(ex.getMessage());
result.add(response);
return this.toApiJsonSerializer.serialize(result);
@@ -70,6 +70,7 @@ public class CommandStrategyProvider {
this.commandStrategies.put(CommandContext.resource("clients").method("POST").build(), "createClientCommandStrategy");
this.commandStrategies.put(CommandContext.resource("clients\\/\\d+").method("PUT").build(), "updateClientCommandStrategy");
this.commandStrategies.put(CommandContext.resource("loans").method("POST").build(), "applyLoanCommandStrategy");
this.commandStrategies.put(CommandContext.resource("savingsaccounts").method("POST").build(), "applySavingsCommandStrategy");
}
}
@@ -9,8 +9,6 @@ import org.mifosplatform.portfolio.loanaccount.api.LoansApiResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
/**
* Implements {@link org.mifosplatform.batch.command.CommandStrategy} and applies a new loan
* on an existing client. It passes the contents of the body from the BatchRequest
@@ -61,7 +59,7 @@ public class ApplyLoanCommandStrategy implements CommandStrategy{
ErrorInfo ex = ErrorHandler.handler(e);
response.setStatusCode(ex.getStatusCode());
response.setBody(new Gson().toJson(ex));
response.setBody(ex.getMessage());
}
return response;
@@ -0,0 +1,67 @@
package org.mifosplatform.batch.command.internal;
import org.mifosplatform.batch.command.CommandStrategy;
import org.mifosplatform.batch.domain.BatchRequest;
import org.mifosplatform.batch.domain.BatchResponse;
import org.mifosplatform.batch.exception.ErrorHandler;
import org.mifosplatform.batch.exception.ErrorInfo;
import org.mifosplatform.portfolio.savings.api.SavingsAccountsApiResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Implements {@link org.mifosplatform.batch.command.CommandStrategy} and applies a new savings
* on an existing client. It passes the contents of the body from the BatchRequest
* to {@link org.mifosplatform.portfolio.client.api.SavingsAccountsApiResource} and gets back
* the response. This class will also catch any errors raised by
* {@link org.mifosplatform.portfolio.client.api.SavingsAccountsApiResource} and map those errors
* to appropriate status codes in BatchResponse.
*
* @author Rishabh Shukla
*
* @see org.mifosplatform.batch.command.CommandStrategy
* @see org.mifosplatform.batch.domain.BatchRequest
* @see org.mifosplatform.batch.domain.BatchResponse
*/
@Component
public class ApplySavingsCommandStrategy implements CommandStrategy{
private final SavingsAccountsApiResource savingsAccountsApiResource;
@Autowired
public ApplySavingsCommandStrategy(final SavingsAccountsApiResource savingsAccountsApiResource) {
this.savingsAccountsApiResource = savingsAccountsApiResource;
}
@Override
public BatchResponse execute(BatchRequest request) {
final BatchResponse response = new BatchResponse();
final String responseBody;
response.setRequestId(request.getRequestId());
response.setHeaders(request.getHeaders());
//Try-catch blocks to map exceptions to appropriate status codes
try {
//Calls 'submitApplication' function from 'SavingsAccountsApiResource' to Apply Savings to an existing client
responseBody = savingsAccountsApiResource.submitApplication(request.getBody());
response.setStatusCode(200);
//Sets the body of the response after savings is successfully applied
response.setBody(responseBody);
}
catch (RuntimeException e) {
//Gets an object of type ErrorInfo, containing information about raised exception
ErrorInfo ex = ErrorHandler.handler(e);
response.setStatusCode(ex.getStatusCode());
response.setBody(ex.getMessage());
}
return response;
}
}
@@ -9,8 +9,6 @@ import org.mifosplatform.portfolio.client.api.ClientsApiResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
/**
* Implements {@link org.mifosplatform.batch.command.CommandStrategy} to handle
* creation of a new client. It passes the contents of the body from the BatchRequest
@@ -61,7 +59,7 @@ public class CreateClientCommandStrategy implements CommandStrategy{
ErrorInfo ex = ErrorHandler.handler(e);
response.setStatusCode(ex.getStatusCode());
response.setBody(new Gson().toJson(ex));
response.setBody(ex.getMessage());
}
return response;
@@ -9,8 +9,6 @@ import org.mifosplatform.portfolio.client.api.ClientsApiResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
/**
* Implements {@link org.mifosplatform.batch.command.CommandStrategy} and updates the
* information of an existing client. It passes the contents of the body from the BatchRequest
@@ -64,7 +62,7 @@ public class UpdateClientCommandStrategy implements CommandStrategy {
ErrorInfo ex = ErrorHandler.handler(e);
response.setStatusCode(ex.getStatusCode());
response.setBody(new Gson().toJson(ex));
response.setBody(ex.getMessage());
}
return response;
@@ -4,6 +4,7 @@ import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceN
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
import org.mifosplatform.infrastructure.core.exception.UnsupportedParameterException;
import org.springframework.transaction.TransactionException;
/**
* Provides an Error Handler method that returns an object of type {@link ErrorInfo}
@@ -35,14 +36,17 @@ public class ErrorHandler extends RuntimeException{
public static ErrorInfo handler(RuntimeException exception) {
if(exception instanceof AbstractPlatformResourceNotFoundException) {
return new ErrorInfo(404, 1001, "Requested Resource was not Found");
return new ErrorInfo(404, 1001, "Requested Resource was not Found: Check for any unavailable Parameters");
} else if(exception instanceof UnsupportedParameterException) {
return new ErrorInfo(400, 2001, "UnsupportedParameterException: Check for any unsupported Parameters");
} else if(exception instanceof PlatformApiDataValidationException) {
return new ErrorInfo(400, 2002, "ApiDataValidationException: Re-Check the input data");
} else if(exception instanceof PlatformDataIntegrityException) {
return new ErrorInfo(403, 3001, "DataIntegrityException: Check for any duplicate data");
}
} else if(exception instanceof TransactionException) {
return new ErrorInfo(400, 4001, "TransactionException: One or more of your requests were unsuccessful," +
" reverting back all the transactions in this Batch Request");
}
return new ErrorInfo(500, 9999, "Internal Server Error");
@@ -91,7 +91,7 @@ public class BatchApiServiceImpl implements BatchApiService{
//Some detail information about the error
final ErrorInfo conflictError = new ErrorInfo(Status.CONFLICT.getStatusCode(), 8001,
"Parent request with id " + parentResponse.getRequestId() + " was erroneous!");
response.setBody(new Gson().toJson(conflictError));
response.setBody(conflictError.getMessage());
responseList.add(response);
//Proceed with next list entry