private ItemDataBean copyItemDataBean(ItemDataBean src) {
    ItemDataBean result = new ItemDataBean();
    result.setEventCRFId(src.getEventCRFId());
    result.setItemId(src.getItemId());
    result.setValue(src.getValue());
    result.setOrdinal(src.getOrdinal());
    result.setSelected(src.isSelected());
    result.setAuditLog(src.isAuditLog());
    result.setCreatedDate(src.getCreatedDate());
    result.setUpdatedDate(src.getUpdatedDate());
    result.setOwner(src.getOwner());
    result.setOwnerId(src.getOwnerId());
    result.setUpdater(src.getUpdater());
    result.setUpdaterId(src.getUpdaterId());
    result.setStatus(src.getStatus());

    return result;
  }
  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);
      }
    }
  }
  @Override
  public void processRequest() throws Exception {

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

    String action = fp.getString("action");
    if (subjectId == 0 || StringUtil.isBlank(action)) {
      addPageMessage(respage.getString("please_choose_a_subject_to_remove"));
      forwardPage(Page.SUBJECT_LIST_SERVLET);
    } else {

      SubjectBean subject = (SubjectBean) sdao.findByPK(subjectId);

      // find all study subjects
      StudySubjectDAO ssdao = new StudySubjectDAO(sm.getDataSource());
      ArrayList studySubs = ssdao.findAllBySubjectId(subjectId);

      // find study events
      StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
      ArrayList events = sedao.findAllBySubjectId(subjectId);
      if ("confirm".equalsIgnoreCase(action)) {
        request.setAttribute("subjectToRemove", subject);
        request.setAttribute("studySubs", studySubs);
        request.setAttribute("events", events);
        forwardPage(Page.REMOVE_SUBJECT);
      } else {
        logger.info("submit to remove the subject");
        // change all statuses to deleted
        subject.setStatus(Status.DELETED);
        subject.setUpdater(ub);
        subject.setUpdatedDate(new Date());
        sdao.update(subject);

        // remove subject references from study
        for (int i = 0; i < studySubs.size(); i++) {
          StudySubjectBean studySub = (StudySubjectBean) studySubs.get(i);
          if (!studySub.getStatus().equals(Status.DELETED)) {
            studySub.setStatus(Status.AUTO_DELETED);
            studySub.setUpdater(ub);
            studySub.setUpdatedDate(new Date());
            ssdao.update(studySub);
          }
        }

        EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());

        for (int j = 0; j < events.size(); j++) {
          StudyEventBean event = (StudyEventBean) events.get(j);
          if (!event.getStatus().equals(Status.DELETED)) {
            event.setStatus(Status.AUTO_DELETED);
            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);
              if (!eventCRF.getStatus().equals(Status.DELETED)) {
                eventCRF.setStatus(Status.AUTO_DELETED);
                eventCRF.setUpdater(ub);
                eventCRF.setUpdatedDate(new Date());
                ecdao.update(eventCRF);
                // remove all the item data
                ArrayList itemDatas = iddao.findAllByEventCRFId(eventCRF.getId());
                for (int a = 0; a < itemDatas.size(); a++) {
                  ItemDataBean item = (ItemDataBean) itemDatas.get(a);
                  if (!item.getStatus().equals(Status.DELETED)) {
                    item.setStatus(Status.AUTO_DELETED);
                    item.setUpdater(ub);
                    item.setUpdatedDate(new Date());
                    iddao.update(item);
                  }
                }
              }
            }
          }
        }

        String emailBody =
            respage.getString("the_subject")
                + " "
                + respage.getString("has_been_removed_succesfully");

        addPageMessage(emailBody);
        //                sendEmail(emailBody);

        forwardPage(Page.SUBJECT_LIST_SERVLET);
      }
    }
  }
  @Override
  @SuppressWarnings(value = "unchecked")
  public void processRequest() throws Exception {
    ItemDataDAO itemDataDao = new ItemDataDAO(sm.getDataSource());
    EventCRFDAO eventCrfDao = new EventCRFDAO(sm.getDataSource());
    CrfBusinessLogicHelper crfBusinessLogicHelper = new CrfBusinessLogicHelper(sm.getDataSource());
    String action = request.getParameter("action");

    FormProcessor fp = new FormProcessor(request);

    // checks which module the requests are from
    String module = fp.getString(MODULE);
    request.setAttribute(MODULE, module);

    resetPanel();
    panel.setStudyInfoShown(false);
    panel.setOrderedData(true);

    setToPanel(resword.getString("create_CRF"), respage.getString("br_create_new_CRF_entering"));

    setToPanel(
        resword.getString("create_CRF_version"), respage.getString("br_create_new_CRF_uploading"));
    setToPanel(
        resword.getString("revise_CRF_version"), respage.getString("br_if_you_owner_CRF_version"));
    setToPanel(
        resword.getString("CRF_spreadsheet_template"),
        respage.getString("br_download_blank_CRF_spreadsheet_from"));
    setToPanel(
        resword.getString("example_CRF_br_spreadsheets"),
        respage.getString("br_download_example_CRF_instructions_from"));

    if ("confirm".equalsIgnoreCase(action)) {
      List<DisplayItemBeanWrapper> displayItemBeanWrappers =
          (List<DisplayItemBeanWrapper>) session.getAttribute("importedData");
      logger.info("Size of displayItemBeanWrappers : " + displayItemBeanWrappers.size());
      forwardPage(Page.VERIFY_IMPORT_CRF_DATA);
    }

    if ("save".equalsIgnoreCase(action)) {
      List<DisplayItemBeanWrapper> displayItemBeanWrappers =
          (List<DisplayItemBeanWrapper>) session.getAttribute("importedData");
      // System.out.println("Size of displayItemBeanWrappers : " + displayItemBeanWrappers.size());

      for (DisplayItemBeanWrapper wrapper : displayItemBeanWrappers) {

        int eventCrfBeanId = -1;
        EventCRFBean eventCrfBean = new EventCRFBean();

        // TODO : tom , the wrapper object has all the necessary data -
        // as you see we check the
        // is to see if this data is Savable if it is then we go ahead
        // and save it. if not we discard.
        // So the change needs to happen here , instead of discarding we
        // need to file discrepancy notes
        // and save the data. If you look in the
        // Page.VERIFY_IMPORT_CRF_DATA jsp file you can see how I am
        // pulling the errors. and use that in the same way.

        logger.info("right before we check to make sure it is savable: " + wrapper.isSavable());
        if (wrapper.isSavable()) {
          ArrayList<Integer> eventCrfInts = new ArrayList<Integer>();
          // based on the use case: "If any of the data does not meet
          // validations specified in the CRF
          // Template, a discrepancy note is automatically logged.
          // The DN will have a type of Failed Validation Check, and
          // a message of Failed Validation check."
          // System.out.println("wrapper problems found : " +
          // wrapper.getValidationErrors().toString());
          for (DisplayItemBean displayItemBean : wrapper.getDisplayItemBeans()) {
            eventCrfBeanId = displayItemBean.getData().getEventCRFId();
            eventCrfBean = (EventCRFBean) eventCrfDao.findByPK(eventCrfBeanId);
            logger.info("found value here: " + displayItemBean.getData().getValue());
            logger.info("found status here: " + eventCrfBean.getStatus().getName());
            // System.out.println("found event crf bean name here: "
            // +
            // eventCrfBean.getEventName()+" id "+eventCrfBean.getId
            // ());
            // SO, items can be created in a wrapper which is set to
            // overwrite
            // we get around this by checking the bean first, to
            // make sure it's not null
            ItemDataBean itemDataBean = new ItemDataBean();
            itemDataBean =
                itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(
                    displayItemBean.getItem().getId(),
                    eventCrfBean.getId(),
                    displayItemBean.getData().getOrdinal());
            if (wrapper.isOverwrite() && itemDataBean.getStatus() != null) {
              // ItemDataBean itemDataBean = new ItemDataBean();
              // itemDataBean =
              // itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(
              // displayItemBean.getItem().getId(),
              // eventCrfBean.getId(), displayItemBean
              // .getData().getOrdinal());
              // itemDataBean =
              // itemDataDao.findByEventCRFIdAndItemName(
              // eventCrfBean,
              // displayItemBean.getItem().getName());
              logger.info(
                  "just tried to find item data bean on item name "
                      + displayItemBean.getItem().getName());
              itemDataBean.setUpdatedDate(new Date());
              itemDataBean.setUpdater(ub);
              itemDataBean.setValue(displayItemBean.getData().getValue());
              // set status?
              itemDataDao.update(itemDataBean);
              logger.info("updated: " + itemDataBean.getItemId());
              // need to set pk here in order to create dn
              displayItemBean.getData().setId(itemDataBean.getId());
            } else {
              itemDataDao.create(displayItemBean.getData());
              logger.info(
                  "created: "
                      + displayItemBean.getData().getItemId()
                      + "event CRF ID = "
                      + eventCrfBean.getId()
                      + "CRF VERSION ID ="
                      + eventCrfBean.getCRFVersionId());

              // does this dao function work for repeating
              // events/groups?
              // ItemDataBean itemDataBean =
              // itemDataDao.findByEventCRFIdAndItemName(
              // eventCrfBean,
              // displayItemBean.getItem().getName());
              ItemDataBean itemDataBean2 =
                  itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(
                      displayItemBean.getItem().getId(),
                      eventCrfBean.getId(),
                      displayItemBean.getData().getOrdinal());
              logger.info(
                  "found: id " + itemDataBean2.getId() + " name " + itemDataBean2.getName());
              displayItemBean.getData().setId(itemDataBean2.getId());
            }
            // logger.info("created item data bean:
            // "+displayItemBean.getData().getId());
            // logger.info("created:
            // "+displayItemBean.getData().getName());
            // logger.info("continued:
            // "+displayItemBean.getData().getItemId());
            ItemDAO idao = new ItemDAO(sm.getDataSource());
            ItemBean ibean = (ItemBean) idao.findByPK(displayItemBean.getData().getItemId());
            // logger.info("continued2: getName " +
            // ibean.getName());
            // System.out.println("*** checking for validation errors: " + ibean.getName());
            String itemOid =
                displayItemBean.getItem().getOid()
                    + "_"
                    + wrapper.getStudyEventRepeatKey()
                    + "_"
                    + displayItemBean.getData().getOrdinal()
                    + "_"
                    + wrapper.getStudySubjectOid();
            if (wrapper.getValidationErrors().containsKey(itemOid)) {
              ArrayList messageList = (ArrayList) wrapper.getValidationErrors().get(itemOid);
              // if
              // (wrapper.getValidationErrors().containsKey(ibean
              // .getName())) {
              // ArrayList messageList = (ArrayList)
              // wrapper.getValidationErrors
              // ().get(ibean.getName());
              // could be more then one will have to iterate
              // could it be more than one? tbh 08/2008
              for (int iter = 0; iter < messageList.size(); iter++) {
                String message = (String) messageList.get(iter);
                DiscrepancyNoteBean parentDn =
                    ImportSpringJob.createDiscrepancyNote(
                        ibean,
                        message,
                        eventCrfBean,
                        displayItemBean,
                        null,
                        ub,
                        sm.getDataSource(),
                        currentStudy);
                ImportSpringJob.createDiscrepancyNote(
                    ibean,
                    message,
                    eventCrfBean,
                    displayItemBean,
                    parentDn.getId(),
                    ub,
                    sm.getDataSource(),
                    currentStudy);
                // System.out.println("*** created disc note with message: " + message);
                // displayItemBean);
              }
            }
            // logger.info("created:
            // "+displayItemBean.getDbData().getName());
            if (!eventCrfInts.contains(new Integer(eventCrfBean.getId()))) {
              crfBusinessLogicHelper.markCRFComplete(eventCrfBean, ub);
              // System.out.println("*** just updated event crf bean: " + eventCrfBean.getId());
              eventCrfInts.add(new Integer(eventCrfBean.getId()));
            }
          }
          // end of item datas, tbh
          // crfBusinessLogicHelper.markCRFComplete(eventCrfBean, ub);
          // System .out.println("*** just updated event crf bean: "+
          // eventCrfBean.getId());
          // need to update the study event status as well, tbh
          // crfBusinessLogicHelper.updateStudyEvent(eventCrfBean,
          // ub);
          // above should do it for us, tbh 08/2008
        }
      }

      addPageMessage(respage.getString("data_has_been_successfully_import"));
      // forwardPage(Page.SUBMIT_DATA_SERVLET);
      forwardPage(Page.LIST_STUDY_SUBJECTS_SERVLET);
      // replaced tbh, 06/2009
    }
  }
  @Override
  public void processRequest() throws Exception {

    CRFVersionDAO cvdao = new CRFVersionDAO(sm.getDataSource());
    FormProcessor fp = new FormProcessor(request);
    int versionId = fp.getInt("id", true);
    String module = fp.getString("module");
    request.setAttribute("module", module);

    String action = fp.getString("action");
    if (versionId == 0) {
      addPageMessage(respage.getString("please_choose_a_CRF_version_to_remove"));
      forwardPage(Page.CRF_LIST_SERVLET);
    } else {
      if (StringUtil.isBlank(action)) {
        addPageMessage(respage.getString("no_action_specified"));
        forwardPage(Page.CRF_LIST_SERVLET);
        return;
      }
      CRFVersionBean version = (CRFVersionBean) cvdao.findByPK(versionId);

      SectionDAO secdao = new SectionDAO(sm.getDataSource());

      EventCRFDAO evdao = new EventCRFDAO(sm.getDataSource());
      // find all event crfs by version id
      ArrayList eventCRFs = evdao.findUndeletedWithStudySubjectsByCRFVersion(versionId);
      if ("confirm".equalsIgnoreCase(action)) {
        request.setAttribute("versionToRemove", version);
        request.setAttribute("eventCRFs", eventCRFs);
        forwardPage(Page.REMOVE_CRF_VERSION);
      } else {
        logger.info("submit to remove the crf version");
        // version
        version.setStatus(Status.DELETED);
        version.setUpdater(ub);
        version.setUpdatedDate(new Date());
        cvdao.update(version);
        // added below tbh 092007, seems that we don't remove the event
        // crfs in the second pass
        for (int ii = 0; ii < eventCRFs.size(); ii++) {
          EventCRFBean ecbean = (EventCRFBean) eventCRFs.get(ii);
          ecbean.setStatus(Status.AUTO_DELETED);
          ecbean.setUpdater(ub);
          ecbean.setUpdatedDate(new Date());
          evdao.update(ecbean);
        }
        // added above tbh 092007, to fix task
        // all sections
        ArrayList sections = secdao.findAllByCRFVersionId(version.getId());
        for (int j = 0; j < sections.size(); j++) {
          SectionBean section = (SectionBean) sections.get(j);
          if (!section.getStatus().equals(Status.DELETED)) {
            section.setStatus(Status.AUTO_DELETED);
            section.setUpdater(ub);
            section.setUpdatedDate(new Date());
            secdao.update(section);
          }
        }

        // all item data related to event crfs
        ItemDataDAO idao = new ItemDataDAO(sm.getDataSource());
        for (int i = 0; i < eventCRFs.size(); i++) {
          EventCRFBean eventCRF = (EventCRFBean) eventCRFs.get(i);
          if (!eventCRF.getStatus().equals(Status.DELETED)) {
            eventCRF.setStatus(Status.AUTO_DELETED);
            eventCRF.setUpdater(ub);
            eventCRF.setUpdatedDate(new Date());
            evdao.update(eventCRF);

            ArrayList items = idao.findAllByEventCRFId(eventCRF.getId());
            for (int j = 0; j < items.size(); j++) {
              ItemDataBean item = (ItemDataBean) items.get(j);
              if (!item.getStatus().equals(Status.DELETED)) {
                item.setStatus(Status.AUTO_DELETED);
                item.setUpdater(ub);
                item.setUpdatedDate(new Date());
                idao.update(item);
              }
            }
          }
        }

        addPageMessage(
            respage.getString("the_CRF")
                + version.getName()
                + " "
                + respage.getString("has_been_removed_succesfully"));
        forwardPage(Page.CRF_LIST_SERVLET);
      }
    }
  }
  @Override
  public void processRequest() throws Exception {
    String studySubIdString = request.getParameter("id"); // studySubjectId
    String subIdString = request.getParameter("subjectId");
    String studyIdString = request.getParameter("studyId");

    SubjectDAO sdao = new SubjectDAO(sm.getDataSource());
    StudySubjectDAO subdao = new StudySubjectDAO(sm.getDataSource());

    if (StringUtil.isBlank(studySubIdString)
        || StringUtil.isBlank(subIdString)
        || StringUtil.isBlank(studyIdString)) {
      addPageMessage(respage.getString("please_choose_a_study_subject_to_remove"));
      forwardPage(Page.LIST_STUDY_SUBJECT_SERVLET);
    } else {
      int studyId = Integer.valueOf(studyIdString.trim()).intValue();
      int studySubId = Integer.valueOf(studySubIdString.trim()).intValue();
      int subjectId = Integer.valueOf(subIdString.trim()).intValue();

      SubjectBean subject = (SubjectBean) sdao.findByPK(subjectId);

      StudySubjectBean studySub = (StudySubjectBean) subdao.findByPK(studySubId);

      StudyDAO studydao = new StudyDAO(sm.getDataSource());
      StudyBean study = (StudyBean) studydao.findByPK(studyId);

      // find study events
      StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
      ArrayList events = sedao.findAllByStudyAndStudySubjectId(study, studySubId);

      String action = request.getParameter("action");
      if ("confirm".equalsIgnoreCase(action)) {
        if (!studySub.getStatus().equals(Status.AVAILABLE)) {
          addPageMessage(
              respage.getString("this_subject_is_not_available_for_this_study")
                  + " "
                  + respage.getString("please_contact_sysadmin_for_more_information"));
          forwardPage(Page.LIST_STUDY_SUBJECT_SERVLET);
          return;
        }

        request.setAttribute("subject", subject);
        request.setAttribute("study", study);
        request.setAttribute("studySub", studySub);
        request.setAttribute("events", events);

        forwardPage(Page.REMOVE_STUDY_SUBJECT);
      } else {
        logger.info("submit to remove the subject from study");
        // remove subject from study
        studySub.setStatus(Status.DELETED);
        studySub.setUpdater(ub);
        studySub.setUpdatedDate(new Date());
        subdao.update(studySub);

        // remove all study events
        // remove all event crfs
        EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());

        for (int j = 0; j < events.size(); j++) {
          StudyEventBean event = (StudyEventBean) events.get(j);
          if (!event.getStatus().equals(Status.DELETED)) {
            event.setStatus(Status.AUTO_DELETED);
            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);
              if (!eventCRF.getStatus().equals(Status.DELETED)) {
                eventCRF.setStatus(Status.AUTO_DELETED);
                eventCRF.setUpdater(ub);
                eventCRF.setUpdatedDate(new Date());
                ecdao.update(eventCRF);
                // remove all the item data
                ArrayList itemDatas = iddao.findAllByEventCRFId(eventCRF.getId());
                for (int a = 0; a < itemDatas.size(); a++) {
                  ItemDataBean item = (ItemDataBean) itemDatas.get(a);
                  if (!item.getStatus().equals(Status.DELETED)) {
                    item.setStatus(Status.AUTO_DELETED);
                    item.setUpdater(ub);
                    item.setUpdatedDate(new Date());
                    iddao.update(item);
                  }
                }
              }
            }
          }
        }

        String emailBody =
            respage.getString("the_subject")
                + " "
                + subject.getName()
                + " "
                + respage.getString("has_been_removed_from_the_study")
                + study.getName()
                + ".";

        addPageMessage(emailBody);
        sendEmail(emailBody);
        forwardPage(Page.LIST_STUDY_SUBJECT_SERVLET);
      }
    }
  }