@RequestMapping(method = RequestMethod.GET)
  public ModelMap handleMainPage(HttpServletRequest request) {
    ModelMap map = new ModelMap();
    // setUpSidebar(request);
    StudyBean currentStudy = (StudyBean) request.getSession().getAttribute("study");

    eventDefinitionCRFDao = new EventDefinitionCRFDAO(dataSource);
    studyEventDefinitionDao = new StudyEventDefinitionDAO(dataSource);
    crfDao = new CRFDAO(dataSource);
    studyGroupClassDao = new StudyGroupClassDAO(dataSource);
    studyDao = new StudyDAO(dataSource);
    userDao = new UserAccountDAO(dataSource);
    ruleDao = new RuleDAO(dataSource);

    StudyModuleStatus sms = studyModuleStatusDao.findByStudyId(currentStudy.getId());
    if (sms == null) {
      sms = new StudyModuleStatus();
      sms.setStudyId(currentStudy.getId());
    }

    int crfCount = crfDao.findAllByStudy(currentStudy.getId()).size();
    int crfWithEventDefinition = crfDao.findAllActiveByDefinitions(currentStudy.getId()).size();
    int totalCrf = crfCount + crfWithEventDefinition;
    // int eventDefinitionCount = eventDefinitionCRFDao.findAllActiveByStudy(currentStudy).size();
    int eventDefinitionCount = studyEventDefinitionDao.findAllActiveByStudy(currentStudy).size();

    int subjectGroupCount = studyGroupClassDao.findAllActiveByStudy(currentStudy).size();

    List<RuleSetBean> ruleSets = ruleSetService.getRuleSetsByStudy(currentStudy);
    ruleSets = ruleSetService.filterByStatusEqualsAvailableOnlyRuleSetRules(ruleSets);
    int ruleCount = ruleSets != null ? ruleSets.size() : 0;

    int siteCount = studyDao.findOlnySiteIdsByStudy(currentStudy).size();
    int userCount = userDao.findAllUsersByStudy(currentStudy.getId()).size();
    Collection childStudies = studyDao.findAllByParent(currentStudy.getId());
    Map childStudyUserCount = new HashMap();
    for (Object sb : childStudies) {
      StudyBean childStudy = (StudyBean) sb;
      childStudyUserCount.put(
          childStudy.getName(), userDao.findAllUsersByStudy(childStudy.getId()).size());
    }

    if (sms.getCrf() == 0) {
      sms.setCrf(StudyModuleStatus.NOT_STARTED);
    }
    if (sms.getCrf() != 3 && totalCrf > 0) {
      sms.setCrf(StudyModuleStatus.IN_PROGRESS);
    }

    if (sms.getEventDefinition() == 0) {
      sms.setEventDefinition(StudyModuleStatus.NOT_STARTED);
    }
    if (sms.getEventDefinition() != 3 && eventDefinitionCount > 0) {
      sms.setEventDefinition(StudyModuleStatus.IN_PROGRESS);
    }

    if (sms.getSubjectGroup() == 0) {
      sms.setSubjectGroup(StudyModuleStatus.NOT_STARTED);
    }
    if (sms.getSubjectGroup() != 3 && subjectGroupCount > 0) {
      sms.setSubjectGroup(StudyModuleStatus.IN_PROGRESS);
    }

    if (sms.getRule() == 0) {
      sms.setRule(StudyModuleStatus.NOT_STARTED);
    }
    if (sms.getRule() != 3 && ruleCount > 0) {
      sms.setRule(StudyModuleStatus.IN_PROGRESS);
    }

    if (sms.getSite() == 0) {
      sms.setSite(StudyModuleStatus.NOT_STARTED);
    }
    if (sms.getSite() != 3 && siteCount > 0) {
      sms.setSite(StudyModuleStatus.IN_PROGRESS);
    }

    if (sms.getUsers() == 0) {
      sms.setUsers(StudyModuleStatus.NOT_STARTED);
    }
    if (sms.getUsers() != 3 && userCount > 0) {
      sms.setUsers(StudyModuleStatus.IN_PROGRESS);
    }

    map.addObject(sms);
    map.addAttribute("crfCount", totalCrf);
    map.addAttribute("eventDefinitionCount", eventDefinitionCount);
    map.addAttribute("subjectGroupCount", subjectGroupCount);
    map.addAttribute("ruleCount", ruleCount);
    map.addAttribute("siteCount", siteCount);
    map.addAttribute("userCount", userCount);
    map.addAttribute("childStudyUserCount", childStudyUserCount);
    map.addAttribute("studyId", currentStudy.getId());
    map.addAttribute("currentStudy", currentStudy);

    UserAccountBean userBean = (UserAccountBean) request.getSession().getAttribute("userBean");
    request.setAttribute("userBean", userBean);
    request.setAttribute("statusMap", Status.toStudyUpdateMembersList());

    ArrayList pageMessages = new ArrayList();
    if (request.getSession().getAttribute("pageMessages") != null) {
      pageMessages.addAll((ArrayList) request.getSession().getAttribute("pageMessages"));
      request.setAttribute("pageMessages", pageMessages);
      request.getSession().removeAttribute("pageMessages");
    }
    return map;
  }
  @Override
  public void processRequest() throws Exception {
    resetPanel();
    FormProcessor fp = new FormProcessor(request);
    Validator v = new Validator(request);
    int studyId = fp.getInt("id");
    studyId = studyId == 0 ? fp.getInt("studyId") : studyId;
    String action = fp.getString("action");
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    boolean isInterventional = false;

    study = (StudyBean) sdao.findByPK(studyId);
    if (study.getId() != currentStudy.getId()) {
      addPageMessage(
          respage.getString("not_current_study")
              + respage.getString("change_study_contact_sysadmin"));
      forwardPage(Page.MENU_SERVLET);
      return;
    }

    study.setId(studyId);
    StudyConfigService scs = new StudyConfigService(sm.getDataSource());
    study = scs.setParametersForStudy(study);
    request.setAttribute("studyToView", study);

    request.setAttribute("studyId", studyId + "");
    request.setAttribute("studyPhaseMap", CreateStudyServlet.studyPhaseMap);
    ArrayList statuses = Status.toStudyUpdateMembersList();
    statuses.add(Status.PENDING);
    request.setAttribute("statuses", statuses);

    String interventional = resadmin.getString("interventional");
    isInterventional = interventional.equalsIgnoreCase(study.getProtocolType());

    request.setAttribute("isInterventional", isInterventional ? "1" : "0");
    String protocolType = study.getProtocolTypeKey();

    // A. Hamid. 5001
    if (study.getParentStudyId() > 0) {
      StudyBean parentStudy = (StudyBean) sdao.findByPK(study.getParentStudyId());
      request.setAttribute("parentStudy", parentStudy);
    }

    ArrayList interventionArray = new ArrayList();
    if (isInterventional) {
      interventionArray = parseInterventions((study));
      setMaps(isInterventional, interventionArray);
    } else {
      setMaps(isInterventional, interventionArray);
    }

    if (!action.equals("submit")) {

      // First Load First Form
      if (study.getDatePlannedStart() != null) {
        fp.addPresetValue(INPUT_START_DATE, local_df.format(study.getDatePlannedStart()));
      }
      if (study.getDatePlannedEnd() != null) {
        fp.addPresetValue(INPUT_END_DATE, local_df.format(study.getDatePlannedEnd()));
      }
      if (study.getProtocolDateVerification() != null) {
        fp.addPresetValue(INPUT_VER_DATE, local_df.format(study.getProtocolDateVerification()));
      }
      setPresetValues(fp.getPresetValues());
      // first load 2nd form
    }
    if (study == null) {
      addPageMessage(respage.getString("please_choose_a_study_to_edit"));
      forwardPage(Page.STUDY_LIST_SERVLET);
      return;
    }
    if (action.equals("submit")) {

      validateStudy1(fp, v);
      validateStudy2(fp, v);
      validateStudy3(isInterventional, v, fp);
      validateStudy4(fp, v);
      validateStudy5(fp, v);
      validateStudy6(fp, v);
      confirmWholeStudy(fp, v);

      request.setAttribute("studyToView", study);
      if (!errors.isEmpty()) {
        System.out.println("found errors : " + errors.toString());
        request.setAttribute("formMessages", errors);

        forwardPage(Page.UPDATE_STUDY_NEW);
      } else {
        study.setProtocolType(protocolType);
        submitStudy(study);
        addPageMessage(respage.getString("the_study_has_been_updated_succesfully"));
        ArrayList pageMessages = (ArrayList) request.getAttribute(PAGE_MESSAGE);
        session.setAttribute("pageMessages", pageMessages);
        response.sendRedirect(request.getContextPath() + "/pages/studymodule");
        // forwardPage(Page.MANAGE_STUDY_MODULE);
      }
    } else {
      forwardPage(Page.UPDATE_STUDY_NEW);
    }
  }