Example #1
0
 public RuleLockInfo(BizRuleInfo Info)
 {
     Original = new CloneBizRuleInfo();
     Original.AdvancedCodeXML        = Info.AdvancedCodeXML;
     Original.CommentsTxt            = Info.CommentsTxt;
     Original.Condition              = (int)Info.Condition;
     Original.Condition2             = Info.Condition2;
     Original.ConditionState         = Info.ConditionState;
     Original.ConditionState2        = Info.ConditionState2;
     Original.ConditionStateInt      = Info.ConditionStateInt;
     Original.Inactive               = Info.Inactive;
     Original.IsGeneralRule          = Info.IsGeneralRule;
     Original.LastModifiedByFullName = Info.LastModifiedByFullName;
     Original.LastModifiedByUserId   = Info.LastModifiedByUserId;
     Original.LastModifiedByUserInfo = Info.LastModifiedByUserInfo;
     Original.MilestoneID            = Info.MilestoneID;
     Original.RoleID   = Info.RoleID;
     Original.RuleID   = Info.RuleID;
     Original.RuleName = Info.RuleName;
     Original.RuleType = (int)Info.RuleType;
     Original.Active   = Info.Status == BizRule.RuleStatus.Active;
 }
        private static List <BizRuleValidationResult> ValidateBizRules(BizRuleSet bizRuleSet, List <DocumentPlugValidationInfo> documentPlugValidationInfoList)
        {
            if (bizRuleSet == null)
            {
                throw new ArgumentNullException("bizRuleSet", "bizRuleSet cannot be null");
            }

            if (documentPlugValidationInfoList == null || documentPlugValidationInfoList.Count <= 1)
            {
                throw new ArgumentNullException("documentPlugValidationInfoList", "documentPlugValidationInfoList cannot be null and should have > 1 entries");
            }

            List <BizRuleValidationResult> bizRuleValidationResults = new List <BizRuleValidationResult>();

            List <RuleSegments> ruleSegmentsList = new List <RuleSegments>();

            foreach (int messageDomainId in bizRuleSet.MessageDomainIds)
            {
                RuleSegments ruleSegments = null;
                foreach (DocumentPlugValidationInfo documentPlugValidationInfo in documentPlugValidationInfoList)
                {
                    if (documentPlugValidationInfo.DocumentPlug != null && documentPlugValidationInfo.DocumentPlug.DocumentType == messageDomainId)
                    {
                        ruleSegments = new RuleSegments(documentPlugValidationInfo.FatpipeDocument, documentPlugValidationInfo.SpecCertName);
                        break;
                    }
                }
                ruleSegmentsList.Add(ruleSegments);
            }

            // Conditional groups - first get all BizRuleValidationResult for all groups
            // then for each group check if at least one is successful, in case
            // of success, mark others as success?
            Dictionary <string, List <BizRuleValidationResult> > conditionalRulesResult = new Dictionary <string, List <BizRuleValidationResult> >();
            string segmentValue;

            foreach (string firstSegmentPath in bizRuleSet.BizRules.Keys)
            {
                // TODO: Handle segmentPath being null/empty

                List <BizRule> bizRules = bizRuleSet.BizRules[firstSegmentPath];

                bool loopHasValues = true;
                int  loopOccurance = 0;
                while (loopHasValues)
                {
                    foreach (BizRule bizRule in bizRules)
                    {
                        string valueToMatch = null;
                        BizRuleValidationResult bizRuleValidationResult = new BizRuleValidationResult();
                        bizRuleValidationResult.RuleName = bizRule.Name;
                        bizRuleValidationResult.Type     = ResultType.Success;
                        bizRuleValidationResult.RuleInfo = new List <BizRuleInfo>();

                        int  i = 0;
                        bool isRuleSuccessful = true;
                        foreach (SegmentPath segmentPath in bizRule.SegmentPaths)
                        {
                            if (string.IsNullOrWhiteSpace(segmentPath.OriginalPath))
                            {
                                i++;
                                continue;
                            }
                            BizRuleInfo bizRuleInfo = new BizRuleInfo()
                            {
                                SegmentPath = segmentPath.OriginalPath,
                            };

                            if (ruleSegmentsList[i] != null)
                            {
                                bizRuleInfo.FileName = ruleSegmentsList[i].CertName;

                                IDocumentFragment documentFragment = ruleSegmentsList[i].SelectSegment(segmentPath, valueToMatch);
                                if (documentFragment != null)
                                {
                                    segmentValue = documentFragment.GetDataSegmentValue(segmentPath.DataSegmentName);
                                    if (string.IsNullOrWhiteSpace(valueToMatch) || string.IsNullOrWhiteSpace(segmentPath.Value) == false)
                                    {
                                        valueToMatch = segmentValue;
                                    }
                                    else
                                    if (string.Equals(valueToMatch, segmentValue, StringComparison.OrdinalIgnoreCase) == false)
                                    {
                                        isRuleSuccessful = false;
                                    }
                                    bizRuleInfo.Value = segmentValue;
                                }
                                else if (loopOccurance != 0 && i == 0)
                                {
                                    loopHasValues = false;
                                    break;
                                }
                            }
                            else
                            {
                                bizRuleInfo.FileName = "Not provided";
                            }

                            if (string.IsNullOrWhiteSpace(bizRuleInfo.Value))
                            {
                                bizRuleInfo.Value = "<Not Valued>";
                                isRuleSuccessful  = false;
                            }

                            bizRuleValidationResult.RuleInfo.Add(bizRuleInfo);

                            i++;
                        }

                        if (loopHasValues)
                        {
                            // TODO: Conditional mandatory will impact setting type to error.
                            if (isRuleSuccessful == false)
                            {
                                bizRuleValidationResult.Type = bizRule.Type == BizRuleType.Optional ? ResultType.Warning : ResultType.Error;
                            }


                            if (string.IsNullOrWhiteSpace(bizRule.ConditionalGroupName) == false)
                            {
                                List <BizRuleValidationResult> bizRuleResults;
                                if (conditionalRulesResult.TryGetValue(bizRule.ConditionalGroupName, out bizRuleResults) == false)
                                {
                                    bizRuleResults = new List <BizRuleValidationResult>();
                                    conditionalRulesResult.Add(bizRule.ConditionalGroupName, bizRuleResults);
                                }
                                bizRuleResults.Add(bizRuleValidationResult);
                            }

                            bizRuleValidationResults.Add(bizRuleValidationResult);
                        }
                    }

                    foreach (RuleSegments ruleSegments in ruleSegmentsList)
                    {
                        ruleSegments.MoveCurrentToUsed();
                    }

                    loopOccurance++;
                }
            }

            if (conditionalRulesResult.Count > 0)
            {
                foreach (List <BizRuleValidationResult> bizRuleResults in conditionalRulesResult.Values)
                {
                    if (bizRuleResults.Any(bizRuleResult => bizRuleResult.Type == ResultType.Success))
                    {
                        foreach (BizRuleValidationResult bizRuleResult in bizRuleResults)
                        {
                            bizRuleResult.Type = ResultType.Success;
                        }
                    }
                }
            }

            return(bizRuleValidationResults);
        }