package sac.gui.handler;

import sac.fachada.SACFachada;
import sac.medicamento.Medicamento;
import sac.medicamento.MedicamentoJaCadastradoException;
import sac.medicamento.MedicamentoNaoCadastradoException;
import sac.medicamento.Apresentacao;
import sac.medicamento.Laboratorio;
import sac.doenca.DoencaNaoCadastradaException;
import sac.doenca.Doenca;
import sac.cid.CID;
import sac.droga.Droga;
import sac.droga.DrogaNaoCadastradaException;
import sac.exception.ItemJaCadastradoException;
import sac.exception.ItemNaoCadastradoException;
import sac.exception.NullArgumentException;
import sac.exception.InvalidArgumentException;
import sac.persistencia.PersistenceException;
import sac.persistencia.OID;
import sac.exception.NullArgumentException;
import sac.gui.bean.MedicamentoBean;
import sac.gui.bean.DoencaBean;
import sac.gui.bean.CampoInvalidoException;

import java.io.IOException;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.Hashtable;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.sql.SQLException;
import java.util.Vector;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

/**
 * Handler para o formulário de login no sistema.
 *
 */
public class CadastroMedicamentoHandler extends SACHandler {
    private static final String OPERACAO_CADASTRO = "InsercaoOperation";
    private static final String OPERACAO_FILL_FROM = "FillFormOperation";
    private static final String OPERACAO_ALTERACAO = "AlteracaoOperation";
    private static final String OPERACAO_OBTEM_MEDICAMENTO = "ObtemMedicamentoOperation";
    private static final String CONSULTA_MEDICAMENTO_NOME = "ConsultaMedicamentoNomeOperation";
    private static final String CONSULTA_MEDICAMENTO_DROGA = "ConsultaMedicamentoDrogaOperation";
    private static final String CONSULTA_MEDICAMENTO_DOENCA = "ConsultaMedicamentoDoencaOperation";
    private static final String CONSULTA_MEDICAMENTO_LABORATORIO = "ConsultaMedicamentoLaboratorioOperation";
    private static final String CONSULTA_MEDICAMENTO_CID = "ConsultaMedicamentoCIDOperation";

    private final String URL_OPERACAO_OK = "/jsp/cadastro_medicamento_ok.jsp";
    private final String URL_CADASTRO_MEDICAMENTO = "/jsp/cadastro_medicamento.jsp";
    private final String URL_ALTERACAO_MEDICAMENTO = "/jsp/alteracao_medicamento.jsp";
    private final String URL_MEDICAMENTO_ERRO = "/jsp/medicamentoErro.jsp";
    private final String URL_CONSULTA_MEDICAMENTO = "/jsp/consulta_medicamento.jsp";
    private final String URL_RESPONSE_CONSULTA_MEDICAMENTO = "/jsp/response_consulta_medicamento.jsp";
    private final String URL_OBTER_MEDICAMENTO = "/jsp/obter_medicamento.jsp";

    /** Constantes utilizadas no Request desse Handler.*/
    private static  final String MEDICAMENTO_BEAN_REQUEST = "medicamentoBean";
    public static final String MEDICAMENTO_ERRO = "medicamentoErro";

    /** Constantes que definem os nomes dos Inputs do Form. */
    private static final String OID_INPUT = "oid";
    private static final String NOME_INPUT = "nome";
    private static final String APRESENTACAO_INPUT = "apresentacao";
    private static final String LABORATORIOS_INPUT = "laboratorios";
    private static final String INDICACOES_INPUT = "indicacoes";
    private static final String CONTRA_INDICACOES_INPUT = "contraIndicacoes";
    private static final String DROGA_PRINCIPAL_INPUT = "drogaPrincipal";
    private static final String DROGAS_SECUNDARIAS_INPUT = "drogasSecundarias";
    private static final String DOENCAS_RELACIONADAS_INPUT = "doencasRelacionadas";
    private static final String PRECO_VAREJO_INPUT = "precoVarejo";
    private static final String PRECO_ATACADO_INFERIOR_INPUT = "precoAtacadoInferior";
    private static final String PRECO_ATACADO_SUPERIOR_INPUT = "precoAtacadoSuperior";


