Exemplo n.º 1
0
        public bool ValidationPassed(PageQuestionValidationDTO validationDTO, PageQuestionForDisplayDTO questionDTO, QuoteQuestionAnswerDTO answerDTO)
        {
            bool result = false;

            if (answerDTO != null)
            {
                //check if the question has possible answers
                if (questionDTO.HasPossibleAnswers)
                {
                    //check if the answer given is a valid possible for the question
                    if (questionDTO.PossibleAnswers.Any(pa => pa.QuestionPossibleAnswerID == answerDTO.QuestionPossibleAnswerID.GetValueOrDefault(0)))
                    {
                        //valid answer given validation has passed
                        result = true;
                    }
                }
                else if (!String.IsNullOrWhiteSpace(answerDTO.Answer))
                {
                    //answer given
                    result = true;
                }
            }


            return(result);
        }
        public bool ValidationPassed(PageQuestionValidationDTO validationDTO, PageQuestionForDisplayDTO questionDTO, QuoteQuestionAnswerDTO answerDTO)
        {
            bool result = true;

            if (answerDTO != null && !string.IsNullOrWhiteSpace(answerDTO.Answer))
            {
                DateTime givenDate = DateTime.MinValue;
                if (DateTime.TryParse(answerDTO.Answer, out givenDate))
                {
                    if (givenDate.Date >= DateTime.Now.Date)
                    {
                        result = false;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        public bool ValidationPassed(PageQuestionValidationDTO validationDTO, PageQuestionForDisplayDTO questionDTO, QuoteQuestionAnswerDTO answerDTO)
        {
            bool result = true;

            if (answerDTO != null && !string.IsNullOrWhiteSpace(answerDTO.Answer))
            {
                var match = Regex.Match(answerDTO.Answer, validationDTO.ValidationExpression);
                if (!match.Success)
                {
                    result = false;
                }
            }
            return(result);
        }
        public bool ValidationPassed(PageQuestionValidationDTO validationDTO, PageQuestionForDisplayDTO questionDTO, QuoteQuestionAnswerDTO answerDTO, bool isMax, bool offSetInYears)
        {
            bool result = true;

            if (answerDTO != null && !string.IsNullOrWhiteSpace(answerDTO.Answer))
            {
                DateTime givenDate = DateTime.MinValue;
                int      offset    = 0;
                if (DateTime.TryParse(answerDTO.Answer, out givenDate))
                {
                    //add offset to current date
                    offset = Convert.ToInt16(validationDTO.ValidationExpression);
                    DateTime currentDate = DateTime.Now;
                    if (offSetInYears)
                    {
                        currentDate = currentDate.AddYears(offset);
                    }
                    else
                    {
                        currentDate = currentDate.AddDays(offset);
                    }

                    //figure out if we need to check into the future or the past
                    if (offset > 0)
                    {
                        //check in to the future
                        if (isMax)
                        {
                            //maximum date
                            //date must be before or on the target date
                            if (DateTime.Compare(givenDate.Date, currentDate.Date) > 0)
                            {
                                result = false;
                            }
                        }
                        else
                        {
                            //minimum date
                            //date must be after or on the target date
                            if (DateTime.Compare(givenDate.Date, currentDate.Date) < 0)
                            {
                                result = false;
                            }
                        }
                    }
                    else
                    {
                        //check in to the past
                        if (isMax)
                        {
                            //maximum date
                            //date must be after or on the target date
                            if (DateTime.Compare(givenDate.Date, currentDate.Date) < 0)
                            {
                                result = false;
                            }
                        }
                        else
                        {
                            //minimum date
                            //date must be before or on the target date
                            if (DateTime.Compare(givenDate.Date, currentDate.Date) > 0)
                            {
                                result = false;
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Combines given answers with the existing set of answers.
        /// </summary>
        /// <param name="quoteId">id of the quote the answers belong to</param>
        /// <param name="formAnswers">List of AnswerDTO parsed from the form</param>
        /// <param name="existingAnswers">Answers given already for the quote</param>
        /// <param name="questions">list of expected questions (this will be for a particular page)</param>
        /// <returns>update list of QuoteQuestionAnswerDTO objects</returns>
        public List <QuoteQuestionAnswerDTO> ParseAnswers(int quoteId, List <AnswerDTO> formAnswers, List <QuoteQuestionAnswerDTO> existingAnswers, List <PageQuestionForDisplayDTO> questions)
        {
            string    textAnswer       = string.Empty;
            int?      possibleAnswerId = null;
            AnswerDTO formAnswer;
            QuoteQuestionAnswerDTO quoteAnswer;

            //itterate through the list of page questions
            foreach (PageQuestionForDisplayDTO question in questions)
            {
                //have we got an answer for this question
                if (formAnswers.Any(a => a.PageQuestionID == question.PageQuestionID))
                {
                    //we have an answer for the question
                    formAnswer = formAnswers.Where(a => a.PageQuestionID == question.PageQuestionID).First();

                    textAnswer       = string.Empty;
                    possibleAnswerId = null;

                    //need to figure out whether we have a "text" (e.g. from a textbox) answer or a possible answer (e.g. from a drop down list)
                    if (question.PossibleAnswers != null && question.PossibleAnswers.Count > 0)
                    {
                        //must be a possible answer
                        //check that the answer given matches one in the list
                        if (question.PossibleAnswers.Any(pa => pa.AnswerValue == formAnswer.AnswerText && pa.QuestionID == question.QuestionID))
                        {
                            //got a match
                            possibleAnswerId = question.PossibleAnswers.Where(pa => pa.AnswerValue == formAnswer.AnswerText && pa.QuestionID == question.QuestionID).First().QuestionPossibleAnswerID;
                        }
                        else
                        {
                            //no match, someones selected an invalid answer!
                            throw new Exception("invalid answer selected for question id" + question.QuestionID + " answer given is " + formAnswer.AnswerText);
                        }
                    }
                    else
                    {
                        //must be a text answer
                        textAnswer = formAnswer.AnswerText;
                    }

                    //is the answer already in the list of exsiting answers?
                    if (existingAnswers.Any(a => a.QuestionID == question.QuestionID))
                    {
                        //yes - we need to update the answer
                        quoteAnswer        = existingAnswers.Where(a => a.QuestionID == question.QuestionID).First();
                        quoteAnswer.Answer = textAnswer;
                        quoteAnswer.QuestionPossibleAnswerID = possibleAnswerId;
                    }
                    else
                    {
                        //no - we need to insert a new answer
                        quoteAnswer = new QuoteQuestionAnswerDTO()
                        {
                            Answer     = textAnswer,
                            QuestionID = question.QuestionID,
                            QuestionPossibleAnswerID = possibleAnswerId,
                            QuoteID = quoteId
                        };
                        existingAnswers.Add(quoteAnswer);
                    }
                }
            }

            return(existingAnswers);
        }
Exemplo n.º 6
0
        public bool ValidationPassed(List <PageQuestionForDisplayDTO> questionDTOs, List <QuoteQuestionAnswerDTO> answerDTOs, List <PageQuestionVisibilityDTO> questionVisbilityDTOs, out List <ValidationErrorDTO> validationErrorDTOs)
        {
            bool result = true;

            validationErrorDTOs = new List <ValidationErrorDTO>();
            QuoteQuestionAnswerDTO answerDTO = null;

            //itterate the questions with validation
            foreach (PageQuestionForDisplayDTO questionDTO in questionDTOs.Where(q => q.Validators != null && q.Validators.Count > 0))
            {
                //check if the question is visible
                if (questionVisbilityDTOs.Any(v => v.PageQuestionID == questionDTO.PageQuestionID && v.IsVisible))
                {
                    //question is visible - need to check validators for this question

                    //itterate the validators for this question
                    foreach (PageQuestionValidationDTO validatorDTO in questionDTO.Validators)
                    {
                        answerDTO = answerDTOs.Where(a => a.QuestionID == questionDTO.QuestionID).FirstOrDefault();
                        switch (validatorDTO.ValidationType)
                        {
                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Required:
                            if (!RequiredValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Regex:
                            if (!RegExValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_Valid_Format:
                            if (!DateFormatValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_After_Today:
                            if (!DateAfterTodayValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_Before_Today:
                            if (!DateBeforeTodayValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_Max_Fixed_Number_of_Days:
                            if (!FixedOffsetFromTodayValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO, true, false))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_Max_Fixed_Number_of_Years:
                            if (!FixedOffsetFromTodayValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO, true, true))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_Min_Fixed_Number_of_Days:
                            if (!FixedOffsetFromTodayValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO, false, false))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Date_Min_Fixed_Number_of_Years:
                            if (!FixedOffsetFromTodayValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO, false, true))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        case InsuranceEngine.DTO.Questionnaire.QuestionnaireEnums.ValidationTypes.Numeric:
                            if (!NumericValidator.Instance.ValidationPassed(validatorDTO, questionDTO, answerDTO))
                            {
                                result = false;
                                validationErrorDTOs.Add(new ValidationErrorDTO()
                                {
                                    ErrorMessage = validatorDTO.ErrorMessage, PageQuestionID = questionDTO.PageQuestionID
                                });
                            }
                            break;

                        default:
                            throw new Exception("Unknow validator type " + Convert.ToInt16(validatorDTO.ValidationType).ToString());
                        }
                    }
                }
            }

            return(result);
        }
Exemplo n.º 7
0
        public bool ValidationPassed(PageQuestionValidationDTO validationDTO, PageQuestionForDisplayDTO questionDTO, QuoteQuestionAnswerDTO answerDTO)
        {
            bool result = true;

            if (answerDTO != null && !string.IsNullOrWhiteSpace(answerDTO.Answer))
            {
                //using a fixed regex pattern for date format (uk), found here http://stackoverflow.com/questions/3767614/does-anyone-know-of-a-reg-expression-for-uk-date-format
                var match = Regex.Match(answerDTO.Answer, @"^(?:(?:(?:(?:31\/(?:0?[13578]|1[02]))|(?:(?:29|30)\/(?:0?[13-9]|1[0-2])))\/(?:1[6-9]|[2-9]\d)\d{2})|(?:29\/0?2\/(?:(?:(1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:0?[1-9]|1\d|2[0-8])\/(?:(?:0?[1-9])|(?:1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$");
                if (!match.Success)
                {
                    result = false;
                }
            }
            return(result);
        }
        public bool ValidationPassed(PageQuestionValidationDTO validationDTO, PageQuestionForDisplayDTO questionDTO, QuoteQuestionAnswerDTO answerDTO)
        {
            bool result = true;

            if (answerDTO != null && !string.IsNullOrWhiteSpace(answerDTO.Answer))
            {
                Double temp;
                result = Double.TryParse(answerDTO.Answer, NumberStyles.Any, CultureInfo.InvariantCulture, out temp);
            }
            return(result);
        }