package com.bankaudit.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bankaudit.model.GeneralTranslator;
import com.bankaudit.util.BankAuditUtil;

@Repository("generalTranslatorDao")
public class GeneralTranslatorDaoImpl extends AbstractDao implements  GeneralTranslatorDao{
	
	@SuppressWarnings("unchecked")
	@Override
	public List<GeneralTranslator> getGeneralTranslator(Integer legalEntityCode, String modCode, String language,
			String key1, String key2, String code, String maker) {
		
		Session session=getSession();
		
		StringBuilder queryStr1 = new StringBuilder(" from GeneralTranslator gt ");
		StringBuilder queryStr2 = new StringBuilder("");
		
		Boolean conditional=false;
			
		if(legalEntityCode!=null){
			queryStr2=queryStr2.append(" and gt.legalEntityCode=:legalEntityCode");
			conditional=true;
		}
		if(!BankAuditUtil.isEmptyString(modCode)){
			queryStr2=queryStr2.append(" and gt.modCode=:modCode");
			conditional=true;
		}
		if(!BankAuditUtil.isEmptyString(language)){
			queryStr2=queryStr2.append(" and gt.language=:language");
			conditional=true;
		}
		if(!BankAuditUtil.isEmptyString(key1)){
			queryStr2=queryStr2.append(" and gt.key01=:key1");
			conditional=true;
		}
		if(!BankAuditUtil.isEmptyString(key2)){
			queryStr2=queryStr2.append(" and gt.key02=:key2");
			conditional=true;
		}
		if(!BankAuditUtil.isEmptyString(maker)){
			queryStr2=queryStr2.append(" and gt.maker=:maker");
			conditional=true;
		}
		
		if(conditional){
			queryStr1=queryStr1.append(" where "+queryStr2.substring(4));
		}		
		
		Query query=session.createQuery(queryStr1.toString());
		
		if(legalEntityCode!=null){	query.setParameter("legalEntityCode", legalEntityCode); }
		if(!BankAuditUtil.isEmptyString(modCode)){query.setParameter("modCode", modCode);}
		if(!BankAuditUtil.isEmptyString(language)){query.setParameter("language", language);}
		if(!BankAuditUtil.isEmptyString(key1)){query.setParameter("key1", key1);}
		if(!BankAuditUtil.isEmptyString(key2)){query.setParameter("key2", key2);}
		if(!BankAuditUtil.isEmptyString(maker)){query.setParameter("maker",maker);}
		
		return query.list();
	}

}
