/*
 * 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.stocktaking;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.stocktaking.view.HistoryUpdateKm;
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 oscarvargas
 */
public class HistoryUpdateKmController extends ConnectionManager implements Serializable {

    /**
     * 
     * @param starDate
     * @param endDate
     * @return 
     */
    public List<HistoryUpdateKm> fillDataTableBetweenDates(Date starDate, Date endDate) {
        logger.info("fillDataTableBetweenDates");

        List<HistoryUpdateKm> results = null;
        Transaction transaction = null;

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

            results = session.createQuery("FROM HistoryUpdateKm WHERE DATE(createdOn) BETWEEN DATE(:starDate) AND DATE(:endDate) ORDER BY createdOn DESC")
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .getResultList();

            transaction.commit();
            logger.info("Total of history update km founds " + results.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Can not load auto complete list of history update km", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillDataTableBetweenDates", e);
        }

        return results;
    }

  
    /**
     * Save HistoryUpdateKm
     *
     * @param historyUpdateKm HistoryUpdateKm to be saved.
     * @return true is saved was succesful otherwise false.
     */
    public boolean save(HistoryUpdateKm historyUpdateKm) {
        logger.info("save");

        boolean success = false;
        Transaction transaction = null;

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

            session.save(historyUpdateKm);
            logger.info("historyUpdateKm saved " + historyUpdateKm.getId());
            transaction.commit();

            success = true;
        } catch (HibernateException e) {
            logger.error("historyUpdateKm can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method save() ", e);
            rollback(transaction);
        }
        return success;
    }

    private static final long serialVersionUID = -793156951909596418L;

    final Logger logger = LogManager.getLogger(HistoryUpdateKmController.class);
    private final float iva = .16f;

    public HistoryUpdateKmController() {
    }
}
