/*
 * 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 com.arrebol.taxiservicios.controller.view.report;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.view.report.DailySale;
import java.io.Serializable;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
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 Picasso
 */
public class DailySaleController extends ConnectionManager implements Serializable {

    //**
    
    public List<DailySale> findAllDailySalesByDate() {
        logger.info("findAllDailySalesByDate");

        List<DailySale> dailySales = null;
        Transaction transaction = null;

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

            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<DailySale> query = builder.createQuery(DailySale.class);

            Root<DailySale> root = query.from(DailySale.class);

            query.select(root);

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

            transaction.commit();
            logger.info("Total of daily sales records found " + dailySales.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Can not load list of of daily sales", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method findAllDailySalesByDate", e);
        }

        return dailySales;
    }

    private static final long serialVersionUID = 3764852012618598578L;

    final Logger logger = LogManager.getLogger(DailySaleController.class);

    public DailySaleController() {
    }

}
