package com.FitBank.web.servlet;

import com.FitBank.common.Servicios;
import com.FitBank.web.util.EjecutarEventos;
import com.FitBank.xml.Formas.Formulario;
import com.FitBank.xml.Mensaje.MensajeXml;

/**
 * Servlet que ejecuta la lï¿½gica para generar una consulta al sistema.
 * 
 * @author FITBANK
 */
public class Consultar extends BaseServlets {

    @Override
    protected void inicializar() {
        this.aceptarGet();
        this.aceptarPost();
        this.obtenerWriter();
        this.verificarSesion();
    }

    @Override
    public void procesarPedido() {
        String parametrosCargarImagenes = null;

        this.respuesta.setContentType("text/xml; charset=iso-8859-1");
        this.respuesta.setDateHeader("Expires", System.currentTimeMillis());

        boolean consultaNormal = !Servicios.verificarCadena(this.pedido.getParameter("LV")).equals("true");
        boolean onlyLoadForm = Servicios.verificarCadena(this.pedido.getParameter("SV")).equalsIgnoreCase("TRUE");
        String con = Servicios.verificarCadena(this.pedido.getParameter("con"));
        String ownerImagen = Servicios.verificarCadena(this.pedido.getParameter("paramImagesSET"));
        String ownerTipDoc = Servicios.verificarCadena(this.pedido.getParameter("paramImgTipDoc"));
        String ownerTipPrs = Servicios.verificarCadena(this.pedido.getParameter("paramImgTipPrs"));

        if (!ownerImagen.equals("") && ownerTipDoc.equals("") && ownerTipPrs.equals("")) {
            parametrosCargarImagenes = this.evento.procesoXml.armarParametrosCargarImagenes(ownerImagen);
        } else if (!ownerImagen.equals("") && !ownerTipDoc.equals("") && !ownerTipPrs.equals("")) {
            parametrosCargarImagenes = this.evento.procesoXml.armarParametrosCargarImagenes(ownerImagen, ownerTipDoc,
                    ownerTipPrs);
        }

        if (consultaNormal) {
            this.evento.ejecutarConsulta(this.pedido);
            this.evento.procesoXml.cargarAutoGenerate();
            this.evento.generarHtmlFormulario(pedido.getSession().getId());
            this.writer.print(this.evento.getHtmlFormulario());
        } else {
            EjecutarEventos eventoLV = (EjecutarEventos) this.sesion.getAttribute("FBOSLV");

            if (con.equalsIgnoreCase("1")) {
                eventoLV.ejecutarConsulta(this.pedido);
            } else if (con.equalsIgnoreCase("2")) {
                String sub = Servicios.verificarCadena(this.pedido.getParameter("sub"));
                String tra = Servicios.verificarCadena(this.pedido.getParameter("tran"));
                String[][] cab = {{"SUB", sub}, {"TRN", tra}, {"TIP", MensajeXml.TIP_FRM}};
                this.evento.procesoXml.crearMensaje(cab);
                this.evento.procesoXml.getXmlMensaje().borrarDetalle();
                this.evento.procesoXml.getXmlMensaje().borrarResultado();

                this.evento.obtenerTransaccion(this.pedido);

                this.evento.procesoXml.getXmlFormulario().setFormASEstatus(Formulario.FRM_INS);

                if (!onlyLoadForm) {
                    this.evento.ejecutarConsultaConLink(this.pedido);
                }

                this.evento.generarHtmlFormulario(pedido.getSession().getId());

                this.writer.print(this.evento.getHtmlFormulario());

                return;
            } else {
                eventoLV.ejecutarConsultaLV(this.pedido, parametrosCargarImagenes);
            }

            eventoLV.generarHtmlFormularioLV();
            this.sesion.setAttribute("FBOSLV", eventoLV);
            this.writer.print(eventoLV.getHtmlFormulario());
        }
    }
}