private void submitStudy() {
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    StudyParameterValueDAO spvdao = new StudyParameterValueDAO(sm.getDataSource());

    StudyBean study1 = (StudyBean) session.getAttribute("newStudy");
    logger.info("study bean to be updated:" + study1.getName());
    study1.setUpdatedDate(new Date());
    study1.setUpdater((UserAccountBean) session.getAttribute("userBean"));
    System.out.println("study's parentId=" + study1.getParentStudyId());
    sdao.update(study1);

    StudyParameterValueBean spv = new StudyParameterValueBean();

    spv.setStudyId(study1.getId());
    spv.setParameter("collectDob");
    spv.setValue(new Integer(study1.getStudyParameterConfig().getCollectDob()).toString());
    updateParameter(spvdao, spv);

    spv.setParameter("discrepancyManagement");
    spv.setValue(study1.getStudyParameterConfig().getDiscrepancyManagement());
    updateParameter(spvdao, spv);

    spv.setParameter("genderRequired");
    spv.setValue(study1.getStudyParameterConfig().getGenderRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("subjectPersonIdRequired");
    spv.setValue(study1.getStudyParameterConfig().getSubjectPersonIdRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewerNameRequired");
    spv.setValue(study1.getStudyParameterConfig().getInterviewerNameRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewerNameDefault");
    spv.setValue(study1.getStudyParameterConfig().getInterviewerNameDefault());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewerNameEditable");
    spv.setValue(study1.getStudyParameterConfig().getInterviewerNameEditable());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewDateRequired");
    spv.setValue(study1.getStudyParameterConfig().getInterviewDateRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewDateDefault");
    spv.setValue(study1.getStudyParameterConfig().getInterviewDateDefault());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewDateEditable");
    spv.setValue(study1.getStudyParameterConfig().getInterviewDateEditable());
    updateParameter(spvdao, spv);

    spv.setParameter("subjectIdGeneration");
    spv.setValue(study1.getStudyParameterConfig().getSubjectIdGeneration());
    updateParameter(spvdao, spv);

    spv.setParameter("subjectIdPrefixSuffix");
    spv.setValue(study1.getStudyParameterConfig().getSubjectIdPrefixSuffix());
    updateParameter(spvdao, spv);

    spv.setParameter("personIdShownOnCRF");
    spv.setValue(study1.getStudyParameterConfig().getPersonIdShownOnCRF());
    updateParameter(spvdao, spv);

    StudyBean curStudy = (StudyBean) session.getAttribute("study");
    if (curStudy != null && study1.getId() == curStudy.getId()) {
      super.currentStudy = study1;
      session.setAttribute("study", study1);
    }
    // update manage_pedigrees for all sites
    ArrayList children = (ArrayList) sdao.findAllByParent(study1.getId());
    for (int i = 0; i < children.size(); i++) {
      StudyBean child = (StudyBean) children.get(i);
      child.setType(study1.getType()); // same as parent's type
      child.setUpdatedDate(new Date());
      child.setUpdater(ub);
      sdao.update(child);
      // YW << update "collectDob" and "genderRequired" for sites
      StudyParameterValueBean childspv = new StudyParameterValueBean();
      childspv.setStudyId(child.getId());
      childspv.setParameter("collectDob");
      childspv.setValue(new Integer(study1.getStudyParameterConfig().getCollectDob()).toString());
      updateParameter(spvdao, childspv);
      childspv.setParameter("genderRequired");
      childspv.setValue(study1.getStudyParameterConfig().getGenderRequired());
      updateParameter(spvdao, childspv);
      // YW >>
    }

    session.removeAttribute("newStudy");
    session.removeAttribute("interventions");
  }
  public void processRequest() throws Exception {

    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    FormProcessor fp = new FormProcessor(request);
    int studyId = fp.getInt("id");

    StudyBean study = (StudyBean) sdao.findByPK(studyId);
    // find all sites
    ArrayList sites = (ArrayList) sdao.findAllByParent(studyId);

    // find all user and roles in the study, include ones in sites
    UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
    ArrayList userRoles = udao.findAllByStudyId(studyId);

    // find all subjects in the study, include ones in sites
    StudySubjectDAO ssdao = new StudySubjectDAO(sm.getDataSource());
    ArrayList subjects = ssdao.findAllByStudy(study);

    // find all events in the study, include ones in sites
    StudyEventDefinitionDAO sefdao = new StudyEventDefinitionDAO(sm.getDataSource());
    ArrayList definitions = sefdao.findAllByStudy(study);

    String action = request.getParameter("action");
    if (studyId == 0) {
      addPageMessage("Please choose a study to restore.");
      forwardPage(Page.STUDY_LIST_SERVLET);
    } else {
      if ("confirm".equalsIgnoreCase(action)) {
        request.setAttribute("studyToRestore", study);

        request.setAttribute("sitesToRestore", sites);

        request.setAttribute("userRolesToRestore", userRoles);

        request.setAttribute("subjectsToRestore", subjects);

        request.setAttribute("definitionsToRRestore", definitions);
        forwardPage(Page.RESTORE_STUDY);
      } else {
        logger.info("submit to restore the study");
        // change all statuses to unavailable
        StudyDAO studao = new StudyDAO(sm.getDataSource());
        study.setStatus(Status.AVAILABLE);
        study.setUpdater(ub);
        study.setUpdatedDate(new Date());
        studao.update(study);

        // remove all sites
        for (int i = 0; i < sites.size(); i++) {
          StudyBean site = (StudyBean) sites.get(i);
          site.setStatus(Status.AVAILABLE);
          site.setUpdater(ub);
          site.setUpdatedDate(new Date());
          sdao.update(site);
        }

        // remove all users and roles
        for (int i = 0; i < userRoles.size(); i++) {
          StudyUserRoleBean role = (StudyUserRoleBean) userRoles.get(i);
          role.setStatus(Status.AVAILABLE);
          role.setUpdater(ub);
          role.setUpdatedDate(new Date());
          udao.updateStudyUserRole(role, role.getUserName());
        }

        // remove all subjects
        for (int i = 0; i < subjects.size(); i++) {
          StudySubjectBean subject = (StudySubjectBean) subjects.get(i);
          subject.setStatus(Status.AVAILABLE);
          subject.setUpdater(ub);
          subject.setUpdatedDate(new Date());
          ssdao.update(subject);
        }

        // remove all study_group
        StudyGroupDAO sgdao = new StudyGroupDAO(sm.getDataSource());
        SubjectGroupMapDAO sgmdao = new SubjectGroupMapDAO(sm.getDataSource());
        ArrayList groups = sgdao.findAllByStudy(study);
        for (int i = 0; i < groups.size(); i++) {
          StudyGroupBean group = (StudyGroupBean) groups.get(i);
          group.setStatus(Status.AVAILABLE);
          group.setUpdater(ub);
          group.setUpdatedDate(new Date());
          sgdao.update(group);
          // all subject_group_map
          ArrayList subjectGroupMaps = sgmdao.findAllByStudyGroupId(group.getId());
          for (int j = 0; j < subjectGroupMaps.size(); j++) {
            SubjectGroupMapBean sgMap = (SubjectGroupMapBean) subjectGroupMaps.get(j);
            sgMap.setStatus(Status.AVAILABLE);
            sgMap.setUpdater(ub);
            sgMap.setUpdatedDate(new Date());
            sgmdao.update(sgMap);
          }
        }

        // remove all event definitions and event
        EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
        StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
        for (int i = 0; i < definitions.size(); i++) {
          StudyEventDefinitionBean definition = (StudyEventDefinitionBean) definitions.get(i);
          definition.setStatus(Status.AVAILABLE);
          definition.setUpdater(ub);
          definition.setUpdatedDate(new Date());
          sefdao.update(definition);
          ArrayList edcs = (ArrayList) edcdao.findAllByDefinition(definition.getId());
          for (int j = 0; j < edcs.size(); j++) {
            EventDefinitionCRFBean edc = (EventDefinitionCRFBean) edcs.get(j);
            edc.setStatus(Status.AVAILABLE);
            edc.setUpdater(ub);
            edc.setUpdatedDate(new Date());
            edcdao.update(edc);
          }

          ArrayList events = (ArrayList) sedao.findAllByDefinition(definition.getId());
          EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());

          for (int j = 0; j < events.size(); j++) {
            StudyEventBean event = (StudyEventBean) events.get(j);
            event.setStatus(Status.AVAILABLE);
            event.setUpdater(ub);
            event.setUpdatedDate(new Date());
            sedao.update(event);

            ArrayList eventCRFs = ecdao.findAllByStudyEvent(event);

            ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
            for (int k = 0; k < eventCRFs.size(); k++) {
              EventCRFBean eventCRF = (EventCRFBean) eventCRFs.get(k);
              eventCRF.setStatus(Status.AVAILABLE);
              eventCRF.setUpdater(ub);
              eventCRF.setUpdatedDate(new Date());
              ecdao.update(eventCRF);

              ArrayList itemDatas = iddao.findAllByEventCRFId(eventCRF.getId());
              for (int a = 0; a < itemDatas.size(); a++) {
                ItemDataBean item = (ItemDataBean) itemDatas.get(a);
                item.setStatus(Status.AVAILABLE);
                item.setUpdater(ub);
                item.setUpdatedDate(new Date());
                iddao.update(item);
              }
            }
          }
        } // for definitions

        DatasetDAO datadao = new DatasetDAO(sm.getDataSource());
        ArrayList dataset = datadao.findAllByStudyId(study.getId());
        for (int i = 0; i < dataset.size(); i++) {
          DatasetBean data = (DatasetBean) dataset.get(i);
          data.setStatus(Status.AVAILABLE);
          data.setUpdater(ub);
          data.setUpdatedDate(new Date());
          datadao.update(data);
        }

        addPageMessage("This study has been restored successfully.");
        forwardPage(Page.STUDY_LIST_SERVLET);
      }
    }
  }
  private void submitStudy(StudyBean newStudy) {
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    StudyParameterValueDAO spvdao = new StudyParameterValueDAO(sm.getDataSource());

    StudyBean study1 = newStudy;
    logger.info("study bean to be updated:" + study1.getName());
    study1.setUpdatedDate(new Date());
    study1.setUpdater((UserAccountBean) session.getAttribute("userBean"));
    sdao.update(study1);

    ArrayList siteList = (ArrayList) sdao.findAllByParent(newStudy.getId());
    if (siteList.size() > 0) {
      sdao.updateSitesStatus(study1);
    }

    StudyParameterValueBean spv = new StudyParameterValueBean();

    spv.setStudyId(study1.getId());
    spv.setParameter("collectDob");
    spv.setValue(new Integer(study1.getStudyParameterConfig().getCollectDob()).toString());
    updateParameter(spvdao, spv);

    spv.setParameter("discrepancyManagement");
    spv.setValue(study1.getStudyParameterConfig().getDiscrepancyManagement());
    updateParameter(spvdao, spv);

    spv.setParameter("genderRequired");
    spv.setValue(study1.getStudyParameterConfig().getGenderRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("subjectPersonIdRequired");
    spv.setValue(study1.getStudyParameterConfig().getSubjectPersonIdRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewerNameRequired");
    spv.setValue(study1.getStudyParameterConfig().getInterviewerNameRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewerNameDefault");
    spv.setValue(study1.getStudyParameterConfig().getInterviewerNameDefault());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewerNameEditable");
    spv.setValue(study1.getStudyParameterConfig().getInterviewerNameEditable());
    updateParameter(spvdao, spv);

    // BWP 1/12/2009 3169 Update interviewerNameEditable and
    // interviewDateEditable parameters for all sites>>
    List<StudyBean> sites = new ArrayList<StudyBean>();
    sites = (ArrayList) sdao.findAllByParent(newStudy.getId());
    if (sites != null && (!sites.isEmpty())) {
      updateInterviewerForSites(newStudy, sites, spvdao, "interviewerNameEditable");
    }
    // >>

    spv.setParameter("interviewDateRequired");
    spv.setValue(study1.getStudyParameterConfig().getInterviewDateRequired());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewDateDefault");
    spv.setValue(study1.getStudyParameterConfig().getInterviewDateDefault());
    updateParameter(spvdao, spv);

    spv.setParameter("interviewDateEditable");
    spv.setValue(study1.getStudyParameterConfig().getInterviewDateEditable());
    updateParameter(spvdao, spv);
    // BWP 1/12/2009 3169>>
    if (sites != null && (!sites.isEmpty())) {
      updateInterviewerForSites(newStudy, sites, spvdao, "interviewDateEditable");
    }
    // >>
    spv.setParameter("subjectIdGeneration");
    spv.setValue(study1.getStudyParameterConfig().getSubjectIdGeneration());
    updateParameter(spvdao, spv);

    spv.setParameter("subjectIdPrefixSuffix");
    spv.setValue(study1.getStudyParameterConfig().getSubjectIdPrefixSuffix());
    updateParameter(spvdao, spv);

    spv.setParameter("personIdShownOnCRF");
    spv.setValue(study1.getStudyParameterConfig().getPersonIdShownOnCRF());
    updateParameter(spvdao, spv);
    spv.setParameter("secondaryLabelViewable");
    spv.setValue(study1.getStudyParameterConfig().getSecondaryLabelViewable());
    updateParameter(spvdao, spv);

    // tbh, 06/04/2009 3684>>
    spv.setParameter("adminForcedReasonForChange");
    spv.setValue(study1.getStudyParameterConfig().getAdminForcedReasonForChange());
    updateParameter(spvdao, spv);
    // >>

    StudyBean curStudy = (StudyBean) session.getAttribute("study");
    if (curStudy != null && study1.getId() == curStudy.getId()) {
      super.currentStudy = study1;

      session.setAttribute("study", study1);
    }
    // update manage_pedigrees for all sites
    ArrayList children = (ArrayList) sdao.findAllByParent(study1.getId());
    for (int i = 0; i < children.size(); i++) {
      StudyBean child = (StudyBean) children.get(i);
      child.setType(study1.getType()); // same as parent's type
      child.setUpdatedDate(new Date());
      child.setUpdater(ub);
      sdao.update(child);
      // YW << update "collectDob" and "genderRequired" for sites
      StudyParameterValueBean childspv = new StudyParameterValueBean();
      childspv.setStudyId(child.getId());
      childspv.setParameter("collectDob");
      childspv.setValue(new Integer(study1.getStudyParameterConfig().getCollectDob()).toString());
      updateParameter(spvdao, childspv);
      childspv.setParameter("genderRequired");
      childspv.setValue(study1.getStudyParameterConfig().getGenderRequired());
      updateParameter(spvdao, childspv);
    }
  }