public String updateAnchordateInQuestionnaire()

in study-builder/fdahpStudyDesigner/src/main/java/com/fdahpstudydesigner/dao/StudyQuestionnaireDAOImpl.java [5066:5287]


  public String updateAnchordateInQuestionnaire(
      Session session,
      Transaction transaction,
      StudyVersionBo studyVersionBo,
      String questionnaireId,
      SessionObject sessionObject,
      String studyId,
      String stepId,
      String questionId,
      String stepType,
      boolean isChange,
      String customStudyId) {

    logger.entry("begin updateAnchordateInQuestionnaire");
    List<Integer> anchorIds = new ArrayList<Integer>();
    List<Integer> anchorExistIds = new ArrayList<Integer>();
    Boolean isAnchorUsed = false;
    String searchQuery = "";
    String message = FdahpStudyDesignerConstants.FAILURE;
    try {
      if (!stepType.isEmpty()) {
        if (stepType.equalsIgnoreCase(FdahpStudyDesignerConstants.QUESTION_STEP)) {
          searchQuery =
              "select q.anchor_date_id from questions q,questionnaires_steps qsq,questionnaires qq where q.id=qsq.instruction_form_id and qsq.step_type='Question' "
                  + "and qsq.active=1 and qsq.questionnaires_id=qq.id and qq.id=:questionnaireId "
                  + " and q.id=:stepId "
                  + " and qq.active=1 and q.active=1"
                  + " and q.anchor_date_id IS NOT NULL;";
          List<Integer> aIds =
              session
                  .createSQLQuery(searchQuery)
                  .setString("stepId", stepId)
                  .setString("questionnaireId", questionnaireId)
                  .list();

          if ((aIds != null) && (aIds.size() > 0)) {
            anchorIds.addAll(aIds);
          }
        } else if (stepType.equalsIgnoreCase(FdahpStudyDesignerConstants.FORM_STEP)) {
          String subQuery =
              "select q.anchor_date_id from questions q,form_mapping fm,form f,questionnaires_steps qsf,questionnaires qq where q.id=fm.question_id and f.form_id=fm.form_id and f.active=1 "
                  + "and f.form_id=qsf.instruction_form_id and qsf.step_type='Form' and qsf.questionnaires_id=qq.id and qq.id=:questionnaireId "
                  + " and f.form_id=:stepId "
                  + " and q.active=1"
                  + " and q.anchor_date_id IS NOT NULL;";
          List<Integer> aaIds =
              session
                  .createSQLQuery(subQuery)
                  .setString("stepId", stepId)
                  .setString("questionnaireId", questionnaireId)
                  .list();
          if ((aaIds != null) && (aaIds.size() > 0)) {
            anchorIds.addAll(aaIds);
          }
        }
      }
      // Question level deletion
      if (questionId != null) {
        searchQuery =
            "select q.anchor_date_id from questions q where q.active=1 and q.id=:id"
                + " and q.anchor_date_id IS NOT NULL;";
        List<Integer> aIds =
            session.createSQLQuery(searchQuery).setParameter("id", questionId).list();
        if ((aIds != null) && (aIds.size() > 0)) {
          anchorIds.addAll(aIds);
        }
      }
      // Questionnaire level deletion
      if ((stepId == null) && (questionnaireId != null)) {
        // checking in the question step anchor date is selected or not
        searchQuery =
            "select q.anchor_date_id from questions q,questionnaires_steps qsq,questionnaires qq where q.id=qsq.instruction_form_id and qsq.step_type='Question'"
                + " and qsq.active=1 and qsq.questionnaires_id=qq.id and qq.id=:questionnaireId "
                + " and qq.active=1 and q.active=1"
                + " and q.anchor_date_id IS NOT NULL;";
        List<Integer> aIds =
            session
                .createSQLQuery(searchQuery)
                .setString("questionnaireId", questionnaireId)
                .list();
        if ((aIds != null) && (aIds.size() > 0)) {
          anchorIds.addAll(aIds);
        }
        // checking in the form step question anchor date is
        // selected or not
        String subQuery =
            "select q.anchor_date_id from questions q,form_mapping fm,form f,questionnaires_steps qsf,questionnaires qq where q.id=fm.question_id and f.form_id=fm.form_id and f.active=1 "
                + "and f.form_id=qsf.instruction_form_id and qsf.step_type='Form' and qsf.questionnaires_id=qq.id and qq.id=:questionnaireId"
                + " and q.active=1"
                + " and q.anchor_date_id IS NOT NULL;";
        List<Integer> aaIds =
            session.createSQLQuery(subQuery).setString("questionnaireId", questionnaireId).list();
        if ((aaIds != null) && (aaIds.size() > 0)) {
          anchorIds.addAll(aaIds);
        }
      }
      if (!anchorIds.isEmpty() && (anchorIds.size() > 0)) {
        searchQuery =
            "select q.id from questionnaires q where q.schedule_type=:type"
                + " and q.anchor_date_id in( :anchorIds )";
        anchorExistIds =
            session
                .createSQLQuery(searchQuery)
                .setParameter("type", FdahpStudyDesignerConstants.SCHEDULETYPE_ANCHORDATE)
                .setParameterList("anchorIds", anchorIds)
                .list();
        if (!anchorExistIds.isEmpty() && (anchorExistIds.size() > 0)) {
          isAnchorUsed = true;
        } else {
          searchQuery =
              "select q.id from active_task q where q.schedule_type=:type"
                  + " and q.anchor_date_id in( :anchorIds )";
          anchorExistIds =
              session
                  .createSQLQuery(searchQuery)
                  .setParameter("type", FdahpStudyDesignerConstants.SCHEDULETYPE_ANCHORDATE)
                  .setParameterList("anchorIds", anchorIds)
                  .list();
          if (!anchorExistIds.isEmpty() && (anchorExistIds.size() > 0)) {
            isAnchorUsed = true;
          } else {
            searchQuery = "select q.id from resources q where q.anchor_date_id in( :anchorIds )";
            anchorExistIds =
                session.createSQLQuery(searchQuery).setParameterList("anchorIds", anchorIds).list();
            if (!anchorExistIds.isEmpty() && (anchorExistIds.size() > 0)) {
              isAnchorUsed = true;
            }
          }
        }
        if ((studyVersionBo != null) && isChange) {
          if (isAnchorUsed) {
            message = FdahpStudyDesignerConstants.FAILURE + "anchorused";
            return message;
          } else {
            String deleteAncQuery = "delete from anchordate_type where id IN( :anchorIds )";
            query = session.createSQLQuery(deleteAncQuery).setParameterList("anchorIds", anchorIds);
            query.executeUpdate();
            message = FdahpStudyDesignerConstants.SUCCESS;
          }
        } else {
          if (isAnchorUsed) {
            StudySequenceBo studySequence =
                (StudySequenceBo)
                    session
                        .getNamedQuery("getStudySequenceByStudyId")
                        .setString("studyId", studyId)
                        .uniqueResult();
            if (studySequence != null) {
              int count1 =
                  session
                      .createSQLQuery(
                          "update questionnaires set status=0,anchor_date_id=null,"
                              + "modified_by=:userId "
                              + ",modified_date=:currentDateAndTime"
                              + " where active=1 and anchor_date_id in( :anchorIds ) ")
                      .setString("userId", sessionObject.getUserId())
                      .setParameterList("anchorIds", anchorIds)
                      .setString("currentDateAndTime", FdahpStudyDesignerUtil.getCurrentDateTime())
                      .executeUpdate();
              if (count1 > 0) {
                studySequence.setStudyExcQuestionnaries(false);
                auditLogDAO.updateDraftToEditedStatus(
                    session,
                    transaction,
                    sessionObject.getUserId(),
                    FdahpStudyDesignerConstants.DRAFT_QUESTIONNAIRE,
                    studyId);
              }
              int count2 =
                  session
                      .createSQLQuery(
                          "update active_task set action=0 ,anchor_date_id=null, modified_by=:userId "
                              + ",modified_date=:currentDateAndTime"
                              + " where active=1 and anchor_date_id in( :anchorIds )")
                      .setString("userId", sessionObject.getUserId())
                      .setParameterList("anchorIds", anchorIds)
                      .setString("currentDateAndTime", FdahpStudyDesignerUtil.getCurrentDateTime())
                      .executeUpdate();
              if (count2 > 0) {
                studySequence.setStudyExcActiveTask(false);
                auditLogDAO.updateDraftToEditedStatus(
                    session,
                    transaction,
                    sessionObject.getUserId(),
                    FdahpStudyDesignerConstants.DRAFT_ACTIVETASK,
                    studyId);
              }
              int count3 =
                  session
                      .createSQLQuery(
                          "update resources set action=0,anchor_date_id=null "
                              + "where status=1 and anchor_date_id in( :anchorIds )")
                      .setParameterList("anchorIds", anchorIds)
                      .executeUpdate();

              if (count3 > 0) {
                studySequence.setMiscellaneousResources(false);
                auditLogDAO.updateDraftToEditedStatus(
                    session,
                    transaction,
                    sessionObject.getUserId(),
                    FdahpStudyDesignerConstants.DRAFT_STUDY,
                    studyId);
              }
              session.saveOrUpdate(studySequence);
            }
          }
          String deleteAncQuery = "delete from anchordate_type where id IN( :anchorIds )";
          query = session.createSQLQuery(deleteAncQuery).setParameterList("anchorIds", anchorIds);
          query.executeUpdate();
          message = FdahpStudyDesignerConstants.SUCCESS;
        }
      } else {
        message = FdahpStudyDesignerConstants.SUCCESS;
      }
    } catch (Exception e) {
      transaction.rollback();
      logger.error("StudyQuestionnaireDAOImpl - updateAnchordateInQuestionnaire - ERROR ", e);
    }
    logger.exit("updateAnchordateInQuestionnaire - Ends");
    return message;
  }