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.catalog.Location;
import com.arrebol.taxiservicios.model.view.admin.TicketView;
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.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;

/**
 *
 * @author Dax
 */
public class TicketViewController extends ConnectionManager implements Serializable{
    
    private static final long serialVersionUID = 3680049238403721378L;
    
    final Logger logger = LogManager.getLogger(TicketViewController.class);
    
    public TicketViewController(){
    }
    
     public List<TicketView> findAllTicket(String idLocation) {
        logger.info("findAllDailySalesByDate");

        List<TicketView> ticket = null;
        Transaction transaction = null;

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

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

            Root<TicketView> root = query.from(TicketView.class);
            query.select(root);
            query.where(builder.and(builder.equal(root.get("location"), new Location(idLocation))));
            ticket = session.createQuery(query).getResultList();
            
            transaction.commit();
            logger.info("Total of tickets sales records found " + ticket.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Cannot load ticket list", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method findAllTicket", e);
        }

        return ticket;
    }
    
}
