/*
 * 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.predio;

import com.arrebol.taxiservicios.controller.admin.GenericPersonController;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.predio.Predio;
import com.arrebol.taxiservicios.model.predio.PropietarioPredio;
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 Usuario
 */
public class PropietarioPredioController extends GenericPersonController implements Serializable{
    
    private static final long serialVersionUID = -6265444716269535946L;

    final Logger logger = LogManager.getLogger(PropietarioPredioController.class);
    
    public boolean addPropietarioPredio(PropietarioPredio propietarioPredio, User user) {
        logger.info("addPredio");

        boolean success = false;

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

                propietarioPredio.setId(UUID.randomUUID().toString());
                propietarioPredio.setCreatedBy(user);
                propietarioPredio.setCreatedOn(new Date());
                session.save(propietarioPredio);
                transaction.commit();

                logger.info("PropietarioPredio saved " + propietarioPredio.getId());


            success = true;
        } catch (HibernateException e) {
            logger.error("PropietarioPredio can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addPropietarioPredio() ", e);
            rollback(transaction);
        }
        return success;
    }
    
    public List<PropietarioPredio> fillPropietarioPredio() {

        Transaction transaction = null;
        List<PropietarioPredio> result = null;

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

            CriteriaQuery<PropietarioPredio> criteria = builder.createQuery(PropietarioPredio.class);
            Root<PropietarioPredio> root = criteria.from(PropietarioPredio.class);
            criteria.orderBy(builder.asc(root.get("createdOn")));
            
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of PropietarioPredio found : " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load list of PropietarioPredio ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillPropietarioPredio", e);
            rollback(transaction);
        }

        return result;
    }
    
    public List<PropietarioPredio> fillPropietarioPredioByPredio(Predio predio) {

        Transaction transaction = null;
        List<PropietarioPredio> result = null;

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

            CriteriaQuery<PropietarioPredio> criteria = builder.createQuery(PropietarioPredio.class);
            Root<PropietarioPredio> root = criteria.from(PropietarioPredio.class);
            
            Predicate criterio = builder.equal(root.get("predio"), predio);
            
            criteria.where(criterio);
            
            criteria.orderBy(builder.asc(root.get("createdOn")));
            

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

            logger.info("Total of PropietarioPredio found : " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load list of PropietarioPredio ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillPropietarioPredio", e);
            rollback(transaction);
        }

        return result;
    }
    
}
