/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.altamira.ada.module.diary.dao.implement;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.altamira.ada.core.exeption.CommonDaoException;
import org.altamira.ada.core.exeption.HandleExceptions;
import org.altamira.ada.module.diary.dao.DiaryByCriteriaDao;
import org.altamira.ada.module.diary.entities.Diary;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Carlos Soto
 */
@Repository("DiaryByCriteriaDao")
public class DiaryByCriteriaDaoImplement extends HibernateDaoSupport implements DiaryByCriteriaDao {

    public DiaryByCriteriaDaoImplement() {
    }

    /*
     * anotacion de spring para indicar que al construir de hibernateDaosuport
     * se debe inyectaren en el constructor una referencia al SessionFactory
     */
    @Autowired
    public DiaryByCriteriaDaoImplement(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Transactional
    @Override
    public List<Diary> findDiaryByCriteria(long employeId, long status, Date starDate, long idDiary) throws CommonDaoException {
        List<Diary> find = null;
        try {
            find = (List<Diary>) getHibernateTemplate().find("from Diary where employee.id = ? "
                    + "and (? >= (select min(startSchedulingDate) from Diary where employee.id =? and  status.id = ? and id != ?) and ? <= (select max(endSchedulingDate) from Diary where employee.id =? and  status.id = ? and id != ?))", employeId, starDate, employeId, status,idDiary,starDate, employeId, status,idDiary);
        } catch (Exception e) {
            HandleExceptions.getInstance().handleException(e);
        }
        return find;
    }

    @Override
    public List<Diary> findItemsAvailableSelectByRangeDate(Date startDate, Date endDate) throws CommonDaoException {       
        List<Diary> find = null;
        try {
            Criterion start = Restrictions.ge("startSchedulingDate", startDate);
            Criterion end = Restrictions.le("startSchedulingDate", endDate);
            Conjunction conjunction = Restrictions.conjunction();
            conjunction.add(start);
            conjunction.add(end);
            DetachedCriteria criteria = DetachedCriteria.forClass(Diary.class, "diary");
            criteria.add(conjunction);
            find = (List<Diary>) getHibernateTemplate().findByCriteria(criteria);
        } catch (Exception e) {
            HandleExceptions.getInstance().handleException(e);
        }
        return find;
    }

    @Transactional
    @Override
    public List<Diary> findDiaryTopByCriteria(String valueCriteria, int firtsResult, int maxResult) throws CommonDaoException {
        List<Diary> find = null;
        try {
            Criterion documentNumber = Restrictions.like("report", "%" + valueCriteria + "%").ignoreCase();
            Criterion firtsName = Restrictions.like("employee.documentNumber", "%" + valueCriteria + "%").ignoreCase();
            Disjunction disjunction = Restrictions.disjunction();
            disjunction.add(documentNumber);
            disjunction.add(firtsName);
            DetachedCriteria criteria = DetachedCriteria.forClass(Diary.class, "diary");
            criteria.createAlias("diary.employee", "employee");
            criteria.add(disjunction);
            find = (List<Diary>) getHibernateTemplate().findByCriteria(criteria);
        } catch (Exception e) {
            HandleExceptions.getInstance().handleException(e);
        }
        return find;
    }

}
