public void processRequest() throws Exception {
    String action = request.getParameter("action");
    FormProcessor fp = new FormProcessor(request);
    int classId = fp.getInt("id");

    if (classId == 0) {

      addPageMessage("Please choose a subject group class to remove.");
      forwardPage(Page.SUBJECT_GROUP_CLASS_LIST_SERVLET);
    } else {
      StudyGroupClassDAO sgcdao = new StudyGroupClassDAO(sm.getDataSource());
      StudyGroupDAO sgdao = new StudyGroupDAO(sm.getDataSource());
      SubjectGroupMapDAO sgmdao = new SubjectGroupMapDAO(sm.getDataSource());

      if (action.equalsIgnoreCase("confirm")) {
        StudyGroupClassBean sgcb = (StudyGroupClassBean) sgcdao.findByPK(classId);
        if (sgcb.getStatus().equals(Status.DELETED)) {
          addPageMessage("This subject group class has been deleted already.");
          forwardPage(Page.SUBJECT_GROUP_CLASS_LIST_SERVLET);
          return;
        }
        ArrayList groups = sgdao.findAllByGroupClass(sgcb);

        for (int i = 0; i < groups.size(); i++) {
          StudyGroupBean sg = (StudyGroupBean) groups.get(i);
          ArrayList subjectMaps = sgmdao.findAllByStudyGroupClassAndGroup(sgcb.getId(), sg.getId());
          sg.setSubjectMaps(subjectMaps);
        }

        session.setAttribute("group", sgcb);
        session.setAttribute("studyGroups", groups);
        forwardPage(Page.REMOVE_SUBJECT_GROUP_CLASS);

      } else if (action.equalsIgnoreCase("submit")) {
        StudyGroupClassBean group = (StudyGroupClassBean) session.getAttribute("group");
        group.setStatus(Status.DELETED);
        group.setUpdater(ub);
        sgcdao.update(group);

        ArrayList subjectMaps = sgmdao.findAllByStudyGroupClassId(group.getId());
        for (int i = 0; i < subjectMaps.size(); i++) {
          SubjectGroupMapBean sgmb = (SubjectGroupMapBean) subjectMaps.get(i);
          sgmb.setStatus(Status.DELETED);
          sgmb.setUpdater(ub);
          sgmdao.update(sgmb);
        }
        addPageMessage("This subject group class was removed successfully.");
        forwardPage(Page.SUBJECT_GROUP_CLASS_LIST_SERVLET);
      } else {
        addPageMessage("No action specified.");
        forwardPage(Page.SUBJECT_GROUP_CLASS_LIST_SERVLET);
      }
    }
  }
 public ArrayList findAllByStudyGroupClassAndGroup(int studyGroupClassId, int studyGroupId) {
   setTypesExpected();
   this.setTypeExpected(11, TypeNames.STRING);
   HashMap variables = new HashMap();
   variables.put(new Integer(1), new Integer(studyGroupClassId));
   variables.put(new Integer(2), new Integer(studyGroupId));
   ArrayList alist = this.select(digester.getQuery("findAllByStudyGroupClassAndGroup"), variables);
   ArrayList al = new ArrayList();
   Iterator it = alist.iterator();
   while (it.hasNext()) {
     HashMap hm = (HashMap) it.next();
     SubjectGroupMapBean eb = (SubjectGroupMapBean) this.getEntityFromHashMap(hm);
     eb.setSubjectLabel(((String) hm.get("label")));
     al.add(eb);
   }
   return al;
 }
 public SubjectGroupMapBean findAllByStudySubjectAndStudyGroupClass(
     int studySubjectId, int studyGroupClassId) {
   setTypesExpected();
   this.setTypeExpected(11, TypeNames.STRING);
   this.setTypeExpected(12, TypeNames.STRING);
   HashMap variables = new HashMap();
   variables.put(new Integer(1), new Integer(studySubjectId));
   variables.put(new Integer(2), new Integer(studyGroupClassId));
   ArrayList alist =
       this.select(digester.getQuery("findByStudySubjectAndStudyGroupClass"), variables);
   SubjectGroupMapBean eb = null;
   Iterator it = alist.iterator();
   while (it.hasNext()) {
     HashMap hm = (HashMap) it.next();
     eb = (SubjectGroupMapBean) this.getEntityFromHashMap(hm);
     eb.setStudyGroupName(((String) hm.get("group_name")));
     eb.setGroupClassName(((String) hm.get("class_name")));
   }
   return eb;
 }
  /**
   * <b>update </b>, the method that returns an updated subject bean after it updates the database.
   *
   * @return sb, an updated study bean.
   */
  public EntityBean update(EntityBean eb) {
    SubjectGroupMapBean sb = (SubjectGroupMapBean) eb;
    HashMap variables = new HashMap();
    // UPDATE SUBJECT_GROUP_MAP SET STUDY_GROUP_CLASS_ID=?,
    // STUDY_SUBJECT_ID=?,STUDY_GROUP_ID=?,
    // STATUS_ID=?,DATE_UPDATED=?, UPDATE_ID=? , notes = ?
    // WHERE SUBJECT_GROUP_MAP_ID=?
    variables.put(new Integer(1), new Integer(sb.getStudyGroupClassId()));
    variables.put(new Integer(2), new Integer(sb.getStudySubjectId()));
    variables.put(new Integer(3), new Integer(sb.getStudyGroupId()));
    variables.put(new Integer(4), new Integer(sb.getStatus().getId()));

    variables.put(new Integer(5), new java.util.Date());
    variables.put(new Integer(6), new Integer(sb.getUpdater().getId()));
    variables.put(new Integer(8), new Integer(sb.getId()));
    variables.put(new Integer(7), sb.getNotes());

    String sql = digester.getQuery("update");
    this.execute(sql, variables);

    return sb;
  }
  /** Creates a new subject */
  public EntityBean create(EntityBean eb) {
    SubjectGroupMapBean sb = (SubjectGroupMapBean) eb;
    HashMap variables = new HashMap();
    // INSERT INTO SUBJECT_GROUP_MAP (study_group_class_id,
    // study_subject_id, study_group_id,
    // status_id, owner_id,date_created,
    // notes) VALUES (?,?,?,?,?,NOW(),?)
    variables.put(new Integer(1), new Integer(sb.getStudyGroupClassId()));
    variables.put(new Integer(2), new Integer(sb.getStudySubjectId()));
    variables.put(new Integer(3), new Integer(sb.getStudyGroupId()));
    variables.put(new Integer(4), new Integer(sb.getStatus().getId()));
    variables.put(new Integer(5), new Integer(sb.getOwner().getId()));
    variables.put(new Integer(6), sb.getNotes());
    // DATE_CREATED is now()

    this.execute(digester.getQuery("create"), variables);

    return sb;
  }
  /** getEntityFromHashMap, the method that gets the object from the database query. */
  public Object getEntityFromHashMap(HashMap hm) {
    SubjectGroupMapBean eb = new SubjectGroupMapBean();
    super.setEntityAuditInformation(eb, hm);
    // subject_group_map_id serial NOT NULL,
    // study_group_class_id numeric,
    // study_subject_id numeric,
    // study_group_id numeric,
    // status_id numeric,
    // owner_id numeric,
    // date_created date,
    // date_updated date,
    // update_id numeric,
    // notes varchar(255),
    eb.setId(((Integer) hm.get("subject_group_map_id")).intValue());
    eb.setStudyGroupId(((Integer) hm.get("study_group_id")).intValue());
    eb.setStudySubjectId(((Integer) hm.get("study_subject_id")).intValue());
    eb.setStudyGroupClassId(((Integer) hm.get("study_group_class_id")).intValue());
    eb.setNotes((String) hm.get("notes"));

    return eb;
  }
  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);
      }
    }
  }