package com.bankaudit.rest.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
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.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 org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.process.model.AuditDocumentDetails;
/*import com.bankaudit.model.AuditTypeDocumentDetails;
import com.bankaudit.model.AuditTypeDocumentDetailsWrk;*/ 
import com.bankaudit.service.AuditTypeDocumentDetailsService;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/auditTypeDocumentDetails")
public class AuditTypeDocumentDetailsController {
	
	@Autowired
	BusinessDateService businessDateService;
	
	@Autowired
	AuditTypeDocumentDetailsService auditTypeDocumentDetailsService;
	
	private static final int BUFFER_SIZE = 4096;
	static final Logger logger = Logger.getLogger(MaintAuditTypeDescController.class);
	
	
@RequestMapping(value="/getAll",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	DataTableResponse getAuditTypeDocumentDetails(
			@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(value="documentType",required=false)String documentType,
			@RequestParam(value="documentSubType",required=false)String documentSubType
			){
		
		DataTableResponse dataTableResponse=null;
		
		if(!BankAuditUtil.isEmptyString(drawStr)
				&& !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)
				&& !BankAuditUtil.isEmptyString(legalEntityCodeStr)){
			try {
				Integer legalEntityCode=Integer.parseInt(legalEntityCodeStr);
				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 = auditTypeDocumentDetailsService.getAuditTypeDocumentDetails(legalEntityCode, search, orderColumn, orderDirection, 
						page, size, documentType, documentSubType); 
				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="/getByIdAndAuditType/{legalEntityCode}/{auditTypeCode}/{Id}/{status}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditTypeDocumentDetailByIdAndAuditType(
			@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("Id") Integer Id,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("status") String status,
			@PathVariable(value="documentType",required=false)String documentType,
			@PathVariable(value="documentSubType",required=false)String documentSubType
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null && Id!=null){
			
			try {
			    AuditDocumentDetails auditTypeDocumentDetails=auditTypeDocumentDetailsService.getAuditTypeDocumentDetailByIdAndAuditType(legalEntityCode,Id, auditTypeCode ,status);
			    if(auditTypeDocumentDetails!=null){
			    	serviceStatus.setResult(auditTypeDocumentDetails);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAuditActivity not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}*/
	
	/*
@RequestMapping(value="/uploadDocument" ,method=RequestMethod.POST,consumes=MediaType.MULTIPART_FORM_DATA_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus uploadDocument(
			@RequestParam(value="maker")String maker,
			@RequestParam(value="role")String role,
			@RequestParam(value="files")CommonsMultipartFile[] files,
			@RequestParam(value="legalEntityCode")Integer legalEntityCode,
			@RequestParam(value="auditTypeCode")String auditTypeCode,
			@RequestParam(value="id")Integer id,
			@RequestParam(value="documentType")String documentType,			
			@RequestParam(value="documentName")String documentName,
			@RequestParam(value="warningCheck")String warningCheck,
			@RequestParam(value="documentSubType")String documentSubType){
	
		ServiceStatus serviceStatus=new ServiceStatus();
		Map<String, Object> result=new HashMap<>();
		try {
			if(legalEntityCode !=null && id !=null && auditTypeCode !=null && files!=null ){
				//List<AuditTypeDocumentDetails> auditTypeDocumentDetailsLst=maintAuditTypeDescService.getAuditTypeDocumentDetails(legalEntityCode, auditTypeCode, documentType, maker, role);
				String fileName=legalEntityCode.toString()+auditTypeCode+documentType;
				
				Boolean isWarning=false;
				String duplicateFileNames="";
				List<Integer> duplicateDocIds=new ArrayList<>(); 
				for (CommonsMultipartFile commonsMultipartFile : files) {
					if(!BankAuditUtil.validateFileType(commonsMultipartFile, BankAuditConstant.DOC_TYPES)){
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("file type : "+BankAuditUtil.getFileExtension(commonsMultipartFile.getOriginalFilename())
						+" not supported");
						return serviceStatus;
					}
					
					 
				}
				if(isWarning){
					serviceStatus.setStatus("warning");
				    serviceStatus.setMessage("file " + duplicateFileNames + "  already exist do you want to replace  ");
				    serviceStatus.setResult(duplicateDocIds);
				    return serviceStatus;
				}
				result.put("documentUploadId", auditTypeDocumentDetailsService.uploadDocument(legalEntityCode, id, auditTypeCode, documentType, documentName,
						maker, role, files, documentSubType));
				serviceStatus.setResult(result);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully uploaded");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("Invalid payload");
			}
			
		} catch (Exception e) {
			logger.error(e.getStackTrace());
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}*/
	
	/*
@RequestMapping(value="/getDocumentDetails" ,method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getDocumentDetails(
			@RequestParam(value="legalEntityCode") Integer legalEntityCode, 
			@RequestParam(value="status") String status,
			@RequestParam(required=false,value="documentType") String documentType,
			@RequestParam(value="auditTypeCode") String auditTypeCode, 
			@RequestParam(required=false,value="userId") String userId,
			@RequestParam(required=false,value="userRoleId") String userRoleId,
			@RequestParam(required=false,value="documentSubType") String documentSubType){
		ServiceStatus serviceStatus=new ServiceStatus();
		try {  
			//List<AuditTypeDocumentDetails> documentDetails = auditTypeDocumentDetailsService.getAuditTypeDocumentDetails(legalEntityCode, auditTypeCode, documentType, userId, userRoleId, status);
			List<Object> auditTypeDocumentDetails = auditTypeDocumentDetailsService.getAuditTypeDocumentDetailsByAuditTypeAndStatus
					(legalEntityCode, auditTypeCode, documentType, userId, userRoleId, status, documentSubType);
				 
			 
			if((auditTypeDocumentDetails!=null && !auditTypeDocumentDetails.isEmpty()) ){
				serviceStatus.setResult(auditTypeDocumentDetails);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully got the result ");
			} 
			else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("No docs found ");
			}
		} catch (Exception e) {
			logger.error(e.getStackTrace());
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}*/
	
	/*
@RequestMapping(value="/downloadDocument",method = RequestMethod.GET)
    public void  downloadProfileFormat(
    		@RequestParam(value="id",required=true)Integer id,
    		@RequestParam(value="legalEntityCode",required=true)Integer legalEntityCode,
    		@RequestParam(value="auditTypeCode",required=true)String auditTypeCode,
    		@RequestParam(value="status",required=true)String status,
    		@RequestParam(value="documentType",required=false)String documentType,
    		@RequestParam(value="userId",required=true)String userId,
    		@RequestParam(value="userRoleId",required=true)String userRoleId,
    		HttpServletRequest request,
            HttpServletResponse response) throws IOException {    	
    	
    	ServletContext context=request.getServletContext();
    	String filePath =auditTypeDocumentDetailsService.getReferencePathById(legalEntityCode, id, status);
    	 	if(!BankAuditUtil.isEmptyString(filePath)){
		    		File file=new File(filePath);
		    		if(file.isFile()){
		    			 FileInputStream inputStream = new FileInputStream(file);

		    			 String mimeType = context.getMimeType(filePath); // get MIME type of the file
		                 
		                 if (mimeType == null) {
		                     mimeType = "application/octet-stream"; // set to binary type if MIME mapping not found
		                 }
		              
		                 // set content attributes for the response
		                 response.setContentType(mimeType);
		                 response.setContentLength((int) file.length());
		          
		                 // set headers for the response
		                 String headerKey = "Content-Disposition";
		                 String headerValue = String.format("attachment; filename=\"%s\"",file.getName());
		                 response.setHeader(headerKey, headerValue);
		          
		                 // get output stream of the response
		                 OutputStream outStream = response.getOutputStream();
		          
		                 byte[] buffer = new byte[BUFFER_SIZE];
		                 int bytesRead = -1;
		          
		                 // write bytes read from the input stream into the output stream
		                 while ((bytesRead = inputStream.read(buffer)) != -1) {
		                     outStream.write(buffer, 0, bytesRead);
		                 }
		                 inputStream.close();
		                 outStream.close();                  
		        	}else {
		    			 response.getWriter().print("Document not exist");
					}
		    }else {
   			 response.getWriter().print("Document not exist");
			}
   	}*/
	
	
@RequestMapping(method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateAuditTypeDocumentDetails(@RequestBody AuditDocumentDetails auditTypeDocumentDetails){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(auditTypeDocumentDetails!=null
				&& auditTypeDocumentDetails.getLegalEntityCode()!=null
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getAuditTypeCode())
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getDocumentType())
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getMaker())
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getStatus())
				){
			
			try {
				
				auditTypeDocumentDetails.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditTypeDocumentDetails.getLegalEntityCode()));
				auditTypeDocumentDetails.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				if(auditTypeDocumentDetails.getStatus().equals(BankAuditConstant.STATUS_AUTH)){
					auditTypeDocumentDetails.setCheckerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditTypeDocumentDetails.getLegalEntityCode()));
				} else{
					auditTypeDocumentDetails.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditTypeDocumentDetails.getLegalEntityCode()));
				}  
				auditTypeDocumentDetailsService.updateAuditTypeDocumentDetails(auditTypeDocumentDetails);
			    
			    serviceStatus.setStatus("success");
			    serviceStatus.setMessage("successfully updated");
		
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}
			
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}
	
	//// new code Start from here
	
