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.ClaveProductoServicio;

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 ClaveProductoServicioController extends ConnectionManager implements Serializable{
    
    public List<ClaveProductoServicio> fillClaveProductoServicioDatatable() {

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

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

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

            Path<String> idPath = root.get("id");
            Path<String> codigoPath = root.get("codigo");
            Path<String> descripciondPath = root.get("descripcion");
         

            criteria.select(
                    builder.construct(ClaveProductoServicio.class, idPath,codigoPath, descripciondPath));
            
          
            criteria.orderBy(builder.asc(root.get("codigo")));
            result = session.createQuery(criteria).getResultList();

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

        return result;
    }
    
    public List<ClaveProductoServicio> getClaveProductoServicioLike(String like) {
        logger.info("getClaveProductoServiciosLike");

        List<ClaveProductoServicio> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching for claveProsuctoServicio..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Path<String> idPath = root.get("id");
            Path<String> codigoPath = root.get("codigo");
            Path<String> descripciondPath = root.get("descripcion");
         

            criteria.select(
                    builder.construct(ClaveProductoServicio.class, idPath,codigoPath, descripciondPath));
            criteria.where(
                    builder.like(root.get("descripcion"), "%" + like + "%")
            );

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

            transaction.commit();
            logger.info("Coincidences " + resultList);
        } catch (HibernateException e) {
            logger.error("Economic numbers cannot be loaded " + like, e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getEconomicNumberLike(" + like + ") ", e);
            rollback(transaction);
        }
        return resultList;
    }
   
    
    public ClaveProductoServicio getById(String claveProductoServicioId) {
        logger.info("getById");

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

            sanction = session.get(ClaveProductoServicio.class, claveProductoServicioId);

            transaction.commit();
            logger.info("User " + sanction);
        } catch (HibernateException e) {
            logger.error("User cannot be loaded " + claveProductoServicioId, e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getById(" + claveProductoServicioId + ") ", e);
            rollback(transaction);
        }
        return sanction;
    }
    
    private static final long serialVersionUID = 1020525241432403643L;
    
    final Logger logger = LogManager.getLogger(ClaveProductoServicioController.class);

    public ClaveProductoServicioController() {
    }
    
}
