public EligibilityConsentResponse eligibilityConsentMetadata()

in study-datastore/src/main/java/com/hphc/mystudies/dao/StudyMetaDataDao.java [386:776]


  public EligibilityConsentResponse eligibilityConsentMetadata(String studyId) throws DAOException {
    LOGGER.entry("begin eligibilityConsentMetadata()");
    Session session = null;
    EligibilityConsentResponse eligibilityConsentResponse = new EligibilityConsentResponse();
    EligibilityDto eligibilityDto = null;
    ConsentDto consentDto = null;
    List<ConsentInfoDto> consentInfoDtoList = null;
    List<ComprehensionTestQuestionDto> comprehensionQuestionList = null;
    List<ConsentMasterInfoDto> consentMasterInfoList = null;
    ConsentDetailsBean consent = new ConsentDetailsBean();
    StudySequenceDto studySequenceDto = null;
    StudyDto studyDto = null;
    StudyVersionDto studyVersionDto = null;
    List<EligibilityTestDto> eligibilityTestList = null;
    try {
      session = sessionFactory.openSession();

      studyDto =
          (StudyDto)
              session
                  .getNamedQuery("getLiveStudyIdByCustomStudyId")
                  .setString(StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(), studyId)
                  .uniqueResult();
      if (studyDto != null) {
        studyVersionDto =
            (StudyVersionDto)
                session
                    .getNamedQuery("getLiveVersionDetailsByCustomStudyIdAndVersion")
                    .setString(
                        StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(), studyDto.getCustomStudyId())
                    .setFloat(StudyMetaDataEnum.QF_STUDY_VERSION.value(), studyDto.getVersion())
                    .setMaxResults(1)
                    .uniqueResult();

        studySequenceDto =
            (StudySequenceDto)
                session
                    .getNamedQuery("getStudySequenceDetailsByStudyId")
                    .setString(StudyMetaDataEnum.QF_STUDY_ID.value(), studyDto.getId())
                    .uniqueResult();
        if (studySequenceDto != null) {

          if (studySequenceDto
              .getEligibility()
              .equalsIgnoreCase(StudyMetaDataConstants.STUDY_SEQUENCE_Y)) {

            eligibilityDto =
                (EligibilityDto)
                    session
                        .getNamedQuery("eligibilityDtoByStudyId")
                        .setString(StudyMetaDataEnum.QF_STUDY_ID.value(), studyDto.getId())
                        .uniqueResult();
            if (eligibilityDto != null) {

              EligibilityBean eligibility = new EligibilityBean();
              if (null != eligibilityDto.getEligibilityMechanism()) {
                switch (eligibilityDto.getEligibilityMechanism()) {
                  case 1:
                    eligibility.setType(StudyMetaDataConstants.TYPE_TOKEN);
                    break;
                  case 2:
                    eligibility.setType(StudyMetaDataConstants.TYPE_BOTH);
                    break;
                  case 3:
                    eligibility.setType(StudyMetaDataConstants.TYPE_TEST);
                    break;
                  default:
                    eligibility.setType("");
                    break;
                }
              }
              eligibility.setTokenTitle(
                  StringUtils.isEmpty(eligibilityDto.getInstructionalText())
                      ? ""
                      : eligibilityDto.getInstructionalText());

              eligibilityTestList =
                  session
                      .createQuery(
                          "from EligibilityTestDto ETDTO"
                              + " where ETDTO.eligibilityId=:eligId"
                              + " and ETDTO.status=true and ETDTO.active=true"
                              + " ORDER BY ETDTO.sequenceNo")
                      .setString("eligId", eligibilityDto.getId())
                      .list();
              if ((eligibilityTestList != null) && !eligibilityTestList.isEmpty()) {
                List<QuestionnaireActivityStepsBean> test = new ArrayList<>();

                List<HashMap<String, Object>> correctAnswers = new ArrayList<>();
                for (EligibilityTestDto eligibilityTest : eligibilityTestList) {
                  QuestionnaireActivityStepsBean questionStep =
                      new QuestionnaireActivityStepsBean();
                  questionStep.setType(StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_QUESTION);
                  questionStep.setResultType(StudyMetaDataConstants.QUESTION_BOOLEAN);
                  questionStep.setKey(eligibilityTest.getShortTitle());
                  questionStep.setTitle(eligibilityTest.getQuestion());
                  questionStep.setText(StudyMetaDataConstants.ELIGIBILITY_TEXT);
                  questionStep.setSkippable(false);
                  questionStep.setGroupName("");
                  questionStep.setRepeatable(false);
                  questionStep.setRepeatableText("");
                  questionStep.setHealthDataKey("");
                  test.add(questionStep);

                  if (eligibilityTest.getResponseYesOption()) {
                    HashMap<String, Object> correctAnsHashMap = new HashMap<>();
                    correctAnsHashMap.put("key", eligibilityTest.getShortTitle());
                    correctAnsHashMap.put("answer", true);
                    correctAnswers.add(correctAnsHashMap);
                  }

                  if (eligibilityTest.getResponseNoOption()) {
                    HashMap<String, Object> correctAnsHashMap = new HashMap<>();
                    correctAnsHashMap.put("key", eligibilityTest.getShortTitle());
                    correctAnsHashMap.put("answer", false);
                    correctAnswers.add(correctAnsHashMap);
                  }
                }
                eligibility.setTest(test);
                eligibility.setCorrectAnswers(correctAnswers);
              }
              eligibilityConsentResponse.setEligibility(eligibility);
            }
          }

          consentDto =
              (ConsentDto)
                  session
                      .getNamedQuery("consentDetailsByCustomStudyIdAndVersion")
                      .setString(
                          StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(),
                          studyVersionDto.getCustomStudyId())
                      .setFloat(
                          StudyMetaDataEnum.QF_VERSION.value(), studyVersionDto.getConsentVersion())
                      .uniqueResult();
          if (null != consentDto) {
            consent.setVersion(
                consentDto.getVersion() == null
                    ? StudyMetaDataConstants.STUDY_DEFAULT_VERSION
                    : consentDto.getVersion().toString());

            SharingBean sharingBean = new SharingBean();

            /** check whether share data permission is yes or no */
            if (StringUtils.isNotEmpty(consentDto.getShareDataPermissions())
                && consentDto
                    .getShareDataPermissions()
                    .equalsIgnoreCase(StudyMetaDataConstants.YES)) {
              sharingBean.setTitle(
                  StringUtils.isEmpty(consentDto.getTitle()) ? "" : consentDto.getTitle());
              sharingBean.setText(
                  StringUtils.isEmpty(consentDto.getTaglineDescription())
                      ? ""
                      : consentDto.getTaglineDescription());
              sharingBean.setLearnMore(
                  StringUtils.isEmpty(consentDto.getLearnMoreText())
                      ? ""
                      : consentDto.getLearnMoreText());
              sharingBean.setLongDesc(
                  StringUtils.isEmpty(consentDto.getLongDescription())
                      ? ""
                      : consentDto.getLongDescription());
              sharingBean.setShortDesc(
                  StringUtils.isEmpty(consentDto.getShortDescription())
                      ? ""
                      : consentDto.getShortDescription());
              if ((consentDto.getAllowWithoutPermission() != null)
                  && StudyMetaDataConstants.YES.equalsIgnoreCase(
                      consentDto.getAllowWithoutPermission())) {
                sharingBean.setAllowWithoutSharing(true);
              }
            }
            consent.setSharing(sharingBean);
          }

          consentMasterInfoList = session.createQuery("from ConsentMasterInfoDto CMIDTO").list();

          if (studySequenceDto
              .getConsentEduInfo()
              .equalsIgnoreCase(StudyMetaDataConstants.STUDY_SEQUENCE_Y)) {

            consentInfoDtoList =
                session
                    .getNamedQuery("consentInfoDetailsByCustomStudyIdAndVersion")
                    .setString(
                        StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(),
                        studyVersionDto.getCustomStudyId())
                    .setFloat(
                        StudyMetaDataEnum.QF_VERSION.value(), studyVersionDto.getConsentVersion())
                    .list();
            if ((null != consentInfoDtoList) && !consentInfoDtoList.isEmpty()) {

              List<ConsentBean> consentBeanList = new ArrayList<>();
              for (ConsentInfoDto consentInfoDto : consentInfoDtoList) {

                ConsentBean consentBean = new ConsentBean();
                consentBean.setText(
                    StringUtils.isEmpty(consentInfoDto.getBriefSummary())
                        ? ""
                        : consentInfoDto
                            .getBriefSummary()
                            .replaceAll("&#34;", "\"")
                            .replaceAll("&#39;", "'"));
                consentBean.setTitle(
                    StringUtils.isEmpty(consentInfoDto.getDisplayTitle())
                        ? ""
                        : consentInfoDto
                            .getDisplayTitle()
                            .replaceAll("&#34;", "\"")
                            .replaceAll("&#39;", "'"));
                if (consentInfoDto.getConsentItemTitleId() != null
                    && !consentInfoDto.getConsentItemTitleId().isEmpty()) {
                  if ((consentMasterInfoList != null) && !consentMasterInfoList.isEmpty()) {
                    for (ConsentMasterInfoDto masterInfo : consentMasterInfoList) {
                      if (masterInfo.getId().equals(consentInfoDto.getConsentItemTitleId())) {
                        consentBean.setType(masterInfo.getCode());
                        break;
                      }
                    }
                  }
                } else {
                  consentBean.setType(StudyMetaDataConstants.CONSENT_TYPE_CUSTOM.toLowerCase());
                }
                consentBean.setDescription("");
                consentBean.setHtml(
                    StringUtils.isEmpty(consentInfoDto.getElaborated())
                        ? ""
                        : StringEscapeUtils.escapeHtml4(
                            consentInfoDto
                                .getElaborated()
                                .replaceAll("&#34;", "'")
                                .replaceAll("em>", "i>")
                                .replaceAll(
                                    "<a", "<a style='text-decoration:underline;color:blue;'")));
                consentBean.setUrl(
                    StringUtils.isEmpty(consentInfoDto.getUrl()) ? "" : consentInfoDto.getUrl());

                if (StringUtils.isNotEmpty(consentInfoDto.getVisualStep())
                    && consentInfoDto
                        .getVisualStep()
                        .equalsIgnoreCase(StudyMetaDataConstants.YES)) {
                  consentBean.setVisualStep(true);
                } else {
                  consentBean.setVisualStep(false);
                }
                consentBeanList.add(consentBean);
              }
              consent.setVisualScreens(consentBeanList);
            }
          }

          if (studySequenceDto
                  .getComprehensionTest()
                  .equalsIgnoreCase(StudyMetaDataConstants.STUDY_SEQUENCE_Y)
              && ((consentDto != null)
                  && (consentDto.getNeedComprehensionTest() != null)
                  && consentDto
                      .getNeedComprehensionTest()
                      .equalsIgnoreCase(StudyMetaDataConstants.YES))) {

            comprehensionQuestionList =
                session
                    .getNamedQuery("comprehensionQuestionByStudyId")
                    .setString(StudyMetaDataEnum.QF_STUDY_ID.value(), studyDto.getId())
                    .list();
            if ((null != comprehensionQuestionList) && !comprehensionQuestionList.isEmpty()) {
              ComprehensionDetailsBean comprehensionDetailsBean = new ComprehensionDetailsBean();
              if ((consentDto != null) && (consentDto.getComprehensionTestMinimumScore() != null)) {
                comprehensionDetailsBean.setPassScore(
                    consentDto.getComprehensionTestMinimumScore());
              } else {
                comprehensionDetailsBean.setPassScore(0);
              }

              List<QuestionnaireActivityStepsBean> comprehensionList = new ArrayList<>();
              List<CorrectAnswersBean> correctAnswerBeanList = new ArrayList<>();
              for (ComprehensionTestQuestionDto comprehensionQuestionDto :
                  comprehensionQuestionList) {
                QuestionnaireActivityStepsBean questionStep = new QuestionnaireActivityStepsBean();
                questionStep.setType(StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_QUESTION);
                questionStep.setResultType(StudyMetaDataConstants.QUESTION_TEXT_CHOICE);
                questionStep.setKey(comprehensionQuestionDto.getId().toString());
                questionStep.setTitle(comprehensionQuestionDto.getQuestionText());
                questionStep.setText(StudyMetaDataConstants.COMPREHENSION_TEXT);
                questionStep.setSkippable(false);
                questionStep.setGroupName("");
                questionStep.setRepeatable(false);
                questionStep.setRepeatableText("");
                questionStep.setHealthDataKey("");

                List<ComprehensionTestResponseDto> comprehensionTestResponseList =
                    session
                        .getNamedQuery("comprehensionQuestionResponseByCTID")
                        .setString("comprehensionTestQuestionId", comprehensionQuestionDto.getId())
                        .list();
                if ((comprehensionTestResponseList != null)
                    && !comprehensionTestResponseList.isEmpty()) {

                  CorrectAnswersBean correctAnswerBean = new CorrectAnswersBean();
                  Map<String, Object> questionFormat = new LinkedHashMap<>();
                  List<LinkedHashMap<String, Object>> textChoiceMapList = new ArrayList<>();
                  StringBuilder sb = new StringBuilder();

                  for (ComprehensionTestResponseDto compResp : comprehensionTestResponseList) {
                    if (compResp.getCorrectAnswer()) {
                      sb.append(
                          StringUtils.isEmpty(sb)
                              ? compResp.getResponseOption().trim()
                              : "&@##@&" + compResp.getResponseOption().trim());
                    }
                    LinkedHashMap<String, Object> textChoiceMap = new LinkedHashMap<>();
                    textChoiceMap.put(
                        "text",
                        StringUtils.isEmpty(compResp.getResponseOption().trim())
                            ? ""
                            : compResp.getResponseOption().trim());
                    textChoiceMap.put(
                        "value",
                        StringUtils.isEmpty(compResp.getResponseOption().trim())
                            ? ""
                            : compResp.getResponseOption().trim());
                    textChoiceMap.put("detail", "");
                    textChoiceMap.put("exclusive", false);
                    textChoiceMapList.add(textChoiceMap);
                  }

                  questionFormat.put("textChoices", textChoiceMapList);

                  if (comprehensionQuestionDto.getStructureOfCorrectAns()) {
                    questionFormat.put("selectionStyle", "Multiple");
                  } else {
                    questionFormat.put("selectionStyle", "Single");
                  }

                  questionStep.setFormat(questionFormat);
                  if (StringUtils.isNotEmpty(sb.toString())) {
                    correctAnswerBean.setAnswer(sb.toString().split("&@##@&"));
                  }

                  correctAnswerBean.setKey(comprehensionQuestionDto.getId().toString());
                  correctAnswerBean.setEvaluation(
                      comprehensionQuestionDto.getStructureOfCorrectAns()
                          ? StudyMetaDataConstants.COMPREHENSION_RESPONSE_STRUCTURE_ALL
                          : StudyMetaDataConstants.COMPREHENSION_RESPONSE_STRUCTURE_ANY);
                  correctAnswerBeanList.add(correctAnswerBean);
                }

                comprehensionList.add(questionStep);
              }
              comprehensionDetailsBean.setQuestions(comprehensionList);
              comprehensionDetailsBean.setCorrectAnswers(correctAnswerBeanList);
              consent.setComprehension(comprehensionDetailsBean);
            }
          }

          if (consentDto != null) {
            ReviewBean reviewBean = new ReviewBean();
            if (consentDto
                .getConsentDocType()
                .equals(StudyMetaDataConstants.CONSENT_DOC_TYPE_NEW)) {
              reviewBean.setReviewHTML(
                  StringUtils.isEmpty(consentDto.getConsentDocContent())
                      ? ""
                      : StringEscapeUtils.escapeHtml4(
                          StringEscapeUtils.unescapeHtml4(
                              consentDto
                                  .getConsentDocContent()
                                  .replaceAll("&#34;", "'")
                                  .replaceAll("em>", "i>")
                                  .replaceAll(
                                      "<a", "<a style='text-decoration:underline;color:blue;'"))));
            }
            consent.setReview(reviewBean);
          }
          eligibilityConsentResponse.setConsent(consent);

          eligibilityConsentResponse.setMessage(StudyMetaDataConstants.SUCCESS);
        }
      } else {
        eligibilityConsentResponse.setMessage(StudyMetaDataConstants.INVALID_STUDY_ID);
      }
    } catch (Exception e) {
      LOGGER.error("StudyMetaDataDao - eligibilityConsentMetadata() :: ERROR", e);
    } finally {
      if (session != null) {
        session.close();
      }
    }
    LOGGER.exit("eligibilityConsentMetadata() :: Ends");
    return eligibilityConsentResponse;
  }