/** getEntityFromHashMap, the method that gets the object from the database query. */
  public Object getEntityFromHashMap(HashMap hm) {
    DiscrepancyNoteBean eb = new DiscrepancyNoteBean();
    Date dateCreated = (Date) hm.get("date_created");
    Integer ownerId = (Integer) hm.get("owner_id");
    eb.setCreatedDate(dateCreated);
    eb.setOwnerId(ownerId.intValue());

    // discrepancy_note_id serial NOT NULL,
    // description varchar(255),
    // discrepancy_note_type_id numeric,
    // resolution_status_id numeric,

    // detailed_notes varchar(1000),
    // date_created date,
    // owner_id numeric,
    // parent_dn_id numeric,
    eb.setId(selectInt(hm, "discrepancy_note_id"));
    eb.setDescription((String) hm.get("description"));
    eb.setDiscrepancyNoteTypeId(((Integer) hm.get("discrepancy_note_type_id")).intValue());
    eb.setResolutionStatusId(((Integer) hm.get("resolution_status_id")).intValue());
    eb.setParentDnId(((Integer) hm.get("parent_dn_id")).intValue());
    eb.setDetailedNotes((String) hm.get("detailed_notes"));
    eb.setEntityType((String) hm.get("entity_type"));
    eb.setDisType(DiscrepancyNoteType.get(eb.getDiscrepancyNoteTypeId()));
    eb.setResStatus(ResolutionStatus.get(eb.getResolutionStatusId()));
    eb.setStudyId(selectInt(hm, "study_id"));
    return eb;
  }
  @Override
  protected void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);

    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(sm.getDataSource());
    int noteId = fp.getInt(NOTE_ID, true);

    DiscrepancyNoteBean note = (DiscrepancyNoteBean) dndao.findByPK(noteId);

    String entityType = note.getEntityType();
    UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());

    ArrayList notes = dndao.findAllEntityByPK(note.getEntityType(), noteId);

    Date lastUpdatedDate = note.getCreatedDate();
    UserAccountBean lastUpdator = (UserAccountBean) udao.findByPK(note.getOwnerId());

    for (int i = 0; i < notes.size(); i++) {
      DiscrepancyNoteBean n = (DiscrepancyNoteBean) notes.get(i);
      int pId = n.getParentDnId();
      if (pId == 0) {
        note = n;
        note.setLastUpdator((UserAccountBean) udao.findByPK(n.getOwnerId()));
        note.setLastDateUpdated(n.getCreatedDate());
        lastUpdatedDate = note.getLastDateUpdated();
        lastUpdator = note.getLastUpdator();
      }
    }

    for (int i = 0; i < notes.size(); i++) {
      DiscrepancyNoteBean n = (DiscrepancyNoteBean) notes.get(i);
      int pId = n.getParentDnId();
      if (pId > 0) {
        note.getChildren().add(n);
        if (!n.getCreatedDate().before(lastUpdatedDate)) {
          lastUpdatedDate = n.getCreatedDate();
          lastUpdator = (UserAccountBean) udao.findByPK(n.getOwnerId());
          note.setLastUpdator(lastUpdator);
          note.setLastDateUpdated(lastUpdatedDate);
          note.setResolutionStatusId(n.getResolutionStatusId());
          note.setResStatus(ResolutionStatus.get(n.getResolutionStatusId()));
        }
      }
    }
    note.setNumChildren(note.getChildren().size());
    note.setDisType(DiscrepancyNoteType.get(note.getDiscrepancyNoteTypeId()));
    logger.info("Just set Note: " + note.getCrfName() + " column " + note.getColumn());
    request.setAttribute(DIS_NOTE, note);
    forwardPage(Page.VIEW_SINGLE_NOTE);
  }
  /*
   * (non-Javadoc)
   *
   * @see org.akaza.openclinica.control.core.SecureController#processRequest()
   */
  @Override
  protected void processRequest() throws Exception {

    FormProcessor fp = new FormProcessor(request);
    int noteId = fp.getInt(INPUT_NOTE_ID);
    String module = (String) session.getAttribute("module");
    // Integer subjectId = (Integer) session.getAttribute("subjectId");

    StudySubjectDAO studySubjectDAO = new StudySubjectDAO(sm.getDataSource());

    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(sm.getDataSource());
    dndao.setFetchMapping(true);

    // check that the note exists
    DiscrepancyNoteBean discrepancyNoteBean = (DiscrepancyNoteBean) dndao.findByPK(noteId);

    if (!discrepancyNoteBean.isActive()) {
      throw new InconsistentStateException(
          Page.MANAGE_STUDY_SERVLET,
          resexception.getString("you_are_trying_resolve_discrepancy_not_exist"));
    }

    // check that the note has not already been closed
    ArrayList children = dndao.findAllByParent(discrepancyNoteBean);
    discrepancyNoteBean.setChildren(children);
    // This logic has been reverted, issue-7459
    //        if (parentNoteIsClosed(discrepancyNoteBean)) {
    //            throw new InconsistentStateException(Page.VIEW_DISCREPANCY_NOTES_IN_STUDY_SERVLET,
    // respage
    //                    .getString("the_discrepancy_choose_has_been_closed_resolved_create_new"));
    //        }

    // all clear, send the user to the resolved screen
    String entityType = discrepancyNoteBean.getEntityType().toLowerCase();
    discrepancyNoteBean.setResStatus(
        ResolutionStatus.get(discrepancyNoteBean.getResolutionStatusId()));
    discrepancyNoteBean.setDisType(
        DiscrepancyNoteType.get(discrepancyNoteBean.getDiscrepancyNoteTypeId()));
    // BWP 03/17/2009 3166: if it's not an ItemData type note, redirect
    // Monitors to View Subject or
    // View Study Events <<
    if (currentRole.getRole().equals(Role.MONITOR)
        && !"itemdata".equalsIgnoreCase(entityType)
        && !"eventcrf".equalsIgnoreCase(entityType)) {
      redirectMonitor(module, discrepancyNoteBean);
      return;
    }
    // >>
    // If Study is Frozen or Locked
    if (currentStudy.getStatus().isFrozen()
        && !"itemdata".equalsIgnoreCase(entityType)
        && !"eventcrf".equalsIgnoreCase(entityType)) {
      redirectMonitor(module, discrepancyNoteBean);
      return;
    }

    boolean toView = false;
    boolean isCompleted = false;
    if ("itemdata".equalsIgnoreCase(entityType)) {
      ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
      ItemDataBean idb = (ItemDataBean) iddao.findByPK(discrepancyNoteBean.getEntityId());

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

      EventCRFBean ecb = (EventCRFBean) ecdao.findByPK(idb.getEventCRFId());
      StudySubjectBean studySubjectBean =
          (StudySubjectBean) studySubjectDAO.findByPK(ecb.getStudySubjectId());

      discrepancyNoteBean.setSubjectId(studySubjectBean.getId());
      discrepancyNoteBean.setItemId(idb.getItemId());

      if (ecb.getStatus().equals(Status.UNAVAILABLE)) {
        isCompleted = true;
      }

      toView = true; // we want to go to view note page if the note is
      // for item data
    }
    // logger.info("set up pop up url: " + createNoteURL);
    // System.out.println("set up pop up url: " + createNoteURL);
    boolean goNext =
        prepareRequestForResolution(
            request, sm.getDataSource(), currentStudy, discrepancyNoteBean, isCompleted);

    Page p = getPageForForwarding(discrepancyNoteBean, isCompleted);

    // logger.info("found page for forwarding: " + p.getFileName());
    if (p == null) {
      throw new InconsistentStateException(
          Page.VIEW_DISCREPANCY_NOTES_IN_STUDY_SERVLET,
          resexception.getString("the_discrepancy_note_triying_resolve_has_invalid_type"));
    } else {
      if (p.getFileName().contains("?")) {
        if (!p.getFileName().contains("fromViewNotes=1")) {
          p.setFileName(p.getFileName() + "&fromViewNotes=1");
        }
      } else {
        p.setFileName(p.getFileName() + "?fromViewNotes=1");
      }
      String createNoteURL =
          CreateDiscrepancyNoteServlet.getAddChildURL(
              discrepancyNoteBean, ResolutionStatus.CLOSED, true);
      setPopUpURL(createNoteURL);
    }

    if (!goNext) {
      setPopUpURL("");
      addPageMessage(
          respage.getString("you_may_not_perform_admin_edit_on_CRF_not_completed_by_user"));
      p = Page.VIEW_DISCREPANCY_NOTES_IN_STUDY_SERVLET;
    }

    forwardPage(p);
  }