示例#1
0
        public PagedQueryResult <Pass> Search(PassSearchQueryModel queryModel, StringMatchRules freeTextMatchRules)
        {
            var query = PassQuery;

            if (queryModel.IsLibraried != null)
            {
                query = queryModel.IsLibraried.Value
                    ? query.Where(e => e.IsLibraried == true)
                    : query.Where(e => e.IsLibraried == null || e.IsLibraried == false);
            }

            if (!string.IsNullOrWhiteSpace(queryModel.Name))
            {
                var searchCondition = _searchConditionBuilder
                                      .StartAnyWith(queryModel.Name.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries))
                                      .Build();

                query = query.Where(p => EF.Functions.Contains(p.Name, searchCondition));
            }

            query = (queryModel.OrderBy?.Any() ?? false)
                ? query.OrderByMultipleItems(queryModel.OrderBy)
                : query.OrderBy(p => p.Name);

            List <Pass> passList = _mapper.Map <List <Pass> >(
                query.Where(e => freeTextMatchRules.IsMatches(e.Name, queryModel.Name)).AsNoTracking(),
                opt => opt.UseEntityCache(_salesAreaByIdCache));

            return(new PagedQueryResult <Pass>(passList.Count,
                                               passList.ApplyPaging(queryModel.Skip, queryModel.Top).ToList()));
        }
示例#2
0
        protected CallMethodResult Search(
            string description,
            DateTime?executedStartDate,
            DateTime?executedEndDate,
            List <RunStatus> status,
            StringMatchHowManyWordsToMatch howManyWordsToMatch,
            StringMatchWordOrders wordOrder,
            StringMatchWordComparisons wordComparison,
            bool caseSensitive,
            string charactersToIgnore)
        {
            var queryModel = new RunSearchQueryModel
            {
                Description       = description,
                ExecutedStartDate = executedStartDate,
                ExecutedEndDate   = executedEndDate,
                Status            = status
            };

            var stringMatchRules = new StringMatchRules(howManyWordsToMatch, wordOrder, wordComparison, caseSensitive,
                                                        new char[] { ' ' }, charactersToIgnore.ToCharArray());

            var res = Repository.Search(queryModel, stringMatchRules);

            TestContext.LastOperationCount   = res?.Items?.Count ?? 0;
            TestContext.LastCollectionResult = res?.Items;
            TestContext.LastSingleResult     = null;

            return(CallMethodResult.CreateHandled());
        }
示例#3
0
        public IHttpActionResult GetSearch([FromUri(Name = "")] PassSearchQueryModel queryModel)
        {
            if (queryModel == null || !ModelState.IsValid)
            {
                return(this.Error().InvalidParameters());
            }

            // Search
            var freeTextMatchRules = new StringMatchRules(StringMatchHowManyWordsToMatch.AllWords, StringMatchWordOrders.AnyOrder, StringMatchWordComparisons.ContainsWord, false, new Char[] { ' ', ',' }, null);
            var passes             = _passRepository.Search(queryModel, freeTextMatchRules);

            // Map to model
            var passModels = Mappings.MapToPassModels(passes.Items.ToList(), _passRepository, _mapper);
            var results    = new PagedQueryResult <PassModel>(passes.TotalCount, passModels);

            return(Ok(results));
        }
示例#4
0
        protected CallMethodResult Search(
            string name,
            bool?isLibraried,
            StringMatchHowManyWordsToMatch howManyWordsToMatch,
            StringMatchWordOrders wordOrder,
            StringMatchWordComparisons wordComparison,
            bool caseSensitive)
        {
            var queryModel = new PassSearchQueryModel
            {
                Name        = name,
                IsLibraried = isLibraried
            };

            var freeTextMatchRules = new StringMatchRules(howManyWordsToMatch, wordOrder, wordComparison, caseSensitive,
                                                          new Char[] { ' ', ',' }, null);
            var res = Repository.Search(queryModel, freeTextMatchRules);

            TestContext.LastOperationCount   = res?.Items?.Count ?? 0;
            TestContext.LastCollectionResult = res?.Items?.Cast <object>().ToList();
            TestContext.LastSingleResult     = TestContext.LastOperationCount == 1 ? res.Items.First() : null;

            return(CallMethodResult.CreateHandled());
        }
