// This function does not need DalManager and extracted from above method for unit testing purpose.
        public static BizRulesValidationResult ValidateBizRules(string userName, string homeOrg, string ruleCertFileName, BizRuleSet bizRuleSet,
                                                                List <DocumentPlugValidationInfo> documentPlugValidationInfoList, IDalManager dal)
        {
            BizRulesValidationResult bizRulesValidationResult = new BizRulesValidationResult();

            bizRulesValidationResult.BizRuleCertName = ruleCertFileName;

            // Validate All Edi if DocumentPlug is not null, if it is null then add error
            bizRulesValidationResult.EdiValidationResults = ValidateEdi(documentPlugValidationInfoList);

            Stopwatch sw = new Stopwatch();

            sw.Start();
            bizRulesValidationResult.BizRuleValidationResults = ValidateBizRules(bizRuleSet, documentPlugValidationInfoList);
            sw.Stop();

            bizRulesValidationResult.BizRuleValidationExecutionTime = sw.Elapsed;

            AddUsageEvent(userName, homeOrg, bizRulesValidationResult, dal);

            return(bizRulesValidationResult);
        }
        private static void AddUsageEvent(string userName, string homeOrg, BizRulesValidationResult bizRulesValidationResult, IDalManager dal)
        {
            if (bizRulesValidationResult == null)
            {
                return;
            }

            if (bizRulesValidationResult.EdiValidationResults != null)
            {
                string validationStatus = "Error";
                if (bizRulesValidationResult.BizRuleValidationResults != null)
                {
                    validationStatus = bizRulesValidationResult.GetAggregatedResult().ToString();
                }

                string bizRuleCertFileName = Path.GetFileNameWithoutExtension(bizRulesValidationResult.BizRuleCertName);

                foreach (EDIValidationResult result in bizRulesValidationResult.EdiValidationResults)
                {
                    string fileName    = result.FileName;
                    string partnerName = result.FileName.Substring(0, result.FileName.IndexOf(" - "));

                    GCValidatorHelper.AddUsageEvent(userName, homeOrg, partnerName, result.DisplayName, result, result.ExecutionTime,
                                                    fileName, "Biz Rule", dal);

                    // We add usage event for each ST-SE segment in above AddUsageEvent call
                    // however for bizRule validation we currently support only one ST-SE segment.

                    // Add another usage event for each edi file for biz rule validations
                    if (bizRulesValidationResult.BizRuleValidationResults != null)
                    {
                        fileName = string.Format("{0} + {1}", fileName, bizRulesValidationResult.BizRuleCertName);

                        GCValidatorHelper.AddUsageEvent(userName, homeOrg, partnerName, bizRuleCertFileName, validationStatus,
                                                        bizRulesValidationResult.BizRuleValidationExecutionTime, fileName, "Biz Rule", dal);
                    }
                }
            }
        }
        // This function is invoked from Ux
        public static BizRulesValidationResult ValidateBizRules(string userName, string homeOrg, ZipArchive zipArchive,
                                                                BizRuleCertMetadata bizRuleCertMetadata, IDalManager dalManager)
        {
            if (zipArchive == null)
            {
                throw new ArgumentNullException("zipArchive", "zipFile cannot be empty");
            }

            if (bizRuleCertMetadata == null)
            {
                throw new ArgumentNullException("bizRuleCertMetadata", "bizRuleCertMetadata cannot be empty");
            }

            if (dalManager == null)
            {
                throw new ArgumentNullException("dalManager", "dalManager cannot be null");
            }

            #region DummyCode
            if (GCValidatorHelper.AddDummyDataForInstanceValidation)
            {
                List <BizRuleInfo> ruleInfo1 = new List <BizRuleInfo>();
                ruleInfo1.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corp - Spec Cert - 810 - Send.dat", SegmentPath = "BIG->BIG03", Value = "1/2/2014"
                });
                ruleInfo1.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corporation - Spec Cert - 4010 - 856 - receive - Comments.dat", SegmentPath = "PRF->PRF04", Value = "1/3/2014"
                });
                ruleInfo1.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corporation - Spec Cert - 4010 - 850 - send - Comments.dat", SegmentPath = "BEG->BEG05", Value = "1/2/2014"
                });

                List <BizRuleInfo> ruleInfo2 = new List <BizRuleInfo>();
                ruleInfo2.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corp - Spec Cert - 810 - Send", SegmentPath = "BIG->BIG04", Value = "122014"
                });
                ruleInfo2.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corporation - Spec Cert - 4010 - 856 - receive - Comments.dat", SegmentPath = "PRF->PRF01", Value = "122014"
                });
                ruleInfo2.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corporation - Spec Cert - 4010 - 850 - send - Comments.dat", SegmentPath = "BEG->BEG03", Value = "122014"
                });

                List <BizRuleInfo> ruleInfo3 = new List <BizRuleInfo>();
                ruleInfo3.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corp - Spec Cert - 810 - Send.dat", SegmentPath = "IT1->IT101", Value = "123"
                });
                ruleInfo3.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corporation - Spec Cert - 4010 - 856 - receive - Comments.dat", SegmentPath = "LIN->LIN01", Value = "234"
                });
                ruleInfo3.Add(new BizRuleInfo()
                {
                    FileName = "Delphi Corporation - Spec Cert - 4010 - 850 - send - Comments.dat", SegmentPath = "PO1->PO1O1", Value = "345"
                });

                BizRulesValidationResult bizRulesValidationResultDummy = new BizRulesValidationResult()
                {
                    BizRuleCertName = "3M - Biz Rule - X12 810-856-850"
                };
                bizRulesValidationResultDummy.BizRuleValidationResults = new List <BizRuleValidationResult>();
                bizRulesValidationResultDummy.BizRuleValidationResults.Add(new BizRuleValidationResult()
                {
                    RuleName = "Purchase Order Date",
                    Type     = ResultType.Error,
                    RuleInfo = ruleInfo1
                });
                bizRulesValidationResultDummy.BizRuleValidationResults.Add(new BizRuleValidationResult()
                {
                    RuleName = "Purchase Order Number",
                    Type     = ResultType.Success,
                    RuleInfo = ruleInfo2
                });
                bizRulesValidationResultDummy.BizRuleValidationResults.Add(new BizRuleValidationResult()
                {
                    RuleName = "Assigned Identification",
                    Type     = ResultType.Warning,
                    RuleInfo = ruleInfo3
                });

                bizRulesValidationResultDummy.EdiValidationResults = new List <EDIValidationResult>();
                List <SegmentValidationResult> segmentValidationResult = new List <SegmentValidationResult>();
                segmentValidationResult.Add(new SegmentValidationResult()
                {
                    Description    = "Invalid Segment XYZ",
                    Name           = "XYZ",
                    SequenceNumber = 5,
                    Type           = ResultType.Error,
                    StartIndex     = 300,
                    EndIndex       = 305,
                });

                bizRulesValidationResultDummy.EdiValidationResults.Add(new EDIValidationResult()
                {
                    BeautifiedOriginalPayload = "EDI File Contents",
                    FileName   = "Delphi Corp - Spec Cert - 810 - Send.dat",
                    SchemaName = "Delphi Corp - Spec Cert - 810 - Send",
                    SegmentValidationResults = new List <SegmentValidationResult>(),    // No validation failures
                    TransactionNumbers       = new List <string> {
                        "1"
                    },
                    DisplayName = "Delphi Corp - 810 - Send",
                    Type        = "X12_810"
                });
                bizRulesValidationResultDummy.EdiValidationResults.Add(new EDIValidationResult()
                {
                    BeautifiedOriginalPayload = "EDI File Contents",
                    FileName   = "Delphi Corporation - Spec Cert - 4010 - 850 - send - Comments.dat",
                    SchemaName = "Delphi Corporation - Spec Cert - 4010 - 850 - send",
                    SegmentValidationResults = segmentValidationResult,
                    TransactionNumbers       = new List <string> {
                        "1"
                    },
                    DisplayName = "Delphi Corporation - 850 - send",
                    Type        = "X12_850"
                });
                bizRulesValidationResultDummy.EdiValidationResults.Add(new EDIValidationResult()
                {
                    BeautifiedOriginalPayload = "EDI File Contents",
                    FileName   = "Delphi Corporation - Spec Cert - 4010 - 856 - receive - Comments.dat",
                    SchemaName = "Delphi Corporation - Spec Cert - 4010 - 856 - receive",
                    SegmentValidationResults = new List <SegmentValidationResult>(),    // No validation failures
                    TransactionNumbers       = new List <string> {
                        "1"
                    },
                    DisplayName = "Delphi Corporation - 856 - receive",
                    Type        = "X12_856"
                });

                return(bizRulesValidationResultDummy);
            }
            #endregion

            BizRulesValidationResult bizRulesValidationResult = null;
            string errorMessage = null;

            try
            {
                BizRuleSet bizRuleSet = SchemaCache.GetBizRuleSet(bizRuleCertMetadata, dalManager);

                List <ZipFileEntry> fileEntries = ZipFileUtil.GetFileEntries(zipArchive);
                List <DocumentPlugValidationInfo> documentPlugValidationInfoList = new List <DocumentPlugValidationInfo>();
                foreach (ZipFileEntry fileEntry in fileEntries)
                {
                    documentPlugValidationInfoList.Add(GetDocumentPlugValidationInfo(fileEntry, dalManager));
                }

                bizRulesValidationResult = ValidateBizRules(userName, homeOrg, bizRuleCertMetadata.RuleCertFileName,
                                                            bizRuleSet, documentPlugValidationInfoList, dalManager);
            }
            catch (GCEdiValidatorException gcException)
            {
                errorMessage = gcException.Message;
            }
            catch (Exception)
            {
                errorMessage = "Internal error occurred, please contact Maarg";
            }

            if (errorMessage != null)
            {
                // TODO: Add generic error
            }

            return(bizRulesValidationResult);
        }