package com.bankaudit.rest.process.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.process.model.AuditLevelDocumentDetails;
import com.bankaudit.process.model.AuditObservation;
import com.bankaudit.process.model.AuditRatingComplianceObsScoreTmp;
import com.bankaudit.process.model.AuditRatingParameterScoreTmp;
import com.bankaudit.process.model.ChecklistOfflineInspectionTmp;
import com.bankaudit.rest.process.dao.AuditRatingParameterScoreDao;
import com.bankaudit.util.BankAuditUtil;

@Service("ifcEnsureBaasDataOfflineUploadService")
//@Transactional("transactionManager")
public class IfcDataOfflineUploadServiceImpl implements IfcDataOfflineUploadService {
 
	@Autowired
	IfcDataOfflineUploadTransactionServiceImpl ifcEnsureBaasDataOfflineUploadTransactionServiceImpl;
	
	@Autowired
	AuditRatingParameterScoreDao auditRatingParameterScoreDao;
	
	static final Logger logger = Logger.getLogger(IfcDataOfflineUploadServiceImpl.class);
	
	@Override
	public String uploadOfflineInspection(AuditLevelDocumentDetails auditLevelDocumentDetails,
			CommonsMultipartFile[] files, String fileName, String reportLanguage) {
		for (CommonsMultipartFile commonsMultipartFile : files) {
			AuditLevelDocumentDetails auditLevelDocumentDetailsToSave = new AuditLevelDocumentDetails(); 
			
			try{  
				BeanUtils.copyProperties(auditLevelDocumentDetailsToSave, auditLevelDocumentDetails); 
				String pattern = "yyyyMMddHHmmssSSS"; 
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);  
				String referencePath =  BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
				auditLevelDocumentDetailsToSave.setReferencePath(referencePath);  
				auditLevelDocumentDetailsToSave.setFileName(commonsMultipartFile.getOriginalFilename()); 						
				
				/*auditLevelDocumentDetailsToSave.setReferenceNo(ifcEnsureBaasDataDao.getMaxReferenceNumber()); 
				ifcEnsureBaasDataDao.save(auditLevelDocumentDetailsToSave); 
				ifcEnsureBaasDataDao.flushSession();*/
				
				auditLevelDocumentDetailsToSave = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.savePartial(auditLevelDocumentDetailsToSave);	
				
				//uploadId = auditLevelDocumentDetailsToSave.getId(); 
			}catch (Exception e) {
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause());  
				return BankAuditConstant.FAILED;
			}
			
			List<String> rolesListInspector = Arrays.asList(("G2,G8").split(","));  //Inspection
			List<String> rolesListCompliance = Arrays.asList(("G3,G31").split(","));	//Compliance or Response
			List<String> rolesListReviewer = Arrays.asList(("G4,G7").split(","));       //review/Scrutiny			 
			String role = auditLevelDocumentDetails.getUserRoleId();
			
			Integer legalEntityCode = auditLevelDocumentDetailsToSave.getLegalEntityCode();
			String auditId = auditLevelDocumentDetailsToSave.getAuditId();
			String auditTypeCode = auditLevelDocumentDetailsToSave.getAuditTypeCode();				 
			String maker =  auditLevelDocumentDetailsToSave.getMaker();
			Date makerTimestamp = auditLevelDocumentDetailsToSave.getMakerTimestamp();
			
