package com.fp.armas.rules.query.solicitud; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.fp.dto.query.QueryRequest; import com.fp.dto.rules.QueryRule; import com.fp.persistence.commondb.PersistenceHelper; import com.fp.persistence.parmas.soli.TarmDecomisoArma; import com.fp.persistence.pgeneral.gene.TgeneParameters; import com.fp.persistence.pgeneral.gene.TgeneParametersKey; /** * * @author Norge Dominguez Cand * */ public class VerificarArmaQuery extends QueryRule { private static final String CODIGO_DIAS_DECOMISO = "DIASDECOMISO"; private static final String CODIGO_RECL = "CODIGO.RECL"; private static final String CODIGO_ABAN = "CODIGO.ABAN"; private static final String CODIGO_ENTV = "CODIGO.ENTV"; private static final String CODIGO_INCAUTADA = "CODIGO.INCAUTADA"; private static final String CODIGO_DECOMISADA = "CODIGO.DECOMISADA"; private static final long serialVersionUID = 8055423288586131089L; @SuppressWarnings("unchecked") @Override public QueryRequest process(QueryRequest pRequest) throws Exception { //Obtener valores String centroControl = (String) pRequest.get("centroControl"); String numeroDecomiso = (String) pRequest.get("numeroDecomiso"); String numeroserie = (String) pRequest.get("numeroserie"); String fechacorte = (String) pRequest.get("fechacorte"); String codigoDecomiso = codigos(CODIGO_DECOMISADA);// DECOMISO String codigoIncautacion = codigos(CODIGO_INCAUTADA);// INCAUTACION String codigoEntVol = codigos(CODIGO_ENTV);// ENTREGA VOLUNTARIA String codigoAbandono = codigos(CODIGO_ABAN);// ABANDONO String codigoReclamo = codigos(CODIGO_RECL);// RECLAMO String codigoDiasDecomiso = codigos(CODIGO_DIAS_DECOMISO);// DIAS DECOMISO if ( fechacorte == null || fechacorte.isEmpty() ) { fechacorte = new Date().toString(); } //Se filtra por centrocontrol, estados, bodega StringBuffer bufferFiltro = new StringBuffer(); //Filtro por estados, fecha, enBodega bufferFiltro.append("select t.* from tarmdecomisoarma t join tarmdecomiso td on t.cdecomiso = td.cdecomiso join tarmarmas ta on t.carma = ta.carma " + "where trunc(t.fdecomiso) <= TO_DATE('"+fechacorte+"','yyyy-mm-dd') and abs(TRUNC(TO_DATE('"+fechacorte+"','yyyy-mm-dd')) - TRUNC(t.fdecomiso))>= "+codigoDiasDecomiso); bufferFiltro.append(" and (t.estado='"+codigoDecomiso+"' or t.estado='"+codigoIncautacion+"' or t.estado='"+codigoEntVol+"' or t.estado='"+codigoAbandono+"' or t.estado='"+codigoReclamo+"') and t.enbodega= 'Y'"); if(numeroDecomiso!=null && !numeroDecomiso.isEmpty()){ bufferFiltro.append(" and td.numerotransaccion ='"+numeroDecomiso+"'"); } if(numeroserie!=null && !numeroserie.isEmpty()){ bufferFiltro.append(" and ta.lote ='"+numeroserie+"'"); } //Filtro para la tabla decomiso StringBuffer bufferDecomiso = new StringBuffer(); bufferDecomiso.append(bufferFiltro); bufferDecomiso.append(" and t.cdecomisoarmatransaccion is null and t.cdecarmtranrecepcion is null and t.coperaciontransaccion is null and td.ccentrocontrol = '" + centroControl + "'"); List cdecomisos = PersistenceHelper.getEntityManager().createNativeQuery(bufferDecomiso.toString(), TarmDecomisoArma.class).getResultList(); //Filtro para la tabla decomiso StringBuffer bufferOperacion = new StringBuffer(); bufferOperacion.append(bufferFiltro); int intWhere = bufferOperacion.indexOf("where"); bufferOperacion.insert(intWhere, "join tarmdecomisoarmatransaccion tt on t.cdecomisoarmatransaccion = tt.cdecomisoarmatransaccion " + "join tarmtransaccionarmabodega tb on t.carma = tb.carma and t.cdecomisoarmatransaccion = tb.cdecomisoarmatransaccion "); bufferOperacion.append(" and tt.estadofirma = 'Y' and t.coperaciontransaccion = 'RECEPCION' and tb.ccentrocontroldestino = '" + centroControl + "'"); List operaciontransacciones = (List)PersistenceHelper.getEntityManager().createNativeQuery(bufferOperacion.toString(), TarmDecomisoArma.class).getResultList(); List tarmdecomisoarmas = new ArrayList(); //Add toda la collection con filtro de decomisos tarmdecomisoarmas.addAll(cdecomisos); //Add toda la collection con filtro de operacion tarmdecomisoarmas.addAll(operaciontransacciones); pRequest.getResponse().put("RPRTEDESTRUCCIONDEARMAS", tarmdecomisoarmas); return pRequest; } /** * Metodo para obtener los codigos */ private String codigos(String parametro) { String codigo = null; TgeneParameters tgeneParameters; try { tgeneParameters = TgeneParameters.find( PersistenceHelper.getEntityManager(), new TgeneParametersKey( parametro, new Integer(1))); if (tgeneParameters == null || tgeneParameters.getTextvalue() == null) { codigo = codigosTemp(parametro); } else { codigo = tgeneParameters.getTextvalue(); } } catch (Exception e) { return codigosTemp(parametro); } return codigo; } /** * En caso de no poder acceder a la base se asigna codigo temporal * * @param parametro * @return * @throws Exception */ private String codigosTemp(String parametro) { String codigoTemp = null; if (CODIGO_DECOMISADA.equals(parametro)) { codigoTemp = "DEC"; } else if (CODIGO_INCAUTADA.equals(parametro)) { codigoTemp = "INC"; } else if (CODIGO_ENTV.equals(parametro)) { codigoTemp = "ENTR"; } else if (CODIGO_ABAN.equals(parametro)) { codigoTemp = "ABA"; } else if (CODIGO_RECL.equals(parametro)) { codigoTemp = "RECL"; } else if (CODIGO_DIAS_DECOMISO.equals(parametro)) { codigoTemp = "0"; } return codigoTemp; } }