package com.bankaudit.rest.process.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
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.model.MaintAuditTypeDesc;
import com.bankaudit.model.MaintEntity;
import com.bankaudit.process.model.AdhocPlanFinalV;
import com.bankaudit.process.model.AuditPlan;
import com.bankaudit.process.model.AuditPlanV;
import com.bankaudit.process.model.AuditPlanWrk;
import com.bankaudit.process.model.AuditProcessV;
import com.bankaudit.process.model.AuditProcessValidate;
import com.bankaudit.process.model.AuditProcessValidateWrapper;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.AuditScheduleWrk;
import com.bankaudit.rest.process.service.AuditProcessVService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;

import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.MaintAuditTypeDescService;
import com.bankaudit.service.MaintEntityService;

/**
 * This controller includes audit plan process like
 * get Branch units,schedule dates,team and validation.  
 * 
 * @author narendar.burada
 * @version 1.0
 * @since 2014-03-31
 */



@RestController

@RequestMapping("/api/auditProcess")
public class AuditProcessController {

	@Autowired 
	AuditProcessVService auditProcessVService;
	
	@Autowired
	MaintEntityService maintEntityService;
	
	@Autowired
	MaintAuditTypeDescService maintAuditTypeDescService;
	
	//@Autowired
	ServiceStatus serviceStatus;
	
	
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(AuditProcessController.class);
	