示例#5
0
        private List <RunExtendedSearchModel> ApplyFreeTextMatchRules(List <RunExtendedSearchModel> runs, StringMatchRules freeTextMatchRules, string description)
        {
            if (string.IsNullOrWhiteSpace(description))
            {
                return(runs);
            }

            return(runs.Where(r => freeTextMatchRules.IsMatches(r.Description, description) ||
                              (!string.IsNullOrWhiteSpace(r.Author?.Name) &&
                               freeTextMatchRules.IsMatches(r.Author.Name, description)) ||
                              freeTextMatchRules.IsMatches(r.Id.ToString(), description) ||
                              r.ScenarioIds.Any(id => freeTextMatchRules.IsMatches(id, description)) ||
                              r.ScenarioNames.Any(name => freeTextMatchRules.IsMatches(name, description)) ||
                              r.PassNames.Any(name => freeTextMatchRules.IsMatches(name, description)) ||
                              r.PassIds.Any(id => freeTextMatchRules.IsMatches(id, description))).ToList());
        }
示例#6
0
        public PagedQueryResult <RunExtendedSearchModel> Search(RunSearchQueryModel queryModel, StringMatchRules freeTextMatchRules)
        {
            if (queryModel == null)
            {
                throw new ArgumentNullException(nameof(queryModel));
            }

            if (queryModel.Orderby?.Any() ?? false)
            {
                queryModel.Orderby.ForEach(i =>
                {
                    if (string.Equals(i.OrderBy, "ExecuteStartedDateTime", StringComparison.InvariantCultureIgnoreCase))
                    {
                        i.OrderBy = nameof(RunDto.CreatedOrExecuteDateTime);
                    }
                });
            }

            var query =
                (from run in _dbContext.Query <Entities.Tenant.Runs.Run>()
                 join rsj in _dbContext.Query <Entities.Tenant.Runs.RunScenario>() on run.Id equals rsj.RunId
                 join sj in _dbContext.Query <Entities.Tenant.Scenarios.Scenario>() on rsj.ScenarioId equals sj.Id
                 into scenarios
                 from s in scenarios.DefaultIfEmpty()
                 join prj in _dbContext.Query <Entities.Tenant.Scenarios.ScenarioPassReference>() on s.Id equals prj.ScenarioId
                 into passReferences
                 from pr in passReferences.DefaultIfEmpty()
                 join pj in _dbContext.Query <Pass>() on pr.PassId equals pj.Id
                 into passes
                 from p in passes.DefaultIfEmpty()
                 select new { run, s, p }).AsQueryable();

            if (queryModel.RunPeriodStartDate.HasValue && queryModel.RunPeriodEndDate.HasValue)
            {
                query = query.Where(q => q.run.StartDate <= queryModel.RunPeriodEndDate &&
                                    q.run.EndDate >= queryModel.RunPeriodStartDate);
            }

            if (queryModel.ExecutedStartDate.HasValue && queryModel.ExecutedEndDate.HasValue)
            {
                query = query.Where(q => (q.run.ExecuteStartedDateTime ?? q.run.CreatedDateTime) >= queryModel.ExecutedStartDate &&
                                    (q.run.ExecuteStartedDateTime ?? q.run.CreatedDateTime) <= queryModel.ExecutedEndDate);
            }

            if (queryModel.Users?.Any() ?? false)
            {
                query = query.Where(x => queryModel.Users.Contains(x.run.Author.AuthorId));
            }

            if (queryModel.Status?.Any() ?? false)
            {
                var entityStatuses = queryModel.Status.Cast <Entities.RunStatus>().ToArray();
                query = query.Where(x => entityStatuses.Contains(x.run.RunStatus));
            }

            if (!string.IsNullOrEmpty(queryModel.Description))
            {
                var condition = _searchConditionBuilder.StartAnyWith(queryModel.Description.Split(new[] { ' ' })).Build();
                query = query.Where(q =>
                                    EF.Functions.Contains(EF.Property <string>(q.run, Entities.Tenant.Runs.Run.SearchField), condition) ||
                                    EF.Functions.Contains(q.run.Author.Name, condition) ||
                                    EF.Functions.Contains(EF.Property <string>(q.s, Entities.Tenant.Scenarios.Scenario.SearchField), condition) ||
                                    EF.Functions.Contains(EF.Property <string>(q.p, Entities.Tenant.Passes.Pass.SearchField), condition));
            }

            var filteredQuery = query.Select(x => new
            {
                x.run.Id,
                AuthorId   = x.run.Author.Id,
                AuthorName = x.run.Author.Name,
                x.run.RunStatus,
                x.run.StartDate,
                x.run.StartTime,
                x.run.EndDate,
                x.run.EndTime,
                x.run.CreatedOrExecuteDateTime,
                x.run.Description
            }).Distinct();

            var extendedSearchQuery = queryModel.Orderby != null
                ? filteredQuery.OrderByMultipleItems(queryModel.Orderby)
                : filteredQuery.OrderBySingleItem(nameof(Entities.Tenant.Runs.Run.Id), OrderDirection.Asc);

            // If there is a valid description then do not apply paging now,
            // since we need to filter further by applying free text matching rules
            var shouldApplyFreeTextMatchRules = !string.IsNullOrWhiteSpace(queryModel.Description);

            extendedSearchQuery = shouldApplyFreeTextMatchRules ?
                                  extendedSearchQuery :
                                  extendedSearchQuery.ApplyDefaultPaging(queryModel.Skip, queryModel.Top);

            var totalCount = filteredQuery.Count();
            var runIds     = extendedSearchQuery.Select(x => x.Id).ToList();

            var runs = _mapper.Map <List <RunExtendedSearchModel> >(_dbContext.Query <Entities.Tenant.Runs.Run>()
                                                                    .Include(x => x.Author)
                                                                    .Include(x => x.Scenarios)
                                                                    .ThenInclude(x => x.Scenario)
                                                                    .ThenInclude(x => x.PassReferences)
                                                                    .ThenInclude(x => x.Pass)
                                                                    .Include(x => x.Scenarios)
                                                                    .ThenInclude(x => x.ExternalRunInfo)
                                                                    .Where(x => runIds.Contains(x.Id))
                                                                    .AsEnumerable()
                                                                    .Select(x => new RunDto
            {
                Id                          = x.Id,
                CustomId                    = x.CustomId,
                Description                 = x.Description,
                CreatedDateTime             = x.CreatedDateTime,
                StartDate                   = x.StartDate,
                StartTime                   = x.StartTime,
                EndDate                     = x.EndDate,
                EndTime                     = x.EndTime,
                LastModifiedDateTime        = x.LastModifiedDateTime,
                ExecuteStartedDateTime      = x.ExecuteStartedDateTime,
                IsLocked                    = x.IsLocked,
                InventoryLock               = x.InventoryLock,
                Real                        = x.Real,
                Smooth                      = x.Smooth,
                AuthorId                    = x.Author.Id,
                AuthorName                  = x.Author.Name,
                SmoothDateStart             = x.SmoothDateStart,
                SmoothDateEnd               = x.SmoothDateEnd,
                ISR                         = x.ISR,
                ISRDateStart                = x.ISRDateStart,
                ISRDateEnd                  = x.ISRDateEnd,
                Optimisation                = x.Optimisation,
                OptimisationDateStart       = x.OptimisationDateStart,
                OptimisationDateEnd         = x.OptimisationDateEnd,
                RightSizer                  = x.RightSizer,
                RightSizerDateStart         = x.RightSizerDateStart,
                RightSizerDateEnd           = x.RightSizerDateEnd,
                SpreadProgramming           = x.SpreadProgramming,
                SkipLockedBreaks            = x.SkipLockedBreaks,
                IgnorePremiumCategoryBreaks = x.IgnorePremiumCategoryBreaks,
                ExcludeBankHolidays         = x.ExcludeBankHolidays,
                ExcludeSchoolHolidays       = x.ExcludeSchoolHolidays,
                Objectives                  = x.Objectives,
                CreatedOrExecuteDateTime    = x.CreatedOrExecuteDateTime,
                RunStatus                   = x.RunStatus,
                RunScenarios                = x.Scenarios.ToList(),
                Scenarios                   = x.Scenarios.Select(rs => rs.Scenario).ToList(),
                Passes                      = x.Scenarios.Where(rs => rs.Scenario != null).SelectMany(rs => rs.Scenario.PassReferences.Select(p => p.Pass)).ToList()
            }).OrderBy(x => runIds.IndexOf(x.Id)));

            if (shouldApplyFreeTextMatchRules && totalCount > 0)
            {
                // Filter runs, necessary because Sql server cannot filter sufficiently. This is nasty
                // but necessary because Sql server cannot filter the data as required
                runs       = ApplyFreeTextMatchRules(runs, freeTextMatchRules, queryModel.Description);
                totalCount = runs.Count;
                runs       = runs.ApplyDefaultPaging(queryModel.Skip, queryModel.Top).ToList();
            }

            return(new PagedQueryResult <RunExtendedSearchModel>(totalCount, runs));
        }