Untitled

mail@pastecode.io avatar
unknown
java
a year ago
5.1 kB
4
Indexable
Never
 public static boolean isRequiredQuestionsAnswered(ChecklistActiveUserSurvey selectedActiveSurvey) {
        long firm = PreferenceHelper.getSelectedFirm();

        long[] answeredQuestionsIdS = TayqaSale.box(ChecklistSelectedAnswer.class).query()
                .equal(ChecklistSelectedAnswer_.firm, firm)
                .equal(ChecklistSelectedAnswer_.activeSurveyId, selectedActiveSurvey.getId())
                .build()
                .property(ChecklistSelectedAnswer_.questionId)
                .findLongs();
        List<Long> answeredQuestionIdList = new ArrayList<>();
        for (Long id : answeredQuestionsIdS) {
            answeredQuestionIdList.add(id);
        }

        QueryBuilder<ChecklistQuestion> requiredQuestionsQuery = TayqaSale.box(ChecklistQuestion.class).query()
                .equal(ChecklistQuestion_.required, true)
                .equal(ChecklistQuestion_.status, ChecklistQuestion.STATUS_ACTIVE);
        requiredQuestionsQuery.link(ChecklistQuestion_.userSurvey)
                .equal(ChecklistUserSurvey_.surveyId, selectedActiveSurvey.getSurveyId());
        long[] requiredQuestionIds = requiredQuestionsQuery.build()
                .property(ChecklistQuestion_.questionId)
                .findLongs();
        List<Long> requiredQuestionIdList = new ArrayList<>();
        for (long id : requiredQuestionIds) {
            requiredQuestionIdList.add(id);
        }

        QueryBuilder<ChecklistQuestion> questionQueryBuilder = TayqaSale.box(ChecklistQuestion.class).query();
        questionQueryBuilder
                .equal(ChecklistQuestion_.status, ChecklistQuestion.STATUS_ACTIVE)
                .link(ChecklistQuestion_.userSurvey)
                .equal(ChecklistUserSurvey_.surveyId, selectedActiveSurvey.getSurveyId());
        List<ChecklistQuestion> requiredQuestionList = questionQueryBuilder.build().find();

        List<ChecklistSelectedAnswer> checklistSelectedAnswerList =
                TayqaSale.box(ChecklistSelectedAnswer.class)
                        .query()
                        .equal(ChecklistSelectedAnswer_.activeSurveyId, selectedActiveSurvey.getId())
                        .build()
                        .find();

        for (ChecklistQuestion question: requiredQuestionList) {
            if (question.getAnswerType().getAnswerTypeId() == ChecklistQuestion.PHOTO){
                if (!isPhotoTypeQuestionAnswered(question, checklistSelectedAnswerList)){
                    return false;
                }
            } else {
                if(!isAttachmentAddedForAnswer(question, checklistSelectedAnswerList, selectedActiveSurvey)) {
                    return false;
                }
                for (ChecklistSelectedAnswer checklistSelectedAnswer : checklistSelectedAnswerList) {
                    if (checklistSelectedAnswer != null && question.getQuestionId() == checklistSelectedAnswer.getQuestionId()) {
                        if ((checklistSelectedAnswer.getReasonSelectOption() == ChecklistAnswer.ReasonPermission.MANDATORY
                                || question.getAnswerReasonSelectOption() == ChecklistAnswer.ReasonPermission.MANDATORY)) {
                            if (checklistSelectedAnswer.getReasonValue() == null && checklistSelectedAnswer.getReasonId() == null) {
                                return false;
                            }
                        }

                        if ((checklistSelectedAnswer.getReasonSelectOption() == ChecklistAnswer.ReasonPermission.CONDITIONAL_MANDATORY
                                || question.getAnswerReasonSelectOption() == ChecklistAnswer.ReasonPermission.CONDITIONAL_MANDATORY)) {
                            if (checklistSelectedAnswer.isReasonRequiredForConditionalInput(
                                    question.getWeightingType(),
                                    question.getAnswerType().getAnswerTypeId(),
                                    question.getAnswerReasonSelectOption(),
                                    question.getReasonConditionType(),
                                    question.getReasonConditionValue()
                            )) {
                                return false;
                            }
                        }
                    }
                }
            }
        }

        for (long id : requiredQuestionIdList) {
            if (!answeredQuestionIdList.contains(id)) {
                return false;
            }
        }

        for (long id : checkRequiredAttachment(selectedActiveSurvey)) {
            int count = AttachmentsHelper.getCountOfQuestionAttachments(
                    String.valueOf(selectedActiveSurvey.getId()),
                    String.valueOf(selectedActiveSurvey.getSurveyId()),
                    String.valueOf(id),
                    String.valueOf(selectedActiveSurvey.getClientId()));
            if (count == 0) {
                return false;
            }
        }

        return true;
    }