/*
 * 
 */
package com.bankaudit.rest.controller;

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

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 com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintAuditSubgroup;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.MaintAuditSubgroupService;
import com.bankaudit.service.SequenceAppenderService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;


/**
 * The Class {@link MaintAuditSubgroupController} provides the REST services to
 * manage the {@link MaintAuditSubgroup} class objects .
 *
 * @author amit.patel
 * @version 1.0
 */

@RestController

@RequestMapping("/api/maintAuditSubgroup")
public class MaintAuditSubgroupController {



	/**
	 * The maint audit subgroup service is autowired and make methods available
	 * from service layer .
	 */
	@Autowired
	MaintAuditSubgroupService maintAuditSubgroupService;

	/**
	 * The business date service is autowired and make methods available from
	 * service layer .
	 */
	@Autowired
	BusinessDateService businessDateService;

	/**
	 * The sequence appender service is autowired and make methods available
	 * from service layer .
	 */
	@Autowired
	SequenceAppenderService sequenceAppenderService;

	/** The Constant logger is used to specify the . */
	static final Logger logger = Logger.getLogger(MaintAuditSubgroupController.class);

	/**
	 * This method is use to Creates the maint audit subgroup.
	 *
	 * @param maintAuditSubgroup
	 *            specify the maint audit subgroup
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus createMaintAuditSubgroup(@RequestBody MaintAuditSubgroup maintAuditSubgroup) {
		ServiceStatus serviceStatus = new ServiceStatus();
		try {
			if (maintAuditSubgroup != null && maintAuditSubgroup.getLegalEntityCode() != null
					&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditSubGroupCode())
					&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditSubGroupName())
					&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditTypeCode())
					&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditGroupCode())
					&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getMaker())) {
					maintAuditSubgroup.setBusinessdateTimestamp(businessDateService
							.getBusinessDateTimestampByLegalEntityCode(maintAuditSubgroup.getLegalEntityCode()));
					maintAuditSubgroup.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
					if(maintAuditSubgroup.getStatus().equals(BankAuditConstant.STATUS_AUTH)){
						maintAuditSubgroup.setCheckerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintAuditSubgroup.getLegalEntityCode()));
					}else{
						maintAuditSubgroup.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintAuditSubgroup.getLegalEntityCode()));
					}
					
					maintAuditSubgroupService.createMaintAuditSubgroup(maintAuditSubgroup);
	
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully created");
				} else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("invalid payload ");
			}
		}catch (Exception e) {
			serviceStatus.setStatus("failure");
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}else serviceStatus.setMessage("EXCP_OCCURED");
		}

		return serviceStatus;
	}

	/**
	 * This method is use to Update maint audit subgroup.
	 *
	 * @param maintAuditSubgroup
	 *            specify the maint audit subgroup
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateMaintAuditSubgroup(@RequestBody MaintAuditSubgroup maintAuditSubgroup) {
		ServiceStatus serviceStatus = new ServiceStatus();

		if (maintAuditSubgroup != null && maintAuditSubgroup.getLegalEntityCode() != null
				&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditSubGroupName())
				&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditTypeCode())
				&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditGroupCode())
				&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getAuditSubGroupCode())
				&& !BankAuditUtil.isEmptyString(maintAuditSubgroup.getMaker())) {
			try {
				maintAuditSubgroup.setBusinessdateTimestamp(businessDateService
						.getBusinessDateTimestampByLegalEntityCode(maintAuditSubgroup.getLegalEntityCode()));
				maintAuditSubgroup.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				maintAuditSubgroup.setMakerTimestamp(maintAuditSubgroup.getBusinessdateTimestamp());
				maintAuditSubgroupService.updateMaintAuditSubgroup(maintAuditSubgroup);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
			} catch (Exception e) {

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

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

		return serviceStatus;
	}

	/**
	 * This method is use to Gets the maint audit subgroup.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param auditSubGroupCode
	 *            specify the audit sub group code
	 * @param auditGroupCode
	 *            specify the audit group code
	 * @param auditTypeCode
	 *            specify the audit type code
	 * @param status
	 *            specify the status
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value = "/get/{legalEntityCode}/{auditSubGroupCode}/{auditGroupCode}/{auditTypeCode}/{status}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMaintAuditSubgroup(@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("auditSubGroupCode") String auditSubGroupCode,
			@PathVariable("auditGroupCode") String auditGroupCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("status") String status
			) {
		ServiceStatus serviceStatus = new ServiceStatus();

		if (legalEntityCode != null
				&& !BankAuditUtil.isEmptyString(auditSubGroupCode)
				&& !BankAuditUtil.isEmptyString(auditGroupCode)
				&& !BankAuditUtil.isEmptyString(auditTypeCode)
				) {

			try {
				MaintAuditSubgroup maintAuditSubgroup = maintAuditSubgroupService.getMaintAuditSubgroup(legalEntityCode,
						auditSubGroupCode,auditGroupCode,auditTypeCode,status);
				if (maintAuditSubgroup != null) {
					serviceStatus.setResult(maintAuditSubgroup);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved ");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditSubgroup not found");
				}

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

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

		return serviceStatus;
	}



	/**
	 * This method is use to Gets the maint audit subgroupes.
	 *
	 * @param drawStr
	 *            specify the draw str
	 * @param length
	 *            specify the length
	 * @param start
	 *            specify the start
	 * @param search
	 *            specify the search
	 * @param orderColumnStr
	 *            specify the order column str
	 * @param orderDirection
	 *            specify the order direction
	 * @param legalEntityCodeStr
	 *            specify the legal entity code str
	 * @return the data table response class object with response status and
	 *         payload .
	 */
	
@RequestMapping(value="/getAll",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	DataTableResponse getMaintAuditSubgroupes(
			@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){
		
		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 = maintAuditSubgroupService.getMaintAuditSubgroupes(legalEntityCode,search,orderColumn,orderDirection,page,size);
				dataTableResponse.setDraw(draw);    
					
			} catch (Exception e) {
                dataTableResponse=new DataTableResponse();
                dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}
		
		}else {
			dataTableResponse=new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}
		
