示例#1
0
        public bool CheckSvfWindow(CreateFormFact fact, string culture, bool negate, out string explanation)
        {
            if (fact == null)
            {
                throw new ArgumentNullException("fact");
            }
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }
            bool result = false;
            var  now    = DateTimeOffset.UtcNow;
            var  min    = fact.SvfDate.AddDays((-1) * SvfWindowDays);
            var  max    = fact.SvfDate.AddDays(SvfWindowDays);

            if (now >= min && now <= max)
            {
                result = true;
            }
            result = false;
            if (result)
            {
                explanation = String.Format(SVF_WINDOW_TRUE_MESSAGE_FORMAT, SvfWindowDays, now);
            }
            else
            {
                explanation = String.Format(SVF_WINDOW_FALSE_MESSAGE_FORMAT, SvfWindowDays, now);
            }
            return(negate ? !result : result);
        }
        public static CreateFormFact ToFact(this CreateFormQuestion question)
        {
            if (String.IsNullOrWhiteSpace(question.Form))
            {
                throw new ArgumentException("question.Form was null or whitespace", "question");
            }
            CreateFormFact fact = new CreateFormFact();

            fact.Form            = question.Form.Trim();
            fact.AnniversaryDate = question.AnniversaryDate;
            fact.SvfDate         = question.SvfDate;

            if (question.MostRecentForms == null || question.MostRecentForms.Count == 0)
            {
                return(fact);
            }
            question.MostRecentForms.ForEach(x =>
            {
                if (String.IsNullOrWhiteSpace(x.Form))
                {
                    throw new ArgumentException("FormStatus.Form was null or whitespace", "question");
                }
                if (String.IsNullOrWhiteSpace(x.Status))
                {
                    throw new ArgumentException("FormStatus.Status was null or whitespace", "question");
                }
                string form   = x.Form.Trim();
                string status = x.Status.Trim();
                if (form.Equals(Form.INITIAL_ENROLLMENT, StringComparison.OrdinalIgnoreCase))
                {
                    fact.InitialEnrollmentStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else if (form.Equals(Form.REENROLLMENT, StringComparison.OrdinalIgnoreCase))
                {
                    fact.ReenrollmentStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else if (form.Equals(Form.SVF_WITH_CHANGES, StringComparison.OrdinalIgnoreCase))
                {
                    fact.SvfWithChangesStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else if (form.Equals(Form.SVF_NO_CHANGES, StringComparison.OrdinalIgnoreCase))
                {
                    fact.SvfNoChangesStatus = ValidateAndTransformSvfNoChangesStatus(status);
                }
                else if (form.Equals(Form.UPDATE_FORM, StringComparison.OrdinalIgnoreCase))
                {
                    fact.UpdateFormStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else
                {
                    throw new Exception(String.Format("Don't know how to handle form {0}", form));
                }
            });

            return(fact);
        }
        public CreateFormAnswer CanCreateForm(CreateFormQuestion question)
        {
            if (question == null)
            {
                throw new ArgumentNullException("question");
            }
            CreateFormAnswer answer = new CreateFormAnswer();

            try
            {
                CreateFormFact fact  = question.ToFact();
                var            rules = mDecisionTable.Rows
                                       .Where(x => x.Form == fact.Form &&
                                              x.InitialEnrollmentStatus == fact.InitialEnrollmentStatus &&
                                              x.ReenrollmentStatus == fact.ReenrollmentStatus &&
                                              x.SvfWithChangesStatus == fact.SvfWithChangesStatus &&
                                              x.SvfNoChangesStatus == fact.SvfNoChangesStatus &&
                                              x.UpdateFormStatus == fact.UpdateFormStatus)
                                       .ToList();
                if (rules.Count == 0)
                {
                    answer.Error        = true;
                    answer.ErrorMessage = String.Format("Your question did not match any rules in the decision table. "
                                                        + "Question was converted to the following fact. {0}", JsonConvert.SerializeObject(fact));
                }
                if (rules.Count > 1)
                {
                    answer.Error = true;
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Your question matched more than 1 rule in the decision table.");
                    sb.AppendLine(String.Format("Question was converted to the following fact. {0}", JsonConvert.SerializeObject(fact)));
                    sb.AppendLine("The following rules matched the fact:");
                    foreach (var _rule in rules)
                    {
                        sb.AppendLine(JsonConvert.SerializeObject(_rule));
                    }
                }
                CreateFormDecisionRow rule = rules[0];
                if (rule.CheckReenrollmentWindow.Value == FunctionColumn.NO && rule.CheckSvfWindow.Value == FunctionColumn.NO)
                {
                    if (!rule.CanCreate.HasValue)
                    {
                        throw new Exception(String.Format("Rule.CanCreate does not have a value specified. {0}", JsonConvert.SerializeObject(rule)));
                    }
                    answer.CanCreate   = rule.CanCreate.Value;
                    answer.Explanation = rule.Explanation;
                }
                else
                {
                    bool   checkReenrollmentWindowResult      = false;
                    string checkReenrollmentWindowExplanation = null;
                    string checkReenrollmentWindow            = rule.CheckReenrollmentWindow.Value;
                    if (checkReenrollmentWindow == FunctionColumn.YES || checkReenrollmentWindow == FunctionColumn.NEGATE)
                    {
                        checkReenrollmentWindowResult = mDecisionTable.CheckAnniversaryWindow(fact, question.Culture,
                                                                                              checkReenrollmentWindow == FunctionColumn.NEGATE, out checkReenrollmentWindowExplanation);
                    }
                    bool   checkSvfWindowResult     = false;
                    string checkSvfWindowExplantion = "";
                    string checkSvfWindow           = rule.CheckSvfWindow.Value;
                    if (checkSvfWindow == FunctionColumn.YES || checkSvfWindow == FunctionColumn.NEGATE)
                    {
                        checkSvfWindowResult = mDecisionTable.CheckAnniversaryWindow(fact, question.Culture,
                                                                                     checkSvfWindow == FunctionColumn.NEGATE, out checkSvfWindowExplantion);
                    }
                    answer.CanCreate = checkReenrollmentWindowResult && checkSvfWindowResult;
                    StringBuilder sb = new StringBuilder();
                    if (!String.IsNullOrWhiteSpace(checkReenrollmentWindowExplanation))
                    {
                        sb.Append(checkReenrollmentWindowExplanation);
                    }
                    if (!String.IsNullOrWhiteSpace(checkSvfWindowExplantion))
                    {
                        if (!String.IsNullOrWhiteSpace(checkReenrollmentWindowExplanation))
                        {
                            sb.Append(" ");
                        }
                        sb.Append(checkSvfWindowExplantion);
                    }
                    answer.Explanation = sb.ToString();
                }
            }
            catch (Exception ex)
            {
                string errorMessage = String.Format("An error occurred while evaluating the question. {0}", ex.ToStringVerbose());
                mLogger.LogError(errorMessage);
                answer.Error        = true;
                answer.ErrorMessage = errorMessage;
            }
            return(answer);
        }