package com.arrebol.taxiservicios.controller.catalog;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.catalog.RegimenFiscal;

import java.io.Serializable;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

import javax.persistence.criteria.Path;
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 Oscar
 */
public class RegimenFiscalController extends ConnectionManager implements Serializable{
    
    public List<RegimenFiscal> fillRegimenFiscalDatatable() {

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

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

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

            Path<String> idPath = root.get("id");
            Path<String> regimenFiscalPath = root.get("regimenFiscal");
            Path<String> nombrePath = root.get("nombre");
         

            criteria.select(
                    builder.construct(RegimenFiscal.class, idPath,regimenFiscalPath, nombrePath));
            
          
            criteria.orderBy(builder.asc(root.get("regimenFiscal")));
            result = session.createQuery(criteria).getResultList();

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

        return result;
    }
    
   
    
    private static final long serialVersionUID = 1020525241432403643L;
    
    final Logger logger = LogManager.getLogger(RegimenFiscalController.class);

    public RegimenFiscalController() {
    }
    
}
