/**
   * Organize objects in a certain way so that we can show to Users on UI. step1 : Get StudyEvent ,
   * eventCrf , crfVersion from studyEventId.
   *
   * @param crfViewSpecificOrderedObjects
   * @param ruleSet
   * @param rule
   * @return
   */
  private HashMap<RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>>
      populateForCrfBasedRulesView(
          HashMap<RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>>
              crfViewSpecificOrderedObjects,
          RuleSetBean ruleSet,
          RuleBean rule,
          String result,
          StudyBean currentStudy,
          List<RuleActionBean> actions) {

    // step1
    StudyEventBean studyEvent =
        (StudyEventBean)
            getStudyEventDao()
                .findByPK(
                    Integer.valueOf(
                        getExpressionService()
                            .getStudyEventDefenitionOrdninalCurated(
                                ruleSet.getTarget().getValue())));
    EventCRFBean eventCrf =
        (EventCRFBean)
            getEventCrfDao()
                .findAllByStudyEventAndCrfOrCrfVersionOid(
                    studyEvent, getExpressionService().getCrfOid(ruleSet.getTarget().getValue()))
                .get(0);
    CRFVersionBean crfVersion =
        (CRFVersionBean) getCrfVersionDao().findByPK(eventCrf.getCRFVersionId());

    RuleBulkExecuteContainer key =
        new RuleBulkExecuteContainer(crfVersion.getName(), rule, result, actions);
    String key2String =
        getExpressionService()
            .getCustomExpressionUsedToCreateView(
                ruleSet.getTarget().getValue(), studyEvent.getSampleOrdinal());
    String studyEventDefinitionName =
        getExpressionService()
            .getStudyEventDefinitionFromExpression(ruleSet.getTarget().getValue(), currentStudy)
            .getName();
    studyEventDefinitionName += " [" + studyEvent.getSampleOrdinal() + "]";
    // String itemGroupName =
    // getExpressionService().getItemGroupNameAndOrdinal(ruleSet.getTarget().getValue());
    // String itemName =
    // getExpressionService().getItemGroupExpression(ruleSet.getTarget().getValue()).getName();

    String itemGroupName =
        getExpressionService().getItemGroupNameAndOrdinal(ruleSet.getTarget().getValue());
    ItemGroupBean itemGroupBean =
        getExpressionService().getItemGroupExpression(ruleSet.getTarget().getValue());
    ItemBean itemBean =
        getExpressionService().getItemExpression(ruleSet.getTarget().getValue(), itemGroupBean);
    String itemName = itemBean.getName();

    RuleBulkExecuteContainerTwo key2 =
        new RuleBulkExecuteContainerTwo(
            key2String, studyEvent, studyEventDefinitionName, itemGroupName, itemName);
    StudySubjectBean studySubject =
        (StudySubjectBean) getStudySubjectDao().findByPK(studyEvent.getStudySubjectId());

    if (crfViewSpecificOrderedObjects.containsKey(key)) {
      HashMap<RuleBulkExecuteContainerTwo, Set<String>> k = crfViewSpecificOrderedObjects.get(key);
      if (k.containsKey(key2)) {
        k.get(key2).add(String.valueOf(studySubject.getLabel()));
      } else {
        HashSet<String> values = new HashSet<String>();
        values.add(String.valueOf(studySubject.getLabel()));
        k.put(key2, values);
      }
    } else {
      HashMap<RuleBulkExecuteContainerTwo, Set<String>> k =
          new HashMap<RuleBulkExecuteContainerTwo, Set<String>>();
      HashSet<String> values = new HashSet<String>();
      values.add(String.valueOf(studySubject.getLabel()));
      k.put(key2, values);
      crfViewSpecificOrderedObjects.put(key, k);
    }
    return crfViewSpecificOrderedObjects;
  }
  public boolean prepareRequestForResolution(
      HttpServletRequest request,
      DataSource ds,
      StudyBean currentStudy,
      DiscrepancyNoteBean note,
      boolean isCompleted) {
    String entityType = note.getEntityType().toLowerCase();
    int id = note.getEntityId();
    if ("subject".equalsIgnoreCase(entityType)) {
      StudySubjectDAO ssdao = new StudySubjectDAO(ds);
      StudySubjectBean ssb = ssdao.findBySubjectIdAndStudy(id, currentStudy);

      request.setAttribute("action", "show");
      request.setAttribute("id", String.valueOf(note.getEntityId()));
      request.setAttribute("studySubId", String.valueOf(ssb.getId()));
    } else if ("studysub".equalsIgnoreCase(entityType)) {
      request.setAttribute("action", "show");
      request.setAttribute("id", String.valueOf(note.getEntityId()));
    } else if ("eventcrf".equalsIgnoreCase(entityType)) {
      request.setAttribute("editInterview", "1");

      EventCRFDAO ecdao = new EventCRFDAO(ds);
      EventCRFBean ecb = (EventCRFBean) ecdao.findByPK(id);
      request.setAttribute(TableOfContentsServlet.INPUT_EVENT_CRF_BEAN, ecb);
      // If the request is passed along to ViewSectionDataEntryServlet,
      // that code needs
      // an event crf id; the (ecb.getId()+"") is necessary because
      // FormProcessor throws
      // a ClassCastException without the casting to a String
      request.setAttribute(ViewSectionDataEntryServlet.EVENT_CRF_ID, ecb.getId() + "");
    } else if ("studyevent".equalsIgnoreCase(entityType)) {
      StudyEventDAO sedao = new StudyEventDAO(ds);
      StudyEventBean seb = (StudyEventBean) sedao.findByPK(id);
      request.setAttribute(EnterDataForStudyEventServlet.INPUT_EVENT_ID, String.valueOf(id));
      request.setAttribute(UpdateStudyEventServlet.EVENT_ID, String.valueOf(id));
      request.setAttribute(
          UpdateStudyEventServlet.STUDY_SUBJECT_ID, String.valueOf(seb.getStudySubjectId()));
    }

    // this is for item data
    else if ("itemdata".equalsIgnoreCase(entityType)) {
      ItemDataDAO iddao = new ItemDataDAO(ds);
      ItemDataBean idb = (ItemDataBean) iddao.findByPK(id);

      EventCRFDAO ecdao = new EventCRFDAO(ds);

      EventCRFBean ecb = (EventCRFBean) ecdao.findByPK(idb.getEventCRFId());

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

      StudySubjectBean ssb = (StudySubjectBean) ssdao.findByPK(ecb.getStudySubjectId());

      ItemFormMetadataDAO ifmdao = new ItemFormMetadataDAO(ds);
      ItemFormMetadataBean ifmb =
          ifmdao.findByItemIdAndCRFVersionId(idb.getItemId(), ecb.getCRFVersionId());

      if (currentRole.getRole().equals(Role.MONITOR) || !isCompleted) {
        StudyEventDAO sedao = new StudyEventDAO(ds);
        StudyEventBean seb = (StudyEventBean) sedao.findByPK(id);
        request.setAttribute(EVENT_CRF_ID, String.valueOf(idb.getEventCRFId()));
        request.setAttribute(STUDY_SUB_ID, String.valueOf(seb.getStudySubjectId()));

      } else {
        request.setAttribute(
            DataEntryServlet.INPUT_EVENT_CRF_ID, String.valueOf(idb.getEventCRFId()));
        request.setAttribute(
            DataEntryServlet.INPUT_SECTION_ID, String.valueOf(ifmb.getSectionId()));
      }
      DataEntryStage stage = ecb.getStage();

      // if (!stage.equals(DataEntryStage.DOUBLE_DATA_ENTRY_COMPLETE)) {
      // return false;
      // }
    }

    return true;
  }