/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.crov.prase.controller.catalog;

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.core.User;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
import com.crov.prase.model.catalog.Despacho;
import com.crov.prase.model.catalog.TipoRiesgo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
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 Crov
 */
public class DespachoController extends ConnectionManager implements Serializable {

    private static final long serialVersionUID = -8772793021819350069L;
    final Logger logger = LogManager.getLogger(TipoSiniestroController.class);

    public boolean addDespacho(Despacho tabla, User user, boolean save) {
        logger.info("addDespacho");
        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            if (save) {
                tabla.setId(UUID.randomUUID().toString());
                tabla.setCreatedBy(user);
                tabla.setCreatedOn(new Date());
                session.save(tabla);
                transaction.commit();
                logger.info("Tabla saved " + tabla.getId());
            } else {
                tabla.setLastUpdatedOn(new Date());
                tabla.setLastUpdatedBy(user);
                session.update(tabla);
                transaction.commit();
                logger.info("Tabla updated " + tabla.getId());
            }
            success = true;
        } catch (HibernateException e) {
            logger.error("Tabla can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addDespacho() ", e);
            rollback(transaction);
        }
        return success;
    }

    public List<Despacho> fillDespachoByLocation(Location location) {
        logger.info("fillDespachoByLocation");
        List<Despacho> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching despacho for lote..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<Despacho> criteria = builder.createQuery(Despacho.class);
            Root<Despacho> root = criteria.from(Despacho.class);

            Predicate criterio1 = builder.equal(root.get("location"), location);
            Predicate criterio2 = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            criteria.where(builder.and(criterio1, criterio2));
            criteria.orderBy(builder.desc(root.get("createdOn")));

            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total of despacho found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Despacho cannot be loade ");
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillDespachoByLocation ", e);
            rollback(transaction);
        }
        return resultList;
    }

}
