public static async Task <IEnumerable <Provision> > PrecedentSearchAsync(SearchRequest request, string connectionString)
        {
            if (request == null || request.IsEmpty())
            {
                return(null);
            }
            using (var dataAccess = new MasterSideLetterDataAccess(connectionString))
            {
                var searchSettings = await dataAccess.GetSearchSettingsAsync();

                var provisionHits = new List <Provision>();
                var currentScores = new[] { 0d, 0d, 0d, 0d };
                //var currentScore = 0d;
                var currentContent = "";


                foreach (var provision in dataAccess.GetFilteredProvisions(request))
                {
                    if (provision.Content != currentContent)
                    {
                        currentContent = provision.Content;
                        currentScores  = currentContent == request.TargetText ? new[] { 1d, 1d, 1d, 1d } : GetProvisionScores(searchSettings, request.TargetText, currentContent);
                    }

                    if (currentScores[0] >= searchSettings.WeightedThreshold ||
                        currentScores[1] >= searchSettings.Algorithm1Threshold ||
                        currentScores[2] >= searchSettings.Algorithm2Threshold ||
                        currentScores[3] >= searchSettings.Algorithm3Threshold)
                    {
                        provision.HtmlContent = GetProvisionDifferences(request.TargetText, currentContent);
                        provision.MatchScore  = currentScores[0];
                        provisionHits.Add(provision);
                    }
                }
                return(provisionHits);
            }
        }
        public static async Task <SearchResults> GetResultsAsync(this MasterSideLetterDataAccess dataAccess, SearchRequest request)
        {
            var allResults = new SearchResults
            {
                FundRows       = new List <Fund>(),
                InvestorRows   = new List <Investor>(),
                SideLetterRows = new List <FundInvestor>(),
                ProvisionRows  = new List <Provision>()
            };

            if (request == null || request.IsEmpty())
            {
                return(allResults);
            }


            if (request.SearchCategory == "All" || request.SearchCategory == "Fund")
            {
                //only return Funds if the user specified Fund criteria or search text
                if (!string.IsNullOrEmpty(request.TargetText) ||
                    (request.FundValues != null && request.FundValues.Length > 0) ||
                    (request.SponsorValues != null && request.SponsorValues.Length > 0) ||
                    (request.BusinessUnitValues != null && request.BusinessUnitValues.Length > 0) ||
                    (request.StrategyValues != null && request.StrategyValues.Length > 0) ||
                    request.SizeMin.HasValue ||
                    request.SizeMax.HasValue ||
                    request.YearMin.HasValue ||
                    request.YearMax.HasValue)
                {
                    allResults.FundRows.AddRange(await dataAccess.SearchFundsAsync(request));
                }
            }

            if (request.SearchCategory == "All" || request.SearchCategory == "Investor")
            {
                //only return Investors if the user specified Investor criteria or search text
                if (!string.IsNullOrEmpty(request.TargetText) ||
                    (request.InvestorValues != null && request.InvestorValues.Length > 0) ||
                    (request.InvestorTypeValues != null && request.InvestorTypeValues.Length > 0) ||
                    request.AggregateSizeMin.HasValue ||
                    request.AggregateSizeMax.HasValue)
                {
                    allResults.InvestorRows.AddRange(await dataAccess.SearchInvestorsAsync(request));
                }
            }

            if (request.SearchCategory == "All" || request.SearchCategory == "SideLetter")
            {
                if (!string.IsNullOrEmpty(request.TargetText) ||
                    request.FundValues != null && request.FundValues.Length > 0 ||
                    request.InvestorValues != null && request.InvestorValues.Length > 0 ||
                    request.SponsorValues != null && request.SponsorValues.Length > 0 ||
                    request.BusinessUnitValues != null && request.BusinessUnitValues.Length > 0 ||
                    request.StrategyValues != null && request.StrategyValues.Length > 0 ||
                    request.InvestorTypeValues != null && request.InvestorTypeValues.Length > 0 ||
                    request.EntityValues != null && request.EntityValues.Length > 0 ||
                    request.CounselValues != null && request.CounselValues.Length > 0 ||
                    request.SizeMin.HasValue ||
                    request.SizeMax.HasValue ||
                    request.YearMin.HasValue ||
                    request.YearMax.HasValue ||
                    request.CommitmentMin.HasValue ||
                    request.CommitmentMax.HasValue ||
                    request.AggregateSizeMin.HasValue ||
                    request.AggregateSizeMax.HasValue ||
                    request.InvestorId.HasValue ||
                    request.FundId.HasValue)
                {
                    allResults.SideLetterRows.AddRange(await dataAccess.SearchFundInvestorsAsync(request));
                }
            }

            if (request.SearchCategory == "All" || request.SearchCategory == "Provision")
            {
                if (!string.IsNullOrEmpty(request.TargetText) ||
                    request.FundValues != null && request.FundValues.Length > 0 ||
                    request.InvestorValues != null && request.InvestorValues.Length > 0 ||
                    request.SponsorValues != null && request.SponsorValues.Length > 0 ||
                    request.BusinessUnitValues != null && request.BusinessUnitValues.Length > 0 ||
                    request.StrategyValues != null && request.StrategyValues.Length > 0 ||
                    request.InvestorTypeValues != null && request.InvestorTypeValues.Length > 0 ||
                    request.EntityValues != null && request.EntityValues.Length > 0 ||
                    request.CounselValues != null && request.CounselValues.Length > 0 ||
                    request.ProvisionTypeValues != null && request.ProvisionTypeValues.Length > 0 ||
                    request.SizeMin.HasValue ||
                    request.SizeMax.HasValue ||
                    request.YearMin.HasValue ||
                    request.YearMax.HasValue ||
                    request.CommitmentMin.HasValue ||
                    request.CommitmentMax.HasValue ||
                    request.AggregateSizeMin.HasValue ||
                    request.AggregateSizeMax.HasValue ||
                    request.InvestorId.HasValue ||
                    request.FundId.HasValue)
                {
                    allResults.ProvisionRows.AddRange(await dataAccess.SearchProvisionsAsync(request));
                }
            }

            return(allResults);
        }