/*
 * 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.catalog.Location;
import com.arrebol.taxiservicios.model.stocktaking.StocktakingMovements;
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 StocktakingMovementsController extends ConnectionManager implements Serializable {

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

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

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

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

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

        return results;
    }

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

        boolean success = false;
        Transaction transaction = null;

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

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

            success = true;
        } catch (HibernateException e) {
            logger.error("stocktakingMovements 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(StocktakingMovementsController.class);

    public StocktakingMovementsController() {
    }
}
