package com.bankaudit.rest.process.controller;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.List;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.dto.process.AuditDto;
import com.bankaudit.process.model.AuditActionItem;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.RiskSummaryDtls;
import com.bankaudit.rest.process.service.MyAuditsService;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.UserService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/myAudits")
public class MyAuditsController {
	@Autowired
	MyAuditsService myAuditService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(MyAuditsController.class);
	
	private String functionId="myAudits";
	
	
@RequestMapping(value = "/getMyAudits", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAuditSchedules(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam(required=false,value="role") String role,
			@RequestParam(required = false, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				logger.info("legalEntityCode:: "+legalEntityCodeStr +" userId:: "+ userId+" role:: "+role+ "financialYear:: "+ financialYear+" auditTypeCode:: "+auditTypeCode);
				boolean validUsrforRole=userService.isUserHavingEntitlement(Integer.parseInt(legalEntityCodeStr), userId, role, functionId, "getMyAudits");
				if(validUsrforRole) {					
					dataTableResponse = myAuditService.getMuAuditsList(Integer.parseInt(legalEntityCodeStr),userId,search, orderColumn, orderDirection, page, size, role, financialYear, auditTypeCode);
					logger.info("Inside controller dataTableResponse .... "+dataTableResponse);
					dataTableResponse.setDraw(draw);
				}else {
					dataTableResponse=new DataTableResponse();
	                dataTableResponse.setError("User is not entitled for this Role ");
				}

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}

	
@RequestMapping(value = "/getAuditsForView", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAuditsForView(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam(value="unitCode",required=false) String unitCode,
			@RequestParam("roleId")String roleId,
			@RequestParam(required = false, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				System.out.println("legalEntityCode:: "+legalEntityCodeStr +" userId:: "+ userId);
				dataTableResponse = myAuditService.getAuditsForView(Integer.parseInt(legalEntityCodeStr),userId, unitCode, roleId, search, orderColumn, orderDirection, page, size, financialYear, auditTypeCode);
				System.out.println("Inside controller dataTableResponse .... "+dataTableResponse);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}

	/************* 
	* Service to get the list of Branch inspections by AIO/PIO and by BANK User 
	* AIO/PIO : auditId-> Mandatory, (Unit id is of the Audit Id)
	* BANK User: UnitId-> Mandatory (Unit id is of the Bank user)
	*************/
	
@RequestMapping(value = "/getMyBranchAudits", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getMyBranchAudits(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam(required=false,value="auditId")String auditId,  // optional as the same service used for Bank users to get All the Branches under Bank
			@RequestParam("unitId")String unitId,  // AIO/PIO unit id is of the Audit, for BANK user the unit id is of the BANK user.
			@RequestParam(required=false,value="role") String role,
			@RequestParam(required = false, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				logger.info("Inside getMyBranchAudits legalEntityCode:: "+legalEntityCodeStr +" userId:: "+ userId+" auditId::"+ auditId+" unitId::"+unitId +" role:: "+role);
				boolean validUsrforRole=userService.isUserHavingEntitlement(Integer.parseInt(legalEntityCodeStr), userId, role, functionId, "getMyBranchAudits");
				validUsrforRole=true;
				if(validUsrforRole) {					
					dataTableResponse = myAuditService.getMyBranchAudits(Integer.parseInt(legalEntityCodeStr), auditId, unitId, userId, role, search, orderColumn, orderDirection, page, size, financialYear, auditTypeCode);
					logger.info("Inside controller dataTableResponse .... "+dataTableResponse);
					dataTableResponse.setDraw(draw);
				}else {
					dataTableResponse=new DataTableResponse();
	                dataTableResponse.setError("User is not entitled for this Role ");
				}

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}
	
	
@RequestMapping(value="/getByPlanIdAndAuditId/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}/{userId}/{role}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditByPlanIdAndAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId,
			@PathVariable("userId")String userId,
			@PathVariable(required=false,value="role") String role){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) && !BankAuditUtil.isEmptyString(auditId)){
			try {
				
				logger.info("Inside getByPlanIdAndAuditId legalEntityCode:: "+legalEntityCode +" userId:: "+ userId+" role:: "+role);
				boolean validUsrforRole=userService.isUserHavingEntitlement(legalEntityCode, userId, role, functionId, "getByPlanIdAndAuditId");
				if(validUsrforRole) {					
					AuditSchedule auditDtl=myAuditService.getAuditByPlanIdAndAuditId(legalEntityCode,auditTypeCode,planId,auditId);		
					if(auditDtl!=null){
						serviceStatus.setResult(auditDtl);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("ids not found");
					}
				}else {
					AuditSchedule auditDtl=myAuditService.getAuditByPlanIdAndAuditId(legalEntityCode,auditTypeCode,planId,auditId);
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("User is not entitled for this Role ");
				}				
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
@RequestMapping(value="/getRatingAndObsCntGrpSGrpwiseByAuditId/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRatingAndObsCntGrpSGrpwiseByAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) && !BankAuditUtil.isEmptyString(auditId)){
			try {
				List ratingObscntlst=myAuditService.getRatingAndObsCntGrpSGrpwiseByAuditId(legalEntityCode,auditTypeCode,planId,auditId); 
				
				if(ratingObscntlst!=null){
					serviceStatus.setResult(ratingObscntlst);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
	
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateMyAuditStatus(@RequestBody AuditSchedule auditSchedule) {
		ServiceStatus serviceStatus = new ServiceStatus();	
		
		if (auditSchedule != null && auditSchedule.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditTypeCode())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getPlanId())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditId())
				//&& !BankAuditUtil.isEmptyString(auditSchedule.getObservationStatus())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditStatus())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getMaker())) {
			try {
				logger.info(" Inside updateMyAuditStatus of MyAuditsController ");
				logger.info("LegalEntCode:: "+auditSchedule.getLegalEntityCode()+" AuditType:: "+auditSchedule.getAuditTypeCode()+" PanId:: "+auditSchedule.getPlanId() +" AuditId:: "+auditSchedule.getAuditId()+ " audit Status :: "+auditSchedule.getAuditStatus()+" obs Status:: "+auditSchedule.getObservationStatus()+" Maker ::"+auditSchedule.getMaker());
				//logger.info("LegalEntCode:: "+auditSchedule.getLegalEntityCode()+" AuditType:: "+auditSchedule.getAuditTypeCode()+" PanId:: "+auditSchedule.getPlanId() +" AuditId:: "+auditSchedule.getAuditId()+ " audit Status :: "+auditSchedule.getAuditStatus());
				auditSchedule.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditSchedule.getLegalEntityCode()));
				auditSchedule.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				auditSchedule.setMakerTimestamp(auditSchedule.getBusinessdateTimestamp());
				
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
				
				String chkAuditEarlyStartAllowedYesORNo ="Y";  // EARLY START ALLOWED FOR AUDIT
				String isAuditObsPendingBfrAuditSubmit ="N"; // Before submitting the Audit, need to verify the status of all the Observations. The system should not allow to Submit the Audit if there any Observation in Draft or Unauth stage.
				
				if(("AI").equalsIgnoreCase(auditSchedule.getAuditStatus()) && auditSchedule.getObservationStatus()== null ) {
					chkAuditEarlyStartAllowedYesORNo= myAuditService.chkAuditEarlyStartAllowed(auditSchedule);
					logger.info("chkAuditEarlyStartAllowedYesORNo:: "+ chkAuditEarlyStartAllowedYesORNo);
					if(chkAuditEarlyStartAllowedYesORNo.equals("Y")) {
						myAuditService.updateMyAuditStatus(auditSchedule);
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("Early start of the Audit is not allowed.");
					}
					
				}else if(("AS").equalsIgnoreCase(auditSchedule.getObservationStatus())) { //instead of auditStatus, compared with observationStatus
					isAuditObsPendingBfrAuditSubmit= myAuditService.isAuditObsPendingBfrAuditSubmit(auditSchedule);
					logger.info("isAuditObsPendingBfrAuditSubmit:: "+ isAuditObsPendingBfrAuditSubmit);
					//myAuditService.updateMyAuditStatus(auditSchedule);
					if(isAuditObsPendingBfrAuditSubmit.equals("N")) {
						logger.info("in sts N:: "+ isAuditObsPendingBfrAuditSubmit);
						myAuditService.updateMyAuditStatus(auditSchedule);
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("Please approve all the Risks before submitting the Assessment.");
					}
				}else {
					logger.info("Inside UpdateSts .. .. ");
					myAuditService.updateMyAuditStatus(auditSchedule);
					
				}/*else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Early start of the Audit is not allowed.");
				}*/
			} catch (Exception e) {

				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}

		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}

		return serviceStatus;
	}
	
	
@RequestMapping(value="/getAuditsForDashboard",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditsForDashboard(
			@RequestParam("legalEntityCode") Integer legalEntityCode,
			@RequestParam(value="userId",required=false) String userId,
			@RequestParam(value="role",required=false) String role,
			@RequestParam(value="period",required=false) String period,
			@RequestParam(value="fromDate",required=false) String fromDate,
			@RequestParam(value="toDate",required=false) String toDate){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(legalEntityCode.toString()) ){
			try {
				List myAuditsLst= null;
				myAuditsLst=myAuditService.getAuditsForDashboard(legalEntityCode,userId,role,period,fromDate,toDate); 
				
				if(myAuditsLst!=null){
					serviceStatus.setResult(myAuditsLst);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
@RequestMapping(value = "/getAdHocAuditsForAuditor", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAdHocAuditsForAuditor(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam(required = false, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				dataTableResponse = myAuditService.getAdHocAuditsForAuditor(Integer.parseInt(legalEntityCodeStr),userId,search, orderColumn, orderDirection, page, size, financialYear, auditTypeCode);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}
	
	
@RequestMapping(value = "/getAdHocAuditsForManagement", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAdHocAuditsForManagement(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam(required = false, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				dataTableResponse = myAuditService.getAdHocAuditsForManagement(Integer.parseInt(legalEntityCodeStr),userId,search, orderColumn, orderDirection, page, size, financialYear, auditTypeCode);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}
	
	/** This method is for getting the current and previous Audit details of an Audit, for the Audit Report
	 *******/
	
@RequestMapping(value="/getAuditDetailsForReport",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditDetailsForReport(
			@RequestParam("legalEntityCode") Integer legalEntityCode,
			@RequestParam(value="userId",required=false) String userId,
			@RequestParam(value="role",required=false) String role,
			@RequestParam(value="auditId",required=true)String auditId,
			@RequestParam(value="planId",required=true)String planId,
			@RequestParam(value="auditTypeCode",required=true)String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		logger.info("Inside getAuditDetailsForReport .."+ legalEntityCode +" auditId.." +auditId +" planId.." +planId);
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(legalEntityCode.toString()) 
				&& !BankAuditUtil.isEmptyString(auditId.toString()) && !BankAuditUtil.isEmptyString(planId.toString())
				&& !BankAuditUtil.isEmptyString(auditTypeCode.toString())){
			try {
				List myAuditDetails= null;
				myAuditDetails=myAuditService.getAuditDetailsForReport(legalEntityCode,userId,role,auditId,planId,auditTypeCode); 
				
				if(myAuditDetails!=null){
					serviceStatus.setResult(myAuditDetails);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	/** This method is for retrieving and grouping the observations based on Audit Group & Audit SubGroup of an Audit, for the Audit Report
	 *******/
	
@RequestMapping(value="/getAuditObservationDetailsForReport",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditObservationDetailsForReport(
			@RequestParam("legalEntityCode") Integer legalEntityCode,
			@RequestParam(value="userId",required=false) String userId,
			@RequestParam(value="role",required=false) String role,
			@RequestParam(value="auditId",required=true)String auditId,
			@RequestParam(value="planId",required=true)String planId,
			@RequestParam(value="auditTypeCode",required=true)String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		logger.info("Inside myAuditObservationDetails .."+ legalEntityCode +" auditId.." +auditId +" planId.." +planId);
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(legalEntityCode.toString()) 
				&& !BankAuditUtil.isEmptyString(auditId.toString()) && !BankAuditUtil.isEmptyString(planId.toString())
				&& !BankAuditUtil.isEmptyString(auditTypeCode.toString())){
			try {
				List myAuditObservationDetails= null;
				myAuditObservationDetails=myAuditService.getAuditObservationDetailsForReport(legalEntityCode,userId,role,auditId,planId,auditTypeCode); 
				
				if(myAuditObservationDetails!=null){
					serviceStatus.setResult(myAuditObservationDetails);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
	
	/** This method is for retrieving and grouping the observations based on Audit Group & Audit SubGroup of an Audit, for the Audit Report
	 *******/
	
@RequestMapping(value="/getFetchObservationsExtrnlAndInsertInternal",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getFetchObservationsExtrnlAndInsertInternal(
			@RequestParam("legalEntityCode") Integer legalEntityCode,
			@RequestParam(value="userId",required=false) String userId,
			@RequestParam(value="unitId",required=false) String unitId,
			@RequestParam(value="auditId",required=true)String auditId,
			@RequestParam(value="planId",required=true)String planId,
			@RequestParam(value="auditTypeCode",required=true)String auditTypeCode,
			@RequestParam(value="forceFetch",required=false)String forceFetch){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		logger.info("Inside getFetchObservationsExtrnlAndInsertInternal .."+ legalEntityCode +" auditId.." +auditId +" planId.." +planId+" unitId.."+unitId+" auditTypeCode.."+auditTypeCode+"forceFetch.."+forceFetch);
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(legalEntityCode.toString()) 
				&& !BankAuditUtil.isEmptyString(auditId.toString()) && !BankAuditUtil.isEmptyString(planId.toString())
				&& !BankAuditUtil.isEmptyString(auditTypeCode.toString())){
			try {
				String fetchObsStatus ="N";
				if(forceFetch == null || "".equalsIgnoreCase(forceFetch)){
					fetchObsStatus=myAuditService.getAlreadyFetchObsStatus(legalEntityCode,userId,auditId,planId,auditTypeCode);
					logger.info(" fetchObsStatus .."+ fetchObsStatus);
				}
				
				if(("Y".equalsIgnoreCase(forceFetch)) || (fetchObsStatus !=null && fetchObsStatus== "N")){
					Boolean status=myAuditService.getFetchObservationsExtrnlAndInsertInternal(legalEntityCode,userId,unitId,auditId,planId,auditTypeCode);
					logger.info(" getFetchObservationsExtrnlAndInsertInternal status .."+ status);
					if(status!=null && status== true){
						serviceStatus.setResult("N");
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("Account Informations fetched succesfully ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("ids not found");
					}
				}else if(fetchObsStatus!=null && fetchObsStatus== "Y"){
					serviceStatus.setResult(fetchObsStatus);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("Account Information already fetched, would you like to fetch it again ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
				
				
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}

	/***** This method is for verifying weather the Accounts has been already fetched (through Interface) for the Audit or not *******/
	
@RequestMapping(value="/getAlreadyFetchObsStatus",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAlreadyFetchObsStatus(
			@RequestParam("legalEntityCode") Integer legalEntityCode,
			@RequestParam(value="userId",required=false) String userId,
			@RequestParam(value="auditId",required=true)String auditId,
			@RequestParam(value="planId",required=true)String planId,
			@RequestParam(value="auditTypeCode",required=true)String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		logger.info("Inside myAuditObservationDetails .."+ legalEntityCode +" auditId.." +auditId +" planId.." +planId+" auditTypeCode.."+auditTypeCode);
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(legalEntityCode.toString()) 
				&& !BankAuditUtil.isEmptyString(auditId.toString()) && !BankAuditUtil.isEmptyString(planId.toString())
				&& !BankAuditUtil.isEmptyString(auditTypeCode.toString())){
			try {
				List myAuditObservationDetails= null;
				//myAuditObservationDetails=myAuditService.getFetchObservationsExtrnlAndInsertInternal(legalEntityCode,userId,unitId,auditId,planId,auditTypeCode);
				
				String status=myAuditService.getAlreadyFetchObsStatus(legalEntityCode,userId,auditId,planId,auditTypeCode);
				logger.info("Inside MyAuditController getAlreadyFetchObsStatus .."+ status);
				
				if(status!=null && status== "N"){
					serviceStatus.setResult(status);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("Account Informations yet to be fetched ");
				}else if(status!=null && status== "Y"){
					serviceStatus.setResult(status);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("Account Informations already fetched ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
	
	
@RequestMapping(value = "/getAuditsOnDashboard", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAuditsOnDashboard(
			@RequestParam(value="draw",required=false) String drawStr,
			@RequestParam(value="length",required=false) String length,
			@RequestParam(value="start",required=false) String start,
			@RequestParam(value="search[value]",required=false) String search,
			@RequestParam(value="order[0][column]",required=false) String orderColumnStr,
			@RequestParam(value="order[0][dir]",required=false) String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam(value="auditTypeCode",required=false)String auditTypeCode,
			@RequestParam(value="auditStatus",required=false)String auditStatus){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(userId) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt( (BankAuditUtil.isEmptyString(drawStr) == true ?"0":drawStr));
				Integer size = Integer.parseInt( (BankAuditUtil.isEmptyString(length) == true ?"10":length));
				Integer page = Integer.parseInt((BankAuditUtil.isEmptyString(start) == true ?"0":start)) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				logger.info("legalEntityCode:: "+legalEntityCodeStr +" userId:: "+ userId);
				// orderColumnStr = (BankAuditUtil.isEmptyString(orderColumnStr)) == true ? "-1" :orderColumnStr;
				dataTableResponse = myAuditService.getAuditsOnDashboard(Integer.parseInt(legalEntityCodeStr),userId,search, orderColumn, orderDirection, page, size, auditStatus, auditTypeCode);
				logger.info("Inside controller dataTableResponse .... "+dataTableResponse);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}
	
	
	
@RequestMapping(value="/getRiskCntByBusinssAndProdByRiskCapId/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}/{auditStatus}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRiskCntByBusinssAndProdByRiskCapId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId,
			@PathVariable("auditStatus")String auditStatus){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) 
				 	&& !BankAuditUtil.isEmptyString(auditId) && !BankAuditUtil.isEmptyString(auditStatus)){
			try {
				List<RiskSummaryDtls> ratingObscntlst=myAuditService.getRiskCntByBusinssAndProdByRiskCapId(legalEntityCode,auditTypeCode,planId,auditId,auditStatus); 
				
				if(ratingObscntlst!=null){
					serviceStatus.setResult(ratingObscntlst);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
@RequestMapping(method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus createRiskSummary(@RequestBody List<RiskSummaryDtls> riskSummaryDtlsLst){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(riskSummaryDtlsLst!=null){
			try {
				
				myAuditService.createRiskSummary(riskSummaryDtlsLst);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successsfully saved");
			} catch (Exception e) {
				
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				logger.error(e.getStackTrace());
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("at least one observation is required ");
		}
		
		return serviceStatus; 
	}
	
	/************************************** BaaSDOS Implementation ***********************************/
	
@RequestMapping(value = "/getAuditsForLetter", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAuditSForLetter(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam("userId")String userId,
			@RequestParam("role")String role,
			@RequestParam(required = false, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				logger.info("legalEntityCode:: "+legalEntityCodeStr +" userId:: "+ userId);
				dataTableResponse = myAuditService.getAuditsForLetter(Integer.parseInt(legalEntityCodeStr),userId,search, orderColumn, orderDirection, page, size, role, financialYear, auditTypeCode);
				logger.info("Inside controller getAuditSForLetter .... "+dataTableResponse);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}
	
	
@RequestMapping(value="/getObservationCntByAgAsgActByAuditIdOrType/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}/{auditStatus}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getObservationCntByAgAsgPrcByAuditIdOrType(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId,
			@PathVariable("auditStatus")String auditStatus){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) 
				 	&& !BankAuditUtil.isEmptyString(auditId) && !BankAuditUtil.isEmptyString(auditStatus)){
			try {
				List<RiskSummaryDtls> ratingObscntlst=myAuditService.getObservationCntByAgAsgActByAuditIdOrType(legalEntityCode,auditTypeCode,planId,auditId,auditStatus); 
				
				if(ratingObscntlst!=null){
					serviceStatus.setResult(ratingObscntlst);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	// Method to get the Summary details for a Respond and Review screen
	
@RequestMapping(value="/getObservationCntByAgAsgActByAuditIdOrTypeRespRvw/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}/{auditStatus}/{process}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getObservationCntByAgAsgActByAuditIdOrTypeRespRvw(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId,
			@PathVariable("auditStatus")String auditStatus,
			@PathVariable("process")String process){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) 
				 	&& !BankAuditUtil.isEmptyString(auditId) && !BankAuditUtil.isEmptyString(auditStatus)){
			try {
				logger.info("getObservationCntByAgAsgActByAuditIdOrTypeRespRvw .. "+ auditId+" :: "+process);
				List<RiskSummaryDtls> ratingObscntlst=myAuditService.getObservationCntByAgAsgActByAuditIdOrTypeRespRvw(legalEntityCode,auditTypeCode,planId,auditId,auditStatus,process); 
				
				if(ratingObscntlst!=null){
					serviceStatus.setResult(ratingObscntlst);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	// BAAS Implementation, to get review status (Review/Accepted/Further Complance)
		
@RequestMapping(value="/getObservationCntByAuditIdReview/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}/{auditStatus}/{process}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
		ServiceStatus getObservationCntByAuditIdReview(
				@PathVariable("legalEntityCode") Integer legalEntityCode,
				@PathVariable("auditTypeCode") String auditTypeCode,
				@PathVariable("planId") String planId,
				@PathVariable("auditId")String auditId,
				@PathVariable("auditStatus")String auditStatus,
				@PathVariable(value="process" ,required=false)String process){
			ServiceStatus serviceStatus=new ServiceStatus();
			
			if(legalEntityCode!=null
					&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) 
					 	&& !BankAuditUtil.isEmptyString(auditId) && !BankAuditUtil.isEmptyString(auditStatus)){
				try {
					List<RiskSummaryDtls> obsCntlst=myAuditService.getObservationCntByAuditIdReview(legalEntityCode,auditTypeCode,planId,auditId,auditStatus); 
					
					if(obsCntlst!=null){
						serviceStatus.setResult(obsCntlst);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("ids not found");
					}
					
				} catch (Exception e) {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("failure");
					e.printStackTrace();
				}
				
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("invalid parameter");
			}
					return serviceStatus;
		}
		
	
	/*
@RequestMapping(value="/updateReportStatus",method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateReportStatus(@RequestBody AuditSchedule auditSchedule) { 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (auditSchedule != null && auditSchedule.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditId())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditStatus())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getReportStatus())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getReportReviewer())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getMaker())) {
			try {
				logger.info(" Inside updateMyAuditStatus of MyAuditsController ");
				logger.info("LegalEntCode:: "+auditSchedule.getLegalEntityCode()+" AuditType:: "+auditSchedule.getAuditTypeCode()+" PanId:: "+auditSchedule.getPlanId() +" AuditId:: "+auditSchedule.getAuditId()+ " audit Status :: "+auditSchedule.getAuditStatus()+" audit Status:: "+auditSchedule.getAuditStatus()+" Maker ::"+auditSchedule.getMaker());
				
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
				myAuditService.updateReportStatus(auditSchedule);
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}*/
	
	
@RequestMapping(value="/updateReportStatus",method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateReportStatus(@RequestBody AuditDto auditDto) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (auditDto != null && auditDto.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(auditDto.getAuditId())
				&& !BankAuditUtil.isEmptyString(auditDto.getPlanId())
				&& !BankAuditUtil.isEmptyString(auditDto.getAuditTypeCode())
				&& !BankAuditUtil.isEmptyString(auditDto.getAuditStatus())
				&& !BankAuditUtil.isEmptyString(auditDto.getReportStatus())
				/*&& !BankAuditUtil.isEmptyString(auditDto.getReportReviewer())
				&& !BankAuditUtil.isEmptyString(auditDto.getAllocator())*/
				) {	 			
			try {
				logger.info(" Inside updateReportStatus of MyAuditsController ");
				logger.info("LegalEntCode:: "+auditDto.getLegalEntityCode()+" AuditType:: "+auditDto.getAuditTypeCode()+" PanId:: "+auditDto.getPlanId() +" AuditId:: "+auditDto.getAuditId()+ " audit Status :: "+auditDto.getAuditStatus()+" audit Status:: "+auditDto.getAuditStatus()+" Maker ::"+auditDto.getAllocator());
				
				String isAuditObsPending ="N"; // Before submitting the Audit, need to verify the status of all the Observations. The system should not allow to Submit the Audit if there any Observation in Draft or Unauth stage.
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
				
				if("RRR".equalsIgnoreCase(auditDto.getReportStatus()) ) {
					isAuditObsPending= myAuditService.isAuditObsPending(auditDto, "RRR");
					logger.info("isAuditObsPendingBfrAuditSubmit:: "+ isAuditObsPending);
				}
				if(isAuditObsPending.equals("N")) {
					logger.info("in sts N:: "+ isAuditObsPending);
					String updateReportStatus = myAuditService.updateReportStatus(auditDto);
					if(BankAuditConstant.FAILED.equalsIgnoreCase(updateReportStatus)){
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("failure");
					}
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Please attend all the Checklists, before sending it for Review.");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	/* This method is same as getAuditByPlanIdAndAuditId, but included few more values as part of Inspection Report */ 
	//use to get audit for allocation -> edit icon
	
@RequestMapping(value="/getAuditDetailsByPlanIdAndAuditId/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditDetailsByPlanIdAndAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) && !BankAuditUtil.isEmptyString(auditId)){
			try {
				AuditSchedule auditDtl=myAuditService.getAuditByPlanIdAndAuditId(legalEntityCode,auditTypeCode,planId,auditId); 
				
				if(auditDtl!=null){
					serviceStatus.setResult(auditDtl);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}
				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	/* This method is same as getAuditByPlanIdAndAuditId, but included few more values as part of Rating Parameter, can be enhanced*/ 
	
@RequestMapping(value="/getAuditByAuditId/{legalEntityCode}/{auditTypeCode}/{planId}/{auditId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditByAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId,
			@PathVariable("auditId")String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId) && !BankAuditUtil.isEmptyString(auditId)){
			try {
				AuditSchedule auditDtl=myAuditService.getAuditByAuditId(legalEntityCode,auditTypeCode,planId,auditId); 
				if(auditDtl!=null){
					serviceStatus.setResult(auditDtl);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/updateMyAuditRatingParameterStatus",method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateMyAuditRatingParameterStatus(@RequestBody AuditSchedule auditSchedule) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (auditSchedule != null && auditSchedule.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditId())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getRatingParameterMaker())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getRatingParameterStatus())) {
			try {
				logger.info(" Inside updateMyAuditRatingParameterStatus of MyAuditsController ");
				logger.info("LegalEntCode:: "+auditSchedule.getLegalEntityCode()+" AuditId:: "+auditSchedule.getAuditId()+" PMaker:: "+auditSchedule.getRatingParameterMaker() +" PReviewer:: "+auditSchedule.getRatingParameterReviewer()+ " PStatus :: "+auditSchedule.getRatingParameterStatus()+" Rvwr Assign:: "+auditSchedule.getRatingParameterReviewerAssigned());
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
				myAuditService.updateMyAuditRatingParameterStatus(auditSchedule);				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");				
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/updateMyAuditRatingParameterRemarksSave",method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateMyAuditRatingParameterRemarksSave(@RequestBody AuditSchedule auditSchedule) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (auditSchedule != null && auditSchedule.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditId())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getRatingParameterMaker())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getRatingParameterStatus())) {
			try {
				logger.info(" Inside updateMyAuditRatingParameterRemarksSave of MyAuditsController ");
				logger.info("LegalEntCode:: "+auditSchedule.getLegalEntityCode()+" AuditId:: "+auditSchedule.getAuditId()+" PMaker:: "+auditSchedule.getRatingParameterMaker() +" PReviewer:: "+auditSchedule.getRatingParameterReviewer()+ " PStatus :: "+auditSchedule.getRatingParameterStatus()+" Rvwr Assign:: "+auditSchedule.getRatingParameterReviewerAssigned());
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
				myAuditService.updateMyAuditRatingParameterRemarksSave(auditSchedule);				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");				
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/updateMyAuditRatingComplianceObsScoreStatus",method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateMyAuditRatingComplianceObsScoreStatus(@RequestBody AuditSchedule auditSchedule) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (auditSchedule != null && auditSchedule.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(auditSchedule.getAuditId())
				&& !BankAuditUtil.isEmptyString(auditSchedule.getRatingComplianceStatus())) {
			try {
				logger.info(" Inside updateRatingComplianceObsScoreStatus of MyAuditsController ");
				logger.info("LegalEntCode:: "+auditSchedule.getLegalEntityCode()+" AuditId:: "+auditSchedule.getAuditId()+" PMaker:: "+auditSchedule.getRatingParameterMaker() +" PReviewer:: "+auditSchedule.getRatingParameterReviewer()+ " PStatus :: "+auditSchedule.getRatingParameterStatus()+" Rvwr Assign:: "+auditSchedule.getRatingParameterReviewerAssigned());
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
				myAuditService.updateMyAuditRatingComplianceObsScoreStatus(auditSchedule);
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");				
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getAuditActionItem",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditActionItem(
			@RequestParam("legalEntityCode")Integer legalEntityCode, 
			@RequestParam("auditTypeCode") String auditTypeCode, 
			@RequestParam("auditId") String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info(" Inside getAuditActionItem .."+legalEntityCode  +" :: "+auditTypeCode+" :: " +auditId );		 
			try {
				List<AuditActionItem> auditActionItemList = myAuditService.getAuditActionItem(legalEntityCode, auditTypeCode, auditId);
				serviceStatus.setResult(auditActionItemList);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully fetched the record");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				logger.error(e.getStackTrace());
			} 
		
		return serviceStatus; 
	} 
	
	
@RequestMapping(value="/checkLatestITISAuditDetailsForUnit",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus checkLatestITISAuditDetailsForUnit(
			@RequestParam("legalEntityCode")Integer legalEntityCode, 
			@RequestParam("unitId") String unitId){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info(" Inside checkLatestITISAuditDetailsForUnit .."+legalEntityCode  +" :: "+unitId);		 
			try {
				AuditSchedule auditSchedule = myAuditService.checkLatestITISAuditDetailsForUnit(legalEntityCode,unitId);
				if(auditSchedule != null){ 
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("The latest ITIS inspection for the Bank was conducted in  the FY "+auditSchedule.getLastAuditFinancialYearITIS());
				}else{
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("ITIS inspection was not conducted for the Bank in the last three Financial years");
				}
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				logger.error(e);
			} 
		
		return serviceStatus; 
	} 

	/****************************** BaaSDOS Implementation End here **********************************/
	
}
