/*
 * 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.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 TipoRiesgoController extends ConnectionManager implements Serializable {

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

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

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

            CriteriaQuery<TipoRiesgo> criteria = builder.createQuery(TipoRiesgo.class);
            Root<TipoRiesgo> root = criteria.from(TipoRiesgo.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("descripcion")));

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

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

}
