/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.controller.admin;

import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.admin.Bitacora;
import com.arrebol.taxiservicios.model.catalog.Location;
import javax.enterprise.context.RequestScoped;
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 Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@RequestScoped
public class BitacoraServiceImpl implements BitacoraService {

    @Override
    public boolean saveNewRecordInBitacora(Bitacora bitacora, String idLocation) {
        boolean success = false;
        Transaction transaction = null;

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

            bitacora.setLocation(new Location(idLocation));
            session.save(bitacora);
            transaction.commit();

            success = true;

            logger.info("New record was inserted in bitacora");
        } catch (HibernateException e) {
            logger.error("Can not insert a bitacora event", e);
            if (null != transaction) {
                transaction.rollback();
            }
        } catch (Exception e) {
            logger.error("Method saveNewRecordInBitacora()", e);
            if (null != transaction) {
                transaction.rollback();
            }
        }
        return success;
    }
    final Logger logger = LogManager.getLogger(getClass());
}
