AdmMono/src/com/rjconsultores/ventaboletos/dao/hibernate/EmpresaHibernateDAO.java

174 lines
5.2 KiB
Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.rjconsultores.ventaboletos.dao.hibernate;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.rjconsultores.ventaboletos.dao.EmpresaDAO;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.InscricaoEstadual;
import com.rjconsultores.ventaboletos.entidad.PuntoVenta;
/**
*
* @author Administrador
*/
@Repository("empresaDAO")
public class EmpresaHibernateDAO extends GenericHibernateDAO<Empresa, Integer>
implements EmpresaDAO {
@Autowired
private DataSource dataSource;
@Autowired
public EmpresaHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
setSessionFactory(factory);
}
@Override
public List<Empresa> obtenerTodos() {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.addOrder(Order.asc("nombempresa"));
return c.list();
}
public List<Empresa> buscar(String nombempresa, Boolean indExterna, Short indTipo) {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.add(Restrictions.eq("nombempresa", nombempresa));
c.add(Restrictions.eq("indExterna", indExterna));
c.add(Restrictions.eq("indTipo", indTipo));
return c.list();
}
public List<Empresa> buscarTodosExceto(List<Empresa> empresa, Integer... idEmpresa) {
List<Empresa> empresaList = new ArrayList<Empresa>();
for (Integer id : idEmpresa) {
for (Empresa e : empresa) {
e.getEmpresaId().equals(id);
}
}
return empresaList;
}
public List<Empresa> obtenerIndExternoFalse() {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.add(Restrictions.eq("indExterna", Boolean.FALSE));
c.addOrder(Order.asc("nombempresa"));
return c.list();
}
public List<Empresa> obtenerIndTipo2() {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.add(Restrictions.eq("indTipo", new Short("2")));
// Nao Buscar Empresa todas
c.add(Restrictions.ne("empresaId", -1));
c.addOrder(Order.asc("nombempresa"));
return c.list();
}
public List<Empresa> buscarNotInPuntoVtaComissao(PuntoVenta puntoVenta) {
StringBuilder sb = new StringBuilder();
sb.append(" select em ");
sb.append(" from Empresa em ");
sb.append(" where em.activo = 1 ");
sb.append(" and em.empresaId not in ( ");
sb.append(" select pc.empresaId.empresaId from PtovtaComissao pc ");
sb.append(" where pc.activo = 1 and pc.puntoventaId.puntoventaId = :puntoventaId ");
sb.append(" )");
sb.append(" order by em.nombempresa");
Query query = getSession().createQuery(sb.toString());
query.setParameter("puntoventaId", puntoVenta.getPuntoventaId());
List<Empresa> lsEmpresa = query.list();
return lsEmpresa;
}
@Override
public List<InscricaoEstadual> buscaInscricoesEstaduais(Empresa empresa) {
Criteria c = getSession().createCriteria(InscricaoEstadual.class);
c.add(Restrictions.eq("empresa", empresa));
c.add(Restrictions.eq("activo", Boolean.TRUE));
return c.list();
}
@Override
public void actualizaInscEstadual(InscricaoEstadual inscricaoEstadual) {
Session session = getSessionFactory().getCurrentSession();
session.saveOrUpdate(inscricaoEstadual);
session.flush();
}
@Override
public List<Empresa> buscaLike(String nombempresa) {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.add(Restrictions.like("nombempresa", nombempresa, MatchMode.START));
c.addOrder(Order.asc("nombempresa"));
return c.list();
}
@Override
public Empresa suscribir(Empresa entity) throws RuntimeException {
super.suscribir(entity);
gerarSeqNumFolioSistema(entity.getEmpresaId());
return entity;
}
private void gerarSeqNumFolioSistema(Integer idEmpresa) throws RuntimeException {
try {
Connection conn = dataSource.getConnection();
if (!conn.createStatement().executeQuery("select SEQUENCE_NAME from DBA_SEQUENCES where SEQUENCE_NAME like 'FOLIO_SISTEMA_" + idEmpresa + "_SEQ%'").next()) {
String[] sequences = {"AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO" };
for (String sequence : sequences) {
conn.createStatement().execute("CREATE SEQUENCE VTABOL.FOLIO_SISTEMA_" + (sequence == "" ? sequence : (sequence + "_") + idEmpresa + "_SEQ INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE"));
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}