package com.fp.frontend.controller.armas.lov; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import org.apache.commons.lang3.StringUtils; import org.primefaces.context.RequestContext; import com.fp.dto.Request; import com.fp.dto.Response; import com.fp.dto.query.DtoQuery; import com.fp.dto.query.Filter; import com.fp.dto.query.SubQuery; import com.fp.frontend.controller.AbstractController; import com.fp.frontend.controller.pcustomer.PersonDetailController; import com.fp.frontend.enums.EnumLovOption; import com.fp.frontend.helper.CallerHelper; import com.fp.frontend.helper.MessageHelper; import com.fp.persistence.parmas.soli.TarmArmas; import com.fp.persistence.pcustomer.gene.TcustPersonDetail; /** * Controlador principal de armas * @author C.P. * */ @SuppressWarnings("serial") @ManagedBean @ViewScoped public class CertArmasLovController extends AbstractController { private String ccodigosarmas; private String estadosArma; private String codigopersona; public CertArmasLovController() throws Exception { super(TarmArmas.class); } /** * Método que se ejecuta después del constructor */ @PostConstruct private void postconstruct() { this.init(); try { ccodigosarmas = CallerHelper.getLovParameter("ccodigosarmas"); estadosArma = CallerHelper.getLovParameter("estadosarmas"); codigopersona=CallerHelper.getLovParameter("codigopersona"); if(codigopersona!=null && !codigopersona.equalsIgnoreCase("")){ querydatabase(); } // super.startQuery(); } catch (Exception e) { e.printStackTrace(); } } /** * Inicializa valores del controlador */ private void init() { try { recperpage = 5000; // Cambiar al # reg a mirar. record = new TarmArmas(); lrecord = new ArrayList<>(); beanalias = "ARMAS"; } catch (Exception e) { MessageHelper.setMessageError(e); } } @SuppressWarnings("unchecked") @Override protected void querydatabase() { try { DtoQuery dto = this.getDtoQuery(); String sqlFiltroEstados = ""; String sqlFiltroCodigosArmas = "";//1 //codigos de las armas seleccionadas if (ccodigosarmas != null) { sqlFiltroCodigosArmas=ccodigosarmas; } //filtro de los estados de las armas if (estadosArma != null) { sqlFiltroEstados=estadosArma;//estado distinto de destruidas - cambiodepropietario - vendidas - decomisadas - suspendida - cancelada - incautada } if(codigopersona!=null && !codigopersona.equals("null") && codigopersona.trim().length()>1){ sqlFiltroEstados=sqlFiltroEstados+" and t.cregistro in (select per from TarmRegistroArmas per where per.personcode="+codigopersona+") and ((select tae.clase from TarmTipoArmaExplosivo tae WHERE tae.pk = t.ctipoarmaexplosivo) in ('0010000001','0010000008'))"; } dto.setOrderby("pk"); String sqlActual = " t.fecharegistro=(select max(ta.fecharegistro) from TarmArmas ta where t.codigoarma=ta.codigoarma) " + " and (t.peso>0 or t.cantidad>0)";// and (t.cregistro=ta.cregistro) para el manejo de los explosivos String sqlFinal = obtenerFiltroBusquedaGeneral(sqlFiltroEstados, sqlFiltroCodigosArmas, sqlActual); Filter filtroFinal = new Filter(); filtroFinal.setSql(sqlFinal); dto.addFiltro(filtroFinal); HashMap mtables = new HashMap(); mtables.put(this.beanalias, dto); // permite adicionar mas de una tabla. Request request = this.callerhelper.getRequest(); request.setQueryTables(mtables); request.put("queryalias", "ARMASCOMPLETE"); Response resp = this.callerhelper.executeQuery(request); if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) { this.lrecord = new ArrayList(); MessageHelper.setMessageError(resp); } else { this.lrecord = (List) resp.get(this.beanalias); cargarPropietario(); super.postQuery(this.lrecord); } } catch (Throwable e) { MessageHelper.setMessageError(e); } } /** * Método para buscar el propietario del arma y asignarle al arma */ private void cargarPropietario(){ for(TarmArmas arma:this.lrecord){ //asigmnamos un valor al arma para ordenarla arma.getModifiedData().put("ordenarma",arma.getPk()); if(arma.getModifiedData().get("personcode")!=null){ TcustPersonDetail personaDetalle=PersonDetailController.find(arma.getModifiedData().get("personcode").toString()); arma.getModifiedData().put("propietario", personaDetalle.getName()); arma.getModifiedData().put("noDocumento", personaDetalle.getIdentification()); } } } private String obtenerFiltroBusquedaGeneral(String... sqlFiltros ){ StringBuffer sqlFinal = new StringBuffer(); for (int i = 0; i < sqlFiltros.length ; i++) { if(sqlFinal.length() > 0 && !StringUtils.endsWith(StringUtils.trim(sqlFinal.toString()), "AND")){ sqlFinal.append( " AND "); } sqlFinal.append(sqlFiltros[i]); } return sqlFinal.toString(); } /** * Retorna un {@link DtoQuery} con los atributos a presentarse * @return * @throws Exception */ private DtoQuery getDtoQuery() throws Exception{ DtoQuery dto = super.getDtoQuery(true); SubQuery subqueryPaisOrigen= new SubQuery("TgeneCountry","description","paisorigen","i.pk=t.cpaisorigen"); dto.addSubQuery(subqueryPaisOrigen); SubQuery subqueryMarca= new SubQuery("TgeneCatalogDetail","description","marca","i.pk.catalog=t.marca and i.pk.catalogcode=t.marcacodigo"); dto.addSubQuery(subqueryMarca); SubQuery subqueryColor= new SubQuery("TgeneCatalogDetail","description","color","i.pk.catalog=t.color and i.pk.catalogcode=t.colorcodigo"); dto.addSubQuery(subqueryColor); SubQuery subqueryEstado= new SubQuery("TgeneCatalogDetail","description","estado","i.pk.catalog=t.estado and i.pk.catalogcode=t.estadocodigo"); dto.addSubQuery(subqueryEstado); SubQuery subqueryClase= new SubQuery("TarmTipoArmaExplosivo","clase","cclase","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryClase); SubQuery subqueryClaseCodigo= new SubQuery("TarmTipoArmaExplosivo","clasecodigo","cclasecodigo","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryClaseCodigo); SubQuery subqueryLongitud= new SubQuery("TarmTipoArmaExplosivo","longitud","clongitud","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryLongitud); SubQuery subqueryLongitudCodigo= new SubQuery("TarmTipoArmaExplosivo","longitudcodigo","clongitudcodigo","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryLongitudCodigo); SubQuery subqueryCalibre= new SubQuery("TarmTipoArmaExplosivo","calibre","ccalibre","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryCalibre); SubQuery subqueryCalibreCodigo= new SubQuery("TarmTipoArmaExplosivo","calibrecodigo","ccalibrecodigo","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryCalibreCodigo); SubQuery subqueryTipoArma= new SubQuery("TarmTipoArmaExplosivo","tipoarmaexplosivo","ctipoarmaexplosivo","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryTipoArma); SubQuery subqueryTipoArmaCodigo= new SubQuery("TarmTipoArmaExplosivo","tipoarmaexplosivocodigo","ctipoarmaexplosivocodigo","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryTipoArmaCodigo); SubQuery subqueryUnidad= new SubQuery("TarmTipoArmaExplosivo","unidadmedidapeso","cunidadmedidapeso","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryUnidad); SubQuery subqueryUnidadCodigo= new SubQuery("TarmTipoArmaExplosivo","unidadmedidapesocodigo","cunidadmedidapesocodigo","i.pk=t.ctipoarmaexplosivo"); dto.addSubQuery(subqueryUnidadCodigo); SubQuery subqueryUnidadMedida= new SubQuery("TgeneCatalogDetail","description","unidadmedida","i.pk.catalog=t.unidadmedidacantidad and i.pk.catalogcode=t.unidadmedidacantidadcodigo"); dto.addSubQuery(subqueryUnidadMedida); SubQuery subquerycpersoncode= new SubQuery("TarmRegistroArmas","personcode","personcode","i.pk=t.cregistro"); dto.addSubQuery(subquerycpersoncode); SubQuery subqueryTipo= new SubQuery("TgeneCatalogDetail","description","tipo","i.pk.catalog= (select o.tipoarmaexplosivo from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo) and " + " i.pk.catalogcode=(select o.tipoarmaexplosivocodigo from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo)"); dto.addSubQuery(subqueryTipo); SubQuery subqueryTipoArmaExplosivo= new SubQuery("TgeneCatalogDetail","description","tipoarmaexplosivo","i.pk.catalog= (select o.tipoarmaexplosivo from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo) and " + " i.pk.catalogcode=(select o.tipoarmaexplosivocodigo from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo)"); dto.addSubQuery(subqueryTipoArmaExplosivo); SubQuery subqueryClasecatalogo= new SubQuery("TgeneCatalogDetail","description","clase","i.pk.catalog= (select o.clase from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo) and " + " i.pk.catalogcode=(select o.clasecodigo from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo)"); dto.addSubQuery(subqueryClasecatalogo); SubQuery subqueryUnidadTipo= new SubQuery("TgeneCatalogDetail","description","unidadmedidadesc","i.pk.catalog= (select o.unidadmedidapeso from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo) and " + " i.pk.catalogcode=(select o.unidadmedidapesocodigo from TarmTipoArmaExplosivo o where o.pk=t.ctipoarmaexplosivo)"); dto.addSubQuery(subqueryUnidadTipo); // SubQuery subqueryestadopermiso= new SubQuery("TgeneCatalogDetail","description","estadopermiso", // "i.pk.catalog=(select dh.estado from TarmDocumentoHabilitante dh where dh.personcode = (select r.personcode from TarmRegistroArmas r where r.pk = t.cregistro) and dh.carma = t.pk)" // + "and i.pk.catalogcode=(select dh.estadocode from TarmDocumentoHabilitante dh where dh.personcode = (select r.personcode from TarmRegistroArmas r where r.pk = t.cregistro) and dh.carma = t.pk)"); // dto.addSubQuery(subqueryestadopermiso); SubQuery subqueryFechaemiperm= new SubQuery("TarmDocumentoHabilitante","fechaemision","fechaemision", //" i.personcode=(select r.personcode from TarmRegistroArmas r where r.pk = t.cregistro) and i.carma = t.pk and "+ " i.pk=(select max(doc.pk) from TarmDocumentoHabilitante doc where doc.personcode=(select r.personcode from TarmRegistroArmas r where r.pk = t.cregistro)" + " and doc.carma in (select arm1.pk from TarmArmas arm1 where arm1.codigoarma in (select arm.codigoarma from TarmArmas arm where arm.pk=t.pk)))"); dto.addSubQuery(subqueryFechaemiperm); SubQuery subqueryFechaexpperm= new SubQuery("TarmDocumentoHabilitante","fechaexpiracion","fechaexpiracion", // " i.personcode=(select r.personcode from TarmRegistroArmas r where r.pk = t.cregistro) and i.carma = t.pk and "+ " i.pk=(select max(doc.pk) from TarmDocumentoHabilitante doc where doc.personcode=(select r.personcode from TarmRegistroArmas r where r.pk = t.cregistro) " + "and doc.carma in (select arm1.pk from TarmArmas arm1 where arm1.codigoarma in (select arm.codigoarma from TarmArmas arm where arm.pk=t.pk)))"); dto.addSubQuery(subqueryFechaexpperm); return dto; } @Override public void save() { try { } catch (Throwable e) { MessageHelper.setMessageError(e); } } /** * Metodo sobrecargado ejecutado al cerrar el dialogo actual */ public void setcatalog() { Listarmas=new ArrayList(); armas.add(this.record); // se utiliza en la pagina del lov. RequestContext.getCurrentInstance().closeDialog(armas); } /** * Metodo sobrecargado ejecutado al cerrar el dialogo actual */ public void setcatalog(TarmArmas tarmArmas) { Listarmas=new ArrayList(); armas.add(tarmArmas); // se utiliza en la pagina del lov. RequestContext.getCurrentInstance().closeDialog(armas); } public void selecionaTodos(){ RequestContext.getCurrentInstance().closeDialog(lrecord); } public static void openLov(Map> params) { Map options = new HashMap(); options.put(EnumLovOption.MODAL.getLabel(), true); options.put(EnumLovOption.HEIGHT.getLabel(), 450); options.put(EnumLovOption.WIDTH.getLabel(), 1200); options.put(EnumLovOption.RESIZABLE.getLabel(), false); RequestContext.getCurrentInstance().openDialog("/pages/armas/lov/certArmasLov.xhtml", options, params); } }