예제 #1
0
        private object Execute(IRavenQueryable <All_Activities.Result> query, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            var profiler = MiniProfiler.Current;

            if (activityInput.NewerThan.HasValue)
            {
                query = query.Where(x => x.CreatedDateTime > activityInput.NewerThan.Value);
            }
            else if (activityInput.OlderThan.HasValue)
            {
                query = query.Where(x => x.CreatedDateTime < activityInput.OlderThan.Value);
            }

            RavenQueryStatistics stats;

            List <dynamic> activities;

            using (profiler.Step("Get top 10 activity list RavenDB query"))
            {
                activities = query
                             .Statistics(out stats)
                             .OrderByDescending(x => x.CreatedDateTime)
                             .Skip(pagingInput.GetSkipIndex())
                             .Take(pagingInput.GetPageSize())
                             .As <dynamic>()
                             .ToList();
            }


            var contributionIds = activities
                                  .SelectMany(x => new[] { x.ContributionId, x.SubContributionId })
                                  .Where(x => !string.IsNullOrWhiteSpace(x))
                                  .Cast <string>()
                                  .Distinct()
                                  .ToList();

            List <All_Contributions.Result> contributions;

            using (profiler.Step("Get the 10 contributions RavenDB query"))
            {
                contributions = _documentSession
                                .Query <All_Contributions.Result, All_Contributions>()
                                .AsProjection <All_Contributions.Result>()
                                .Include(x => x.Contribution.User.Id)
                                .Where(x => x.ParentContributionId.In(contributionIds))
                                .ToList();
            }

            User authenticatedUser = null;

            using (profiler.Step("Get authenticated user RavenDB query"))
            {
                if (_userContext.IsUserAuthenticated())
                {
                    authenticatedUser = _documentSession.Load <User>(_userContext.GetAuthenticatedUserId());
                }
            }

            using (profiler.Step("Build activity list view model"))
            {
                return(new PagedList <object>()
                {
                    Page = pagingInput.Page,
                    PageSize = pagingInput.PageSize,
                    TotalResultCount = stats.TotalResults,
                    PagedListItems = activities.Select(x =>
                    {
                        dynamic activity = new ExpandoObject();

                        activity.Id = x.Id;
                        activity.Type = x.Type;
                        activity.CreatedDateTime = x.CreatedDateTime;
                        activity.CreatedDateTimeOrder = x.CreatedDateTimeOrder;
                        activity.Description = x.Description;

                        var user = _documentSession.Load <User>((string)x.User.Id);
                        activity.User = _userViewFactory.Make(user, null);
                        activity.Groups = x.Groups;
                        activity.ContributionId = x.ContributionId;
                        activity.SubContributionId = x.SubContributionId;

                        if (x.Type == "sightingadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && (y.ParentContributionType == "observation" || y.ParentContributionType == "record") && y.SubContributionType == null);

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("sighting");
                            }
                            else
                            {
                                activity.ObservationAdded = new
                                {
                                    Observation = _sightingViewFactory.Make(result.Contribution as Sighting, result.User, result.Groups, authenticatedUser)
                                };
                            }

                            //if (x.RecordAdded != null)
                            //{
                            //    activity.RecordAdded = new
                            //        {
                            //            Record = sighting
                            //        };
                            //}
                        }

                        if (x.Type == "identificationadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.SubContributionId == x.SubContributionId && y.SubContributionType == "identification");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("identification");
                            }
                            else
                            {
                                activity.IdentificationAdded = new
                                {
                                    Sighting = _sightingViewFactory.Make(result.ParentContribution as Sighting, result.User, result.Groups, authenticatedUser),
                                    Identification = _identificationViewFactory.Make(result.ParentContribution as Sighting, result.Contribution as IdentificationNew, result.User, authenticatedUser)
                                };
                            }
                        }

                        if (x.Type == "sightingnoteadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.SubContributionId == x.SubContributionId && y.SubContributionType == "note");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("note");
                            }
                            else
                            {
                                activity.SightingNoteAdded = new
                                {
                                    Sighting = _sightingViewFactory.Make(result.ParentContribution as Sighting, result.User, result.Groups, authenticatedUser),
                                    SightingNote = _sightingNoteViewFactory.Make(result.ParentContribution as Sighting, result.Contribution as SightingNote, result.User, authenticatedUser)
                                };
                            }
                        }

                        if (x.Type == "postadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.ParentContributionType == "post");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("news item");
                            }
                            else
                            {
                                activity.PostAdded = new
                                {
                                    Post = _postViewFactory.Make(result.Contribution as Post, result.User, result.Groups.First(), authenticatedUser)
                                };
                            }
                        }

                        return activity;
                    })
                });
            }
        }
        public object BuildSpeciesList(SpeciesQueryInput speciesQueryInput, PagingInput pagingInput)
        {
            Check.RequireNotNull(pagingInput, "pagingInput");

            RavenQueryStatistics stats;

            var field     = string.IsNullOrWhiteSpace(speciesQueryInput.Field) ? string.Empty : speciesQueryInput.Field;
            var queryText = string.IsNullOrWhiteSpace(speciesQueryInput.Query) ? string.Empty : speciesQueryInput.Query;
            var category  = string.IsNullOrWhiteSpace(speciesQueryInput.Category) ? string.Empty : speciesQueryInput.Category;

            if (field.ToLower() == "allranks")
            {
                // Eg: query=Animalia: Chordata: Amphibia&field=rankall to get all browsable ranks for each rank level of the specified taxonomic classification
                var ranks        = System.Web.HttpUtility.HtmlDecode(queryText).Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim());
                var queryResults = new List <object>();

                queryResults.Add(
                    _documentSession
                    .Advanced
                    .LuceneQuery <All_Species.Result, All_Species>()
                    .SelectFields <All_Species.Result>("Taxonomy", "Name", "RankPosition", "RankName", "RankType", "ParentRankName", "Ranks", "Category", "SpeciesCount", "CommonGroupNames", "CommonNames", "Synonyms", "SightingCount")
                    .Statistics(out stats)
                    .WhereEquals("RankPosition", 1)
                    .OrderBy(x => x.Name)
                    .Take(1024)
                    .ToList()
                    .Select(x => MakeSpecies(x, speciesQueryInput.LimitCommonNames))
                    .ToPagedList(
                        pagingInput.GetPage(),
                        pagingInput.GetPageSize(),
                        stats.TotalResults
                        ));

                // Get all the ranks of the specified taxonomy, plus the next rank, so as the user can browse it
                var rankCountToGet = ranks.Count() + 1 > 8 ? 8 : ranks.Count() + 1;

                for (var rankIndex = 1; rankIndex < rankCountToGet; rankIndex++)
                {
                    queryResults.Add(
                        _documentSession
                        .Advanced
                        .LuceneQuery <All_Species.Result, All_Species>()
                        .SelectFields <All_Species.Result>("Taxonomy", "Name", "RankPosition", "RankName", "RankType", "ParentRankName", "Ranks", "Category", "SpeciesCount", "CommonGroupNames", "CommonNames", "Synonyms", "SightingCount")
                        .Statistics(out stats)
                        .WhereEquals("ParentRankName", ranks.ElementAt(rankIndex - 1))
                        .AndAlso()
                        .WhereEquals("RankPosition", rankIndex + 1)
                        .OrderBy(x => x.Name)
                        .Take(1024)
                        .ToList()
                        .Select(x => MakeSpecies(x, speciesQueryInput.LimitCommonNames))
                        .ToPagedList(
                            pagingInput.GetPage(),
                            pagingInput.GetPageSize(),
                            stats.TotalResults
                            ));
                }

                return(queryResults);
            }

            var getAllPages = false;

            var query = _documentSession
                        .Advanced
                        .LuceneQuery <All_Species.Result, All_Species>()
                        .SelectFields <All_Species.Result>("Taxonomy", "Name", "RankPosition", "RankName", "RankType", "ParentRankName", "Ranks", "Category", "SpeciesCount", "CommonGroupNames", "CommonNames", "Synonyms", "SightingCount")
                        .Statistics(out stats);

            if (field.ToLower() == "taxonomy")
            {
                query.WhereEquals("Taxonomy", queryText);
            }
            else if (field.ToLower() == "scientific")
            {
                query.WhereStartsWith("AllScientificNames", queryText);
            }
            else if (field.ToLower() == "common")
            {
                query.WhereStartsWith("AllCommonNames", queryText);
            }
            else if (field.ToLower() == "rankposition")
            {
                // Eg: query=1&field=rankposition to get all rank values in position 1
                query.WhereEquals("RankPosition", queryText);
            }
            else if (field.ToLower() == "ranktype")
            {
                // Eg: query=kingdom&field=ranktype to get all kingdoms
                query.WhereEquals("RankType", queryText);
            }
            else if (field.ToLower() == "rank2" ||
                     field.ToLower() == "rank3" ||
                     field.ToLower() == "rank4" ||
                     field.ToLower() == "rank5" ||
                     field.ToLower() == "rank6" ||
                     field.ToLower() == "rank7" ||
                     field.ToLower() == "rank8")
            {
                // Eg: query=animalia&field=rank2 to get all ranks in rank 2 that have a parent rank named "animalia"
                query
                .WhereEquals("ParentRankName", queryText)
                .AndAlso()
                .WhereEquals("RankPosition", field.ToLower().Replace("rank", string.Empty));

                getAllPages = true;
            }
            else
            {
                query.WhereStartsWith("AllNames", queryText);
            }

            if (!string.IsNullOrWhiteSpace(category))
            {
                // Additionally restrict to a single category
                query.AndAlso().WhereEquals("Category", category);
            }

            return(query
                   .OrderBy(x => x.Name)
                   .Skip(pagingInput.GetSkipIndex())
                   .Take(getAllPages ? 1024 : pagingInput.GetPageSize())
                   .ToList()
                   .Select(x => MakeSpecies(x, speciesQueryInput.LimitCommonNames, queryText))
                   .ToPagedList(
                       pagingInput.GetPage(),
                       pagingInput.GetPageSize(),
                       stats.TotalResults
                       ));
        }