package com.bankaudit.template.rest.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.jboss.logging.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dao.AbstractDao;
import com.bankaudit.process.model.AuditDocumentDetails;
import com.bankaudit.process.model.AuditDocumentDetailsWrk;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.spring.config.BankAuditConfig;
import com.bankaudit.template.model.MaintTemplateDtls;
import com.bankaudit.util.BankAuditUtil;

@Repository("uploadDownloadDao")
public class UploadDownloadDaoImpl extends AbstractDao implements UploadDownloadDao {
	
	static final Logger logger = Logger.getLogger(UploadDownloadDaoImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaintTemplateDtls> getTemplateDetails(Integer legalEntityCode, String role){
		Session session =getSession();
		session.setDefaultReadOnly(true);
		logger.info("Inside getTemplateDetails .... "+ legalEntityCode+" :: "+ role);
		return session.createQuery(" FROM MaintTemplateDtls t where t.legalEntityCode=:legalEntityCode ").setParameter("legalEntityCode", legalEntityCode).list();
	}	
	
	@Override
	public String getTemplateReferencePath(Integer legalEntityCode, String templateCode){
		Session session = getSession();
		session.setDefaultReadOnly(true);
		String tmpRefPath=null;
		try {
			String queryString="  SELECT t FROM MaintTemplateDtls t "
					+ " WHERE t.legalEntityCode =:legalEntityCode AND t.templateCode=:templateCode ";
			
			MaintTemplateDtls tmpDtls= (MaintTemplateDtls)session.createQuery(queryString.toString())
			 .setParameter("legalEntityCode", legalEntityCode).setParameter("templateCode", templateCode).uniqueResult();
			String tmpRefFileName= tmpDtls.getTemplateFileName();
			tmpRefPath = BankAuditConfig.getInstance().getConfigValue(BankAuditConstant.DIR_TEMPLATE_UPLOAD)+ "/" +tmpRefFileName;
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}		
		return tmpRefPath;
	}

}
