/*
 * 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.spa.Cabinas;
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 CabinasController extends GenericPersonController implements Serializable {

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

    public boolean addCabina(Cabinas cabina, User user, boolean save) {
        logger.info("addCabina");
        boolean success = false;
        Transaction transaction = null;

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

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

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

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

            CriteriaQuery<Cabinas> criteria = builder.createQuery(Cabinas.class);
            Root<Cabinas> root = criteria.from(Cabinas.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 cabina productos found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Cabinas cannot be loaded" + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCabinasByLocation(): " + e);
            rollback(transaction);
        }
        return resultList;
    }

}
