com.sip.pe.dao.ClienteDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.sip.pe.dao.ClienteDAO.java

Source

/*
 * 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.
 */
package com.sip.pe.dao;

import com.sip.pe.entidad.SipCliente;
import com.sip.pe.util.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javassist.bytecode.analysis.Type;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import static org.hibernate.type.StandardBasicTypes.FLOAT;
import static org.hibernate.type.StandardBasicTypes.INTEGER;

/**
 *
 * @author Dagnino
 */
public class ClienteDAO implements Serializable {
    Session session = null;

    public ClienteDAO() {
    } //{ openSession(); }

    //private void openSession(){ session = HibernateUtil.getSessionFactory().openSession(); }

    public List AutenticarUsuario(SipCliente cliente) {
        boolean autenticacion = false;

        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();
        List<SipCliente> listaUsuarios = new ArrayList();
        try {

            Query q = ses.createQuery("from SipCliente where Usuario=:u AND Password=:p");
            q.setParameter("u", cliente.getUsuario());
            q.setParameter("p", cliente.getPassword());
            listaUsuarios = q.list();

            autenticacion = listaUsuarios.size() > 0;
        } catch (HibernateException e) {
            System.out.println("Error en la aplicacion: " + e.toString());
            System.out.println("causa: " + e.getCause());
            e.printStackTrace();
        } finally {
            ses.close();
        }
        return listaUsuarios;
    }

    public Integer GuardarUsuario(SipCliente cliente) {
        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();
        Transaction tx = null;
        Integer clienteID = null;

        try {
            tx = ses.beginTransaction();

            //primero grabar la reserva            
            clienteID = (Integer) ses.save(cliente);

            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            ses.close();
        }

        return clienteID;
    }

    public List<Object> ObtenerHistorialCliente(int idCliente) {

        List<Object> listaHistorial = null;

        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();

        try {

            SQLQuery q = ses.createSQLQuery("exec sp_ObtenerHistorialCliente :IdCliente");

            q.addScalar("ID_TRANSACCION", INTEGER);
            q.addScalar("FE_TRANSACCION");
            q.addScalar("TIPO_CUENTA");
            q.addScalar("SERVICIO");
            q.addScalar("ENTIDAD");
            q.addScalar("BANCO");
            q.addScalar("NU_CUENTA");
            q.addScalar("IMPORTE_PAGO", FLOAT);

            q.setInteger("IdCliente", idCliente);
            listaHistorial = q.list();

        } catch (HibernateException e) {
            System.out.println("Error en la aplicacion: " + e.toString());
            System.out.println("causa: " + e.getClass());
            e.printStackTrace();
        } finally {
            ses.close();
        }
        return listaHistorial;
    }
}