com.demo.impl.PromesadePagoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.demo.impl.PromesadePagoImpl.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.demo.impl;

import com.demo.dao.PromesadePagoDao;
import com.demo.model.PromesaPago;
import com.demo.model.TablaCuentasGestion;
import com.demo.util.HibernateUtil;
import com.demo.util.LogSistema;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ivan
 */
public class PromesadePagoImpl implements PromesadePagoDao {

    @Override
    public boolean altaPromesa(PromesaPago pago) {
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = sess.beginTransaction();
        try {
            sess.save(pago);
            tx.commit();
            if (sess.isOpen()) {
                sess.close();
            }
            return true;
        } catch (HibernateException he) {
            tx.rollback();
            LogSistema
                    .guardarlog(this.getClass().getName() + " Method: altaPromesa, Exception: " + he.getMessage());
            return false;
        }
    }

    @Override
    public boolean actuPromesa(PromesaPago pago) {
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = sess.beginTransaction();
        try {
            sess.update(pago);
            tx.commit();
            if (sess.isOpen()) {
                sess.close();
            }
            return true;
        } catch (HibernateException he) {
            tx.rollback();
            LogSistema
                    .guardarlog(this.getClass().getName() + " Method: actuPromesa, Exception: " + he.getMessage());
            return false;
        }
    }

    @Override
    public PromesaPago obtenerUltimoPago(TablaCuentasGestion credito) {

        PromesaPago prom = new PromesaPago();
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = sess.beginTransaction();

        try {

            switch (credito.getTipoCredito()) {
            case 1:
                prom = (PromesaPago) sess.createSQLQuery(
                        "select a.* from Promesa_Pago a join Convenio b where a.Convenio_idConvenio = b.idConvenio and b.Credito_Auto_CreditoAut = '"
                                + credito.getFolio() + "' order by Promesa_Fecha limit 1;")
                        .addEntity(PromesaPago.class).uniqueResult();
                break;
            case 2:
                prom = (PromesaPago) sess.createSQLQuery(
                        "select a.* from Promesa_Pago a join Convenio b where a.Convenio_idConvenio = b.idConvenio and b.Credito_Fianzas_NumContratoFZ = '"
                                + credito.getFolio() + "' order by Promesa_Fecha limit 1;")
                        .addEntity(PromesaPago.class).uniqueResult();
                break;
            case 3:
                prom = (PromesaPago) sess.createSQLQuery(
                        "select a.* from Promesa_Pago a join Convenio b where a.Convenio_idConvenio = b.idConvenio and b.Credito_Hipotecario_CreditoHip = '"
                                + credito.getFolio() + "' order by Promesa_Fecha limit 1;")
                        .addEntity(PromesaPago.class).uniqueResult();
                break;
            case 4:
                String consulta = "select a.* from Promesa_Pago a join Convenio b where a.Convenio_idConvenio = b.idConvenio and b.Credito_SF_LT_NT_CT_CreditosII = '"
                        + credito.getFolio() + "' and a.Promesa_Status='Pendiente' order by Promesa_Fecha limit 1;";
                prom = (PromesaPago) sess.createSQLQuery(consulta).addEntity(PromesaPago.class).uniqueResult();
                System.out.println(consulta);
                //LogSistema.guardarlog(consulta);
                break;
            }

            tx.commit();
            return prom;

        } catch (HibernateException he) {
            tx.rollback();
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method: obtenerUltimoPago, Exception: " + he.getMessage());
            return prom;
        }

    }

    @Override
    public boolean eliminarPromesa(PromesaPago pago) {
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = sess.beginTransaction();
        try {
            sess.delete(pago);
            tx.commit();
            if (sess.isOpen()) {
                sess.close();
            }
            return true;
        } catch (HibernateException he) {
            tx.rollback();
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method: eliminarPromesa, Exception: " + he.getMessage());
            return false;
        }
    }

}