package com.arrebol.taxiservicios.controller.catalog;

import com.arrebol.taxiservicios.controller.admin.GenericPersonController;
import com.arrebol.taxiservicios.controller.predio.PredioController;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.catalog.EtapasJuridico;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
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 Dax
 */
public class EtapasJuridicoController extends GenericPersonController implements Serializable{
    
    private static final long serialVersionUID = -6265444716269535946L;
    final Logger logger = LogManager.getLogger(PredioController.class);
    
    public List<EtapasJuridico> fillAllEtapasJuridicas(Location location) {
        List<EtapasJuridico> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for etapas juridicas..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<EtapasJuridico> criteria = builder.createQuery(EtapasJuridico.class);
            Root<EtapasJuridico> root = criteria.from(EtapasJuridico.class);
            Predicate criterioLocation = builder.equal(root.get("location"), location);

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

            logger.info("Total of etapas juridicas found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Etapas juridicas cannot be loaded " + e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillAllEtapasJuridicas() " + e);
            rollback(transaction);
        }
        return resultList;
    }
    
    public EtapasJuridico fillEtapaJuridicoById(String id) {
        Transaction transaction = null;
        EtapasJuridico result = null;

        try {
            logger.info("Searching lote..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<EtapasJuridico> criteria = builder.createQuery(EtapasJuridico.class);
            Root<EtapasJuridico> root = criteria.from(EtapasJuridico.class);
            
            Predicate criterio = builder.equal(root.get("id"), id);
            criteria.where(builder.and(criterio));

            result = session.createQuery(criteria).getSingleResult();

            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load EtapasJuridico", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillEtapasJuridico ", e);
            rollback(transaction);
        }

        return result;
    }
    
     public boolean addEtapaJuridica(EtapasJuridico etapa, User user, boolean save) {
        logger.info("addEtapaJuridica");
        boolean success = false;
        Transaction transaction = null;

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

            if (save) {
                etapa.setCreatedBy(user);
                etapa.setCreatedOn(new Date());
                session.save(etapa);
                transaction.commit();
                logger.info("Etapa juridica saved " + etapa.getId());
            } else {
                etapa.setLastUpdatedBy(user);
                etapa.setLastUpdatedOn(new Date());
                session.update(etapa);
                transaction.commit();
                logger.info("Etapa juridica updated " + etapa.getId());
            }

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