/*
 * 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.arrebol.taxiservicios.controller.spa;

import com.arrebol.taxiservicios.controller.admin.GenericPersonController;
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.arrebol.taxiservicios.model.enums.TipoProductoSpa;
import com.arrebol.taxiservicios.model.spa.CatalogoProductos;
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 CatalogoProductosController extends GenericPersonController implements Serializable {

    private static final long serialVersionUID = -6265444716269535946L;
    final Logger logger = LogManager.getLogger(getClass());

    public boolean addCatalogoProductos(CatalogoProductos producto, User user, boolean save) {
        logger.info("addCatalogoProductos");
        boolean success = false;
        Transaction transaction = null;

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

            if (save) {
                producto.setId(UUID.randomUUID().toString());
                producto.setCreatedBy(user);
                producto.setCreatedOn(new Date());
                session.save(producto);
                transaction.commit();
                logger.info("Producto saved " + producto.getId());
            } else {
                producto.setLastUpdatedOn(new Date());
                producto.setLastUpdatedBy(user);
                session.update(producto);
                transaction.commit();
                logger.info("Producto updated " + producto.getId());
            }

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

    public List<CatalogoProductos> fillCatalogoProductosByLocation(Location location) {
        logger.info("fillCatalogoProductosByLocation");
        List<CatalogoProductos> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for catalogo productos..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Predicate criterio = (builder.equal(root.get("location"), location));
            Predicate criterio2 = (builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            criteria.where(builder.and(criterio, criterio2));
            criteria.orderBy(builder.asc(root.get("createdOn")));
            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total off catalogo productos found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("catalogo productos cannot be loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCatalogoProductosByLocation() " + e);
            rollback(transaction);
        }
        return resultList;
    }
    
    public List<CatalogoProductos> fillCatalogoProductosByLocationAndTipoProductoServicio(Location location) {
        logger.info("fillCatalogoProductosByLocation");
        List<CatalogoProductos> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for catalogo productos..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Predicate criterio = (builder.equal(root.get("location"), location));
            Predicate criterio2 = (builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            Predicate criterio3 = (builder.equal(root.get("tipo"), TipoProductoSpa.PRODUCTO));
            Predicate criterio4 = (builder.equal(root.get("tipo"), TipoProductoSpa.SERVICIO));
            criteria.where(builder.and(criterio, criterio2), builder.or(criterio3, criterio4));
            criteria.orderBy(builder.asc(root.get("createdOn")));
            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total off catalogo productos found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("catalogo productos cannot be loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCatalogoProductosByLocation() " + e);
            rollback(transaction);
        }
        return resultList;
    }
    
    
    public List<CatalogoProductos> fillCatalogoProductosByLocationAndTipoPaquete(Location location) {
        logger.info("fillCatalogoProductosByLocation");
        List<CatalogoProductos> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for catalogo productos..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Predicate criterio = (builder.equal(root.get("location"), location));
            Predicate criterio2 = (builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            Predicate criterio3 = (builder.equal(root.get("tipo"), TipoProductoSpa.PAQUETE));            
            criteria.where(builder.and(criterio, criterio2, criterio3));
            criteria.orderBy(builder.asc(root.get("createdOn")));
            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total off catalogo productos found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("catalogo productos cannot be loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCatalogoProductosByLocation() " + e);
            rollback(transaction);
        }
        return resultList;
    }

    public List<CatalogoProductos> fillCatalogoProductosByLocationAndTipoProducto(Location location, TipoProductoSpa tipo) {
        logger.info("fillCatalogoProductosByLocationAndTipoProducto");
        List<CatalogoProductos> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for catalogo productos..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Predicate criterio = (builder.equal(root.get("location"), location));
            Predicate criterio2 = (builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            Predicate criterio3 = (builder.equal(root.get("tipo"), tipo));
            criteria.where(builder.and(criterio, criterio2, criterio3));
            criteria.orderBy(builder.asc(root.get("createdOn")));
            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total off catalogo productos found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("catalogo productos cannot be loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCatalogoProductosByLocationAndTipoProducto() " + e);
            rollback(transaction);
        }
        return resultList;
    }

    public CatalogoProductos fillCatalogoProductosById(String id) {
        logger.info("fillCatalogoProductosById");
        CatalogoProductos resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching  Producto..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Predicate criterio = (builder.equal(root.get("id"), id));
            criteria.where(builder.and(criterio));
            resultList = session.createQuery(criteria).getSingleResult();

            logger.info("Productos found: " + resultList.getId());
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Producto can not loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCatalogoProductosById() " + e);
            rollback(transaction);
        }
        return resultList;
    }

    public List<CatalogoProductos> fillCatalogoProductosByLocationAndtipo(Location location, TipoProductoSpa tipo) {
        logger.info("fillCatalogoProductosByLocationAndtipo");
        List<CatalogoProductos> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for catalogo productos..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Predicate criterio = (builder.equal(root.get("location"), location));
            Predicate criterio2 = (builder.equal(root.get("tipo"), tipo));
            Predicate criterio3 = (builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            criteria.where(builder.and(criterio, criterio2, criterio3));
            criteria.orderBy(builder.asc(root.get("createdOn")));
            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total off catalogo productos found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("catalogo productos cannot be loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCatalogoProductosByLocationAndtipo() " + e);
            rollback(transaction);
        }
        return resultList;
    }

}
