com.demo.impl.ConvenioImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.demo.impl.ConvenioImpl.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.ConvenioDao;
import com.demo.model.Convenio;
import com.demo.util.HibernateUtil;
import com.demo.util.LogSistema;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ivan
 */
public class ConvenioImpl implements ConvenioDao {

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

    @Override
    public Convenio convenioActivo(String id, int tipocredito) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction t = s.beginTransaction();
        Convenio c = null;
        try {

            switch (tipocredito) {
            case 1:
                c = (Convenio) s.createQuery(
                        "from Convenio c where c.creditoAuto.creditoAut='" + id + "' and c.estatus='En Curso'")
                        .uniqueResult();
                t.commit();
                break;
            case 2:
                c = (Convenio) s.createQuery("from Convenio c where c.creditoFianzas.numContratoFz='" + id
                        + "' and c.estatus='En Curso'").uniqueResult();
                t.commit();
                break;
            case 3:
                c = (Convenio) s.createQuery("from Convenio c where c.creditoHipotecario.creditoHip='" + id
                        + "' and c.estatus='En Curso'").uniqueResult();
                t.commit();
                break;
            case 4:
                c = (Convenio) s.createQuery(
                        "from Convenio c where c.creditoSfLtNtCt.creditosIi='" + id + "' and c.estatus='En Curso'")
                        .uniqueResult();
                t.commit();
                break;
            }
            return c;
        } catch (HibernateException e) {
            LogSistema.guardarlog(this.getClass().getName()
                    + " Method: convenioActivo, Excepcion HibernateException: " + e.getMessage());
            return null;
        }
    }

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

    @Override
    public List<Convenio> conveniosPagos(int gestor_clv) {
        Session sess = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = sess.beginTransaction();
        List<Convenio> convenios = new ArrayList<>();
        try {
            convenios = sess.createSQLQuery("select c.* from Convenio c, Pago p where \n"
                    + "c.Cat_Gestores_Cat_Gestor_clv=" + gestor_clv + " and \n"
                    + "c.idConvenio=p.Convenio_idConvenio1 and\n"
                    + "p.Cat_Quincenas_Quincena_Clv=(select MAX(cq.Quincena_Clv) FROM Cat_Quincenas cq) and\n"
                    + "p.Rev_Aprovado='Aprobado' GROUP by c.idConvenio;").addEntity(Convenio.class).list();
            tx.commit();
            if (sess.isOpen()) {
                sess.close();
            }

            if (convenios == null) {
                convenios = new ArrayList<>();
            }
            return convenios;
        } catch (HibernateException he) {
            LogSistema.guardarlog(this.getClass().getName()
                    + " Method: conveniosPagos, Excepcion HibernateException: " + he.getMessage());
            return convenios;
        }
    }

    @Override
    public float totalPagosConv(Convenio conv) {
        Session sess = null;
        String consulta = "select sum(p.Aprobado_Corp) from convenio c join pago p where c.idConvenio = p.Convenio_idConvenio1 and c.idConvenio = "
                + conv.getIdConvenio() + " \n" + "and p.Aprobado = 'Aprobado';";

        float total = 0;
        try {
            sess = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = sess.beginTransaction();
            System.out.println("CONVENIO ID" + conv.getIdConvenio());
            System.out.println("CONSULTA PAGOS = " + consulta);
            total = Float.parseFloat(sess.createSQLQuery(consulta).uniqueResult().toString());

            System.out.println("TOTAL = " + total);
            tx.commit();
            if (sess.isOpen()) {
                sess.close();
            }
            return total;
        } catch (HibernateException he) {
            LogSistema.guardarlog(this.getClass().getName()
                    + " Method: totalPagosConv, Excepcion HibernateException: " + he.getMessage());
            return total;
        } catch (NullPointerException n) {
            LogSistema.guardarlog(this.getClass().getName()
                    + " Method: totalPagosConv, Excepcion NullPointerException: " + n.getMessage());
            total = 0;
            return total;
        }

    }

}