package dao;


import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanConstructorResultTransformer;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.DateType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;

import bean.LogModificheBean;
import bean.LogServizioBean;
import common.dao.GenericDAO;
import common.dao.IDAO;
import javassist.bytecode.analysis.Type;
import sicura.entities.LogSicuraScheduler;

@Repository
public class LogSicuraSchedulerDAO extends GenericDAO<LogSicuraScheduler, Integer>
		implements IDAO<LogSicuraScheduler, Integer> {
	
	
	public Integer count(String inputFile) {
		boolean tx = startTransaction();
		try {
			String sql = generateSQL(true, inputFile, null);
			Query q = currentSession().createSQLQuery(sql);
					
			Integer res = ((Integer) q.uniqueResult());
			
			return res;
		} finally {
			if (tx)
				tx = commit();
		}
	}

	public List<LogSicuraScheduler> list(int startRecord, int length, String inputFile, String orderBy)  {
		boolean tx = startTransaction();

		try {
			String sql = generateSQL(false, inputFile, orderBy);
			SQLQuery q = currentSession().createSQLQuery(sql).addEntity(LogSicuraScheduler.class);
			//q.setResultTransformer(new AliasToBeanResultTransformer(LogServizioBean.class));
			//q.setResultTransformer(Transformers.aliasToBean(LogServizioBean.class));
			q.setFirstResult(startRecord).setMaxResults(length);
			

//			q.addScalar("id",IntegerType.INSTANCE);
//			q.addScalar("dataAcquisizione",DateType.INSTANCE);
//			q.addScalar("tipoAcquisizione",StringType.INSTANCE);
//			q.addScalar("esito",StringType.INSTANCE);
//			q.addScalar("descrizioneEsito",StringType.INSTANCE);
//			q.addScalar("inputFile",StringType.INSTANCE);
//			q.addScalar("backupFile",StringType.INSTANCE);
//			q.addScalar("logServizio",StringType.INSTANCE);
//			
			@SuppressWarnings("unchecked")
			List<LogSicuraScheduler> res = null;
			try{
				res = q.list();
			} catch(Exception ex) {
				ex.printStackTrace();
			}
			
			return res;
		} finally {
			if (tx)
				tx = commit();
		}
	}

	public String generateSQL(boolean count, String inputFile, String orderBy) {
		String sql = "Select " + 
					(count
							? "count(*)"
							:"*") //id,dataAcquisizione,tipoAcquisizione,esito,descrizioneEsito,inputFile,backupFile,Cast(logServizio as varchar) logServizio")
			+ " from LogSicuraScheduler ";
		if (inputFile!=null && !"".equals(inputFile))
			sql += " where inputFile like '%" + inputFile +"%'";
		if (orderBy!=null && !"".equals(orderBy))
			sql += getSQLOrderBy(orderBy);
		return sql;
	}
	
}
