/**
   * Redirect the request to another page if the user is a Monitor type and the discrepancy note is
   * a type other than item data or event crf.
   *
   * @param module A String like "managestudy" or "admin"
   * @param discrepancyNoteBean
   */
  private void redirectMonitor(String module, DiscrepancyNoteBean discrepancyNoteBean) {

    if (discrepancyNoteBean != null) {

      String createNoteURL = "";
      // This String will determine whether the type is other than
      // itemdata.
      String entityType = discrepancyNoteBean.getEntityType().toLowerCase();
      // The id of the subject, study subject, or study event
      int entityId = discrepancyNoteBean.getEntityId();
      RequestDispatcher dispatcher = null;
      DiscrepancyNoteUtil discNoteUtil = new DiscrepancyNoteUtil();

      if (entityType != null
          && !"".equalsIgnoreCase(entityType)
          && !"itemdata".equalsIgnoreCase(entityType)
          && !"eventcrf".equalsIgnoreCase(entityType)) {
        // redirect to View Study Subject
        // addPageMessage(resword.getString("monitors_do_not_have_permission_to_resolve_discrepancy_notes"));
        if ("studySub".equalsIgnoreCase(entityType)) {
          dispatcher =
              request.getRequestDispatcher(
                  "/ViewStudySubject?id=" + entityId + "&module=" + module);
          discrepancyNoteBean.setSubjectId(entityId);
        } else if ("subject".equalsIgnoreCase(entityType)) {

          int studySubId =
              discNoteUtil.getStudySubjectIdForDiscNote(
                  discrepancyNoteBean, sm.getDataSource(), currentStudy.getId());

          dispatcher =
              request.getRequestDispatcher(
                  "/ViewStudySubject?id=" + studySubId + "&module=" + module);
          discrepancyNoteBean.setSubjectId(studySubId);
        } else if ("studyevent".equalsIgnoreCase(entityType)) {
          dispatcher = request.getRequestDispatcher("/EnterDataForStudyEvent?eventId=" + entityId);
        }

        // This code creates the URL for a popup window, which the
        // processing Servlet will initiate.
        // 'true' parameter means that ViewDiscrepancyNote is the
        // handling Servlet.
        createNoteURL =
            CreateDiscrepancyNoteServlet.getAddChildURL(
                discrepancyNoteBean, ResolutionStatus.CLOSED, true);
        request.setAttribute(POP_UP_URL, createNoteURL);

        try {
          if (dispatcher != null) {
            dispatcher.forward(request, response);
          }
        } catch (ServletException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  /*
   * (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);
  }