/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.controller;

import com.arrebol.apc.controller.util.ConnectionManager;
import com.arrebol.apc.controller.util.HibernateUtil;
import com.arrebol.apc.model.admin.ClosingDay;
import com.arrebol.apc.model.core.User;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class GenericValidationController extends ConnectionManager implements Serializable{
    
    /**
     * 
     * @param date
     * @param user
     * @return 
     */
    public boolean existClosingDayByCreatedOn(Date date, User user) {
        logger.info("existClosingDayByCreatedOn");

        boolean success = true;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT COUNT(ssb.id) "
                    + "FROM ClosingDay ssb "
                    + "WHERE DATE(ssb.createdOn) = DATE(:date) "
                    + "AND ssb.activeStatus = 'ENEBLED' "
                    + "AND ssb.user = :user";

            Long count = (Long) session.createQuery(query)
                    .setParameter("date", date)
                    .setParameter("user", user).uniqueResult();

            if (null != count && count == 0) {
                success = false;
            }

            transaction.commit();

            logger.info("Total of closing day found: " + count);
        } catch (HibernateException e) {
            logger.error("Can not find stable small box by created on", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method existStableSmallBoxByCreatedOn()", e);
            rollback(transaction);
        }

        return success;
    }

    public boolean existStableGeneralBoxByCurdate(User user) {
        logger.info("existStableGeneralBoxByCurdate");

        boolean success = true;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT COUNT(ssb.id) "
                    + "FROM StableGeneralBox ssb "
                    + "WHERE DATE(ssb.createdOn) = CURDATE() "
                    + "AND ssb.activeStatus = 'ENEBLED'";

            Long count = (Long) session.createQuery(query).uniqueResult();

            if (null != count && count == 0) {
                success = false;
            }

            transaction.commit();

            logger.info("Total of StableGeneralBox found: " + count);
        } catch (HibernateException e) {
            logger.error("Can not find StableGeneralBox by created on", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method existStableGeneralBoxByCurdate()", e);
            rollback(transaction);
        }

        return success;
    }
    
    public Date lastStableGeneralBoxByDate(User user) {
        logger.info("lastStableGeneralBoxByDate");

        Date success = null;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT cd.createdOn FROM StableGeneralBox cd "
                    + "WHERE DATE(cd.createdOn) <= CURDATE() AND cd.activeStatus = 'ENEBLED' "
                    + "order by cd.createdOn DESC";

            success = (Date) session.createQuery(query).setMaxResults(1)
                    .uniqueResult();

            transaction.commit();

            logger.info("Total of closing day found: ");
        } catch (HibernateException e) {
            logger.error("Can not find stable small box by created on", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method lastStableGeneralBoxByDate()", e);
            rollback(transaction);
        }

        return success;
    }
    
      public Date lastStableSmallBoxByDate(User user) {
        logger.info("lastStableSmallBoxByDate");

        Date success = null;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT cd.createdOn FROM StableSmallBox cd "
                    + "WHERE DATE(cd.createdOn) <= CURDATE() AND cd.activeStatus = 'ENEBLED' "
                    + "order by cd.createdOn DESC";

            success = (Date) session.createQuery(query).setMaxResults(1)
                    .uniqueResult();

            transaction.commit();

            logger.info("Total of closing day found: ");
        } catch (HibernateException e) {
            logger.error("Can not find stable small box by created on", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method lastStableSmallBoxByDate()", e);
            rollback(transaction);
        }

        return success;
    }
   
    public List<ClosingDay> allClosingDayByDate( ) {
        logger.info("allClosingDayByDate");

        List<ClosingDay> success = null;
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String query = "SELECT cd FROM ClosingDay cd "
                    + "WHERE DATE(cd.createdOn) = CURDATE() AND cd.activeStatus = 'ENEBLED'";

            success = session.createQuery(query).getResultList();

            transaction.commit();

            logger.info("Total of closing day found: ");
        } catch (HibernateException e) {
            logger.error("Can not find stable small box by created on", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method lastStableSmallBoxByDate()", e);
            rollback(transaction);
        }

        return success;
    }
    
    final Logger logger = LogManager.getLogger(GenericValidationController.class);
    private static final long serialVersionUID = -6732331411572526429L;
    
}
