package com.rjconsultores.ventaboletos.dao.hibernate; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.criterion.MatchMode; 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.ConexionRutaConfDAO; import com.rjconsultores.ventaboletos.entidad.ConexionRutaConf; @Repository("conexionRutaConfDAO") public class ConexionRutaConfHibernateDAO extends GenericHibernateDAO implements ConexionRutaConfDAO { @Autowired public ConexionRutaConfHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { setSessionFactory(factory); } @Override public List buscarPorDescricao(String descricao) { Criteria c = getSession().createCriteria(getPersistentClass()); c.add(Restrictions.eq("activo", Boolean.TRUE)); c.add(Restrictions.ilike("descricao", descricao, MatchMode.ANYWHERE)); return c.list(); } @Override public List obtenerTodosActivo() { Criteria c = getSession().createCriteria(getPersistentClass()); c.add(Restrictions.eq("activo", Boolean.TRUE)); return c.list(); } @Override public void excluirConfiguracao(ConexionRutaConf conexion, Integer usuarioId, boolean excluirConexionRutaConf) { List comandos = carregarComandosInativarConfiguracao(excluirConexionRutaConf); for (String comando : comandos) { Query qr = getSession().createSQLQuery(comando); qr.setParameter("usuarioId", usuarioId); qr.setParameter("conexionrutaconfId", conexion.getConexionRutaConfId()); qr.setParameter("fecmodif", Calendar.getInstance().getTime()); qr.executeUpdate(); } } private List carregarComandosInativarConfiguracao(boolean excluirConexionRutaConf) { List comandos = new ArrayList(0); StringBuilder sb = new StringBuilder(); if(excluirConexionRutaConf) { sb.append("update conexion_ruta_conf ") .append("set activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif ") .append("where conexionrutaconf_id = :conexionrutaconfId"); comandos.add(sb.toString()); } sb = new StringBuilder(); sb.append("update conexion_ruta_ctrl ") .append("set activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif ") .append("where conexionrutaconf_id = :conexionrutaconfId"); comandos.add(sb.toString()); sb = new StringBuilder(); sb.append("update conexion_ruta_excepcion_ptovta ") .append("set activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif ") .append("where conexionrutaconf_id = :conexionrutaconfId"); comandos.add(sb.toString()); sb = new StringBuilder(); sb.append("update conexion_rutaexcepciontipopta ") .append("set activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif ") .append("where conexionrutaconf_id = :conexionrutaconfId"); comandos.add(sb.toString()); sb = new StringBuilder(); sb.append("merge into conexion_ruta_tramo_ctrl c ") .append("using ( ") .append("select c1.conexionrutatramoctrl_id ") .append("from conexion_ruta_tramo_ctrl c1 ") .append("join conexion_ruta_ctrl c2 on c1.conexionrutactrl_id = c2.conexionrutactrl_id ") .append("where c2.conexionrutaconf_id = :conexionrutaconfId and c1.activo = 1) t on (t.conexionrutatramoctrl_id = c.conexionrutatramoctrl_id) ") .append("when matched then update set c.activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif "); comandos.add(sb.toString()); sb = new StringBuilder(); sb.append("merge into conexion c ") .append("using ( ") .append("select c.conexion_id ") .append("from conexion c ") .append("join conexion_ruta_tramo_ctrl c1 on c.conexionrutatramoctrl_id = c1.conexionrutatramoctrl_id ") .append("join conexion_ruta_ctrl c2 on c1.conexionrutactrl_id = c2.conexionrutactrl_id ") .append("where c2.conexionrutaconf_id = :conexionrutaconfId and c.activo = 1) t on (t.conexion_id = c.conexion_id) ") .append("when matched then update set c.activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif"); comandos.add(sb.toString()); sb = new StringBuilder(); sb.append("merge into conexion_ctrl c ") .append("using ( ") .append("select distinct c.conexionctrl_id ") .append("from conexion_ctrl c ") .append("join conexion c3 on c3.conexionctrl_id = c.conexionctrl_id ") .append("join conexion_ruta_tramo_ctrl c1 on c3.conexionrutatramoctrl_id = c1.conexionrutatramoctrl_id ") .append("join conexion_ruta_ctrl c2 on c1.conexionrutactrl_id = c2.conexionrutactrl_id ") .append("where c2.conexionrutaconf_id = :conexionrutaconfId and c.activo = 1) t on (t.conexionctrl_id = c.conexionctrl_id) ") .append("when matched then update set c.activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif"); comandos.add(sb.toString()); sb = new StringBuilder(); sb.append("merge into conexion_conf c ") .append("using ( ") .append("select distinct c.conexionconf_id ") .append("from conexion_conf c ") .append("join conexion_ctrl c4 on c4.conexionctrl_id = c.conexionctrl_id ") .append("join conexion c3 on c3.conexionctrl_id = c4.conexionctrl_id ") .append("join conexion_ruta_tramo_ctrl c1 on c3.conexionrutatramoctrl_id = c1.conexionrutatramoctrl_id ") .append("join conexion_ruta_ctrl c2 on c1.conexionrutactrl_id = c2.conexionrutactrl_id ") .append("where c2.conexionrutaconf_id = :conexionrutaconfId and c.activo = 1) t on (t.conexionconf_id = c.conexionconf_id) ") .append("when matched then update set c.activo = 0, usuario_id = :usuarioId, fecmodif = :fecmodif "); comandos.add(sb.toString()); return comandos; } }