public ArrayList findAllEventCRFByStudy(StudyBean study) {
    this.setTypesExpected();
    ArrayList alist = new ArrayList();
    this.setTypeExpected(11, TypeNames.STRING); // ss.label
    this.setTypeExpected(12, TypeNames.DATE); // date_start
    this.setTypeExpected(13, TypeNames.STRING); // sed_name
    this.setTypeExpected(14, TypeNames.STRING); // crf_name
    this.setTypeExpected(15, TypeNames.STRING); // column_name
    this.setTypeExpected(16, TypeNames.INT); // event_crf_id

    HashMap variables = new HashMap();
    variables.put(new Integer(1), new Integer(study.getId()));
    variables.put(new Integer(2), new Integer(study.getId()));
    alist = this.select(digester.getQuery("findAllEventCRFByStudy"), variables);

    ArrayList al = new ArrayList();
    Iterator it = alist.iterator();
    while (it.hasNext()) {
      HashMap hm = (HashMap) it.next();
      DiscrepancyNoteBean eb = (DiscrepancyNoteBean) this.getEntityFromHashMap(hm);
      eb.setEventName((String) hm.get("sed_name"));
      eb.setEventStart((Date) hm.get("date_start"));
      eb.setCrfName((String) hm.get("crf_name"));
      eb.setSubjectName((String) hm.get("label"));
      eb.setColumn((String) hm.get("column_name"));
      eb.setEntityId(((Integer) hm.get("event_crf_id")).intValue());
      al.add(eb);
    }
    return al;
  }
  private void createDiscrepancyNoteBean(
      String description,
      String detailedNotes,
      int itemDataId,
      StudyBean studyBean,
      UserAccountBean ub,
      DiscrepancyNoteBean parentDiscrepancyNote) {
    DiscrepancyNoteBean dnb = new DiscrepancyNoteBean();
    dnb.setEntityId(itemDataId); // this is needed for DN Map object
    dnb.setStudyId(studyBean.getId());
    dnb.setEntityType(DiscrepancyNoteBean.ITEM_DATA);
    dnb.setDescription(description);
    dnb.setDetailedNotes(detailedNotes);
    dnb.setDiscrepancyNoteTypeId(
        parentDiscrepancyNote.getDiscrepancyNoteTypeId()); // set to parent DN Type Id
    dnb.setResolutionStatusId(4); // set to closed
    dnb.setColumn("value"); // this is needed for DN Map object
    dnb.setAssignedUserId(ub.getId());
    dnb.setOwner(ub);
    dnb.setParentDnId(parentDiscrepancyNote.getId());
    dnb.setActivated(false);
    dnb = (DiscrepancyNoteBean) getDnDao().create(dnb); // create child DN
    getDnDao().createMapping(dnb); // create DN mapping

    DiscrepancyNoteBean itemParentNote =
        (DiscrepancyNoteBean) getDnDao().findByPK(dnb.getParentDnId());
    itemParentNote.setResolutionStatusId(ResolutionStatus.CLOSED.getId());
    itemParentNote.setAssignedUserId(ub.getId());
    getDnDao().update(itemParentNote); // update parent DN
    getDnDao().updateAssignedUser(itemParentNote); // update parent DN assigned user
  }
  private DiscrepancyNoteBean getMappingFromHashMap(HashMap hm, DiscrepancyNoteBean note) {
    String entityType = note.getEntityType();
    String entityIDColumn = getEntityIDColumn(entityType);

    if (!entityIDColumn.equals("")) {
      note.setEntityId(selectInt(hm, entityIDColumn));
    }
    note.setColumn(selectString(hm, "column_name"));
    return note;
  }
  public static DiscrepancyNoteBean createDiscrepancyNote(
      ItemBean itemBean,
      String message,
      EventCRFBean eventCrfBean,
      DisplayItemBean displayItemBean,
      Integer parentId,
      UserAccountBean uab,
      DataSource ds,
      StudyBean study) {
    // DisplayItemBean displayItemBean) {
    DiscrepancyNoteBean note = new DiscrepancyNoteBean();
    StudySubjectDAO ssdao = new StudySubjectDAO(ds);
    note.setDescription(message);
    note.setDetailedNotes("Failed Validation Check");
    note.setOwner(uab);
    note.setCreatedDate(new Date());
    note.setResolutionStatusId(ResolutionStatus.OPEN.getId());
    note.setDiscrepancyNoteTypeId(DiscrepancyNoteType.FAILEDVAL.getId());
    if (parentId != null) {
      note.setParentDnId(parentId);
    }

    note.setField(itemBean.getName());
    note.setStudyId(study.getId());
    note.setEntityName(itemBean.getName());
    note.setEntityType("ItemData");
    note.setEntityValue(displayItemBean.getData().getValue());

    note.setEventName(eventCrfBean.getName());
    note.setEventStart(eventCrfBean.getCreatedDate());
    note.setCrfName(displayItemBean.getEventDefinitionCRF().getCrfName());

    StudySubjectBean ss = (StudySubjectBean) ssdao.findByPK(eventCrfBean.getStudySubjectId());
    note.setSubjectName(ss.getName());

    note.setEntityId(displayItemBean.getData().getId());
    note.setColumn("value");

    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(ds);
    note = (DiscrepancyNoteBean) dndao.create(note);
    // so that the below method works, need to set the entity above
    // System.out.println("trying to create mapping with " + note.getId() + " " + note.getEntityId()
    // + " " + note.getColumn() + " " + note.getEntityType());
    dndao.createMapping(note);
    // System.out.println("just created mapping");
    return note;
  }
  public ArrayList findAllEntityByPK(String entityName, int noteId) {
    this.setTypesExpected();
    ArrayList alist = new ArrayList();
    this.setTypeExpected(11, TypeNames.STRING); // ss.label

    HashMap variables = new HashMap();
    variables.put(new Integer(1), new Integer(noteId));
    variables.put(new Integer(2), new Integer(noteId));
    if ("subject".equalsIgnoreCase(entityName)) {
      this.setTypeExpected(12, TypeNames.STRING); // column_name
      alist = this.select(digester.getQuery("findAllSubjectByPK"), variables);
    } else if ("studySub".equalsIgnoreCase(entityName)) {
      this.setTypeExpected(12, TypeNames.STRING); // column_name
      alist = this.select(digester.getQuery("findAllStudySubjectByPK"), variables);
    } else if ("eventCrf".equalsIgnoreCase(entityName)) {
      this.setTypeExpected(12, TypeNames.DATE); // date_start
      this.setTypeExpected(13, TypeNames.STRING); // sed_name
      this.setTypeExpected(14, TypeNames.STRING); // crf_name
      this.setTypeExpected(15, TypeNames.STRING); // column_name
      alist = this.select(digester.getQuery("findAllEventCRFByPK"), variables);
    } else if ("studyEvent".equalsIgnoreCase(entityName)) {
      this.setTypeExpected(12, TypeNames.DATE); // date_start
      this.setTypeExpected(13, TypeNames.STRING); // sed_name
      this.setTypeExpected(14, TypeNames.STRING); // column_name
      alist = this.select(digester.getQuery("findAllStudyEventByPK"), variables);
    } else if ("itemData".equalsIgnoreCase(entityName)) {
      this.setTypeExpected(12, TypeNames.DATE); // date_start
      this.setTypeExpected(13, TypeNames.STRING); // sed_name
      this.setTypeExpected(14, TypeNames.STRING); // crf_name
      this.setTypeExpected(15, TypeNames.STRING); // item_name
      this.setTypeExpected(16, TypeNames.STRING); // value
      // YW <<
      this.setTypeExpected(17, TypeNames.INT); // item_data_id
      this.setTypeExpected(18, TypeNames.INT); // item_id
      // YW >>
      alist = this.select(digester.getQuery("findAllItemDataByPK"), variables);
    }

    ArrayList al = new ArrayList();
    Iterator it = alist.iterator();
    while (it.hasNext()) {
      HashMap hm = (HashMap) it.next();
      DiscrepancyNoteBean eb = (DiscrepancyNoteBean) this.getEntityFromHashMap(hm);
      if ("subject".equalsIgnoreCase(entityName) || "studySub".equalsIgnoreCase(entityName)) {
        eb.setSubjectName((String) hm.get("label"));
        eb.setColumn((String) hm.get("column_name"));
      } else if ("eventCrf".equalsIgnoreCase(entityName)) {
        eb.setSubjectName((String) hm.get("label"));
        eb.setEventName((String) hm.get("sed_name"));
        eb.setEventStart((Date) hm.get("date_start"));
        eb.setCrfName((String) hm.get("crf_name"));
        eb.setColumn((String) hm.get("column_name"));
      } else if ("itemData".equalsIgnoreCase(entityName)) {
        eb.setSubjectName((String) hm.get("label"));
        eb.setEventName((String) hm.get("sed_name"));
        eb.setEventStart((Date) hm.get("date_start"));
        eb.setCrfName((String) hm.get("crf_name"));
        eb.setEntityName((String) hm.get("item_name"));
        eb.setEntityValue((String) hm.get("value"));
        // YW <<
        eb.setEntityId(((Integer) hm.get("item_data_id")).intValue());
        eb.setItemId(((Integer) hm.get("item_id")).intValue());
        // YW >>

      } else if ("studyEvent".equalsIgnoreCase(entityName)) {
        eb.setSubjectName((String) hm.get("label"));
        eb.setEventName((String) hm.get("sed_name"));
        eb.setEventStart((Date) hm.get("date_start"));
        eb.setColumn((String) hm.get("column_name"));
      }
      if (fetchMapping) {
        eb = findSingleMapping(eb);
      }
      al.add(eb);
    }
    return al;
  }