/*
 * 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.SavingDriver;
import java.io.Serializable;
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 Picasso
 */
public class SavingDriverController extends ConnectionManager implements Serializable {

    /**
     *
     * @param economicNumber
     * @return
     */
    public List<SavingDriver> autoCompleEconomicNumber(String economicNumber) {
        logger.info("autoCompleEconomicNumber");

        List<SavingDriver> savingDrivers = null;
        Transaction transaction = null;

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

            savingDrivers = session.createQuery("FROM SavingDriver WHERE economicNumber LIKE :economicNumber ORDER BY personName")
                    .setParameter("economicNumber", economicNumber)
                    .getResultList();

            transaction.commit();
            logger.info("Total of drivers founs in auto complete economic number are " + savingDrivers.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Can not load auto complete economic number list", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method autoCompleEconomicNumber", e);
        }

        return savingDrivers;
    }

    /**
     *
     * @param driverName
     * @return
     */
    public List<SavingDriver> autoCompleDriverNamer(String driverName) {
        logger.info("autoCompleDriverNamer");

        List<SavingDriver> savingDrivers = null;
        Transaction transaction = null;

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

            savingDrivers = session.createQuery("FROM SavingDriver WHERE personName LIKE :driverName ORDER BY personName")
                    .setParameter("driverName", driverName)
                    .getResultList();

            transaction.commit();
            logger.info("Total of drivers founds in auto complete search " + savingDrivers.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Can not load auto complete list of drivers", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method autoCompleDriverNamer", e);
        }

        return savingDrivers;
    }

    /**
     *
     * @param id
     * @return
     */
    public SavingDriver findSavingDriverById(String id) {
        SavingDriver driver = null;
        Transaction transaction = null;

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

            driver = session.find(SavingDriver.class, id);

            transaction.commit();
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Can not faind saving driver by id", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method findSavingDriverById", e);
        }

        return driver;
    }

    private static final long serialVersionUID = -6208327646228124619L;

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

    public SavingDriverController() {
    }

}
