package com.bankaudit.rest.process.controller;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.dto.DataTableResponse;
import com.bankaudit.dto.process.AuditRatingScoreDto;
import com.bankaudit.dto.process.AuditRatingSummaryDto;
import com.bankaudit.dto.process.RatingParameterReportDto;
import com.bankaudit.process.model.AuditRatingModelOverlay;
import com.bankaudit.process.model.AuditRatingParameterScore;
import com.bankaudit.process.model.AuditRatingParameterWrapper;
import com.bankaudit.rest.process.service.AuditRatingParameterScoreService;
import com.bankaudit.rest.process.service.MyAuditsService;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/auditRatingParameterScore")
public class AuditRatingParameterScoreController {
	@Autowired
	MyAuditsService myAuditService;
	
	@Autowired
	AuditRatingParameterScoreService auditRatingParameterScoreService;
	
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(AuditRatingParameterScoreController.class);	
	
	
@RequestMapping(value = "/getAuditSchedulesforRating", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAuditSchedulesforRating(
			@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="role",required=false)String role,
			@RequestParam(value="userLevel",required=false)String userLevel,
			@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 getAuditSchedulesforRating legalEntityCode:: "+legalEntityCodeStr +" userId:: "+ userId+" financialYear ::"+financialYear+" auditTypeCode ::"+auditTypeCode);
				dataTableResponse = auditRatingParameterScoreService.getMuAuditsList(Integer.parseInt(legalEntityCodeStr),userId,search, orderColumn, orderDirection, page, size,role, userLevel, financialYear, 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 = "/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){

		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, null, null);
				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;
	}
			
	
@RequestMapping(value="/verifyCSVUpload/{legalEntityCode}/{auditId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus verifyCSVUpload(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId")String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				boolean csvUpload=auditRatingParameterScoreService.verifyCSVUpload(legalEntityCode,auditId); 
				if(csvUpload){
					serviceStatus.setResult(csvUpload);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("CSV not uploaded");
				}				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
@RequestMapping(value="/insertAuditParameter/{legalEntityCode}/{auditId}/{unitId}/{auditTypeCode}/{userId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus insertAuditParameter(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("unitId")String unitId,
			@PathVariable("auditTypeCode")String auditTypeCode,
			@PathVariable("userId")String userId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId) && !BankAuditUtil.isEmptyString(unitId) && !BankAuditUtil.isEmptyString(auditTypeCode)){
			try {
				List<AuditRatingParameterScore> auditParamScrLst=auditRatingParameterScoreService.insertAuditParameter(legalEntityCode,auditId,unitId,auditTypeCode,userId); 
				if(auditParamScrLst!=null){
					serviceStatus.setResult(auditParamScrLst);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Insertion failure");
				}				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getParametersByAuditId/{legalEntityCode}/{auditId}/{auditTypeCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getParametersByAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("userType")String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				List<AuditRatingParameterScore> auditParamsLst=auditRatingParameterScoreService.getParametersByAuditId(legalEntityCode,auditId,auditTypeCode, userType); 
				
				if(auditParamsLst!=null){
					serviceStatus.setResult(auditParamsLst);
					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="/updateAuditParamScores/{legalEntityCode}/{auditId}/{userType}",method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateAuditParamScores(@RequestBody List<AuditRatingParameterScore> auditRatingParameterScores,
			@PathVariable Integer legalEntityCode, @PathVariable String auditId, @PathVariable String userType){
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside updateAuditParamScores .... ");
		try{
			// if(auditRatingParameterWrapper!=null &&  auditRatingParameterWrapper.getAuditratingParameterScores()!=null) {
			//	List<AuditRatingParameterScore> auditRatingParameterScores= auditRatingParameterWrapper.getAuditratingParameterScores();
				if(auditRatingParameterScores!=null && auditRatingParameterScores.size()>0){
					auditRatingParameterScoreService.updateAuditParamScores(auditRatingParameterScores, legalEntityCode, auditId, userType); 
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully created");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("No data tobe save...");
				}
			//}			
		}catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure"+e.getMessage());
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/executeAnUpdateWeightedScore/{legalEntityCode}/{auditId}/{rmCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus executeAnUpdateWeightedScore(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditId") String auditId, @PathVariable("rmCode") String rmCode, @PathVariable("userType") String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
			    Boolean execute=auditRatingParameterScoreService.executeAnUpdateWeightedScore(legalEntityCode,auditId,userType);
			    serviceStatus.setResult(execute);
			    serviceStatus.setStatus("success");
			    serviceStatus.setMessage("successfully updated ");					
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getAuditRatingSummary/{legalEntityCode}/{auditId}/{rmCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditRatingSummary(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("rmCode") String rmCode,
			@PathVariable("userType") String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				List<AuditRatingSummaryDto> auditSummary=auditRatingParameterScoreService.getAuditRatingSummary(legalEntityCode,auditId,rmCode,userType); 
				if(auditSummary!=null){
					serviceStatus.setResult(auditSummary);
					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="/isModelOverlayAppliedForAuditId/{legalEntityCode}/{auditId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus isModelOverlayAppliedForAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId") String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				boolean isModelOverlayApplied=auditRatingParameterScoreService.isModelOverlayApplied(legalEntityCode,auditId); 
				if(isModelOverlayApplied){
					serviceStatus.setResult(isModelOverlayApplied);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("Model overlay applied");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Model overlay not applied yet.");
				}				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
				return serviceStatus;
	}
	
	
@RequestMapping(value="/getModelOverlayByAuditId/{legalEntityCode}/{auditId}/{rmCode}/{userId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getModelOverlayByAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId") String auditId,
			@PathVariable("rmCode") String rmCode,
			@PathVariable("userId")String userId){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId) && !BankAuditUtil.isEmptyString(rmCode) && !BankAuditUtil.isEmptyString(userId)){
			try {
				List<AuditRatingModelOverlay> auditOverlayLst=auditRatingParameterScoreService.getModelOverlayByAuditId(legalEntityCode,auditId,rmCode,userId); 
				if(auditOverlayLst!=null){
					serviceStatus.setResult(auditOverlayLst);
					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="/updateAuditRatingModelOverlays",method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateAuditRatingModelOverlays(@RequestBody List<AuditRatingModelOverlay> auditOverlays){
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside updateAuditRatingModelOverlays .... ");
		try{
			// if(auditRatingParameterWrapper!=null &&  auditRatingParameterWrapper.getAuditratingParameterScores()!=null) {
			//	List<AuditRatingModelOverlay> auditOverlays= auditRatingParameterWrapper.getAuditRatingModelOverlays();
				if(auditOverlays!=null && auditOverlays.size()>0){
					auditRatingParameterScoreService.updateAuditRatingModelOverlays(auditOverlays); 
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully created");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("No data tobe save...");
				}
			//}			
		}catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure"+e.getMessage());
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getAuditRatingScores/{legalEntityCode}/{auditId}/{rmCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditRatingScores(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("rmCode") String rmCode,
			@PathVariable("userType") String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				AuditRatingScoreDto auditScoreDto=auditRatingParameterScoreService.getAuditRatingScores(legalEntityCode,auditId,rmCode,userType); 
				if(auditScoreDto!=null){
					serviceStatus.setResult(auditScoreDto);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("sucess");
					serviceStatus.setMessage("No Scores");
				}				
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid parameter");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getAuditRatingSummaryWithOverlay/{legalEntityCode}/{auditId}/{rmCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditRatingSummaryWithOverlay(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("rmCode") String rmCode,
			@PathVariable("userType") String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				List<AuditRatingSummaryDto> auditSummary=auditRatingParameterScoreService.getAuditRatingSummaryWithOverlay(legalEntityCode,auditId,rmCode,userType); 
				if(auditSummary!=null){
					serviceStatus.setResult(auditSummary);
					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;
	}
	
	// To update the category score, when submitted by PIO or Review Team
	
@RequestMapping(value="/updateAuditRatingParameterCategoryScore/{legalEntityCode}/{auditId}/{auditTypeCode}",method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateAuditRatingParameterCategoryScore(@RequestBody List<AuditRatingSummaryDto> auditRatingParameterCategoryLst,
			@PathVariable Integer legalEntityCode, @PathVariable String auditId, @PathVariable String auditTypeCode){
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside updateAuditRatingParameterCategoryScore .... ");
		try{
			if(auditRatingParameterCategoryLst!=null && auditRatingParameterCategoryLst.size()>0){
				auditRatingParameterScoreService.updateAuditRatingParameterCategoryScore(auditRatingParameterCategoryLst, legalEntityCode, auditId, auditTypeCode); 
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully created");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("No data tobe save...");
			}		
		}catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure"+e.getMessage());
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value = "/downloadParameterScoresByAuditId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	void downloadParameterScoresByAuditId(
			@RequestParam("legalEntityCode")Integer legalEntityCode,
			@RequestParam("auditId")String auditId, 
			@RequestParam("auditTypeCode")String auditTypeCode, 
			@RequestParam(value="role")String role, 
			@RequestParam(value="userType")String userType,
			@RequestParam(value="ratingParameterStatus",required=false) String ratingParameterStatus,
			HttpServletRequest request,     
			HttpServletResponse response){
			
		try { 
			if(legalEntityCode!=null &&  !BankAuditUtil.isEmptyString(auditId) ){ 	
				ByteArrayOutputStream byteData = auditRatingParameterScoreService.downloadRatingParameterScoresByAuditId(legalEntityCode, auditId, auditTypeCode, role, userType, ratingParameterStatus);
				if(byteData!=null){ 	
					 String fileName = "QualitativeDataUpload"+"_"+auditId+"_"+legalEntityCode+".xlsx"; 	 					
					 String mimeType ="application/vnd.ms-excel";  						 
		             response.setContentType(mimeType); 
		             String headerKey = "Content-Disposition";
		             String headerValue = String.format("attachment; filename=\"%s\"", fileName);
		             response.setHeader(headerKey, headerValue); 
		             OutputStream outStream = response.getOutputStream();
		             outStream.write(byteData.toByteArray());		    
		             outStream.close();               
		    	}else {
					 response.getWriter().print("Document not exist");
		    	}     
			}else{
				response.getWriter().print("Document not exist");
			}
		}catch (Exception e) {
			e.printStackTrace();					
		}		
	} 
	
	/**** To get the data for Rating Report ****/
	
@RequestMapping(value="/getRatingReportDataByAuditId/{legalEntityCode}/{auditId}/{auditTypeCode}/{unitId}/{rmCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRatingReportDataByAuditId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("unitId")String unitId,
			@PathVariable("rmCode")String rmCode,
			@PathVariable("userType")String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				RatingParameterReportDto reportDto=auditRatingParameterScoreService.getRatingReportDataByAuditId(legalEntityCode,auditId,auditTypeCode,unitId,rmCode,userType); 
				
				if(reportDto!=null){
					serviceStatus.setResult(reportDto);
					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;
	}
	/**** ****/
	
	/************************* To get the Rating Report Data for HO with RO *************************************/
	
@RequestMapping(value="/getRatingReportDataByAuditIdHO/{legalEntityCode}/{auditId}/{auditTypeCode}/{unitId}/{rmCode}/{userType}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRatingReportDataByAuditIdHO(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("auditId")String auditId,
			@PathVariable("unitId")String unitId,
			@PathVariable("rmCode")String rmCode,
			@PathVariable("userType")String userType){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
				RatingParameterReportDto reportDto=null;
				if("RM03".equalsIgnoreCase(rmCode) || "RM04".equalsIgnoreCase(rmCode)) {
					reportDto=auditRatingParameterScoreService.getRatingReportDataByAuditIdHOROExisting(legalEntityCode,auditId,auditTypeCode,unitId,rmCode,userType);
				}else {
					reportDto=auditRatingParameterScoreService.getRatingReportDataByAuditIdHORO(legalEntityCode,auditId,auditTypeCode,unitId,rmCode,userType); 
				}
				if(reportDto!=null){
					serviceStatus.setResult(reportDto);
					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;
	}
	/************************* End here ******************************/
	
	
	/*
@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;
	}	*/
	
}
