public ActivityResponse studyActivityList()

in study-datastore/src/main/java/com/hphc/mystudies/dao/ActivityMetaDataDao.java [110:341]


  public ActivityResponse studyActivityList(String studyId, String authorization)
      throws DAOException {
    LOGGER.entry("begin studyActivityList()");
    Session session = null;
    ActivityResponse activityResponse = new ActivityResponse();
    List<ActiveTaskDto> activeTaskDtoList = null;
    List<QuestionnairesDto> questionnairesList = null;
    List<ActivitiesBean> activitiesBeanList = new ArrayList<>();
    StudyDto studyDto = null;
    StudyVersionDto studyVersionDto = null;
    String deviceType = "";
    try {
      deviceType =
          StudyMetaDataUtil.platformType(authorization, StudyMetaDataConstants.STUDY_AUTH_TYPE_OS);
      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();

        activeTaskDtoList =
            session
                .getNamedQuery("getActiveTaskDetailsByCustomStudyId")
                .setString(
                    StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(),
                    studyVersionDto.getCustomStudyId())
                .setInteger(StudyMetaDataEnum.QF_LIVE.value(), 1)
                .setInteger(StudyMetaDataEnum.QF_ACTIVE.value(), 0)
                .list();

        if ((null != activeTaskDtoList) && !activeTaskDtoList.isEmpty()) {
          for (ActiveTaskDto activeTaskDto : activeTaskDtoList) {
            boolean isSupporting = true;

            /** Allow spatial span memory and tower of hanoi active tasks only for iOS platform */
            if (deviceType.equalsIgnoreCase(StudyMetaDataConstants.STUDY_PLATFORM_ANDROID)
                && !activeTaskDto.getTaskTypeId().equals(String.valueOf(1))) {
              isSupporting = false;
            }

            if (isSupporting) {
              ActivitiesBean activityBean = new ActivitiesBean();
              activityBean.setTitle(
                  StringUtils.isEmpty(activeTaskDto.getDisplayName())
                      ? ""
                      : activeTaskDto.getDisplayName());
              activityBean.setType(StudyMetaDataConstants.ACTIVITY_ACTIVE_TASK);
              activityBean.setState(
                  ((activeTaskDto.getActive() != null) && (activeTaskDto.getActive() > 0))
                      ? StudyMetaDataConstants.ACTIVITY_STATUS_ACTIVE
                      : StudyMetaDataConstants.ACTIVITY_STATUS_DELETED);

              activityBean.setActivityVersion(
                  ((activeTaskDto.getVersion() == null) || (activeTaskDto.getVersion() < 1.0f))
                      ? StudyMetaDataConstants.STUDY_DEFAULT_VERSION
                      : activeTaskDto.getVersion().toString());
              activityBean.setBranching(false);
              activityBean.setLastModified(
                  StringUtils.isEmpty(activeTaskDto.getModifiedDate())
                      ? ""
                      : StudyMetaDataUtil.getFormattedDateTimeZone(
                          activeTaskDto.getModifiedDate(),
                          StudyMetaDataConstants.SDF_DATE_TIME_PATTERN,
                          StudyMetaDataConstants.SDF_DATE_TIME_TIMEZONE_MILLISECONDS_PATTERN));

              ActivityFrequencyBean frequencyDetails = new ActivityFrequencyBean();
              frequencyDetails =
                  this.getFrequencyRunsDetailsForActiveTasks(
                      activeTaskDto, frequencyDetails, session);
              frequencyDetails.setType(
                  StringUtils.isEmpty(activeTaskDto.getFrequency())
                      ? ""
                      : activeTaskDto.getFrequency());
              activityBean.setFrequency(frequencyDetails);

              activityBean =
                  this.getTimeDetailsByActivityIdForActiveTask(
                      activeTaskDto, activityBean, session);

              /** For deleted task modified date time will be the end date time of active task */
              if ((activeTaskDto.getActive() == null) || activeTaskDto.getActive().equals(0)) {
                activityBean.setEndTime(
                    StudyMetaDataUtil.getFormattedDateTimeZone(
                        activeTaskDto.getModifiedDate(),
                        StudyMetaDataConstants.SDF_DATE_TIME_PATTERN,
                        StudyMetaDataConstants.SDF_DATE_TIME_TIMEZONE_MILLISECONDS_PATTERN));
              }

              activityBean.setActivityId(activeTaskDto.getShortTitle());

              if (null != activeTaskDto.getTaskTypeId()) {
                if ("1".equals(activeTaskDto.getTaskTypeId())) {
                  activityBean.setTaskSubType(
                      StudyMetaDataConstants.ACTIVITY_AT_FETAL_KICK_COUNTER);
                } else if ("2".equals(activeTaskDto.getTaskTypeId())) {
                  activityBean.setTaskSubType(StudyMetaDataConstants.ACTIVITY_AT_TOWER_OF_HANOI);
                } else if ("3".equals(activeTaskDto.getTaskTypeId())) {
                  activityBean.setTaskSubType(
                      StudyMetaDataConstants.ACTIVITY_AT_SPATIAL_SPAN_MEMORY);
                }
              }
              /** Phase2 a code for anchor date * */
              if ((activeTaskDto.getScheduleType() != null)
                  && !activeTaskDto.getScheduleType().isEmpty()) {
                activityBean.setSchedulingType(activeTaskDto.getScheduleType());
                if (activeTaskDto
                    .getScheduleType()
                    .equals(StudyMetaDataConstants.SCHEDULETYPE_ANCHORDATE)) {
                  activityBean =
                      this.getAnchordateDetailsByActivityIdForActivetask(
                          activeTaskDto, activityBean, session);
                }
              }

              if (activityBean.getIsStudyLifeTime()) {
                activityBean.setEndTime("");
              }
              /** Phase2a code for anchor date * */
              activitiesBeanList.add(activityBean);
            }
          }
        }

        questionnairesList =
            session
                .getNamedQuery("getQuestionnaireDetailsByCustomStudyId")
                .setString(
                    StudyMetaDataEnum.QF_CUSTOM_STUDY_ID.value(),
                    studyVersionDto.getCustomStudyId())
                .setInteger(StudyMetaDataEnum.QF_LIVE.value(), 1)
                .setBoolean(StudyMetaDataEnum.QF_ACTIVE.value(), false)
                .list();
        if ((questionnairesList != null) && !questionnairesList.isEmpty()) {

          for (QuestionnairesDto questionaire : questionnairesList) {

            ActivitiesBean activityBean = new ActivitiesBean();
            activityBean.setTitle(
                StringUtils.isEmpty(questionaire.getTitle()) ? "" : questionaire.getTitle());
            activityBean.setType(StudyMetaDataConstants.ACTIVITY_QUESTIONNAIRE);
            activityBean.setState(
                questionaire.getActive()
                    ? StudyMetaDataConstants.ACTIVITY_STATUS_ACTIVE
                    : StudyMetaDataConstants.ACTIVITY_STATUS_DELETED);

            ActivityFrequencyBean frequencyDetails = new ActivityFrequencyBean();
            frequencyDetails =
                this.getFrequencyRunsDetailsForQuestionaires(
                    questionaire, frequencyDetails, session);
            frequencyDetails.setType(
                StringUtils.isEmpty(questionaire.getFrequency())
                    ? ""
                    : questionaire.getFrequency());
            activityBean.setFrequency(frequencyDetails);
            activityBean.setActivityId(questionaire.getShortTitle());
            activityBean.setActivityVersion(
                ((questionaire.getVersion() == null) || (questionaire.getVersion() < 1.0f))
                    ? StudyMetaDataConstants.STUDY_DEFAULT_VERSION
                    : questionaire.getVersion().toString());
            activityBean.setBranching(
                ((questionaire.getBranching() == null) || !questionaire.getBranching())
                    ? false
                    : true);
            activityBean.setLastModified(
                StringUtils.isEmpty(questionaire.getModifiedDate())
                    ? ""
                    : StudyMetaDataUtil.getFormattedDateTimeZone(
                        questionaire.getModifiedDate(),
                        StudyMetaDataConstants.SDF_DATE_TIME_PATTERN,
                        StudyMetaDataConstants.SDF_DATE_TIME_TIMEZONE_MILLISECONDS_PATTERN));
            activityBean =
                this.getTimeDetailsByActivityIdForQuestionnaire(
                    questionaire, activityBean, session);

            /** For deleted task modified date time will be the end date time of questionnaire */
            if (!questionaire.getActive()) {
              activityBean.setEndTime(
                  StudyMetaDataUtil.getFormattedDateTimeZone(
                      questionaire.getModifiedDate(),
                      StudyMetaDataConstants.SDF_DATE_TIME_PATTERN,
                      StudyMetaDataConstants.SDF_DATE_TIME_TIMEZONE_MILLISECONDS_PATTERN));
            }
            /** Phase2 a code for anchor date * */
            if ((questionaire.getScheduleType() != null)
                && !questionaire.getScheduleType().isEmpty()) {
              activityBean.setSchedulingType(questionaire.getScheduleType());
              if (questionaire
                  .getScheduleType()
                  .equals(StudyMetaDataConstants.SCHEDULETYPE_ANCHORDATE)) {
                activityBean =
                    this.getAnchordateDetailsByActivityIdForQuestionnaire(
                        questionaire, activityBean, session);
              }
            }

            if (activityBean.getIsStudyLifeTime()) {

              activityBean.setEndTime("");
            }
            /** Phase2a code for anchor date * */
            activitiesBeanList.add(activityBean);
          }
        }

        activityResponse.setActivities(activitiesBeanList);
        activityResponse.setMessage(StudyMetaDataConstants.SUCCESS);
      } else {
        activityResponse.setMessage(StudyMetaDataConstants.INVALID_STUDY_ID);
      }
    } catch (Exception e) {
      LOGGER.error("ActivityMetaDataDao - studyActivityList() :: ERROR", e);
    } finally {
      if (session != null) {
        session.close();
      }
    }
    LOGGER.exit("studyActivityList() :: Ends");
    return activityResponse;
  }