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

import java.sql.Timestamp;

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

import com.bankaudit.dao.MaintLegalEntityDao;
import com.bankaudit.model.BusinessDate;
import com.bankaudit.model.EntityLevelCodeDesc;
import com.bankaudit.model.MaintEntity;
import com.bankaudit.model.MaintLegalEntity;

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

	/**
	 * The maint legal entity dao is autowired and make methods available from
	 * dao layer .
	 */
	@Autowired
	MaintLegalEntityDao maintLegalEntityDao;
	
	/**
	 * The business date service is autowired and make methods available from
	 * dao layer .
	 */
	@Autowired
	BusinessDateService businessDateService;
	
	/**
	 * The entity level code desc service is autowired and make methods
	 * available from dao layer .
	 */
	@Autowired 
	EntityLevelCodeDescService entityLevelCodeDescService;
	
	/**
	 * The maint entity service is autowired and make methods available from dao
	 * layer .
	 */
	@Autowired
	MaintEntityService maintEntityService;
	

	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintLegalEntityService#createMaintLegalEntity(com.bankaudit.model.MaintLegalEntity)
	 */
	@Override
	public void createMaintLegalEntity(MaintLegalEntity maintLegalEntity) {
	
		maintLegalEntityDao.save(maintLegalEntity);
		BusinessDate businessDate=new BusinessDate();
		businessDate.setLegalEntityCode(maintLegalEntity.getLegalEntityCode());
		businessDate.setBusinessDate(maintLegalEntity.getBusinessdateTimestamp());
		businessDate.setLastBusinessDate(maintLegalEntity.getBusinessdateTimestamp());
		businessDate.setMaker(maintLegalEntity.getMaker());
		businessDate.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
		businessDate.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
		businessDate.setProcessDate(new Timestamp(System.currentTimeMillis()));
	    businessDate.setStatus("A");
		businessDateService.createBusinessDate(businessDate);
		
		EntityLevelCodeDesc entityLevelCodeDesc =null;
		
		for (Integer i = 1; i <= maintLegalEntity.getNumberOfLevels(); i++) {
			entityLevelCodeDesc =new EntityLevelCodeDesc();
			entityLevelCodeDesc.setLegalEntityCode(maintLegalEntity.getLegalEntityCode());
			entityLevelCodeDesc.setLevelCode(i.toString());
			entityLevelCodeDesc.setLevelDesc("Level "+i+" Description");
			entityLevelCodeDesc.setStatus("A");
			entityLevelCodeDesc.setBusinessdateTimestamp(maintLegalEntity.getBusinessdateTimestamp());
			entityLevelCodeDesc.setMaker(maintLegalEntity.getMaker());
			entityLevelCodeDesc.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
			entityLevelCodeDesc.setChecker(maintLegalEntity.getMaker());
			entityLevelCodeDesc.setCheckerTimestamp(new Timestamp(System.currentTimeMillis()));
			entityLevelCodeDesc.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
			
			entityLevelCodeDescService.createEntityLevelCodeDesc(entityLevelCodeDesc);
			
		}
		
		MaintEntity maintEntity = new MaintEntity();
		
		maintEntity.setLegalEntityCode(maintLegalEntity.getLegalEntityCode());
		maintEntity.setBusinessdateTimestamp(maintLegalEntity.getBusinessdateTimestamp());
		maintEntity.setLevelCode("1");
		maintEntity.setUnitCode("001");
		maintEntity.setUnitName(maintLegalEntity.getLegalEntityName());
		maintEntity.setParentUnitCode("00");
		maintEntity.setEntityStatus("A");
		maintEntity.setStatus("A");
		maintEntity.setBusinessdateTimestamp(maintLegalEntity.getBusinessdateTimestamp());
		maintEntity.setMaker(maintLegalEntity.getMaker());
		maintEntity.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
		maintEntity.setChecker(maintLegalEntity.getMaker());
		maintEntity.setCheckerTimestamp(new Timestamp(System.currentTimeMillis()));
		maintEntity.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
		
		maintLegalEntityDao.save(maintEntity);
			
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintLegalEntityService#updateEntityStatus(java.lang.Integer, java.lang.String)
	 */
	@Override
	public void updateEntityStatus(Integer legalEntityCode, String entityStatus) {
		
		maintLegalEntityDao.updateEntityStatus(legalEntityCode,entityStatus);
		
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintLegalEntityService#getMaintLegalEntityById(java.lang.Integer)
	 */
	@Override
	public MaintLegalEntity getMaintLegalEntityById(Integer legalEntityCode) {
		
		return (MaintLegalEntity)maintLegalEntityDao.get(MaintLegalEntity.class, legalEntityCode);
	}
	
}