@RequestMapping(value="/uploadMultipleDocument" ,method=RequestMethod.POST,consumes=MediaType.MULTIPART_FORM_DATA_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus uploadMultipleDocument(
			@RequestParam(value="maker")String maker,
			@RequestParam(value="role")String role,
			@RequestParam(value="files")CommonsMultipartFile[] files,
			@RequestParam(value="legalEntityCode")Integer legalEntityCode,
			@RequestParam(value="auditTypeCode")String auditTypeCode,			 
			@RequestParam(value="documentType")String documentType,
			@RequestParam(value="documentName")String documentName,
			@RequestParam(value="warningCheck")String warningCheck,
			@RequestParam(value="documentSubType")String documentSubType){
	
		ServiceStatus serviceStatus=new ServiceStatus();
		Map<String, Object> result=new HashMap<>();
		try {
			if(legalEntityCode !=null   && auditTypeCode !=null && files!=null ){
				//List<AuditTypeDocumentDetails> auditTypeDocumentDetailsLst=maintAuditTypeDescService.getAuditTypeDocumentDetails(legalEntityCode, auditTypeCode, documentType, maker, role);
				String fileName=legalEntityCode.toString()+auditTypeCode+documentType;
				
				Boolean isWarning=false;
				String duplicateFileNames="";
				List<Integer> duplicateDocIds=new ArrayList<>(); 
				for (CommonsMultipartFile commonsMultipartFile : files) {
					if(!BankAuditUtil.validateFileType(commonsMultipartFile, BankAuditConstant.DOC_TYPES)){
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("file type : "+BankAuditUtil.getFileExtension(commonsMultipartFile.getOriginalFilename())
						+" not supported");
						return serviceStatus;
					}
					
					/*if(warningCheck.equalsIgnoreCase("yes") && auditTypeDocumentDetailsLst!=null){
						for (AuditTypeDocumentDetails auditTypeDocumentDetails : auditTypeDocumentDetailsLst) {
		                	if(complianceDocumentDetails.getReferencePath().contains(fileName+"_"+commonsMultipartFile.getOriginalFilename())){
								isWarning=true;
								duplicateFileNames=duplicateFileNames+" "+commonsMultipartFile.getOriginalFilename();
								duplicateDocIds.add(complianceDocumentDetails.getId());
							}
						}
					}*/
				}
				if(isWarning){
					serviceStatus.setStatus("warning");
				    serviceStatus.setMessage("file " + duplicateFileNames + "  already exist do you want to replace  ");
				    serviceStatus.setResult(duplicateDocIds);
				    return serviceStatus;
				} 
				  Map<String, Integer> uploadStatus = auditTypeDocumentDetailsService.uploadMultipleDocument
						  (legalEntityCode, auditTypeCode, documentType, documentName, maker, role, files, documentSubType);
						   
				  result.put("documentUploadIds", uploadStatus);
				  
				serviceStatus.setResult(result);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully uploaded");
				
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("Invalid payload");
			}
			
		} catch (Exception e) {
			logger.error(e.getStackTrace());
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/updateAuditTypeDocumentDetails" ,method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateAuditTypeDocumentDetailsMulti(@RequestBody AuditDocumentDetails auditTypeDocumentDetails){
		ServiceStatus serviceStatus=new ServiceStatus();
		
/*		@RequestParam(value="legalEntityCode")Integer legalEntityCode,
		@RequestParam(value="auditTypeCode")String auditTypeCode,
		@RequestParam(value="checker")String checker,
		@RequestParam(value="status") String status, 
		@RequestParam(value="documentType")String documentType, 
		@RequestParam(value="authRejRemarks")String authRejRemarks*/
		 
		
		if(auditTypeDocumentDetails!=null
				&& auditTypeDocumentDetails.getLegalEntityCode()!=null
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getAuditTypeCode())
				//&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getDocumentType())
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getChecker())
				&& !BankAuditUtil.isEmptyString(auditTypeDocumentDetails.getStatus())){
			
			try {
				
				/*String updatestatus = auditTypeDocumentDetailsService.updateAuditTypeDocumentDetails(auditTypeDocumentDetails.getLegalEntityCode(), 
						auditTypeDocumentDetails.getAuditTypeCode(), 
						auditTypeDocumentDetails.getChecker(), 
						auditTypeDocumentDetails.getStatus(), 
						auditTypeDocumentDetails.getDocumentType(), 
						auditTypeDocumentDetails.getAuthRejRemarks());*/
				
				String updatestatus = auditTypeDocumentDetailsService.updateAuthorizeAuditTypeDocumentDetails(auditTypeDocumentDetails);
			    
				if(updatestatus.equals(BankAuditConstant.SUCCESS)){
					serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully updated");
				} else{
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("failure");
				}
		
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}
			
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}
	
	
@RequestMapping(value="/deleteAuditTypeDocument" ,method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus  deleteAuditTypeDocument(				  
			@RequestParam(value="legalEntityCode") Integer legalEntityCode, 
			@RequestParam(value="id") Integer id, 
			@RequestParam(value="auditTypeCode") String auditTypeCode, 
			@RequestParam(value="status") String status,
			@RequestParam(value="entityStatus") String entityStatus,
			@RequestParam(value="documentType") String documentType, 
			@RequestParam(value="documentSubType") String documentSubType)  {
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(!BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(status) 
				&& id!=null && id>0
				&& legalEntityCode!=null && legalEntityCode>0){
			try{
			   String updateStatus =	auditTypeDocumentDetailsService.deleteAuditTypeDocumentFromUI
					   (legalEntityCode, id, auditTypeCode, status, entityStatus, documentType, documentSubType);
				 
				if(updateStatus.equals(BankAuditConstant.SUCCESS)){
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully deleted the selected document");
				}else{
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("failed to delete the selected document");
				}
			}catch (Exception e) {
				logger.error(e.getStackTrace());
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else{
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("missing parameter value document id");
		} 			
		
		return serviceStatus;
	}
	
	
@RequestMapping(value="/deleteAllAuditTypeDocument" ,method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus deleteAllAuditTypeDocument(
			@RequestParam(value="legalEntityCode")Integer legalEntityCode,
			@RequestParam(value="auditTypeCode")String auditTypeCode, 
			@RequestParam(value="documentType")String documentType,
			@RequestParam(value="status") String status,  
			@RequestParam(value="documentSubType") String documentSubType
			){
	
		ServiceStatus serviceStatus=new ServiceStatus();
		
		try {
			
			String updateStatus = auditTypeDocumentDetailsService.deleteAllAuditTypeDocument(legalEntityCode, auditTypeCode, documentType, status, documentSubType); 
			
			if(updateStatus.equals(BankAuditConstant.SUCCESS)){
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully deleted all the documents");
			}else{
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to delete  documents");
			}
			
		} catch (Exception e) {
			logger.error(e.getStackTrace());
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}

	
@RequestMapping(value="/getByAuditType/{legalEntityCode}/{auditTypeCode}/{status}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAuditTypeDocumentDetailByAuditType(
			@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("status") String status,
			@PathVariable(value="documentType",required=false)String documentType,
			@PathVariable(value="documentSubType",required=false) String documentSubType
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null){
			
			try {
			    AuditDocumentDetails auditTypeDocumentDetails=auditTypeDocumentDetailsService.getAuditTypeDocumentDetailByAuditType(legalEntityCode, auditTypeCode, status, documentType, documentSubType);
			    		 
			    if(auditTypeDocumentDetails!=null){
			    	serviceStatus.setResult(auditTypeDocumentDetails);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAuditActivity not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}

}