    /** Guarda o mapeamento (operacao, Handler)   */
    protected Map functionImplementers;

    /** Guarda o último handler solicitado. */
    protected SACHandler currentImplementer;

    /** Guarda a última operação solicitada. */
    protected String currentFunction;

    /** Creates new MenuHandler */
    public CadastroMedicamentoHandler() {
        super();
        this.functionImplementers = new Hashtable();
        this.functionImplementers.put(OPERACAO_CADASTRO, new InsercaoMedicamentoHandler());
        this.functionImplementers.put(OPERACAO_ALTERACAO, new AlteracaoMedicamentoHandler());
        this.functionImplementers.put(OPERACAO_FILL_FROM, new FillFormHandler());
        this.functionImplementers.put(OPERACAO_OBTEM_MEDICAMENTO, new ObtemMedicamentoHandler());
        this.functionImplementers.put(CONSULTA_MEDICAMENTO_NOME, new ConsultaMedicamentoNomeHandler());
        this.functionImplementers.put(CONSULTA_MEDICAMENTO_DOENCA, new ConsultaMedicamentoDoencaHandler());
        this.functionImplementers.put(CONSULTA_MEDICAMENTO_DROGA, new ConsultaMedicamentoDrogaHandler());
        this.functionImplementers.put(CONSULTA_MEDICAMENTO_LABORATORIO, new ConsultaMedicamentoLaboratorioHandler());                
    }

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @param fachada fachada do Sistema
     * @return String a url do form a ser retornado
     */
    public String processRequest(HttpServletRequest request, HttpServletResponse response, SACFachada sacFachada)
    throws ServletException, IOException {
        String functionName = request.getParameter(SACHandler.FUNCTION_ID);
        String res = null;
        Object handler;
        if (functionName.equals(currentFunction)) {
            handler = currentImplementer;
        } else {
            handler = functionImplementers.get(functionName);
        }
        if (handler != null) {
            res = ((SACHandler) handler).processRequest(request, response, sacFachada);
            this.currentFunction = functionName;
            this.currentImplementer = (SACHandler) handler;
        } else {
            System.out.println("Nao conseguiu obter o handler " + functionName);
        }
        System.out.println("Saiu do processRequest inicial");
        return res;
    }

    /**
     * Handler para a operacao de cadastro de Medicamento no sistema.
     */
    private class InsercaoMedicamentoHandler extends SACHandler {

        /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
         * @param request servlet request
         * @param response servlet response
         * @param fachada fachada do Sistema
         * @return String a url do form a ser retornado
         */
         public String processRequest(HttpServletRequest request, HttpServletResponse response,
                 SACFachada fachada) throws ServletException, IOException {


            String res = "";
            Medicamento medicamento = null;
            MedicamentoBean bean = null;
            List drogas = null, doencas = null;

            try {
                drogas = fachada.obterDrogas();
                doencas = fachada.obterDoencas();

                medicamento = getMedicamentoFromRequest(request, fachada);
                bean = getMedicamentoBean(medicamento, fachada);
                bean.validaBean();

                System.out.println("Cadastrando medicamento");
                fachada.cadastrarMedicamento(medicamento);
                System.out.println("Medicamento " + medicamento.getNome() + " cadastrado");
                res = URL_OPERACAO_OK;
                System.out.println("Retornando a URL="+res);
            } catch(CampoInvalidoException exc) {
                request.setAttribute(MEDICAMENTO_BEAN_REQUEST,bean);
                request.setAttribute("drogas", drogas);
                request.setAttribute("doencas", doencas);
                res = URL_CADASTRO_MEDICAMENTO;
                System.out.println("Retornando a URL="+res);
            } catch (ItemJaCadastradoException exc) {
                bean.setMensagemErro("Item ja cadastrado");
                System.out.println("item ja cadastrado");
                request.setAttribute(MEDICAMENTO_BEAN_REQUEST, bean);
                request.setAttribute("drogas", drogas);
                request.setAttribute("doencas", doencas);
                res = URL_CADASTRO_MEDICAMENTO;
            } catch (PersistenceException exc) {
                throw new ServletException(exc);
            } catch (NullArgumentException exc) {
                throw new ServletException(exc);
            } catch (DrogaNaoCadastradaException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;

            } catch (DoencaNaoCadastradaException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (SQLException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            }
            return res;
        }

    }// fim da class InsercaoMedicamentoHandler