	/**
	 * This method is to fetch all branches which are fall under schedule plan start and end dates. 
	 * @param legalEntityCode
	 * @param auditTypecode
	 * @param planStartDate
	 * @param planEndDate
	 * @param maker
	 * @return AuditPlansList
	 */
	
@RequestMapping(value = "/getAuditProcessAuditTypeAndPlanStartDateAndPlanEndDate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditProcessAuditTypeAndPlanStartDateAndPlanEndDate(
			@RequestParam("legalEntityCode") int legalEntityCode,
			@RequestParam(value="auditType",required=false) String auditTypecode,
			@RequestParam(value="planStartDate",required=false) String planStartDate,
			@RequestParam(value="planEndDate",required=false) String planEndDate,
			@RequestParam(value="maker",required=false)  String maker){

		serviceStatus=new ServiceStatus();
		List auditProcessV = null;

		try {
			logger.info("getAuditProcessAuditTypeAndPlanStartDateAndPlanEndDate .. "+legalEntityCode + " :: "+ auditTypecode );
			if((legalEntityCode>=0) && (auditTypecode!=null && !BankAuditUtil.isEmptyString(auditTypecode)) && (planStartDate!=null)  && (planEndDate!=null) && (maker!=null && !BankAuditUtil.isEmptyString(maker))){
				String ErrorMessage="";	
				Date psDate = BankAuditUtil.parseDateTime(planStartDate);
				Date peDate = BankAuditUtil.parseDateTime(planEndDate);
				
				if(psDate.compareTo(peDate) > 0) {
					ErrorMessage="PlanStartData should be less then PlanEndDate.";
				}
				/* Commented for RCSA NABARD
				else if(!auditProcessVService.isAuditorsExist(legalEntityCode)) {
					ErrorMessage="No Users are mapped with Auditor.";
				}else if(!auditProcessVService.isAuditParametersExist(legalEntityCode,auditTypecode)){
					ErrorMessage="Audit Parameters are not mapped.";
				}
				*/
				
				if("".equals(ErrorMessage)) {

					//delete
					//auditProcessVService.deleteAuditPlanTemp(legalEntityCode,maker);
					 
					auditProcessV=auditProcessVService.getAuditProcessByAuditPlan(legalEntityCode,auditTypecode,planStartDate, planEndDate,maker);	// Called the STP, Product way
					//auditProcessV=auditProcessVService.getAssessmentsByAuditType(legalEntityCode,auditTypecode,planStartDate, planEndDate,maker);      // Execute the query to get one record per Unit.
					logger.info(" auditProcessV .."+auditProcessV);

					if(auditProcessV!=null && auditProcessV.size()>0){
						serviceStatus.setResult(auditProcessV);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("No Result Found With Above Search.");
					}

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

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

		}/*catch (ParseException e) {  // Commented for RCSA NABARD					
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Incorrect input date formate.");
		}*/catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}

		return serviceStatus;
	}

/**
 * 
 * @param legalEntityCode
 * @param auditTypeCode
 * @return
 */
	
@RequestMapping(value="/getAuditProcessNextPlan/{legalEntityCode}/{auditTypeCode}")
	ServiceStatus getAuditProcessNextPlan(@PathVariable("legalEntityCode") int legalEntityCode,
			@PathVariable(value="auditTypeCode",required=false)  String auditTypeCode){
		serviceStatus=new ServiceStatus();
		
			try {
				List<AuditPlanV> ls=auditProcessVService.getAuditProcessNextPlan(legalEntityCode,auditTypeCode);
				if(ls!=null && ls.size()>0){
					serviceStatus.setResult(ls);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Plan not Exist.");
				}
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
		
		return serviceStatus;
	}

	/**
	 * 
	 * @param legalEntityCode
	 * @return
	 */
	
@RequestMapping(value="/getAuditProcessNextPlan/{legalEntityCode}")
	ServiceStatus getAuditProcessNextPlan(@PathVariable("legalEntityCode") int legalEntityCode
			){
		serviceStatus=new ServiceStatus();
		 
			try {
				List<AuditPlanV> ls=auditProcessVService.getAuditProcessNextPlan(legalEntityCode,null);
				if(ls!=null && ls.size()>0){
					serviceStatus.setResult(ls);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Plan not Exist.");
				}
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
		
		return serviceStatus;
	}
	
	/**
	 * This method is to fetch plan start and end date for the selected Planner Unit. 
	 * @param legalEntityCode
	 * @param auditTypeCode
	 * @param plannerUnitCode  //ParentUnitCode -> Planning for the units under ParentUnitCode
	 * @return
	 */
		
@RequestMapping(value="/getAuditProcessNextPlan/{legalEntityCode}/{auditTypeCode}/{parentUnitCode}")
		ServiceStatus getAuditProcessNextPlan(@PathVariable("legalEntityCode") int legalEntityCode,
				@PathVariable(value="auditTypeCode",required=false)  String auditTypeCode,
				@PathVariable(value="parentUnitCode",required=false)  String plannerUnitCode){
			serviceStatus=new ServiceStatus();
			
				try {
					List<AuditPlanV> ls=auditProcessVService.getAuditProcessNextPlan(legalEntityCode,auditTypeCode,plannerUnitCode);
					if(ls!=null && ls.size()>0){
						serviceStatus.setResult(ls);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("Plan not Exist.");
					}
				} catch (Exception e) {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("failure");
					e.printStackTrace();
				}
			
			return serviceStatus;
		}


	/**
	 * 
	 * @param drawStr
	 * @param length
	 * @param start
	 * @param search
	 * @param orderColumnStr
	 * @param orderDirection
	 * @param legalEntityCodeStr
	 * @return
	 */
	
@RequestMapping(value = "/getAuditSchedules", 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("maker")String maker,
			@RequestParam(value="planType",required=false)String planType
			) {
		DataTableResponse dataTableResponse = null;
		try {
			if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length) && !BankAuditUtil.isEmptyString(start)) {

				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 = auditProcessVService.getAuditSchedules(Integer.parseInt(legalEntityCodeStr),search, orderColumn, orderDirection, page, size,maker,planType);
				dataTableResponse.setDraw(draw);

			} else {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError("invalid input.");
			}
		} catch (Exception e) {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("failure");
			e.printStackTrace();
		}
		return dataTableResponse;
	}

/**
 * 
 * @param auditType
 * @param userId
 * @param legalEntityCode
 * @return
 */
	
@RequestMapping(value="/getauditProcessTeam", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getauditProcessTeam(
			@RequestParam(value="auditType",required=false) String auditType,
			@RequestParam(value="userId",required=false) String userId,
			@RequestParam("legalEntityCode") int legalEntityCode
			
			){
		serviceStatus=new ServiceStatus();
		try {
			if(legalEntityCode>-1 
					//&& !BankAuditUtil.isEmptyString(userId)
					) {
				List ls=auditProcessVService.getauditProcessTeam(legalEntityCode,auditType,userId);
				if(ls!=null){
					serviceStatus.setResult(ls);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Plan data not fond.");
				}

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

		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus;
	}
	
	/**
	 * 
	 * @param legalEntityCode
	 * @param userId
	 * @param unitCode
	 * @param auditTypeCode
	 * @return
	 */
	
@RequestMapping(value="/isValidSkillSet", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus skillSetValidation(@RequestParam("legalEntityCode") String legalEntityCode,
									 @RequestParam("userId") String userId,
									 @RequestParam("unitCode") String unitCode,
									 @RequestParam("auditTypeCode") String auditTypeCode) {
		serviceStatus=new ServiceStatus();
		try {
			if(!BankAuditUtil.isEmptyString(legalEntityCode) && !BankAuditUtil.isEmptyString(userId) 
					&& !BankAuditUtil.isEmptyString(unitCode) && !BankAuditUtil.isEmptyString(auditTypeCode)) {

				String ls=auditProcessVService.skillSetValidation( legalEntityCode, userId, unitCode, auditTypeCode);
				if(ls!=null){
					serviceStatus.setResult(ls);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("Plan data not fond.");
				}

			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("invalid input.");
			}
		}catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus;
	}
	
	/**
	 * this method is to check given start date is Valid Business Date.
	 * this method accept date format in yyyy-MM-dd other then this format is not acceptable.
	 * @param session
	 * @param apv
	 * @param currStartDate
	 * @param currEndDate
	 * @param frequency
	 * @param plnStDate
	 * @param plnEnDate
	 * @return
	 */
	
@RequestMapping(value="/isValidBusinessStartDate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus isValiedBusinessStartDate(
			@RequestParam("legalEntityCode") String legalEntityCode,
			@RequestParam("unitId") String unitId,
			@RequestParam(value="currStartDate") String currentStartDate,		
			@RequestParam(value="planStartDate",required=false) String planStartDate,
			@RequestParam(value="planEndDate",required=false) String planEndDate){
		serviceStatus=new ServiceStatus();
		Map<String, String> map=null;
		try {
			
			if( (legalEntityCode!=null && !BankAuditUtil.isEmptyString(legalEntityCode))  &&  (unitId!=null && !BankAuditUtil.isEmptyString(unitId)) 
					&& (currentStartDate!=null && !BankAuditUtil.isEmptyString(currentStartDate)) 
					// && (planStartDate!=null && !BankAuditUtil.isEmptyString(planStartDate)) 
					// && (planEndDate!=null && !BankAuditUtil.isEmptyString(planEndDate))
					)
			{	
				//businessDateService = new BusinessDateServiceImpl();
				
				String ErrorMessage ="";
				
				//Date bnsDate=businessDateService.getBusinessDateTimestampByLegalEntityCode((new Integer(legalEntityCode)).intValue());
								
				//Date csDate = BankAuditUtil.parseDateTime(currentStartDate);
				
				//if(bnsDate.compareTo(csDate) > 0) {
				///	ErrorMessage="Selected Date should be grater then or equal to Business Date.";
				//}
				
				if(!"".equals(ErrorMessage)) {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage(ErrorMessage);
				}else if(BankAuditUtil.parseStringToDate(currentStartDate)!=null 
						//&& BankAuditUtil.parseStringToDate(planStartDate)!=null 
						//&& BankAuditUtil.parseStringToDate(planEndDate)!=null
						){
					map=auditProcessVService.isValiedBusinessDate(legalEntityCode.trim(), unitId.trim(), "0", 1, currentStartDate.trim(),null,((planStartDate!=null)?planStartDate.trim():null),((planEndDate!=null)?planEndDate.trim():null));
					if(map!=null && "1".equalsIgnoreCase(map.get("validDate"))) {
						map=auditProcessVService.isDateFallOnFortnight(legalEntityCode.trim(), unitId.trim(), currentStartDate.trim());
						if(map!=null && "1".equalsIgnoreCase(map.get("validDate")))
							map=auditProcessVService.isDateFallOnHoliday(legalEntityCode.trim(), unitId.trim(), currentStartDate.trim());
					}
					if(map!=null){
						serviceStatus.setResult(map);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("Not a valied Business Start Date.");
					}
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("Invalid input date format. Accepted format is yyyy-MM-dd.");
				}
				

			}else{
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("Invalid input.");
			}
		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus;
	}

/**
 * this method is to check given end date is Valid Business Date 
 * like Selected Date should be grater then or equal to Business Date,
 * date format is in yyyy-MM-dd format or not.
 * 
 * @param legalEntityCode
 * @param unitId
 * @param currentEndDate
 * @param planStartDate
 * @return
 */
	
	
@RequestMapping(value="/isValidBusinessEndDate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus isValiedBusinessEndDate(
			@RequestParam("legalEntityCode") String legalEntityCode,
			@RequestParam("unitId") String unitId,   			
			@RequestParam(value="currEndDate") String currentEndDate,
			@RequestParam(value="planStartDate",required=false) String planStartDate)	{
		serviceStatus=new ServiceStatus();
		try {
			Map<String, String> map=null;
			if( (legalEntityCode!=null && !BankAuditUtil.isEmptyString(legalEntityCode))  &&  (unitId!=null && !BankAuditUtil.isEmptyString(unitId)) 
					&& (currentEndDate!=null && !BankAuditUtil.isEmptyString(currentEndDate))  
					//&& (planStartDate!=null && !BankAuditUtil.isEmptyString(planStartDate))
					
					) {

				String ErrorMessage ="";
				//businessDateService = new BusinessDateServiceImpl();
				//Date bnsDate=businessDateService.getBusinessDateTimestampByLegalEntityCode((new Integer(legalEntityCode)).intValue());
				
				
				//Date ceDate = BankAuditUtil.parseDateTime(currentEndDate);
				
				//if(bnsDate.compareTo(ceDate) > 0) {
				//	ErrorMessage="Selected Date should be grater then or equal to Business Date.";
				//}
				
				if(!"".equals(ErrorMessage)) {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage(ErrorMessage);
				}else if(BankAuditUtil.parseStringToDate(currentEndDate)!=null 
						// && BankAuditUtil.parseStringToDate(planStartDate)!=null
						){
					map=auditProcessVService.isValiedBusinessDate( legalEntityCode, unitId, "0", 1, null,currentEndDate,((planStartDate!=null)?planStartDate:null), null);
					if(map!=null && "1".equalsIgnoreCase(map.get("validDate"))) {
						map=auditProcessVService.isDateFallOnFortnight(legalEntityCode.trim(), unitId.trim(), currentEndDate.trim());
						if(map!=null && "1".equalsIgnoreCase(map.get("validDate")))
							map=auditProcessVService.isDateFallOnHoliday(legalEntityCode.trim(), unitId.trim(), currentEndDate.trim());
					}
					if(map!=null){
						serviceStatus.setResult(map);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("Not a valid Business End Date.");
					}
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("Invalid input date format. Accepted format is yyyy-MM-dd.");
				}	

			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("Invalid input.");
			}
		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus;
	}

	/**
	 * this method is to validate Audit Plan data like
	 * valid skill set, team member schedule date should not fall on same date for 2-different audits  
	 * Maker Id should not be null for the plan to validate
	 * Plan data not fond, Please change the plan.
	 * @param auditProcessValidateWrp
	 * @return
	 */
	
@RequestMapping(value="/validateAuditProcess",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus validateAuditProcess(@RequestBody AuditProcessValidateWrapper auditProcessValidateWrp){
		serviceStatus=new ServiceStatus();
		try {
			if(auditProcessValidateWrp!=null &&  auditProcessValidateWrp.getAuditProcessValidateList()!=null) {
			/*	System.out.println("AuditProcessValidateLst  size .... "+auditProcessValidateWrp.getAuditProcessValidateList().size());*/
				List<AuditProcessValidate> auditProcessValidateLst= auditProcessValidateWrp.getAuditProcessValidateList();

				if(auditProcessValidateLst!=null && auditProcessValidateLst.get(0)!=null){


					AuditProcessValidate auditProcessValidate=auditProcessValidateLst.get(0);

					if(auditProcessValidate!=null && auditProcessValidate.getMaker()!=null) {

						//delete
						auditProcessVService.deleteAuditPlanTemp(auditProcessValidate.getLegalEntityCode(),auditProcessValidate.getMaker());

						//save
						auditProcessVService.createAuditPlanTemp(auditProcessValidateLst);

						//validation & return	   
						
						Map<String, Object> apv = null;
						if(auditProcessValidate!=null) {
							apv = auditProcessVService.validateAuditProcess(auditProcessValidate);
							//apv = auditProcessVService.validateAuditProcess(auditProcessValidate.getLegalEntityCode(),auditProcessValidate.getAuditTypeCode(),auditProcessValidate.getPlanStartDate(),auditProcessValidate.getPlanEndDate(),auditProcessValidate.getRemarkPlanIds(),auditProcessValidate.getMaker());
						}
						if(apv!=null){
							serviceStatus.setResult(apv);
							serviceStatus.setStatus("success");
							serviceStatus.setMessage("successfuly retrieved ");
						}else {
							serviceStatus.setStatus("failure");
							serviceStatus.setMessage("Plan data not fond, Please change the plan.");
						}   				

					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("Maker Id should not be null for the plan to validate.");
					}

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

		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}
		}
		
		return serviceStatus;
	}
	
	/**
	 * this method is to validate Audit Plan data like
	 * valid skill set, team member schedule date should not fall on same date for 2-different audits  
	 * Maker Id should not be null for the plan to validate
	 * Plan data not fond, Please change the plan.
	 * @param auditProcessValidateWrp
	 * @return
	 */
	
@RequestMapping(value="/validateAuditUpdateProcess",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus validateAuditUpdateProcess(@RequestBody AuditProcessValidateWrapper auditProcessValidateWrp){
		serviceStatus=new ServiceStatus();
		try {
			if(auditProcessValidateWrp!=null &&  auditProcessValidateWrp.getAuditProcessValidateList()!=null) {
				if(!auditProcessValidateWrp.getAuditProcessValidateList().isEmpty()){
					AuditProcessValidate auditProcessValidate=auditProcessValidateWrp.getAuditProcessValidateList().get(0);

					if(auditProcessValidate!=null && auditProcessValidate.getAuditId()!=null) {
						
						//validation & return	   
						Map<String, Object> apv = null;
						if(auditProcessValidate!=null) {
							apv = auditProcessVService.validateAuditUpdateProcess(auditProcessValidate);
							//apv = auditProcessVService.validateAuditProcess(auditProcessValidate.getLegalEntityCode(),auditProcessValidate.getAuditTypeCode(),auditProcessValidate.getPlanStartDate(),auditProcessValidate.getPlanEndDate(),auditProcessValidate.getRemarkPlanIds(),auditProcessValidate.getMaker());
						}
						if(apv!=null){
							serviceStatus.setResult(apv);
							serviceStatus.setStatus("success");
							serviceStatus.setMessage("successfuly retrieved ");
						}else {
							serviceStatus.setStatus("failure");
							serviceStatus.setMessage("Plan data not fond, Please change the plan.");
						}   				

					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("Maker Id should not be null for the plan to validate.");
					}

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

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

	/**
	 * 
	 * @param legalEntityCode
	 * @param auditTypeCode
	 * @return
	 */
	
@RequestMapping(value = "/getValidateAuditProcess", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getValidateAuditProcess(@RequestParam("legalEntityCode") int legalEntityCode,
			@RequestParam("auditTypeCode") String auditTypeCode){
		serviceStatus=new ServiceStatus();
		try {
			List auditProcessV=auditProcessVService.getValidateAuditProcess(legalEntityCode,auditTypeCode);						
			if(auditProcessV!=null){
				serviceStatus.setResult(auditProcessV);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfuly retrieved ");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("No Plan exist, Please change the plan.");
			}
		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus;
	}

	/**
	 * this method is for fetch existing plan data.
	 * 
	 * @param legalEntityCode
	 * @param auditTypeCode
	 * @param planId
	 * @return
	 */
	
@RequestMapping(value="/getByPlanId/{legalEntityCode}/{auditTypeCode}/{planId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditsByPlanId(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("planId") String planId){
		serviceStatus=new ServiceStatus();
		try {
			if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(planId)){

				AuditPlan auditPln=auditProcessVService.getAuditByPlanId(legalEntityCode,auditTypeCode,planId);  

				if(auditPln!=null){
					serviceStatus.setResult(auditPln);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("ids not found");
				}

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

		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			//e.printStackTrace();
		}
		return serviceStatus;
	}
      
      
      
@RequestMapping(value = "/getAdhocUnitSheduleDetails", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAllUnits(@RequestParam("legalEntityCode") int legalEntityCode,
							  @RequestParam("auditTypeCode") String auditTypeCode,
							  @RequestParam(value="unitCode",required=false) String unitCode){
		serviceStatus=new ServiceStatus();
		try {
  			
  			Date businesDate=businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode);
  			String currBusinesDate = BankAuditUtil.parseDateToString(businesDate);

  			System.out.println("businessDate as Date : --> "+businesDate);
  			System.out.println("businessDate as String: --> "+currBusinesDate);
  					
  			String ls=auditProcessVService.getAdhocUnitSheduleDetails( legalEntityCode, auditTypeCode, unitCode, currBusinesDate);						
  			if(ls!=null){
  				serviceStatus.setResult(ls);
  				serviceStatus.setStatus("success");
  				serviceStatus.setMessage("successfuly retrieved ");
  			}else {
  				serviceStatus.setStatus("success");
  				serviceStatus.setMessage("No Plan exist."); 
  			}
  		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure"); 
			e.printStackTrace();
		}
		return serviceStatus;
	} 
      
      
@RequestMapping(value = "/getAdhocPlan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  	ServiceStatus getAdhocPlan(@RequestParam("legalEntityCode") int legalEntityCode,
  							   @RequestParam("auditTypeCode") String auditTypeCode,
  							   @RequestParam(value="unitCode",required=false) String unitCode){ 
  		serviceStatus=new ServiceStatus();
  		try {
  			
  			Date businesDate=businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode);
  			String currBusinesDate = BankAuditUtil.parseDateToString(businesDate);

  			System.out.println("businessDate as Date : --> "+businesDate);
  			System.out.println("businessDate as String: --> "+currBusinesDate);
  					
  			List ls=auditProcessVService.getAdhocPlan( legalEntityCode, auditTypeCode, unitCode, currBusinesDate);						
  			if(ls!=null){
  				serviceStatus.setResult(ls);
  				serviceStatus.setStatus("success");
  				serviceStatus.setMessage("successfuly retrieved ");
  			}else {
  				serviceStatus.setStatus("failure");
  				serviceStatus.setMessage("No Adhoc Plan details exist, Please change the plan.");
  			}
  		} catch (Exception e) {
  			serviceStatus.setStatus("failure");
  			serviceStatus.setMessage("failure");
  			e.printStackTrace();
  		}
  		return serviceStatus;
  	} 
     
      
      
@RequestMapping(value = "/updateAdhocPlan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	ServiceStatus updateAdhocPlan(@RequestParam("legalEntityCode") int legalEntityCode,
    							   @RequestParam("auditTypeCode") String auditTypeCode,
    							   @RequestParam(value="planId") String planId,
    							   @RequestParam(value="unitCode",required=false) String unitCode){ 
    		serviceStatus=new ServiceStatus();
    		try { 
    			
    			Date businesDate=businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode);
    			String currBusinesDate = BankAuditUtil.parseDateToString(businesDate);

    			System.out.println("businessDate as Date : --> "+businesDate);
    			System.out.println("businessDate as String: --> "+currBusinesDate);
    					
    			//List ls=auditProcessVService.getAdhocPlan( legalEntityCode, auditTypeCode, planId/*, currBusinesDate*/);
    			
    			AuditPlan auditPln=auditProcessVService.getAuditByPlanId(legalEntityCode,auditTypeCode,planId);
    			
    				
    			List<AdhocPlanFinalV> ls=null;
    			Map<String,Object> hm=null;
    			
    			if(auditPln!=null){
    				Object obj=auditPln.getAuditSchedules().get(0);
    				if(obj instanceof AuditScheduleWrk) {
    					AuditScheduleWrk asw=(AuditScheduleWrk)obj;
    					ls=auditProcessVService.getAdhocPlan( legalEntityCode, auditTypeCode,asw.getUnitId() , currBusinesDate);	
    				}else {
    					AuditSchedule asw=(AuditSchedule)obj;
    					ls=auditProcessVService.getAdhocPlan( legalEntityCode, auditTypeCode,asw.getUnitId() , currBusinesDate);	
    				}

    				hm=new HashMap<String,Object>();
    				hm.put("header", ls);
    				hm.put("line", auditPln);

    				if(ls!=null) {
    					serviceStatus.setResult(hm);
    					serviceStatus.setStatus("success");
    					serviceStatus.setMessage("successfuly retrieved ");
    				}else {
    					serviceStatus.setStatus("failure");
    					serviceStatus.setMessage("No Adhoc Plan details exist.");
    				}

    			}else {
    				serviceStatus.setStatus("failure");
    				serviceStatus.setMessage("No Adhoc Plan details exist.");
    			}
    		} catch (Exception e) {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    			e.printStackTrace();
    		}
    		return serviceStatus; 
    	} 
      
      //jurisdictionByUserOrUnit
    
@RequestMapping(value = "/jurisdictionByUserOrUnit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  	ServiceStatus jurisdictionByUserOrUnit(@RequestParam("legalEntityCode") int legalEntityCode,
  							      @RequestParam(value="userOrUnitCode") String userOrUnitCode,
  							      @RequestParam(value="type") String type,
  							      @RequestParam(value="auditType",required=false) String auditTypeCode
  							     ){ 
  		serviceStatus=new ServiceStatus();
  		List<String> unitsList = null;
  		String unitsListToConcatinatedString="";
  		
  		Map<String,String> usrDashBoard = new HashMap<String,String>();
  		Integer legalEntity=new Integer(legalEntityCode);
  		List<MaintAuditTypeDesc> matdes= null;
  		
  		try { 
  			
  			if( legalEntityCode>-1 && !BankAuditUtil.isEmptyString(userOrUnitCode) && !BankAuditUtil.isEmptyString(type) ) {	
  				
  				long startFirst= System.currentTimeMillis();
  				logger.info("1->S Get Audit Types ...."+ System.currentTimeMillis());
  				long start= System.currentTimeMillis();
  				if(!BankAuditUtil.isEmptyString(auditTypeCode)) {
  					matdes = new ArrayList<MaintAuditTypeDesc>();
  					matdes.add(maintAuditTypeDescService.getMaintAuditTypeDesc(new Integer(legalEntityCode), auditTypeCode));
  				}else {	
  					matdes=maintAuditTypeDescService.getMaintAuditTypeDescByLegalEntityCode(legalEntity);
  				}
  				logger.info("1->E Get Audit Types .... Time Taken .... "+ (System.currentTimeMillis()-start));
  				logger.info("matdes .."+ matdes);
  				
  				start= System.currentTimeMillis();
  				unitsList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntity,"user",userOrUnitCode);
  				logger.info("2->E unitsList .... Time Taken .... "+ (System.currentTimeMillis()-start));
  				logger.info("unitsList .."+ unitsList);
  				
  				long startcom= System.currentTimeMillis();
  				if(unitsList!=null&&!unitsList.isEmpty()){ 

  					String curFinancialStartDate=null;
  					String curFinancialEndDate=null;
  					@SuppressWarnings("rawtypes")
  					Map mp=null;

  					String planed = null;
  					String closed = null;
  					String inProcess = null;
  					
  					start= System.currentTimeMillis();
  					for (String unitId : unitsList) {
  						if(unitsListToConcatinatedString.length()==0) 
  							unitsListToConcatinatedString="'"+unitId+"'";	
  						unitsListToConcatinatedString+=",'"+unitId+"'";	
  					}
  					
  					logger.info("No of Units : "+unitsList.size());
  					logger.info("Units blow are :"+unitsListToConcatinatedString);	
  					
  					Long allAuditUnits = auditProcessVService.getAllOperationAuditUnits(legalEntityCode,auditTypeCode, unitsListToConcatinatedString);
  					logger.info("allAuditUnits .."+ allAuditUnits);
  	  				
  					Long auditUnits = auditProcessVService.getOperationAuditUnits(legalEntityCode,auditTypeCode, unitsListToConcatinatedString);
  					logger.info("auditUnits .."+ auditUnits);
  					
  	  				mp=auditProcessVService.financialYearDates(legalEntityCode);
  					logger.info("financialYearDates .."+ mp);
  					
  					curFinancialStartDate = (String)mp.get("startDate")+"T00:00:00.000Z";
  					curFinancialEndDate =  (String)mp.get("endDate")+"T00:00:00.000Z";

  					logger.info("financialYear start date : --> "+curFinancialStartDate + "financialYear end date: --> "+curFinancialEndDate);
  					
  					
  					if(!BankAuditUtil.isEmptyString(auditTypeCode)) {
  						auditProcessVService.getAuditProcessByAuditPlan(legalEntityCode,auditTypeCode,curFinancialStartDate, curFinancialEndDate,userOrUnitCode);
  					}else {
  						for (MaintAuditTypeDesc maintAuditTypeDesc : matdes) {
  							auditProcessVService.getAuditProcessByAuditPlan(legalEntityCode,maintAuditTypeDesc.getAuditTypeCode(),curFinancialStartDate, curFinancialEndDate,userOrUnitCode);
  						}
  					} 
  					logger.info("financialYearDates .."+ mp);
  					
  	  				Long targetUnitCount=auditProcessVService.getTargetAudits(legalEntityCode,unitsListToConcatinatedString,auditTypeCode,userOrUnitCode);  
  	  				logger.info("targetUnits : --> "+targetUnitCount);

  					
  					Date psDate = BankAuditUtil.parseDateTime(curFinancialStartDate);
  					Date peDate = BankAuditUtil.parseDateTime(curFinancialEndDate);
  					
  					String plnStDate = BankAuditUtil.parseDateToString(psDate);
  					String plnEnDate = BankAuditUtil.parseDateToString(peDate);  
  				
  					logger.info("Pls SD :"+plnStDate + "Pls SE :"+plnEnDate);
  					
  					start= System.currentTimeMillis();
  					if(unitsListToConcatinatedString!=null && unitsListToConcatinatedString.length()>0)
  					{
  						mp=auditProcessVService.jurisdictionByShedule(legalEntityCode,unitsListToConcatinatedString,auditTypeCode,plnStDate,plnEnDate);
  						if(mp!=null && !mp.isEmpty()) {
  							planed =(String)mp.get("planed");
  							closed =  (String)mp.get("closed");
  							inProcess =  (String)mp.get("inProcess");
  						}
  					}
  					logger.info("jurisdictionByShedule : --> "+mp);

  	  				String watchListCount = auditProcessVService.getWatchListForAllAudits(legalEntityCode,auditTypeCode);
  					logger.info("watchListCount .... "+watchListCount);
  					
  					usrDashBoard.put("audits",((matdes!=null && !matdes.isEmpty())?matdes.size()+"":null));
  					usrDashBoard.put("NoOfAllUnits",allAuditUnits+"");  					
  					usrDashBoard.put("NoOfUnits", auditUnits+"");
  					usrDashBoard.put("target", targetUnitCount+"");
  					usrDashBoard.put("planed", planed);
  					usrDashBoard.put("closed", closed);
  					usrDashBoard.put("inProcess", inProcess);
  					usrDashBoard.put("watchList", watchListCount);

  					serviceStatus.setResult(usrDashBoard);
  					serviceStatus.setStatus("success");
  					serviceStatus.setMessage("successfuly retrieved ");
  				}else {
  					serviceStatus.setStatus("failure");
  					serviceStatus.setMessage(" not found any");
  				}
  				
  			}else {
  			serviceStatus.setStatus("failure");
  			serviceStatus.setMessage(" Invalied payload.");
  		}
			
			
  		} catch (Exception e) {
  			serviceStatus.setStatus("failure");
  			serviceStatus.setMessage("failure");
  			e.printStackTrace();
  		}
  		return serviceStatus; 
  	} 
      
      
    
@RequestMapping(value = "/previousAuditDetails", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 	ServiceStatus previousAuditDetails(@RequestParam("legalEntityCode") int legalEntityCode,
		      @RequestParam(value="auditType") String auditType,
		      @RequestParam(value="unitCode") String unitCode,
		      @RequestParam(value="auditId") String auditId
		     ){
		serviceStatus=new ServiceStatus();
		try { 
			
			if( !BankAuditUtil.isEmptyString(auditType) 
					&& !BankAuditUtil.isEmptyString(unitCode) 
					&& !BankAuditUtil.isEmptyString(auditId)) {
				String previousAuditId=auditProcessVService.previousAuditDetails(legalEntityCode,auditType,unitCode,auditId);
				if(previousAuditId!=null){
					serviceStatus.setResult(previousAuditId);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved ");
				}else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("No previous audit exist.");
				}
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("Invalid PayLoad.");
			}
			
		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus; 
	}
      
    
@RequestMapping(value = "/getFinancialYearDates", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    ServiceStatus previousAuditDetails(@RequestParam("legalEntityCode") int legalEntityCode){
    	serviceStatus=new ServiceStatus();
    	Map mp=null;
    	try { 
    		mp=auditProcessVService.financialYearDates(legalEntityCode);
    		if(mp!=null) {
    			serviceStatus.setResult(mp);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfuly retrieved ");
    		}else {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage(" No data found.");
    		}

    	}catch (Exception e) {
    		serviceStatus.setStatus("failure");
    		serviceStatus.setMessage("failure");
    		e.printStackTrace();
    	}
    	return serviceStatus; 

    }
    
    
@RequestMapping(value = "/numberOfBusinessDaysBtweenStartEndDates", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    ServiceStatus numberOfBusinessDaysBtweenStartEndDates(@RequestParam("legalEntityCode") int legalEntityCode,
    		  @RequestParam(value="unitCode") String unitCode,
		      @RequestParam(value="StDate") String StDate,
		      @RequestParam(value="EnDate") String EnDate){
    	serviceStatus=new ServiceStatus();
    	try { 
    		Long noOfBussDays=auditProcessVService.numberOfBusinessDaysBtweenStartEndDates( legalEntityCode+"", unitCode, StDate, EnDate);
    		if(noOfBussDays!=null) {
    			serviceStatus.setResult(noOfBussDays);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfuly retrieved ");
    		}else {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage(" No data found.");
    		}

    	}catch (Exception e) {
    		serviceStatus.setStatus("failure");
    		serviceStatus.setMessage("failure");
    		e.printStackTrace();
    	}
    	return serviceStatus; 

    }
    
    
@RequestMapping(value = "/getAssessmentsByAuditType", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAssessmentsByAuditType(
			@RequestParam("legalEntityCode") int legalEntityCode,
			@RequestParam(value="auditType",required=false) String auditTypecode,
			@RequestParam(value="planStartDate",required=false) String planStartDate,
			@RequestParam(value="planEndDate",required=false) String planEndDate,
			@RequestParam(value="maker",required=false)  String maker){

		serviceStatus=new ServiceStatus();
		List auditProcessV = null;

		try {
			logger.info("getAssessmentsByAuditType .. "+legalEntityCode + " :: "+ auditTypecode+" :: "+planStartDate +" :: "+ planEndDate +" :: "+ maker );
			if((legalEntityCode>0) && (auditTypecode!=null && !BankAuditUtil.isEmptyString(auditTypecode)) && (planStartDate!=null)  && (planEndDate!=null) && (maker!=null && !BankAuditUtil.isEmptyString(maker))){
				String ErrorMessage="";	
				Date psDate = BankAuditUtil.parseDateTime(planStartDate);
				Date peDate = BankAuditUtil.parseDateTime(planEndDate);
				
				if(psDate.compareTo(peDate) > 0) {
					ErrorMessage="PlanStartData should be less then PlanEndDate.";
				}
				/* Commented for RCSA NABARD
				else if(!auditProcessVService.isAuditorsExist(legalEntityCode)) {
					ErrorMessage="No Users are mapped with Auditor.";
				}else if(!auditProcessVService.isAuditParametersExist(legalEntityCode,auditTypecode)){
					ErrorMessage="Audit Parameters are not mapped.";
				}
				*/
				
				if("".equals(ErrorMessage)) {

					//delete
					//auditProcessVService.deleteAuditPlanTemp(legalEntityCode,maker);
					 
					auditProcessV=auditProcessVService.getAssessmentsByAuditType(legalEntityCode,auditTypecode,planStartDate, planEndDate,maker);	

					if(auditProcessV!=null && auditProcessV.size()>0){
						serviceStatus.setResult(auditProcessV);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("No Result Found With Above Search.");
					}

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

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

		}/*catch (ParseException e) {  // Commented for RCSA NABARD					
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Incorrect input date formate.");
		}*/catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}

		return serviceStatus;
	}
    
    /**
     * 
     * @param auditType
     * @param userId
     * @param legalEntityCode
     * @return
     */
    	
@RequestMapping(value="/getRiskAssessorsTeam", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	ServiceStatus getRiskAssessorsTeam(
    			@RequestParam(value="auditType",required=false) String auditType,
    			@RequestParam(value="unitCode",required=false) String unitCode,
    			@RequestParam(value="userId",required=false) String userId,
    			@RequestParam("legalEntityCode") int legalEntityCode
    			
    			){
    		serviceStatus=new ServiceStatus();
    		try {
    			if(legalEntityCode>-1 
    					//&& !BankAuditUtil.isEmptyString(userId)
    					) {
    				List ls=auditProcessVService.getRiskAssessorsTeam(legalEntityCode,auditType,unitCode,userId);
    				if(ls!=null){
    					serviceStatus.setResult(ls);
    					serviceStatus.setStatus("success");
    					serviceStatus.setMessage("successfuly retrieved ");
    				}else {
    					serviceStatus.setStatus("failure");
    					serviceStatus.setMessage("Plan data not fond.");
    				}

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

    		} catch (Exception e) {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    			e.printStackTrace();
    		}
    		return serviceStatus;
    	}
    	
    	
@RequestMapping(value="/getRiskAssessorsTeamChekcer", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	ServiceStatus getRiskAssessorsTeamChekcer(
    			@RequestParam(value="auditType",required=false) String auditType,
    			@RequestParam(value="unitCode",required=false) String unitCode,
    			@RequestParam(value="userId",required=false) String userId,
    			@RequestParam("legalEntityCode") int legalEntityCode
    			
    			){
    		serviceStatus=new ServiceStatus();
    		try {
    			if(legalEntityCode>-1 
    					//&& !BankAuditUtil.isEmptyString(userId)
    					) {
    				List ls=auditProcessVService.getRiskAssessorsTeamChekcer(legalEntityCode,auditType,unitCode,userId);
    				if(ls!=null){
    					serviceStatus.setResult(ls);
    					serviceStatus.setStatus("success");
    					serviceStatus.setMessage("successfuly retrieved ");
    				}else {
    					serviceStatus.setStatus("failure");
    					serviceStatus.setMessage("Plan data not fond.");
    				}

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

    		} catch (Exception e) {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    			e.printStackTrace();
    		}
    		return serviceStatus;
    	}
    
    	
@RequestMapping(value="/getauditProcessPIOTeam", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	ServiceStatus getauditProcessPIOTeam(
    			@RequestParam(value="auditType",required=false) String auditType,
    			@RequestParam(value="userId",required=false) String userId,
    			@RequestParam("legalEntityCode") int legalEntityCode
    			){
    		serviceStatus=new ServiceStatus();
    		try {
    			if(legalEntityCode>-1) {
    				List ls=auditProcessVService.getauditProcessPIOTeam(legalEntityCode,auditType,userId);
    				if(ls!=null){
    					serviceStatus.setResult(ls);
    					serviceStatus.setStatus("success");
    					serviceStatus.setMessage("successfuly retrieved ");
    				}else {
    					serviceStatus.setStatus("failure");
    					serviceStatus.setMessage("Plan data not fond.");
    				}
    			}else {
    				serviceStatus.setStatus("failure");
    				serviceStatus.setMessage("invalid input.");
    			}

    		} catch (Exception e) {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    			e.printStackTrace();
    		}
    		return serviceStatus;
    	}
    	
    	/**
    	 * 
    	 * @param legalEntityCode
    	 * @param userId
    	 * @param unitCode
    	 * @param auditTypeCode
    	 * @return
    	 */
    	
@RequestMapping(value="/isPlanAlreadyExist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	public ServiceStatus isPlanAlreadyExist(@RequestParam("legalEntityCode") String legalEntityCode,
    									 @RequestParam("userId") String userId,
    									 @RequestParam("parentUnitCode") String parentUnitCode,
    									 @RequestParam("auditTypeCode") String auditTypeCode) {
    		serviceStatus=new ServiceStatus();
    		try {
    			if(!BankAuditUtil.isEmptyString(legalEntityCode) && !BankAuditUtil.isEmptyString(userId) 
    					&& !BankAuditUtil.isEmptyString(parentUnitCode) && !BankAuditUtil.isEmptyString(auditTypeCode)) {

    				//String ls=auditProcessVService.isPlanAlreadyExist( legalEntityCode, userId, parentUnitCode, auditTypeCode);
    				List<AuditPlanWrk> ls=auditProcessVService.isPlanAlreadyExist( Integer.parseInt(legalEntityCode), userId, parentUnitCode, auditTypeCode);
    				if(ls!=null){
    					serviceStatus.setResult(ls);
    					serviceStatus.setStatus("success");
    					serviceStatus.setMessage("successfuly retrieved ");
    				}else {
    					serviceStatus.setStatus("failure");
    					serviceStatus.setMessage("Plan data not fond.");
    				}

    			}else {
    				serviceStatus.setStatus("failure");
    				serviceStatus.setMessage("invalid input.");
    			}
    		}catch (Exception e) {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    			e.printStackTrace();
    		}
    		return serviceStatus;
    	}
   
    	
    	    	
    	/**
    	 * This method is to fetch all branches which are fall under schedule plan start and end dates. 
    	 * @param legalEntityCode
    	 * @param auditTypecode
    	 * @param planStartDate
    	 * @param planEndDate
    	 * @param maker
    	 * @return AuditPlansList
    	 */
    	
@RequestMapping(value = "/getAuditProcessAuditTypeAndPlanerUnitAndPlanStartDateAndPlanEndDate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	ServiceStatus getAuditProcessAuditTypeAndPlanerUnitAndPlanStartDateAndPlanEndDate(
    			@RequestParam("legalEntityCode") int legalEntityCode,
    			@RequestParam(value="auditType",required=false) String auditTypecode,
    			@RequestParam(value="parentUnitCode",required=false) String parentUnitCode,
    			@RequestParam(value="planStartDate",required=false) String planStartDate,
    			@RequestParam(value="planEndDate",required=false) String planEndDate,
    			@RequestParam(value="maker",required=false)  String maker){

    		serviceStatus=new ServiceStatus();
    		List auditProcessV = null;

    		try {
    			logger.info("getAuditProcessAuditTypeAndPlanerUnitAndPlanStartDateAndPlanEndDate .. "+legalEntityCode + " :: "+ auditTypecode +" :: "+parentUnitCode);
    			if((legalEntityCode>=0) && (auditTypecode!=null && !BankAuditUtil.isEmptyString(auditTypecode)) && (parentUnitCode!=null && !BankAuditUtil.isEmptyString(parentUnitCode)) && (planStartDate!=null)  && (planEndDate!=null) && (maker!=null && !BankAuditUtil.isEmptyString(maker))){
    				String ErrorMessage="";	
    				Date psDate = BankAuditUtil.parseDateTime(planStartDate);
    				Date peDate = BankAuditUtil.parseDateTime(planEndDate);
    				
    				if(psDate.compareTo(peDate) > 0) {
    					ErrorMessage="PlanStartData should be less then PlanEndDate.";
    				}
    				
    				if("".equals(ErrorMessage)) {

    					//delete
    					//auditProcessVService.deleteAuditPlanTemp(legalEntityCode,maker);
    					 
    					auditProcessV=auditProcessVService.getAuditProcessByAuditPlanAtParent(legalEntityCode,auditTypecode,parentUnitCode, planStartDate, planEndDate,maker);	// Called the STP, Product way
    					//auditProcessV=auditProcessVService.getAssessmentsByAuditType(legalEntityCode,auditTypecode,planStartDate, planEndDate,maker);      // Execute the query to get one record per Unit.
    					logger.info(" auditProcessV .."+auditProcessV);

    					if(auditProcessV!=null && auditProcessV.size()>0){
    						serviceStatus.setResult(auditProcessV);
    						serviceStatus.setStatus("success");
    						serviceStatus.setMessage("successfuly retrieved ");
    					}else {
    						serviceStatus.setStatus("failure");
    						serviceStatus.setMessage("No Result Found With Above Search.");
    					}

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

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

    		}/*catch (ParseException e) {  // Commented for RCSA NABARD					
    			e.printStackTrace();
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("Incorrect input date formate.");
    		}*/catch (Exception e) {
    			e.printStackTrace();
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    		}

    		return serviceStatus;
    	}
    	
    	
@RequestMapping(value="/isDateFallOnFortnight", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    	ServiceStatus isValiedBusinessStartDate(
    			@RequestParam("legalEntityCode") String legalEntityCode,
    			@RequestParam("unitId") String unitId,
    			@RequestParam(value="currStartDate") String currentStartDate){
    		serviceStatus=new ServiceStatus();
    		Map<String, String> map=null;
    		try {
    			
    			if( (legalEntityCode!=null && !BankAuditUtil.isEmptyString(legalEntityCode))  &&  (unitId!=null && !BankAuditUtil.isEmptyString(unitId)) 
    					&& (currentStartDate!=null && !BankAuditUtil.isEmptyString(currentStartDate)) )
    			{	
    				if(BankAuditUtil.parseStringToDate(currentStartDate)!=null ){
    					map=auditProcessVService.isDateFallOnFortnight(legalEntityCode.trim(), unitId.trim(), currentStartDate.trim());
    						if(map!=null && "1".equalsIgnoreCase(map.get("validDate")))
    							map=auditProcessVService.isDateFallOnHoliday(legalEntityCode.trim(), unitId.trim(), currentStartDate.trim());
    				}
    				if(map!=null){
    						serviceStatus.setResult(map);
    						serviceStatus.setStatus("success");
    						serviceStatus.setMessage("successfuly retrieved ");
    				}else {
    						serviceStatus.setStatus("success");
    						serviceStatus.setMessage("Not a valied Business Start Date.");
    				}
    			}else{
    				serviceStatus.setStatus("failure");
    				serviceStatus.setMessage("Invalid input.");
    			}
    		} catch (Exception e) {
    			serviceStatus.setStatus("failure");
    			serviceStatus.setMessage("failure");
    			e.printStackTrace();
    		}
    		return serviceStatus;
    	}

}
