/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.patientmanagement.servlet;

import hu.astrid.debgen.authentication.domain.DebGenUser;
import hu.astrid.debgen.patientmanagement.facade.PatientManagerFacade;
import hu.astrid.debgen.patientmanagement.facade.modules.ResultManagerFacade;
import hu.astrid.debgen.patientmanagement.facade.modules.requests.RequestManagerFacade;
import hu.astrid.debgen.server.requestprocessor.ActionParameter;
import hu.astrid.debgen.server.requestprocessor.facade.RequestProcessorFacade;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Autocompletes search patterns using the DB
 * @author István Zoltán Nagy
 */
public class AutoCompleteServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ActionParameter actionParameter = ActionParameter.getActionParameter(request);
        String action = actionParameter.getAction();
        DebGenUser user = RequestProcessorFacade.getDebGenUser(request);
        try {
            String query = request.getParameter("q");
            PatientManagerFacade patientManagerFacade = new PatientManagerFacade();
            RequestManagerFacade requestManagerFacade = new RequestManagerFacade();
            ResultManagerFacade resultManagerFacade = new ResultManagerFacade();
            List<String> results = null;
            if (action.equals("autocompletesendername")) {
                results = requestManagerFacade.listRequestField("nameOfSenderInstitute", query);
            }
            if (action.equals("autocompletesendercode")) {
                results = requestManagerFacade.listRequestField("senderCode", query);
            }
            if (action.equals("autocompletefishtests")) {
                results = resultManagerFacade.listResultField("fishUsedTest", query);
            }
            if (action.equals("autocompleteachotests")) {
                results = resultManagerFacade.listResultField("achoUsedTest", query);
            }
            if (action.equals("autocompletehypotests")) {
                results = resultManagerFacade.listResultField("hypoUsedTest", query);
            }
            if (action.equals("autocompletefmrtests")) {
                results = resultManagerFacade.listResultField("fmrUsedTest", query);
            }
            if (action.equals("autocompleteindication")) {
                results = resultManagerFacade.listResultField("indicationOfAnalysis", query);
            }
            if (action.equals("autocompletediagnosis")) {
                results = requestManagerFacade.listRequestField("diagnosis", query);
            }
            if (action.equals("autocompletebnocode")) {
                results = requestManagerFacade.listRequestField("BNOCode", query);
            }
            if (action.equals("autocompletename")) {
                results = patientManagerFacade.listNames(query);
            /*}
            if (action.equals("autocompletetaj")) {*/
                results.addAll(patientManagerFacade.listTAJ(query));
            /*}
            if (action.equals("autocompletecode")) {*/
                results.addAll(patientManagerFacade.listCode(query, user.isDataRecorder()));
            }
            if (results != null) {
                for (String s : results) {
                    out.println(s);
                }
            }
        } catch (Exception ex) {
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