		return dataTableResponse;
	}

	/**
	 * This method is use to Gets the maint audit subgroupes.
	 *
	 * @param legalEntityCodeStr
	 *            specify the legal entity code str
	 * @param auditTypeCode
	 *            specify the audit type code
	 * @param auditGroupCode
	 *            specify the audit group code
	 * @param auditSubGroupCode
	 *            specify the audit sub group code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/get",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	ServiceStatus getMaintAuditSubgroupes(
			@RequestParam("legalEntityCode")String legalEntityCodeStr,
			@RequestParam(required=false,value="auditTypeCode") String auditTypeCode,
			@RequestParam(required=false,value="auditGroupCode") String auditGroupCode,
			@RequestParam(required=false,value="auditSubGroupCode") String auditSubGroupCode
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		
		if(!BankAuditUtil.isEmptyString(legalEntityCodeStr)){
			try {
				Integer legalEntityCode=Integer.parseInt(legalEntityCodeStr);
				List<MaintAuditSubgroup> maintAuditSubgroups = maintAuditSubgroupService.getMaintAuditSubgroupes(legalEntityCode,auditTypeCode,auditGroupCode,auditSubGroupCode);
				if(maintAuditSubgroups!=null && !maintAuditSubgroups.isEmpty()){
					
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved");
					serviceStatus.setResult(maintAuditSubgroups);
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("no records found");
				}
					
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
		
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("legalEntityCode required");
		}
		
		
		return serviceStatus;
	}
	
	/**
	 * This method is use to Gets the maint audit subgroupe.
	 *
	 * @param legalEntityCodeStr
	 *            specify the legal entity code str
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/getByLegalEntityCode",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	ServiceStatus getMaintAuditSubgroupe(@RequestParam("legalEntityCode")String legalEntityCodeStr){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(!BankAuditUtil.isEmptyString(legalEntityCodeStr)){
			try {
				Integer legalEntityCode=Integer.parseInt(legalEntityCodeStr);
				List<MaintAuditSubgroup> maintAuditSubgroup = maintAuditSubgroupService.getMaintAuditSubgroupe(legalEntityCode);
				if(maintAuditSubgroup!=null && !maintAuditSubgroup.isEmpty()){
					
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfuly retrieved");
					serviceStatus.setResult(maintAuditSubgroup);
				
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("no records found");
				}
					
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				e.printStackTrace();
			}
		
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("legalEntityCode required");
		}
		
		
		return serviceStatus;
	}
	
	/**
	 * This method is use to Checks if is maint audit subgroup.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param auditTypeCode
	 *            specify the audit type code
	 * @param auditGroupCode
	 *            specify the audit group code
	 * @param auditSubGroupCode
	 *            specify the audit sub group code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/isMaintAuditSubgroup/{legalEntityCode}/{auditTypeCode}/{auditGroupCode}/{auditSubGroupCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus isMaintAuditSubgroup(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode,
			@PathVariable("auditGroupCode") String auditGroupCode,
			@PathVariable("auditSubGroupCode") String auditSubGroupCode
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null 
				&& !BankAuditUtil.isEmptyString(auditTypeCode)
				&& !BankAuditUtil.isEmptyString(auditGroupCode)
				&& !BankAuditUtil.isEmptyString(auditSubGroupCode)){
			try {
			    Boolean maintAuditSubgroup=maintAuditSubgroupService.isMaintAuditSubgroup(legalEntityCode,auditTypeCode,auditGroupCode,auditSubGroupCode);
		    	serviceStatus.setResult(maintAuditSubgroup);
			    serviceStatus.setStatus("success");
			    serviceStatus.setMessage("successfully retrieved ");	
		    
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}
	
	
@RequestMapping(value="/isMaintAuditSubgroupLE/{legalEntityCode}/{auditSubGroupCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus isMaintAuditSubgroupLE(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditSubGroupCode") String auditSubGroupCode	){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditSubGroupCode)){
			try {
			    Boolean maintAuditSubgroup=maintAuditSubgroupService.isMaintAuditSubgroupLE(legalEntityCode,auditSubGroupCode);
		    	serviceStatus.setResult(maintAuditSubgroup);
			    serviceStatus.setStatus("success");
			    serviceStatus.setMessage("successfully retrieved ");	
		  } catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}
	
	// Service to delete the Records, 
	
@RequestMapping(value="/deleteAuditSubGroupByID/{legalEntityCode}/{auditTypeCode}/{auditGroupCode}/{auditSubGroupCode}/{userId}",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus deleteAuditSubGroupByID(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditTypeCode") String auditTypeCode, @PathVariable("auditGroupCode") String auditGroupCode, 
			@PathVariable("auditSubGroupCode") String auditSubGroupCode, @PathVariable("userId") String userId){
	ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode !=null && !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(auditGroupCode)){
			try {
				maintAuditSubgroupService.deleteAuditSubGroupByID(legalEntityCode, auditTypeCode, auditGroupCode, auditSubGroupCode, userId);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("Record deleted successfully.");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				e.printStackTrace();
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}	
	
}
