public StudyInfoResponse studyInfo()

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


  public StudyInfoResponse studyInfo(String studyId) throws DAOException {
    LOGGER.entry("begin studyInfo()");
    Session session = null;
    StudyInfoResponse studyInfoResponse = new StudyInfoResponse();
    List<StudyPageDto> studyPageDtoList = null;
    StudyDto studyDto = null;
    try {
      session = sessionFactory.openSession();
      studyDto =
          (StudyDto)
              session
                  .getNamedQuery("getLiveStudyIdByCustomStudyId")
                  .setString(StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(), studyId)
                  .uniqueResult();
      if (studyDto == null) {
        studyDto =
            (StudyDto)
                session
                    .getNamedQuery("getPublishedStudyByCustomId")
                    .setString(StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(), studyId)
                    .uniqueResult();
      }

      if (studyDto != null) {

        studyInfoResponse.setStudyWebsite(
            StringUtils.isEmpty(studyDto.getStudyWebsite()) ? "" : studyDto.getStudyWebsite());

        List<InfoBean> infoList = new ArrayList<>();
        studyPageDtoList =
            session
                .getNamedQuery("studyPageDetailsByStudyId")
                .setString(StudyMetaDataEnum.QF_STUDY_ID.value(), studyDto.getId())
                .list();
        if ((null != studyPageDtoList) && !studyPageDtoList.isEmpty()) {
          for (StudyPageDto studyPageInfo : studyPageDtoList) {
            InfoBean info = new InfoBean();

            if (infoList.isEmpty()) {
              info.setType(StudyMetaDataConstants.TYPE_VIDEO);
              info.setVideoLink(
                  StringUtils.isEmpty(studyDto.getMediaLink()) ? "" : studyDto.getMediaLink());
            } else {
              info.setType(StudyMetaDataConstants.TYPE_TEXT);
              info.setVideoLink("");
            }

            info.setTitle(
                StringUtils.isEmpty(studyPageInfo.getTitle()) ? "" : studyPageInfo.getTitle());
            info.setImage(
                StringUtils.isEmpty(studyPageInfo.getImagePath())
                    ? ""
                    : StudyMetaDataUtil.getResources(
                        propMap.get("cloud.bucket.name"),
                        StudyMetaDataConstants.STUDIES
                            + "/"
                            + studyDto.getCustomStudyId()
                            + "/"
                            + propMap.get(StudyMetaDataConstants.FDA_SMD_STUDY_PAGE_PATH).trim()
                            + studyPageInfo.getImagePath(),
                        StudyMetaDataConstants.DATA_IMAGE));
            info.setText(
                StringUtils.isEmpty(studyPageInfo.getDescription())
                    ? ""
                    : studyPageInfo.getDescription());
            infoList.add(info);
          }
        } else {

          InfoBean info = new InfoBean();

          if (infoList.isEmpty()) {
            info.setType(StudyMetaDataConstants.TYPE_VIDEO);
            info.setVideoLink(
                StringUtils.isEmpty(studyDto.getMediaLink()) ? "" : studyDto.getMediaLink());
          } else {
            info.setType(StudyMetaDataConstants.TYPE_TEXT);
            info.setVideoLink("");
          }

          info.setTitle(StringUtils.isEmpty(studyDto.getName()) ? "" : studyDto.getName());
          info.setImage(
              StringUtils.isEmpty(studyDto.getThumbnailImage())
                  ? ""
                  : StudyMetaDataUtil.getResources(
                      propMap.get("cloud.bucket.name"),
                      StudyMetaDataConstants.STUDIES
                          + "/"
                          + studyDto.getCustomStudyId()
                          + "/"
                          + propMap.get(StudyMetaDataConstants.FDA_SMD_STUDY_THUMBNAIL_PATH).trim()
                          + studyDto.getThumbnailImage(),
                      StudyMetaDataConstants.DATA_IMAGE));
          info.setText(StringUtils.isEmpty(studyDto.getFullName()) ? "" : studyDto.getFullName());
          infoList.add(info);
        }
        studyInfoResponse.setInfo(infoList);

        if (!studyDto
            .getStatus()
            .equalsIgnoreCase(StudyMetaDataConstants.STUDY_STATUS_PRE_PUBLISH)) {
          List<QuestionnairesDto> questionnairesList =
              session
                  .createQuery(
                      "from QuestionnairesDto QDTO"
                          + " where QDTO.customStudyId=:custStudyId"
                          + " and QDTO.active=true"
                          + " and QDTO.status=true and QDTO.live=1")
                  .setString("custStudyId", studyDto.getCustomStudyId())
                  .list();
          if ((questionnairesList != null) && !questionnairesList.isEmpty()) {

            List<String> questionnaireIdsList = new ArrayList<>();
            Map<String, QuestionnairesDto> questionnaireMap = new TreeMap<>();
            Map<String, QuestionnairesStepsDto> stepsMap = new TreeMap<>();
            Map<String, QuestionsDto> questionsMap = null;
            Map<String, FormMappingDto> formMappingMap = new TreeMap<>();

            for (QuestionnairesDto questionnaire : questionnairesList) {
              questionnaireIdsList.add(questionnaire.getId());
              questionnaireMap.put(questionnaire.getId(), questionnaire);
            }

            if (!questionnaireIdsList.isEmpty()) {

              List<String> questionIdsList = new ArrayList<>();
              List<String> formIdsList = new ArrayList<>();
              List<QuestionnairesStepsDto> questionnairesStepsList =
                  session
                      .createQuery(
                          "from QuestionnairesStepsDto QSDTO"
                              + " where QSDTO.active=true and QSDTO.status=true"
                              + " and QSDTO.questionnairesId in (:questionnaireIdsList)"
                              + " and QSDTO.stepType in (:questType,:questForm)")
                      .setParameterList("questionnaireIdsList", questionnaireIdsList)
                      .setString(
                          "questType", StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_QUESTION)
                      .setString("questForm", StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_FORM)
                      .list();
              if ((questionnairesStepsList != null) && !questionnairesStepsList.isEmpty()) {

                for (QuestionnairesStepsDto stepsDto : questionnairesStepsList) {
                  if (stepsDto
                      .getStepType()
                      .equalsIgnoreCase(StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_QUESTION)) {
                    questionIdsList.add(stepsDto.getInstructionFormId());
                    stepsMap.put(
                        stepsDto.getInstructionFormId()
                            + StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_QUESTION,
                        stepsDto);
                  } else {
                    formIdsList.add(stepsDto.getInstructionFormId());
                    stepsMap.put(
                        stepsDto.getInstructionFormId()
                            + StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_FORM,
                        stepsDto);
                  }
                }

                if (!questionIdsList.isEmpty()) {
                  List<QuestionsDto> questionnsList =
                      session
                          .createQuery(
                              "from QuestionsDto QDTO"
                                  + " where QDTO.active=true and QDTO.status=true"
                                  + " and QDTO.id in (:questionIdsList)"
                                  + " and QDTO.responseType=10 and QDTO.useAnchorDate=true")
                          .setMaxResults(1)
                          .setParameterList("questionIdsList", questionIdsList)
                          .list();
                  if ((questionnsList != null) && !questionnsList.isEmpty()) {

                    questionsMap = new TreeMap<>();
                    for (QuestionsDto question : questionnsList) {
                      questionsMap.put(question.getId(), question);
                    }
                  }
                }

                if ((questionsMap == null) && !formIdsList.isEmpty()) {

                  List<String> formQuestionsList = new ArrayList<>();
                  List<FormMappingDto> formMappingList =
                      session
                          .createQuery(
                              "from FormMappingDto FMDTO"
                                  + " where FMDTO.formId in (select FDTO.formId"
                                  + " from FormDto FDTO"
                                  + " where FDTO.formId in (:formIdsList)"
                                  + " and FDTO.active=true) and FMDTO.active=true"
                                  + " ORDER BY FMDTO.formId, FMDTO.sequenceNo")
                          .setParameterList("formIdsList", formIdsList)
                          .list();
                  if ((formMappingList != null) && !formMappingList.isEmpty()) {

                    for (FormMappingDto formMapping : formMappingList) {
                      formQuestionsList.add(formMapping.getQuestionId());
                      formMappingMap.put(formMapping.getQuestionId(), formMapping);
                    }

                    if (!formQuestionsList.isEmpty()) {
                      List<QuestionsDto> questionnsList =
                          session
                              .createQuery(
                                  "from QuestionsDto QDTO"
                                      + " where QDTO.active=true and QDTO.status=true"
                                      + " and QDTO.id in (:formQuestionsList)"
                                      + " and QDTO.responseType=10 and QDTO.useAnchorDate=true")
                              .setMaxResults(1)
                              .setParameterList("formQuestionsList", formQuestionsList)
                              .list();
                      if ((questionnsList != null) && !questionnsList.isEmpty()) {

                        questionsMap = new TreeMap<>();
                        for (QuestionsDto question : questionnsList) {
                          questionsMap.put(question.getId(), question);
                        }
                      }
                    }
                  }
                }

                if (questionsMap != null) {
                  AnchorDateBean anchorDate = new AnchorDateBean();
                  anchorDate.setType(StudyMetaDataConstants.ANCHORDATE_TYPE_QUESTION);
                  for (Map.Entry<String, QuestionsDto> map : questionsMap.entrySet()) {
                    QuestionsDto questionDto = map.getValue();
                    if (questionDto != null) {
                      QuestionnairesStepsDto questionnairesSteps;

                      if (StringUtils.isNotEmpty(questionDto.getShortTitle())) {
                        FormMappingDto formMapping = formMappingMap.get(questionDto.getId());
                        questionnairesSteps =
                            stepsMap.get(
                                formMapping.getFormId()
                                    + StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_FORM);
                      } else {
                        questionnairesSteps =
                            stepsMap.get(
                                questionDto.getId()
                                    + StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_QUESTION);
                      }

                      if (questionnairesSteps != null) {
                        QuestionnairesDto questionnairesDto =
                            questionnaireMap.get(questionnairesSteps.getQuestionnairesId());

                        if (questionnairesDto != null) {
                          QuestionInfoBean questionInfoBean = new QuestionInfoBean();
                          questionInfoBean.setActivityId(questionnairesDto.getShortTitle());
                          questionInfoBean.setActivityVersion(
                              questionnairesDto.getVersion().toString());

                          if (questionnairesSteps
                              .getStepType()
                              .equalsIgnoreCase(
                                  StudyMetaDataConstants.QUESTIONAIRE_STEP_TYPE_FORM)) {
                            questionInfoBean.setKey(questionDto.getShortTitle());
                          } else {
                            questionInfoBean.setKey(questionnairesSteps.getStepShortTitle());
                          }
                          anchorDate.setQuestionInfo(questionInfoBean);
                        }
                      }
                    }
                  }
                  studyInfoResponse.setAnchorDate(anchorDate);
                }
              }
            }
          }
        }
        studyInfoResponse.setMessage(StudyMetaDataConstants.SUCCESS);
      } else {
        studyInfoResponse.setMessage(StudyMetaDataConstants.INVALID_STUDY_ID);
      }
    } catch (Exception e) {
      LOGGER.error("StudyMetaDataDao - studyInfo() :: ERROR", e);
    } finally {
      if (session != null) {
        session.close();
      }
    }
    LOGGER.exit("studyInfo() :: Ends");
    return studyInfoResponse;
  }