/*
 * 
 */
package com.bankaudit.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.dao.MaintRoleEntitlementDao;
import com.bankaudit.dto.TreeviewItem;
import com.bankaudit.model.GeneralParameter;
import com.bankaudit.model.MaintRoleEntitlement;

/**
 * The Class {@link MaintRoleEntitlementServiceImpl} Implements the business
 * logic method.
 *
 * @author amit.patel
 * @version 1.0
 */
@Service("maintRoleEntitlementService")
@Transactional("transactionManager")
public class MaintRoleEntitlementServiceImpl implements  MaintRoleEntitlementService {

	/**
	 * The maint role entitlement dao is autowired and make methods available
	 * from dao layer .
	 */
	@Autowired
	MaintRoleEntitlementDao maintRoleEntitlementDao;
	
	
	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#updateRoleEntitlement(com.bankaudit.model.MaintRoleEntitlement)
	 */
	@Override
	public void updateRoleEntitlement(MaintRoleEntitlement maintRoleEntitlement) {
		
		MaintRoleEntitlement roleEntitlement=null;
		
		maintRoleEntitlementDao.deleteRoleEntitlements(maintRoleEntitlement.getLegalEntityCode(),maintRoleEntitlement.getUgRoleCode());
		
		for (String functionId : maintRoleEntitlement.getFunctionIds()) {
			
			roleEntitlement=new MaintRoleEntitlement();
			
			roleEntitlement.setBusinessdateTimestamp(maintRoleEntitlement.getBusinessdateTimestamp());
			roleEntitlement.setSysdateTimestamp(maintRoleEntitlement.getSysdateTimestamp());
			roleEntitlement.setFunctionId(functionId);
			roleEntitlement.setLegalEntityCode(maintRoleEntitlement.getLegalEntityCode());
			roleEntitlement.setMaker(maintRoleEntitlement.getMaker());
			roleEntitlement.setUgRoleCode(maintRoleEntitlement.getUgRoleCode());
			roleEntitlement.setMakerTimestamp(maintRoleEntitlement.getMakerTimestamp());
			
			maintRoleEntitlementDao.save(roleEntitlement);
		}
		
		
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#getMaintRoleEntitlementByLegalEntityCodeAndUgRoleCode(java.lang.Integer, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MaintRoleEntitlement> getMaintRoleEntitlementByLegalEntityCodeAndUgRoleCode(Integer legalEntityCode,
			String ugRoleCode) {
		Map<String,Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode", legalEntityCode);
		properties.put("ugRoleCode",ugRoleCode);
		return maintRoleEntitlementDao.getEntitiesByMatchingProperties(MaintRoleEntitlement.class, properties);
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#getRoleEntitlementByRole(java.lang.Integer, java.lang.String)
	 */
	@Override
	public List<TreeviewItem> getRoleEntitlementByRole(Integer legalEntityCode, String ugRoleCode) {
		
		
		List<TreeviewItem> treeviewItemsList1=new ArrayList<TreeviewItem>();
		
		treeviewItemsList1 =maintRoleEntitlementDao.getMenusByLegalEntityCodeAndKey1(legalEntityCode,"MAINMENU");  // I/P=MAINMENU,  O/P = key2 & value
		
		
		for (TreeviewItem treeviewItem1 : treeviewItemsList1) {
			
			treeviewItem1.setChildren(maintRoleEntitlementDao.getMenusByLegalEntityCodeAndKey1(legalEntityCode,treeviewItem1.getValue())); // I/P=value,  O/P =key2, value & description 
			
			for (TreeviewItem treeviewItem2 : treeviewItem1.getChildren()) {
				
				treeviewItem2.setChildren(maintRoleEntitlementDao.getMenusByLegalEntityCodeAndKey1Distinct(legalEntityCode,treeviewItem2.getValue())); // I/P= value, O/P = disctinct key2, key2
				
				for (TreeviewItem treeviewItem3 : treeviewItem2.getChildren()) {
					
					treeviewItem3.setChildren(maintRoleEntitlementDao.getMappedEntitlement(legalEntityCode,ugRoleCode,treeviewItem3.getText()));
				}
				
			}
			
		}
		
		return treeviewItemsList1;
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#deleteRoleEntitlements(java.lang.Integer, java.lang.String)
	 */
	@Override
	public void deleteRoleEntitlements(Integer legalEntityCode, String ugRoleCode) {
		maintRoleEntitlementDao.deleteRoleEntitlements(legalEntityCode,ugRoleCode);
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#getRoleEntitlementByLegalEntityCode(java.lang.Integer)
	 */
	@Override
	public List<TreeviewItem> getRoleEntitlementByLegalEntityCode(Integer legalEntityCode) {
		
		
		List<TreeviewItem> treeviewItemsList1=new ArrayList<TreeviewItem>();
		
		treeviewItemsList1 =maintRoleEntitlementDao.getMenusByLegalEntityCodeAndKey1(legalEntityCode,"MAINMENU");
		
		
		for (TreeviewItem treeviewItem1 : treeviewItemsList1) {
			
			treeviewItem1.setChildren(maintRoleEntitlementDao.getMenusByLegalEntityCodeAndKey1(legalEntityCode,treeviewItem1.getValue()));
			
			for (TreeviewItem treeviewItem2 : treeviewItem1.getChildren()) {
				
				treeviewItem2.setChildren(maintRoleEntitlementDao.getMenusByLegalEntityCodeAndKey1Distinct(legalEntityCode,treeviewItem2.getValue()));
				
				for (TreeviewItem treeviewItem3 : treeviewItem2.getChildren()) {
					
					treeviewItem3.setChildren(maintRoleEntitlementDao.getEntitlement(legalEntityCode,treeviewItem3.getText()));
				}
				
			}
			
		}
		
		return treeviewItemsList1;
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#getFunctionIds(java.lang.Integer, java.lang.String, java.lang.String)
	 */
	@Override
	public List<String> getFunctionIds(Integer legalEntityCode, String ugRoleCode, String status) {
		
		return maintRoleEntitlementDao.getFunctionIds( legalEntityCode,  ugRoleCode,  status);
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintRoleEntitlementService#deleteMaintRoleEntitlementWrk(java.lang.Integer, java.lang.String)
	 */
	@Override
	public void deleteMaintRoleEntitlementWrk(Integer legalEntityCode, String ugRoleCode) {
		maintRoleEntitlementDao.deleteMaintRoleEntitlementWrk( legalEntityCode,  ugRoleCode);
		
	}

}






