    private class AlteracaoMedicamentoHandler extends SACHandler {
        public String processRequest(HttpServletRequest request, HttpServletResponse response,
                 SACFachada fachada) throws ServletException, IOException {

            String res = null;
            Medicamento oldMed = null, newMed = null;
            MedicamentoBean bean = null;
            List doencas = null, drogas = null;

            try {
                doencas = fachada.obterDoencas();
                drogas = fachada.obterDrogas();

                newMed = getMedicamentoFromRequest(request, fachada);
                bean = getMedicamentoBean(newMed, fachada);
                System.out.println("Criou o bean");
                bean.validaBean();
                System.out.println("Validou o bean");
                System.out.println("Procurando medicamento velho");
                oldMed = fachada.procurarMedicamento(newMed.getId());
                System.out.println("Achou medicamento velho: " + oldMed.getNome());
                System.out.println("Alterando...");
                fachada.alterarMedicamento(oldMed, newMed);
                System.out.println("Alterou para: " + newMed.getNome());
                res = URL_OPERACAO_OK;
            } catch(CampoInvalidoException exc) {
                request.setAttribute(MEDICAMENTO_BEAN_REQUEST, bean);
                request.setAttribute("doencas", doencas);
                request.setAttribute("drogas", drogas);
                res = URL_ALTERACAO_MEDICAMENTO;
            } catch(NullArgumentException exc) {
                bean.setMensagemErro(exc.getMessage());
                request.setAttribute(MEDICAMENTO_BEAN_REQUEST, bean);
                res = URL_ALTERACAO_MEDICAMENTO;
            }catch (ItemNaoCadastradoException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (PersistenceException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (SQLException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            }
            return res;
        }
    } // fim do AlteracaoMedicamentoHandler

    private class FillFormHandler extends SACHandler {

        public String processRequest(HttpServletRequest request, HttpServletResponse response,
                 SACFachada fachada) throws ServletException, IOException {
            MedicamentoBean medicamentoBean;
            Medicamento medicamento;
            String res;

            try{
                String oid = request.getParameter(OID_INPUT);
                if (oid == null) {
                    medicamentoBean = new MedicamentoBean("", "", null, null, "","", "", null, null, "","","","",fachada.obterDrogas(), fachada.obterDoencas());
                    res = URL_CADASTRO_MEDICAMENTO;
                } else {
                    medicamento = fachada.procurarMedicamento(new OID(Long.parseLong(oid)));
                    medicamentoBean = getMedicamentoBean(medicamento, fachada);
                    res = URL_ALTERACAO_MEDICAMENTO;                    
                }

                List drogas = fachada.obterDrogas(), doencas = fachada.obterDoencas();
                request.setAttribute(MEDICAMENTO_BEAN_REQUEST, medicamentoBean);
                request.setAttribute("drogas", drogas);
                request.setAttribute("doencas", doencas);

                return res;
            } catch(PersistenceException pe){

            } catch(NullArgumentException nae){

            } catch(SQLException exc) {

            } catch(MedicamentoNaoCadastradoException exc) {

            }
            return URL_CADASTRO_MEDICAMENTO;
        }
    } // fim do FillFormHandler

    private class ObtemMedicamentoHandler extends SACHandler {
        public String processRequest(HttpServletRequest request, HttpServletResponse response,
                 SACFachada fachada) throws ServletException, IOException {

            String res = null;
            try {
                String oid = request.getParameter(OID_INPUT);
                Medicamento medicamento = fachada.procurarMedicamento(new OID(Long.parseLong(oid)));
                MedicamentoBean medicamentoBean = getMedicamentoBean(medicamento, fachada);
                request.setAttribute(MEDICAMENTO_BEAN_REQUEST, medicamentoBean);
                res = URL_OBTER_MEDICAMENTO;

            }catch (ItemNaoCadastradoException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch(NullArgumentException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (PersistenceException exc) {
                throw new ServletException(exc);
            } catch (SQLException exc){
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            }
            return res;
        }
    }

    private class ConsultaMedicamentoNomeHandler extends SACHandler {
        public String processRequest(HttpServletRequest request, HttpServletResponse response,
                 SACFachada fachada) throws ServletException, IOException {
            String res = null;
            String campo = "", valor = "";
            List medicamentos = null;
            MedicamentoBean bean = null;

            try {

                campo = request.getParameter("campo");
                valor = request.getParameter("valor");
                if (campo.equals("Nome")) {
                    medicamentos = fachada.procurarMedicamentoPeloNome(valor);
                    System.out.println(campo + " - " + valor);
                } else {
                    if (campo.equals("Droga")) {
                        Droga droga = fachada.procurarDroga(valor);
                        medicamentos = fachada.procurarMedicamentoPelaDroga(droga);
                        System.out.println(campo + " - " + valor);
                    } else {
                        if (campo.equals("Laboratorio")) {
                            Laboratorio laboratorio = new Laboratorio();
                            laboratorio.setNome(valor);
                            medicamentos = fachada.procurarMedicamentoPeloLaboratorio(laboratorio);
                            System.out.println(campo + " - " + valor);
                        } else {
                            if (campo.equals("CID")) {
                                CID cid = new CID(Long.parseLong(valor));
                                medicamentos = fachada.procurarMedicamentoPeloCID(cid);
                                System.out.println(campo + " - " + valor);
                            } else {
                                if (campo.equals("Doenca")) {
                                    Doenca doenca = fachada.procurarDoenca(valor);
                                    medicamentos = fachada.procurarMedicamentoPelaDoenca(doenca);
                                    System.out.println(campo + " - " + valor);
                                }
                            }
                        }
                    }

                }
                request.setAttribute("medicamentos",medicamentos);
                request.setAttribute("campo", campo);
                request.setAttribute("valor",valor);
                res = URL_RESPONSE_CONSULTA_MEDICAMENTO;
            } catch (SQLException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (MedicamentoNaoCadastradoException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (PersistenceException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (NullArgumentException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (DrogaNaoCadastradaException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (DoencaNaoCadastradaException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            }
            return res;
        }

    }

    private class ConsultaMedicamentoDrogaHandler extends SACHandler {
        public String processRequest(HttpServletRequest request, HttpServletResponse response,
                 SACFachada fachada) throws ServletException, IOException {
            String res = null;
            String campo = "", valor = "";
            List medicamentos = null;
            MedicamentoBean bean = null;

            try {
                valor = request.getParameter("droga");
                if (campo.equals("Nome")) {
                    medicamentos = fachada.procurarMedicamentoPeloNome(valor);
                    System.out.println(campo + " - " + valor);
                } else {
                    if (campo.equals("Droga")) {
                        Droga droga = fachada.procurarDroga(valor);
                        medicamentos = fachada.procurarMedicamentoPelaDroga(droga);
                        System.out.println(campo + " - " + valor);
                    } else {
                        if (campo.equals("Laboratorio")) {
                            Laboratorio laboratorio = new Laboratorio();
                            laboratorio.setNome(valor);
                            medicamentos = fachada.procurarMedicamentoPeloLaboratorio(laboratorio);
                            System.out.println(campo + " - " + valor);
                        } else {
                            if (campo.equals("CID")) {
                                CID cid = new CID(Long.parseLong(valor));
                                medicamentos = fachada.procurarMedicamentoPeloCID(cid);
                                System.out.println(campo + " - " + valor);
                            } else {
                                if (campo.equals("Doenca")) {
                                    Doenca doenca = fachada.procurarDoenca(valor);
                                    medicamentos = fachada.procurarMedicamentoPelaDoenca(doenca);
                                    System.out.println(campo + " - " + valor);
                                }
                            }
                        }
                    }

                }
                request.setAttribute("medicamentos",medicamentos);
                request.setAttribute("campo", campo);
                request.setAttribute("valor",valor);
                res = URL_RESPONSE_CONSULTA_MEDICAMENTO;
            } catch (SQLException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (MedicamentoNaoCadastradoException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (PersistenceException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (NullArgumentException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (DrogaNaoCadastradaException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            } catch (DoencaNaoCadastradaException exc) {
                request.setAttribute(MEDICAMENTO_ERRO, exc.getMessage());
                res = URL_MEDICAMENTO_ERRO;
            }
            return res;
        }

    }

    public Medicamento getMedicamentoFromRequest(HttpServletRequest request, SACFachada fachada)
    throws NullArgumentException, DrogaNaoCadastradaException, PersistenceException, DoencaNaoCadastradaException{
        String precoVarejo, precoAtacInf, precoAtacSup;
        Medicamento medicamento = new Medicamento();
        medicamento.setNome(request.getParameter(NOME_INPUT));
        medicamento.setApresentacao(getApresentacao(request));
        medicamento.setLaboratorios(getLaboratorios(request));
        medicamento.setIndicacoes(request.getParameter(INDICACOES_INPUT));
        medicamento.setcontraIndicacoes(request.getParameter(CONTRA_INDICACOES_INPUT));
        medicamento.setDrogaPrincipal(fachada.procurarDroga(request.getParameter(DROGA_PRINCIPAL_INPUT)));
        medicamento.setDrogasSecundarias(getDrogas(request, fachada));
        medicamento.setDoencasRelacionadas(getDoencas(request, fachada));

        precoVarejo = request.getParameter(PRECO_VAREJO_INPUT);
        precoAtacInf = request.getParameter(PRECO_ATACADO_INFERIOR_INPUT);
        precoAtacSup = request.getParameter(PRECO_ATACADO_SUPERIOR_INPUT);
        if (!precoVarejo.equals("")) {
            precoVarejo = precoVarejo.replace(',','.');
            medicamento.setPrecoVarejo(Double.parseDouble(precoVarejo));
        }
        if (!precoAtacInf.equals("")) {
            precoAtacInf = precoAtacInf.replace(',','.');
            medicamento.setPrecoAtacadoInferior(Double.parseDouble(precoAtacInf));
        }
        if (!precoAtacSup.equals("")) {
            precoAtacSup = precoAtacSup.replace(',','.');
            medicamento.setPrecoAtacadoSuperior(Double.parseDouble(precoAtacSup));
        }
        String oid = request.getParameter(OID_INPUT);
        if (oid != null) {
            medicamento.setId(new OID (Long.parseLong(oid)));
        }
        return medicamento;
    }

    public MedicamentoBean getMedicamentoBean(Medicamento medicamento, SACFachada fachada)
    throws PersistenceException, NullArgumentException{
        MedicamentoBean medicamentoBean = new MedicamentoBean();
        medicamentoBean.setApresentacao(medicamento.getApresentacao());
        medicamentoBean.setcontraIndicacoes(medicamento.getcontraIndicacoes());
        medicamentoBean.setDoencasRelacionadas(medicamento.getDoencasRelacionadas());
        medicamentoBean.setDrogaPrincipal(medicamento.getDrogaPrincipal().getNome());
        medicamentoBean.setDrogasSecundarias(medicamento.getDrogasSecundarias());
        if (medicamento.getId() != null) {
            medicamentoBean.setId(medicamento.getId().getLongValue() + "");
        }
        medicamentoBean.setIndicacoes(medicamento.getIndicacoes());
        medicamentoBean.setLaboratorios(medicamento.getLaboratorios());
        medicamentoBean.setMensagemErro("");
        medicamentoBean.setNome(medicamento.getNome());
        medicamentoBean.setPrecoAtacadoInferior(medicamento.getPrecoAtacadoInferior()+"");
        medicamentoBean.setPrecoAtacadoSuperior(medicamento.getPrecoAtacadoSuperior()+"");
        medicamentoBean.setPrecoVarejo(medicamento.getPrecoVarejo()+"");
        medicamentoBean.setDoencas(fachada.obterDoencas());
        medicamentoBean.setDrogas(fachada.obterDrogas());
        return medicamentoBean;
    }

    public List getApresentacao(HttpServletRequest request) throws NullArgumentException{
            List resp = new Vector();
        	  Apresentacao apresObj = null;
            String apres = request.getParameter(APRESENTACAO_INPUT);
            if (!apres.equals("")) {
                StringTokenizer str = new StringTokenizer(apres, "\n");
                System.out.println("Pegando "+ str.countTokens() +" apresentacoes ...");
                while (str.hasMoreElements()) {
                    apresObj = new Apresentacao();
                    apresObj.setNome((String) str.nextElement());
                    System.out.println("Apresentacao: " + apresObj.getNome());
                    resp.add(apresObj);
                }
            }
            return resp;
        }

      public List getLaboratorios(HttpServletRequest request) throws NullArgumentException{
            List resp = null;
        	  Laboratorio labObj = null;
            String labs = request.getParameter(LABORATORIOS_INPUT);
            if (!labs.equals("")) {
                resp = new Vector();
                StringTokenizer str = new StringTokenizer(labs, "\n");
                System.out.println("Pegando " + str.countTokens() + " laboratorios ...");
                while (str.hasMoreElements()) {
                    labObj = new Laboratorio();
                    labObj.setNome((String) str.nextElement());
                    System.out.println("Laboratorio: " + labObj.getNome());
                    resp.add(labObj);
                }
            }
            return resp;
        }

        public List getDrogas(HttpServletRequest request, SACFachada fachada) throws NullArgumentException,
        DrogaNaoCadastradaException, PersistenceException{
            List resp = null;
        	  Droga droga = null;
            String[] drogas = request.getParameterValues(DROGAS_SECUNDARIAS_INPUT);
            if (drogas != null) {
                resp = new Vector();
                System.out.println("Pegando "+drogas.length+" drogas ...");
                for (int i = 0; i < drogas.length; i++) {
                    droga = fachada.procurarDroga(drogas[i]);
                    System.out.println("Droga: " + drogas[i]);
                    resp.add(droga);
                }
            }
            return resp;
        }

        public List getDoencas(HttpServletRequest request, SACFachada fachada)  throws NullArgumentException,
        PersistenceException, DoencaNaoCadastradaException{
            List resp = null;
        	  Doenca doenca = null;
            String[] doencas = request.getParameterValues(DOENCAS_RELACIONADAS_INPUT);
            if (doencas != null) {
                resp = new Vector();
                System.out.println("Pegando "+doencas.length+" doencas ...");
                for (int i = 0; i < doencas.length; i++) {
                    doenca = fachada.procurarDoenca(doencas[i]);
                    System.out.println("Doenca: " + doenca.getNome());
                    resp.add(doenca);
                }
            }
            return resp;
        }

}
