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

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

import javax.ws.rs.Path;

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.RestController;

import com.bankaudit.dto.TreeviewItem;
import com.bankaudit.model.MaintRoleEntitlement;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.MaintRoleEntitlementService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;


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

@RestController

@RequestMapping("/api/maintRoleEntitlement")
public class MaintRoleEntitlementController {


	/**
	 * The maint role entitlement service is autowired and make methods
	 * available from service layer .
	 */
	@Autowired
	MaintRoleEntitlementService maintRoleEntitlementService;
	
	/** The Constant logger is used to specify the . */
	static final Logger logger = Logger.getLogger(MaintAuditFindingController.class);
	
	/**
	 * The business date service is autowired and make methods available from
	 * service layer .
	 */
	@Autowired
	BusinessDateService businessDateService;

	/**
	 * This method is use to Update role entitlement.
	 *
	 * @param maintRoleEntitlement
	 *            specify the maint role entitlement
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateRoleEntitlement(@RequestBody MaintRoleEntitlement maintRoleEntitlement){
		ServiceStatus serviceStatus =new ServiceStatus();
		
		if(maintRoleEntitlement!=null 
				&& maintRoleEntitlement.getFunctionIds()!=null
				&& !maintRoleEntitlement.getFunctionIds().isEmpty()
				&& !BankAuditUtil.isEmptyString(maintRoleEntitlement.getMaker())
				&& !BankAuditUtil.isEmptyString(maintRoleEntitlement.getUgRoleCode())
				&& maintRoleEntitlement.getLegalEntityCode()!=null
				){
			try {
				maintRoleEntitlement.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintRoleEntitlement.getLegalEntityCode()));
				maintRoleEntitlement.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				maintRoleEntitlement.setMakerTimestamp(maintRoleEntitlement.getBusinessdateTimestamp());
				maintRoleEntitlementService.updateRoleEntitlement(maintRoleEntitlement);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
			
			} catch (Exception e) {
			
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failrue");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}
			
			
		}else {
			serviceStatus.setMessage("invalid payload");
            serviceStatus.setStatus("failure");
		}
		
		return serviceStatus;
				
		
	}
	
	/**
	 * This method is use to Gets the role entitlement by role.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param ugRoleCode
	 *            specify the ug role code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="getByRole/{legalEntityCode}/{ugRoleCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRoleEntitlementByRole(@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("ugRoleCode")String ugRoleCode){
		
		ServiceStatus serviceStatus=new ServiceStatus();
		List<TreeviewItem> treeviewItems=new ArrayList<TreeviewItem>();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(ugRoleCode)){
			
			try {
				
				treeviewItems=maintRoleEntitlementService.getRoleEntitlementByRole(legalEntityCode,ugRoleCode);
				
				serviceStatus.setResult(treeviewItems);
				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 Ids ");
		}
		
		return serviceStatus;
		
	}
	
	/**
	 * This method is use to Gets the role entitlement by legal entity code.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="getByLegalEntityCode/{legalEntityCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRoleEntitlementByLegalEntityCode(@PathVariable("legalEntityCode") Integer legalEntityCode
			){
		
		ServiceStatus serviceStatus=new ServiceStatus();
		List<TreeviewItem> treeviewItems=new ArrayList<TreeviewItem>();
		
		if(legalEntityCode!=null
				){
			
			try {
				
				treeviewItems=maintRoleEntitlementService.getRoleEntitlementByLegalEntityCode(legalEntityCode);
				
				serviceStatus.setResult(treeviewItems);
				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 Ids ");
		}
		
		return serviceStatus;
		
	}
	
	
@RequestMapping(value="getFunctionIdsByRole/{legalEntityCode}/{ugRoleCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getFunctionIdsByRole(@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("ugRoleCode")String ugRoleCode){
		
		ServiceStatus serviceStatus=new ServiceStatus();
		List<String> functionIdsLst=new ArrayList();
		
		if(legalEntityCode!=null
				&& !BankAuditUtil.isEmptyString(ugRoleCode)){
			
			try {
				
				functionIdsLst=maintRoleEntitlementService.getFunctionIds(legalEntityCode,ugRoleCode,"A");
				
				serviceStatus.setResult(functionIdsLst);
				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 Ids ");
		}
		
		return serviceStatus;
	}
	
}























