package edu.duke.cabig.c3pr.web.study;

import edu.duke.cabig.c3pr.dao.StudyDao;
import edu.duke.cabig.c3pr.domain.Identifier;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.utils.ConfigurationProperty;
import edu.duke.cabig.c3pr.utils.Lov;
import edu.duke.cabig.c3pr.web.SearchCommand;
import edu.duke.cabig.c3pr.web.ajax.StudyAjaxFacade;
import gov.nih.nci.cabig.ctms.web.tabs.Flow;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchStudyController extends SimpleFormController {

    private static Log log = LogFactory.getLog(SearchStudyController.class);
    private ConfigurationProperty configurationProperty;
    private StudyDao studyDao;
    private StudyAjaxFacade studyAjaxFacade;


    @Override
    protected boolean isFormSubmission(HttpServletRequest request) {
        // TODO Auto-generated method stub
        if (request.getParameter("test") != null)
            return true;
        return super.isFormSubmission(request);
    }

    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object oCommand, BindException errors) throws Exception {
        SearchCommand searchStudyCommand = (SearchCommand) oCommand;
        Study study = new Study();
        String type = searchStudyCommand.getSearchType();
        String searchtext = searchStudyCommand.getSearchText().trim();

        log.debug("search string = " + searchtext + "; type = " + type);

        if ("status".equals(type))
            study.setStatus(searchtext);
        else if ("id".equals(type)) {
            Identifier id = new Identifier();
            id.setValue(searchtext);
            study.addIdentifier(id);
        } else if ("shortTitle".equals(type))
            study.setShortTitleText(searchtext);

        List<Study> studies = studyDao.searchByExample(study, true);
        log.debug("Search results size " + studies.size());
        Map<String, List<Lov>> configMap = configurationProperty.getMap();

        Map map = errors.getModel();
        map.put("studyResults", studies);
        map.put("searchTypeRefData", configMap.get("studySearchType"));
        if (isSubFlow(request)) {
            processSubFlow(request, map);
            map.put("actionReturnType", "SearchResults");
        }
        Object viewData = studyAjaxFacade.getTableForExport(map, request);
        request.setAttribute("studies", viewData);
        ModelAndView modelAndView = new ModelAndView(getSuccessView(), map);
        return modelAndView;
    }

    protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
        Map<String, Object> refdata = new HashMap<String, Object>();
        Map<String, List<Lov>> configMap = configurationProperty.getMap();
        refdata.put("searchTypeRefData", configMap.get("studySearchType"));
        if (isSubFlow(request)) {
            processSubFlow(request, refdata);
        }
        return refdata;
    }

    private boolean isSubFlow(HttpServletRequest request) {
        if (request.getParameter("inRegistration") != null || request.getParameter("subjectId") != null)
            return true;
        return false;
    }

    private void processSubFlow(HttpServletRequest request, Map map) {
        if (request.getParameter("subjectId") != null) {
            map.put("subjectId", request.getParameter("subjectId"));
//    		getAlternateDisplayOrder(request).getTab(2).setShowSummary("true");
        } else {
//    		getRegistrationFlow(request).getTab(1).setShowSummary("false");
        }
        map.put("registrationTab", getRegistrationFlow(request).getTab(1));
        map.put("inRegistration", "true");
    }

    private Flow getRegistrationFlow(HttpServletRequest request) {
        return (Flow) request.getSession().getAttribute("registrationFlow");
    }

    public ConfigurationProperty getConfigurationProperty() {
        return configurationProperty;
    }

    private Flow getAlternateDisplayOrder(HttpServletRequest request) {
        return (Flow) request.getSession().getAttribute("registrationAltFlow");
    }

    public void setConfigurationProperty(ConfigurationProperty configurationProperty) {
        this.configurationProperty = configurationProperty;
    }

    public StudyDao getStudyDao() {
        return studyDao;
    }

    public void setStudyDao(StudyDao studyDao) {
        this.studyDao = studyDao;
    }

    public StudyAjaxFacade getStudyAjaxFacade() {
        return studyAjaxFacade;
    }

    public void setStudyAjaxFacade(StudyAjaxFacade studyAjaxFacade) {
        this.studyAjaxFacade = studyAjaxFacade;
    }
}

@@ -44,366 +44,166 @@
 ort 
-edu.duke.cabig.c3pr.dao.StudyDao;%0Aimport edu.duke.cabig.c3pr.domain.Identifier;%0Aimport edu.duke.cabig.c3pr.domain.Study;%0Aimport edu.duke.cabig.c3pr.utils.ConfigurationProperty;%0Aimport edu.duke.cabig.c3pr.utils.Lov;%0Aimport edu.duke.cabig.c3pr.web.SearchCommand;%0Aimport edu.duke.cabig.c3pr.web.ajax.StudyAjaxFacade;%0Aimport gov.nih.nci.cabig.ctms.web.tabs.Flow;
+java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A
 %0Aimp
@@ -467,163 +467,320 @@
 ort 
-javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.util.HashMap;%0Aimport java
+edu.duke.cabig.c3pr.dao.StudyDao;%0Aimport edu.duke.cabig.c3pr.domain.Identifier;%0Aimport edu.duke.cabig.c3pr.domain.Study;%0Aimport edu.duke.cabig.c3pr.utils.ConfigurationProperty;%0Aimport edu.duke.cabig.c3pr
 .util
+s
 .L
-ist;%0Aimport java.util.Map
+ov;%0Aimport edu.duke.cabig.c3pr.web.SearchCommand;%0Aimport edu.duke.cabig.c3pr.web.ajax.StudyAjaxFacade
 ;%0A%0Ap
@@ -2463,152 +2463,8 @@
 ));%0A
-        if (isSubFlow(request)) %7B%0A            processSubFlow(request, map);%0A            map.put(%22actionReturnType%22, %22SearchResults%22);%0A        %7D%0A
     
@@ -3025,1246 +3025,129 @@
     
-if (isSubFlow(request)) %7B%0A            processSubFlow(request, refdata);%0A        %7D%0A        return refdata;%0A    %7D%0A%0A    private boolean isSubFlow(HttpServletRequest request) %7B%0A        if (request.getParameter(%22inRegistration%22) != null %7C%7C request.getParameter(%22subjectId%22) != null)%0A            return true;%0A        return false;%0A    %7D%0A%0A    private void processSubFlow(HttpServletRequest request, Map map) %7B%0A        if (request.getParameter(%22subjectId%22) != null) %7B%0A            map.put(%22subjectId%22, request.getParameter(%22subjectId%22));%0A//    %09%09getAlternateDisplayOrder(request).getTab(2).setShowSummary(%22true%22);%0A        %7D else %7B%0A//    %09%09getRegistrationFlow(request).getTab(1).setShowSummary(%22false%22);%0A        %7D%0A        map.put(%22registrationTab%22, getRegistrationFlow(request).getTab(1));%0A        map.put(%22inRegistration%22, %22true%22);%0A    %7D%0A%0A    private Flow getRegistrationFlow(HttpServletRequest request) %7B%0A        return (Flow) request.getSession().getAttribute(%22registrationFlow%22);%0A    %7D%0A%0A    public ConfigurationProperty getConfigurationProperty() %7B%0A        return configurationProperty;%0A    %7D%0A%0A    private Flow getAlternateDisplayOrder(HttpServletRequest request) %7B%0A        return (Flow) request.getSession().getAttribute(%22registrationAltFlow%22)
+return refdata;%0A    %7D%0A%0A    public ConfigurationProperty getConfigurationProperty() %7B%0A        return configurationProperty
 ;%0A  

clean up