package com.rms.tfc.dao;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rms.tfc.entity.Linea;
import com.rms.tfc.util.LogUtil;

@Repository
@Transactional
public class LineaDaoImpl implements LineaDao {

	@Autowired
    private SessionFactory sessionFactory;

    private final String className = "com.rms.tfc.dao.LineaDaoImpl";
    
    public Long getNextId(){
    	int newId = 0;
    	Session session = null;
		try{
			String queryStr = "SELECT IFNULL(MAX(idLinea), 0) + 1 FROM linea";
			session = sessionFactory.openSession();
			newId = ((BigInteger) session.createSQLQuery(queryStr).uniqueResult()).intValue();
		} catch (Exception e){
			LogUtil.printLog(className, "Error obteniendo el siguiente Id");
			e.printStackTrace();
		} finally {
			if (session != null){
				session.close();
			}
		}
		return new Long(newId);
    }
    
    public synchronized Long createLinea(Linea linea){
    	Session session = null;
    	try{
    		session = sessionFactory.openSession();
			Transaction tx = session.getTransaction();
			if (!tx.isActive()){
               		tx.begin();
           		}
			session.save(linea);
			tx.commit();
		} catch (Exception e){
			LogUtil.printLog(className, "Error al hacer el save de la linea");
			e.printStackTrace();
			return null;
		} finally {
			if (session != null){
				session.close();
			}
		}
		return linea.getIdLinea();
	}
	
    public synchronized Linea updateLinea(Linea linea){
    	Session session = null;
    	try{
    		session = sessionFactory.openSession();
			Transaction tx = session.getTransaction();
			if (!tx.isActive()){
               		tx.begin();
           		}
			session.update(linea);
			tx.commit();
		} catch (Exception e){
			LogUtil.printLog(className, "Error al hacer el update de la linea");
			e.printStackTrace();
		} finally {
			if (session != null){
				session.close();
			}
		}
		return linea;
	}
	
	public Linea getLineaById(Long idLinea){
		List<Linea> lista = null;
		Session session = null;
		try{
            String queryStr = "SELECT * FROM linea WHERE idLinea = " + idLinea;
            session = sessionFactory.openSession();
            lista = (List<Linea>) session.createSQLQuery(queryStr).addEntity(Linea.class).list();
        }catch (Exception e){
            LogUtil.printLog(className, "Error realizando la busqueda de la linea");
            e.printStackTrace();
        } finally {
			if (session != null){
				session.close();
			}
		}
        if (lista != null && lista.size() > 0){
            return (Linea) lista.get(0);
        } else {
            return null;
        }
	}
	
	public List<Linea> getAllLineasByPedido(Long idPedido){
		List<Linea> lista = null;
		Session session = null;
		try{
            String queryStr = "SELECT * FROM linea WHERE idPedido = " + idPedido + " ORDER BY idLinea";
            session = sessionFactory.openSession();
            lista = (List<Linea>) session.createSQLQuery(queryStr).addEntity(Linea.class).list();
        }catch (Exception e){
            LogUtil.printLog(className, "Error realizando la busqueda de lineas del pedido: " + idPedido);
            e.printStackTrace();
        } finally {
			if (session != null){
				session.close();
			}
		}
        return lista;
	}
}