			/****  read excel Start  ********/
			List<ChecklistOfflineInspectionTmp> checklistOfflineInspectionList = new ArrayList<ChecklistOfflineInspectionTmp>();
			try(XSSFWorkbook uploadExcel = new XSSFWorkbook(commonsMultipartFile.getInputStream())){
				//XSSFWorkbook uploadExcel = new XSSFWorkbook(commonsMultipartFile.getInputStream());
				XSSFSheet sheet = uploadExcel.getSheetAt(0);
				sheet.removeRow(sheet.getRow(0)); 
				
				//Validate AuditId and AuditType Code
				Row firstRow = sheet.getRow(1);
				String auditIdExcel = firstRow.getCell(1).getStringCellValue();
				String auditTypeCodeExcel =firstRow.getCell(2).getStringCellValue();
				if(!auditIdExcel.equalsIgnoreCase(auditId) || !auditTypeCodeExcel.equalsIgnoreCase(auditTypeCode)){					
					auditLevelDocumentDetailsToSave.setUploadStatus(BankAuditConstant.VALIDATION_FAILED);	
					auditLevelDocumentDetailsToSave = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.updateDocStatus(auditLevelDocumentDetailsToSave);	
					return BankAuditConstant.VALIDATION_FAILED;
				}
				
				Iterator<Row> rowIterator = sheet.iterator();	
				
				if(rolesListInspector.contains(role)){
			        while (rowIterator.hasNext()) {
			        	ChecklistOfflineInspectionTmp checklistOfflineInspection =  new ChecklistOfflineInspectionTmp();
			        	Row row = rowIterator.next();  
			        	  
			        	checklistOfflineInspection.setLegalEntityCode(legalEntityCode); 
			        	checklistOfflineInspection.setObservationId(readCellValue(row.getCell(0))); 
			        	checklistOfflineInspection.setGroupObservationId(readCellValue(row.getCell(0))); 
			        	checklistOfflineInspection.setAuditId(readCellValue(row.getCell(1)));
			        	checklistOfflineInspection.setAuditTypeCode(readCellValue(row.getCell(2)));
			        	checklistOfflineInspection.setUnitCode(readCellValue(row.getCell(3)));
			        	checklistOfflineInspection.setChapter(readCellValue(row.getCell(4)));
			        	checklistOfflineInspection.setSection(readCellValue(row.getCell(5)));
			        	checklistOfflineInspection.setProduct(readCellValue(row.getCell(6)));
			        	checklistOfflineInspection.setProcess(readCellValue(row.getCell(7)));
			        	checklistOfflineInspection.setChecklist(readCellValue(row.getCell(8)));
			        	checklistOfflineInspection.setChecklistCode(readCellValue(row.getCell(9)));
			        	checklistOfflineInspection.setMappingId(readCellValue(row.getCell(10)));
			        	checklistOfflineInspection.setSubmissionStatus(readCellValue(row.getCell(11)));
			        	checklistOfflineInspection.setComplianceStatus(readCellValue(row.getCell(12)));
			        	checklistOfflineInspection.setCriticality(readCellValue(row.getCell(13)));
			        	checklistOfflineInspection.setIoComments(readCellValue(row.getCell(14)));
			        	checklistOfflineInspection.setStandardStmt("");
						checklistOfflineInspection.setStandardStmtHindi("");
						checklistOfflineInspection.setStandardStmtPrintFlag("");
			            
			        	checklistOfflineInspectionList.add(checklistOfflineInspection);
			        }
				}else if(rolesListCompliance.contains(role) || rolesListReviewer.contains(role)){
					 while (rowIterator.hasNext()) {
				        	ChecklistOfflineInspectionTmp checklistOfflineInspection =  new ChecklistOfflineInspectionTmp();
				        	Row row = rowIterator.next();  
				        	  
				        	checklistOfflineInspection.setLegalEntityCode(legalEntityCode); 
				        	checklistOfflineInspection.setObservationId(readCellValue(row.getCell(0))); 
				        	checklistOfflineInspection.setGroupObservationId(readCellValue(row.getCell(0))); 
				        	checklistOfflineInspection.setAuditId(readCellValue(row.getCell(1)));
				        	checklistOfflineInspection.setAuditTypeCode(readCellValue(row.getCell(2)));
				        	checklistOfflineInspection.setUnitCode(readCellValue(row.getCell(3)));
				        	checklistOfflineInspection.setChapter(readCellValue(row.getCell(4)));
				        	checklistOfflineInspection.setSection(readCellValue(row.getCell(5)));
				        	checklistOfflineInspection.setProduct(readCellValue(row.getCell(6)));
				        	checklistOfflineInspection.setProcess(readCellValue(row.getCell(7)));
				        	checklistOfflineInspection.setChecklist(readCellValue(row.getCell(8)));
				        	checklistOfflineInspection.setChecklistCode(readCellValue(row.getCell(9)));
				        	checklistOfflineInspection.setMappingId(readCellValue(row.getCell(10)));
				        	checklistOfflineInspection.setSubmissionStatus(readCellValue(row.getCell(11)));
				        	checklistOfflineInspection.setComplianceStatus(readCellValue(row.getCell(12)));
				        	checklistOfflineInspection.setCriticality(readCellValue(row.getCell(13)));
				        	checklistOfflineInspection.setIoComments(readCellValue(row.getCell(14)));
				        	checklistOfflineInspection.setStandardStmt("");
							checklistOfflineInspection.setStandardStmtHindi("");
							checklistOfflineInspection.setStandardStmtPrintFlag("");
				        	
				        	checklistOfflineInspection.setResponseStatus(readCellValue(row.getCell(15)));  
							checklistOfflineInspection.setReviewStatus(readCellValue(row.getCell(16))); 							
							checklistOfflineInspection.setReviewStatusAttribute(readCellValue(row.getCell(17)));  							
							checklistOfflineInspection.setLastResponseReviewRemarks(readCellValue(row.getCell(18)));	 				
							checklistOfflineInspection.setResponseReviewRemarks(readCellValue(row.getCell(19)));
							
							if(rolesListReviewer.contains(role)){
								String score = readCellValue(row.getCell(20));
								if(!BankAuditConstant.NOT_APPLICABLE.equalsIgnoreCase(score)){
									score = BankAuditUtil.isEmptyString(score)?"":(long)Double.parseDouble(score)+"";
				        		}
								checklistOfflineInspection.setDeskEffectiveScore(score);
								checklistOfflineInspection.setDeskReviewerMakerRemarks(readCellValue(row.getCell(21)));
							}
				        	checklistOfflineInspectionList.add(checklistOfflineInspection);
				        }
		        }
			}catch (Exception e) {
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause());  
				e.printStackTrace(System.out);
				return BankAuditConstant.FAILED;
			}
			/****  read excel End  ********/ 
			  
			/****  update observation detail start *********/ 			 			 
			try{				 
				int docId = auditLevelDocumentDetailsToSave.getId();				
				Map<String, AuditObservation>  auditObservationMap = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.getAllAuditObservation(legalEntityCode, auditTypeCode, auditId);
				if(auditObservationMap!=null && auditObservationMap.size()>0){
					//Insert observation to checklist temp table from Excel
					ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.saveChecklistData(auditLevelDocumentDetailsToSave, checklistOfflineInspectionList, docId, reportLanguage, auditObservationMap, role);
	
					//update report, observation table  from temp 
					ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.updateComplianceStatusRemarksAndStandardStmt(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, role, auditLevelDocumentDetailsToSave.getMaker() );
					return   BankAuditConstant.SUCCESS;
				} 
				return BankAuditConstant.FAILED;
							
				//uploadId = auditLevelDocumentDetailsToSave.getId();
			}catch (Exception e) {
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause()); 
				e.printStackTrace(System.out);
				return BankAuditConstant.FAILED;
			}				
			/****  update observation detail End *********/				 
		}		
		return BankAuditConstant.FAILED;
	}
	
	
	@Override
	public String uploadRatingParameterScoresByAuditId( AuditLevelDocumentDetails auditLevelDocumentDetails, CommonsMultipartFile[] files, String fileName, String userType, String userId)  {
			  	  
		logger.info("Inside uploadRatingParameterScoresByAuditId :: "+ userType);
		for (CommonsMultipartFile commonsMultipartFile : files) {
			AuditLevelDocumentDetails auditLevelDocumentDetailsToSave = new AuditLevelDocumentDetails(); 
			
			try{  
				BeanUtils.copyProperties(auditLevelDocumentDetailsToSave, auditLevelDocumentDetails); 
				String pattern = "yyyyMMddHHmmssSSS"; 
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);  
				String referencePath =  BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
				auditLevelDocumentDetailsToSave.setReferencePath(referencePath);  
				auditLevelDocumentDetailsToSave.setFileName(commonsMultipartFile.getOriginalFilename());  

				auditLevelDocumentDetailsToSave = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.savePartial(auditLevelDocumentDetailsToSave);	
				
			}catch (Exception e) {
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause());  
				return BankAuditConstant.FAILED;
			}
			
			Integer docId= auditLevelDocumentDetailsToSave.getId();
			Integer legalEntityCode = auditLevelDocumentDetailsToSave.getLegalEntityCode();
			String auditId = auditLevelDocumentDetailsToSave.getAuditId();
			String auditTypeCode = auditLevelDocumentDetailsToSave.getAuditTypeCode();	 
			String role =  auditLevelDocumentDetailsToSave.getUserRoleId(); 			
				
			logger.info(" Details :: "+auditId+ " :: "+auditTypeCode+" :: "+ role);
			/****  read excel Start and save score ********/ 
			try(XSSFWorkbook uploadExcel = new XSSFWorkbook(commonsMultipartFile.getInputStream())){ 
				XSSFSheet sheet = uploadExcel.getSheetAt(0);
				//sheet.removeRow(sheet.getRow(0));
				
				//Validate AuditId and AuditType Code
				Row firstRow = sheet.getRow(1);
				String auditIdExcel = firstRow.getCell(0).getStringCellValue();
				String auditTypeCodeExcel = firstRow.getCell(1).getStringCellValue();
				String userTypeExcel = firstRow.getCell(3).getStringCellValue();
				if(!auditIdExcel.equalsIgnoreCase(auditId) || !auditTypeCodeExcel.equalsIgnoreCase(auditTypeCode) 
						 || !userTypeExcel.equalsIgnoreCase(userType+"__"+role) || docId==0){ 
					auditLevelDocumentDetailsToSave.setUploadStatus(BankAuditConstant.VALIDATION_FAILED);	
					auditLevelDocumentDetailsToSave = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.updateDocStatus(auditLevelDocumentDetailsToSave);	
					return BankAuditConstant.VALIDATION_FAILED;
				}
				List<String> rolesListInspector = Arrays.asList(("G2,G8").split(","));  //Inspection
				List<String> rolesListHO_RO = Arrays.asList(("G12").split(",")); //HO and RO 	
				
				String rmCode = firstRow.getCell(2).getStringCellValue();
				sheet.removeRow(sheet.getRow(0));
				sheet.removeRow(sheet.getRow(1));
				sheet.removeRow(sheet.getRow(2));
				
				Iterator<Row> rowIterator = sheet.iterator();				
				List<AuditRatingParameterScoreTmp> auditRatingParameterScoreTmpList = new ArrayList<>();
		        while (rowIterator.hasNext()) { 
		        	Row row = rowIterator.next();		        	  
		        	String paramCode = readCellValue(row.getCell(0));//.getStringCellValue(); 
		        	String updatedScore = readCellValue(row.getCell(5));//.getStringCellValue();
		        	String updatedRemarks = readCellValue(row.getCell(7)); 
		        	String remarks =  readCellValue(row.getCell(6)); 	        	 
		        	if(BankAuditUtil.isEmptyString(updatedRemarks)){
		        		updatedRemarks = remarks;
		        	}
		        			        	
		        	if(!BankAuditUtil.isEmptyString(paramCode) && !BankAuditUtil.isEmptyString(updatedScore) ){
		        		AuditRatingParameterScoreTmp auditRatingParameterScoreTmp = new AuditRatingParameterScoreTmp();
		        		auditRatingParameterScoreTmp.setLegalEntityCode(legalEntityCode);
		        		auditRatingParameterScoreTmp.setAuditId(auditId);
		        		auditRatingParameterScoreTmp.setAuditTypeCode(auditTypeCode);
		        		auditRatingParameterScoreTmp.setRmCode(rmCode);
		        		auditRatingParameterScoreTmp.setParamCode(paramCode);
		        		auditRatingParameterScoreTmp.setDocId(docId);
			        	if(rolesListInspector.contains(role)){	        		
			        		auditRatingParameterScoreTmp.setScoreAssessed(updatedScore);
			        		auditRatingParameterScoreTmp.setScoreEffective(updatedScore);
			        		auditRatingParameterScoreTmp.setMakerRemarks(updatedRemarks);
			        	}else if(rolesListHO_RO.contains(role)){
			        		if(userType.equalsIgnoreCase("RO")){
			        			auditRatingParameterScoreTmp.setScoreEffective(updatedScore);
			        			auditRatingParameterScoreTmp.setScoreEffectiveRO(updatedScore);	
			        			auditRatingParameterScoreTmp.setReviewerRemarks(updatedRemarks);
			        			auditRatingParameterScoreTmp.setReviewerRemarksId(userId);
			        		} else { // for HO
			        			auditRatingParameterScoreTmp.setScoreEffectiveHO(updatedScore);
			        			auditRatingParameterScoreTmp.setHoRemarks(updatedRemarks);
			        			auditRatingParameterScoreTmp.setHoRemarksId(userId);
			        		}
			        	}
			        	auditRatingParameterScoreTmpList.add(auditRatingParameterScoreTmp);
		        	}		             
		        }
		        
		        ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.saveRatingParameterScoresToTemp(auditRatingParameterScoreTmpList);
				ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.updateRatingParameterScoresToMaster(legalEntityCode, auditTypeCode, auditId, docId,
						rmCode, role, userType);				
				
				return   BankAuditConstant.SUCCESS;		
				
			}catch (Exception e) {
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause());  
				e.printStackTrace(System.out);
				return BankAuditConstant.FAILED;
			}		
		}
		return BankAuditConstant.FAILED;
	}
	
	@Override
	public String uploadAuditRatingComplianceObsScore( AuditLevelDocumentDetails auditLevelDocumentDetails, CommonsMultipartFile[] files, String fileName, String ratingComplianceStatus)  {
			  	  
		logger.info("Inside uploadAuditRatingComplianceObsScore :: ");
		for (CommonsMultipartFile commonsMultipartFile : files) {
			AuditLevelDocumentDetails auditLevelDocumentDetailsToSave = new AuditLevelDocumentDetails(); 
			
			try{  
				BeanUtils.copyProperties(auditLevelDocumentDetailsToSave, auditLevelDocumentDetails); 
				String pattern = "yyyyMMddHHmmssSSS"; 
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);  
				String referencePath =  BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
				auditLevelDocumentDetailsToSave.setReferencePath(referencePath);  
				auditLevelDocumentDetailsToSave.setFileName(commonsMultipartFile.getOriginalFilename());  

				auditLevelDocumentDetailsToSave = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.savePartial(auditLevelDocumentDetailsToSave);	
				
			}catch (Exception e) {
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause());  
				return BankAuditConstant.FAILED;
			}
			
			Integer docId= auditLevelDocumentDetailsToSave.getId();
			Integer legalEntityCode = auditLevelDocumentDetailsToSave.getLegalEntityCode();
			String auditId = auditLevelDocumentDetailsToSave.getAuditId();
			String auditTypeCode = auditLevelDocumentDetailsToSave.getAuditTypeCode();	 
			String role =  auditLevelDocumentDetailsToSave.getUserRoleId();
			String unitId = auditLevelDocumentDetailsToSave.getUnitCode();
			String maker = auditLevelDocumentDetailsToSave.getMaker();
	 
			logger.info(" Details :: "+auditId+ " :: "+auditTypeCode+" :: "+ role+" :: "+unitId+" :: "+maker);
			/****  read excel Start and save score ********/ 
			try(XSSFWorkbook uploadExcel = new XSSFWorkbook(commonsMultipartFile.getInputStream())){ 
				XSSFSheet sheet = uploadExcel.getSheetAt(0); 
				
				//Validate AuditId and AuditType Code
				Row firstRow = sheet.getRow(1);
				String auditIdExcel = firstRow.getCell(0).getStringCellValue();
				String auditTypeCodeExcel = firstRow.getCell(1).getStringCellValue(); 
				if(!auditIdExcel.equalsIgnoreCase(auditId) || !auditTypeCodeExcel.equalsIgnoreCase(auditTypeCode) || docId==0){ 
					auditLevelDocumentDetailsToSave.setUploadStatus(BankAuditConstant.VALIDATION_FAILED);	
					auditLevelDocumentDetailsToSave = ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.updateDocStatus(auditLevelDocumentDetailsToSave);	
					return BankAuditConstant.VALIDATION_FAILED;
				}
				
				List<String> rolesListInspector = Arrays.asList(("G2,G8").split(","));  //Inspection
				List<String> rolesListReviewer = Arrays.asList(("G8,G12").split(",")); //Reviewer
				List<String> rolesListScrutinizer = Arrays.asList(("G4,G7").split(",")); // Scrutinizer/review team
				 
				sheet.removeRow(sheet.getRow(0));
				sheet.removeRow(sheet.getRow(1));
				sheet.removeRow(sheet.getRow(2));
				
				Iterator<Row> rowIterator = sheet.iterator();				
				List<AuditRatingComplianceObsScoreTmp> auditRatingComplianceObsScoreTmpList = new ArrayList<>();
		        while (rowIterator.hasNext()) { 
		        	Row row = rowIterator.next();		        	  
		        	String observationId = readCellValue(row.getCell(0));  		        	
		        	String score = readCellValue(row.getCell(4)); 
		        	String updatedScore = readCellValue(row.getCell(5)); 
		        	String remarks =  readCellValue(row.getCell(6)); 	
		        	String updatedRemarks = readCellValue(row.getCell(7)); 
		        	
		        	if((!score.equalsIgnoreCase(updatedScore) || !remarks.equalsIgnoreCase(updatedRemarks))
		        		 && (!BankAuditUtil.isEmptyString(updatedScore) || !BankAuditUtil.isEmptyString(updatedRemarks))
		        		 && !BankAuditUtil.isEmptyString(observationId)){ 
			        	
		        		AuditRatingComplianceObsScoreTmp auditRatingComplianceObsScoreTmp = new AuditRatingComplianceObsScoreTmp();
		        		auditRatingComplianceObsScoreTmp.setLegalEntityCode(legalEntityCode);
		        		auditRatingComplianceObsScoreTmp.setAuditId(auditId);
		        		auditRatingComplianceObsScoreTmp.setAuditTypeCode(auditTypeCode); 
		        		auditRatingComplianceObsScoreTmp.setObservationId(observationId);
		        		auditRatingComplianceObsScoreTmp.setUnitId(unitId);
		        		auditRatingComplianceObsScoreTmp.setDocId(docId);
		        		auditRatingComplianceObsScoreTmp.setMaker(maker);
		        		
		        		if(BankAuditUtil.isEmptyString(updatedScore)){
		        			updatedScore = score;
		        		}else if(BankAuditUtil.isEmptyString(updatedRemarks)){
		        			updatedRemarks = remarks;
		        		}		
		        		if(!BankAuditConstant.NOT_APPLICABLE.equalsIgnoreCase(updatedScore)){
		        			updatedScore = BankAuditUtil.isEmptyString(updatedScore)?"":(long)Double.parseDouble(updatedScore)+"";
		        		}
		        		if(rolesListScrutinizer.contains(role)
		        				&& (BankAuditUtil.isEmptyString(ratingComplianceStatus) || ratingComplianceStatus.equalsIgnoreCase("RCDDF"))){	        			
		        			auditRatingComplianceObsScoreTmp.setDeskAssessedScore(updatedScore);
		        			auditRatingComplianceObsScoreTmp.setDeskEffectiveScore(updatedScore);		  
		        			auditRatingComplianceObsScoreTmp.setReviewerMakerRemarks(updatedRemarks);
		        			auditRatingComplianceObsScoreTmp.setReviewerCheckerRemarks(updatedRemarks);			        			 
						}else if(rolesListScrutinizer.contains(role)
								&& (ratingComplianceStatus.equalsIgnoreCase("RCDU") || ratingComplianceStatus.equalsIgnoreCase("RCDM"))){
							auditRatingComplianceObsScoreTmp.setDeskEffectiveScore(updatedScore);
							auditRatingComplianceObsScoreTmp.setReviewerCheckerRemarks(updatedRemarks);							
						}else if(rolesListInspector.contains(role)  
								&& (BankAuditUtil.isEmptyString(ratingComplianceStatus) || ratingComplianceStatus.equalsIgnoreCase("RCDS") || ratingComplianceStatus.equalsIgnoreCase("RCFDF"))){
							auditRatingComplianceObsScoreTmp.setFieldAssessedScore(updatedScore);
							auditRatingComplianceObsScoreTmp.setFieldEffectiveScore(updatedScore);							
							auditRatingComplianceObsScoreTmp.setAuditorMakerRemarks(updatedRemarks);
							auditRatingComplianceObsScoreTmp.setAuditorCheckerRemarks(updatedRemarks);							 
						}else if(rolesListReviewer.contains(role) 
								&& (ratingComplianceStatus.equalsIgnoreCase("RCFU") ||ratingComplianceStatus.equalsIgnoreCase("RCFM"))){
							auditRatingComplianceObsScoreTmp.setFieldEffectiveScore(updatedScore);
							auditRatingComplianceObsScoreTmp.setAuditorCheckerRemarks(updatedRemarks);
						}	        		
			        	auditRatingComplianceObsScoreTmpList.add(auditRatingComplianceObsScoreTmp);
			        	 
		        	}
		        }
		        
		        ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.saveAuditRatingComplianceObsScoreTmp(auditRatingComplianceObsScoreTmpList);
		      	ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.updateAuditRatingComplianceObsScoreToMaster(legalEntityCode, auditTypeCode, auditId, docId,
						 role, ratingComplianceStatus);		 
				
				return   BankAuditConstant.SUCCESS;		
				
			}catch (Exception e) {				
				ifcEnsureBaasDataOfflineUploadTransactionServiceImpl.deleteAuditRatingComplianceObsScoreTmp(legalEntityCode, auditTypeCode, auditId, docId);
				logger.error("Error .." + e.getMessage()+" :: "+e.getCause());  
				e.printStackTrace(System.out);
				return BankAuditConstant.FAILED;
			}		
		}
		return BankAuditConstant.FAILED;
	}
	
	private String readCellValue(Cell cell) {		
		String cellValue = null;
		switch(cell.getCellType()){
			case  Cell.CELL_TYPE_STRING:
				cellValue = cell.getStringCellValue();
				break;
			case  Cell.CELL_TYPE_NUMERIC:
				cellValue = cell.getNumericCellValue()+"";
				break;
			case  Cell.CELL_TYPE_BOOLEAN:
				cellValue = cell.getBooleanCellValue()+"";
				break;
			default:
				cell.setCellType(Cell.CELL_TYPE_STRING);
				cellValue = cell.getStringCellValue();
				break;	   	 
		 }
		return cellValue;
    }	
	
}
