Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk Fiscal Event implementation in iFix-Core and iFix-adapter #28

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EventController {
private EventService eventService;

@PostMapping("/events/v1/_push")
public ResponseEntity<?>pushEvents( @Valid @RequestBody EventRequest eventRequest) {
public ResponseEntity<EventResponse>pushEvents(@RequestBody EventRequest eventRequest) {
log.debug("request received");
eventService.pushEvent(eventRequest);
EventResponse response=new EventResponse();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.egov.ifix.exception;

import org.egov.common.contract.response.Error;
import org.egov.common.contract.response.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@ControllerAdvice
public class ExceptionAdvise {

/**
* @param request
* @param exception
* @return
*/
@ExceptionHandler({Exception.class})
@ResponseBody
public ResponseEntity<?> exceptionHandler(HttpServletRequest request, Exception exception) {
ErrorResponse errorResponse = new ErrorResponse();
List<Error> errorList = new ArrayList<>();
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

try {
if (exception instanceof BindException) {
BindException bindException = (BindException) exception;
errorResponse.setErrors(getBindingErrors(bindException.getBindingResult()));
} else if (exception instanceof HttpCustomException) {
HttpCustomException customException = (HttpCustomException) exception;
Map<String, String> errorMap = customException.getErrors();

if (errorMap != null && !errorMap.isEmpty()) {
for (Map.Entry entry : errorMap.entrySet()) {
Error error = new Error();
error.setCode((String) entry.getKey());
error.setMessage((String) entry.getValue());
errorList.add(error);
}
} else {
Error error = new Error();
error.setCode(customException.getCode());
error.setMessage(customException.getMessage());
errorList.add(error);
}
errorResponse.setErrors(errorList);
httpStatus = customException.getHttpStatus();
}else {
errorResponse.setErrors(new ArrayList(Collections.singletonList(
new Error(HttpStatus.INTERNAL_SERVER_ERROR.name(), exception.getMessage(),
exception.getLocalizedMessage()))));
}
} catch (Exception e) {
errorResponse.setErrors(new ArrayList(Collections.singletonList(
new Error(HttpStatus.INTERNAL_SERVER_ERROR.name(), "An unhandled exception occurred",
"Exception occur while binding exceptions"))));
}

return new ResponseEntity(errorResponse, httpStatus);
}


/**
* @param bindingResult
* @return
*/
private List<Error> getBindingErrors(BindingResult bindingResult) {
List<Error> errors = new ArrayList<>();
List<ObjectError> objectErrors = bindingResult.getAllErrors();

for (ObjectError objectError : objectErrors) {
Error error = new Error();
String[] codes = objectError.getCodes();
error.setCode(codes[0]);
error.setMessage(objectError.getDefaultMessage());
errors.add(error);
}
return errors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.egov.ifix.exception;

import lombok.Data;

import java.util.Map;

@Data
public class GenericCustomException extends RuntimeException{
private String code;
private String message;
private Map<String, String> errors;

public GenericCustomException(String code, String message, Map<String, String> errors) {
this.code = code;
this.message = message;
this.errors = errors;
}

public GenericCustomException(String code, String message) {
this.code = code;
this.message = message;
}

public GenericCustomException(Map<String, String> errors) {
this.errors = errors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.egov.ifix.exception;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.util.Map;

@Data
public class HttpCustomException extends RuntimeException{
private String code;
private String message;
private Map<String, String> errors;
private HttpStatus httpStatus = null;

public HttpCustomException() {
}

public HttpCustomException(String code, String message, HttpStatus httpStatus) {
this.code = code;
this.message = message;
this.httpStatus = httpStatus;
}

public HttpCustomException(Map<String, String> errors, HttpStatus httpStatus) {
this.message = errors.toString();
this.errors = errors;
this.httpStatus = httpStatus;
}
public HttpCustomException(Map<String, String> errors) {
this.message = errors.toString();
this.errors = errors;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.egov.ifix.models.EventTypeEnum;
import org.egov.ifix.models.FiscalEvent;
import org.egov.ifix.service.ChartOfAccountService;
import org.egov.ifix.service.ProjectService;
import org.egov.ifix.utils.ApplicationConfiguration;
import org.egov.ifix.utils.EventConstants;
import org.egov.ifix.utils.MasterDataMappingUtil;
Expand Down Expand Up @@ -42,8 +43,9 @@ public class BillEventTypeImpl implements EventMapper {
private ApplicationConfiguration applicationConfiguration;

private ChartOfAccountService chartOfAccountService;



@Autowired
private ProjectService projectService;

@Autowired
public BillEventTypeImpl(ApplicationConfiguration applicationConfiguration,ChartOfAccountService chartOfAccountService ) {
Expand All @@ -56,6 +58,9 @@ public BillEventTypeImpl(ApplicationConfiguration applicationConfiguration,Chart
public List<FiscalEvent> transformData(JsonObject data) {
log.info("Bill event impl executing");
JsonArray entities = data.getAsJsonObject(EventConstants.EVENT).getAsJsonArray(EventConstants.ENTITY);
String clientProjectCode = data.getAsJsonObject(EventConstants.EVENT).get(EventConstants.PROJECT_ID).getAsString();
String iFixProjectId = projectService.getProjectId(clientProjectCode, data);

List<FiscalEvent> fiscalEvents = new ArrayList<FiscalEvent>();
for (int i = 0; i < entities.size(); i++) {
JsonArray demands = entities.get(i).getAsJsonObject().getAsJsonArray(DEMAND);
Expand All @@ -66,7 +71,8 @@ public List<FiscalEvent> transformData(JsonObject data) {
FiscalEvent fiscalEvent = FiscalEvent.builder().tenantId(applicationConfiguration.getTenantId())
.eventType(getEventType()).eventTime(Instant.now().toEpochMilli())
.referenceId(demand.get(REFERANCE_ID).getAsString()).parentEventId(null).parentReferenceId(null)
.amountDetails(getAmounts(demand,data)).build();
.amountDetails(getAmounts(demand,data))
.projectId(iFixProjectId).build();
fiscalEvents.add(fiscalEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.egov.ifix.models.EventTypeEnum;
import org.egov.ifix.models.FiscalEvent;
import org.egov.ifix.service.ChartOfAccountService;
import org.egov.ifix.service.ProjectService;
import org.egov.ifix.utils.ApplicationConfiguration;
import org.egov.ifix.utils.EventConstants;
import org.egov.ifix.utils.MasterDataMappingUtil;
Expand Down Expand Up @@ -43,6 +44,9 @@ public class DemandEventTypeImpl implements EventMapper {

private ChartOfAccountService chartOfAccountService;

@Autowired
private ProjectService projectService;

@Autowired
public DemandEventTypeImpl(ApplicationConfiguration applicationConfiguration, ChartOfAccountService chartOfAccountService) {
this.applicationConfiguration = applicationConfiguration;
Expand All @@ -52,6 +56,9 @@ public DemandEventTypeImpl(ApplicationConfiguration applicationConfiguration, Ch
@Override
public List<FiscalEvent> transformData(JsonObject data) {
JsonArray entities = data.getAsJsonObject(EventConstants.EVENT).getAsJsonArray(EventConstants.ENTITY);
String clientProjectCode = data.getAsJsonObject(EventConstants.EVENT).get(EventConstants.PROJECT_ID).getAsString();
String iFixProjectId = projectService.getProjectId(clientProjectCode, data);

List<FiscalEvent> fiscalEvents = new ArrayList<FiscalEvent>();
for (int i = 0; i < entities.size(); i++) {
JsonArray demands = entities.get(i).getAsJsonObject().getAsJsonArray("Demands");
Expand All @@ -62,7 +69,8 @@ public List<FiscalEvent> transformData(JsonObject data) {
FiscalEvent fiscalEvent = FiscalEvent.builder().tenantId(applicationConfiguration.getTenantId())
.eventType(getEventType()).eventTime(Instant.now().toEpochMilli())
.referenceId(demand.get(REFERANCE_ID).getAsString()).parentEventId(null).parentReferenceId(null)
.amountDetails(getAmounts(demand,data)).build();
.amountDetails(getAmounts(demand,data))
.projectId(iFixProjectId).build();
fiscalEvents.add(fiscalEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.egov.ifix.models.EventTypeEnum;
import org.egov.ifix.models.FiscalEvent;
import org.egov.ifix.service.ChartOfAccountService;
import org.egov.ifix.service.ProjectService;
import org.egov.ifix.utils.ApplicationConfiguration;
import org.egov.ifix.utils.EventConstants;
import org.egov.ifix.utils.MasterDataMappingUtil;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class PaymentEventTypeImpl implements EventMapper {

private ChartOfAccountService chartOfAccountService;

@Autowired
private ProjectService projectService;

@Autowired
public PaymentEventTypeImpl(ApplicationConfiguration applicationConfiguration,
ChartOfAccountService chartOfAccountService) {
Expand All @@ -63,14 +67,18 @@ public String getEventType() {
public List<FiscalEvent> transformData(JsonObject data) {
log.info("PAYMENT event impl executing");
JsonArray payments = data.getAsJsonObject(EventConstants.EVENT).getAsJsonArray(EventConstants.ENTITY);
String clientProjectCode = data.getAsJsonObject(EventConstants.EVENT).get(EventConstants.PROJECT_ID).getAsString();
String iFixProjectId = projectService.getProjectId(clientProjectCode, data);

List<FiscalEvent> fiscalEvents = new ArrayList<FiscalEvent>();
for (int i = 0; i < payments.size(); i++) {
JsonObject payment = payments.get(i).getAsJsonObject().getAsJsonObject(PAYMENT);

FiscalEvent fiscalEvent = FiscalEvent.builder().tenantId(applicationConfiguration.getTenantId())
.eventType(getEventType()).eventTime(Instant.now().toEpochMilli())
.referenceId(payment.get(REFERANCE_ID).getAsString()).parentEventId(null).parentReferenceId(null)
.amountDetails(getAmounts(payment,data)).build();
.amountDetails(getAmounts(payment,data))
.projectId(iFixProjectId).build();

fiscalEvents.add(fiscalEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.egov.ifix.models.EventTypeEnum;
import org.egov.ifix.models.FiscalEvent;
import org.egov.ifix.service.ChartOfAccountService;
import org.egov.ifix.service.ProjectService;
import org.egov.ifix.utils.ApplicationConfiguration;
import org.egov.ifix.utils.EventConstants;
import org.egov.ifix.utils.MasterDataMappingUtil;
Expand Down Expand Up @@ -44,6 +45,9 @@ public class ReceiptEventTypeImpl implements EventMapper {

private ChartOfAccountService chartOfAccountService;

@Autowired
private ProjectService projectService;

@Autowired
public ReceiptEventTypeImpl(ApplicationConfiguration applicationConfiguration,
ChartOfAccountService chartOfAccountService) {
Expand All @@ -59,15 +63,21 @@ public String getEventType() {
@Override
public List<FiscalEvent> transformData(JsonObject data) {
log.info("Receipt event impl executing");
JsonArray payments = data.getAsJsonObject(EventConstants.EVENT).getAsJsonArray(EventConstants.ENTITY);
JsonObject eventJsonObject = data.getAsJsonObject(EventConstants.EVENT);

JsonArray payments = eventJsonObject.getAsJsonArray(EventConstants.ENTITY);
List<FiscalEvent> fiscalEvents = new ArrayList<FiscalEvent>();
for (int i = 0; i < payments.size(); i++) {
JsonObject payment = payments.get(i).getAsJsonObject().getAsJsonObject(PAYMENT);

String clientProjectCode = eventJsonObject.get(EventConstants.PROJECT_ID).getAsString();
String iFixProjectId = projectService.getProjectId(clientProjectCode, data);

FiscalEvent fiscalEvent = FiscalEvent.builder().tenantId(applicationConfiguration.getTenantId())
.eventType(getEventType()).eventTime(Instant.now().toEpochMilli())
.referenceId(payment.get(REFERANCE_ID).getAsString()).parentEventId(null).parentReferenceId(null)
.amountDetails(getAmounts(payment, data)).build();
.amountDetails(getAmounts(payment, data))
.projectId(iFixProjectId).build();

fiscalEvents.add(fiscalEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;


/**
* Fiscal event request along with request metadata
Expand All @@ -28,7 +30,7 @@ public class FiscalEventRequest {
private RequestHeader requestHeader = null;

@JsonProperty("fiscalEvent")
private FiscalEvent fiscalEvent = null;
private List<FiscalEvent> fiscalEvent = null;


}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class EventPostingDetail {

@Size(max=4000,min=0)
private String error;

@Size(max=36)
private String projectId;


@Lob
Expand Down
Loading