Ejemplo n.º 1
0
        /// <summary>
        /// Create a new instance.
        /// </summary>
        /// <param name="entryCriteria">The <see cref="EntryCriteria"/> used for the search.</param>
        /// <param name="resultOrder">The order of the results.</param>
        public EntrySearchCacheKey(EntryCriteria entryCriteria, ListOrder resultOrder)
        {
            Assert.ArgumentNotNull(entryCriteria, nameof(entryCriteria));

            EntryCriteria = entryCriteria;
            ResultOrder   = resultOrder;
        }
Ejemplo n.º 2
0
        protected SearchResults <Model.Entry> GetEntriesCloseToEntry(EntryItem entry, ListOrder resultOrder, Action <EntryCriteria> mutator)
        {
            var blogHomeItem = BlogManager.GetCurrentBlog(entry);

            if (blogHomeItem == null)
            {
                return(null);
            }

            var pageSizes = new[] { 5, 50, 100, int.MaxValue };

            foreach (var pageSize in pageSizes)
            {
                var criteria = new EntryCriteria
                {
                    PageNumber = 1,
                    PageSize   = pageSize
                };

                mutator.Invoke(criteria);

                var entries = EntryManager.GetBlogEntries(blogHomeItem, criteria, resultOrder);

                for (var i = 0; i < entries.Results.Count; i++)
                {
                    if (entries.Results[i].Uri.ItemID == entry.ID && i < entries.Results.Count - 1)
                    {
                        return(entries);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        public void GetBlogEntries_LimitedMultipleBlogs()
        {
            var blog1       = TestUtil.CreateNewBlog(TestContentRoot);
            var entryLuna   = TestUtil.CreateNewEntry(blog1, "Luna", entryDate: new DateTime(2012, 3, 1));
            var entryDeimos = TestUtil.CreateNewEntry(blog1, "Deimos", entryDate: new DateTime(2012, 3, 2));
            var entryPhobos = TestUtil.CreateNewEntry(blog1, "Phobos", entryDate: new DateTime(2012, 3, 3));

            var blog2         = TestUtil.CreateNewBlog(TestContentRoot);
            var entryAdrastea = TestUtil.CreateNewEntry(blog2, "Adrastea", entryDate: new DateTime(2012, 3, 1));
            var entryAitne    = TestUtil.CreateNewEntry(blog2, "Aitne", entryDate: new DateTime(2012, 3, 2));
            var entryAmalthea = TestUtil.CreateNewEntry(blog2, "Amalthea", entryDate: new DateTime(2012, 3, 3));
            var entryAnanke   = TestUtil.CreateNewEntry(blog2, "Ananke", entryDate: new DateTime(2012, 3, 4));

            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 3
            };

            var results = manager.GetBlogEntries(entryAmalthea, criteria, ListOrder.Descending);
            var ids     = from result in results.Results select result.Uri.ItemID;

            Assert.That(ids, Is.EqualTo(new[] { entryAnanke.ID, entryAmalthea.ID, entryAitne.ID }));
            Assert.That(results.HasMoreResults, Is.True);
        }
    public IEnumerable<AtomEntry> GetEntries(EntryCriteria criteria, int pageIndex, int pageSize, out int totalRecords)
    {
      string[] paths = criteria.EntryId != null ?
                pathResolver.GetEntryFilePaths(criteria.EntryId, criteria.Annotations, criteria.Deep) :
        pathResolver.GetEntryFilePaths(criteria.WorkspaceName, criteria.CollectionName, criteria.Annotations, criteria.Deep);

      Func<AtomEntry, bool> where = (entry) =>
        whereAuth(entry, criteria) &&
      (criteria.StartDate.HasValue && criteria.EndDate.HasValue ? whereDate(entry, criteria) : true) &&
      (criteria.CategoryTerm != null ? whereCategory(entry, criteria) : true) &&
      (criteria.PersonName != null ? wherePerson(entry, criteria) : true) &&
      (criteria.SearchTerm != null ? whereSearch(entry, criteria) : true) &&
      (criteria.Approved != null ? whereApproved(entry, criteria) : true) &&
      (criteria.Draft != null ? whereDraft(entry, criteria) : true) &&
      (criteria.Pending != null ? wherePending(entry, criteria) : true) &&
      (criteria.Published != null ? wherePublished(entry, criteria) : true) &&
      (criteria.Spam != null ? whereSpam(entry, criteria) : true);

      IEnumerable<AtomEntry> entries = GetEntries(paths, where);
      totalRecords = entries.Count();
      if (criteria.SortMethod == SortMethod.EditDesc) entries = entries.OrderByDescending(e => e.Edited);
      else if (criteria.SortMethod == SortMethod.DateAsc) entries = entries.OrderBy(e => e.Date);
      else if (criteria.SortMethod == SortMethod.DateDesc) entries = entries.OrderByDescending(e => e.Date);
      else if (criteria.SortMethod == SortMethod.Default && criteria.Annotations) entries = entries.OrderBy(e => e.Date);
      else if (criteria.SortMethod == SortMethod.Default && criteria.SearchTerm != null) entries = entries.OrderByDescending(e =>
          Regex.Matches(e.Xml.Value, criteria.SearchTerm, RegexOptions.IgnoreCase).Count);

      return entries.Skip(pageIndex * pageSize).Take(pageSize);
    }
Ejemplo n.º 5
0
        public void GetHashCode_DifferentProperties_DifferentHashCode()
        {
            // arrange
            var sut1 = new EntryCriteria
            {
                PageNumber = 2,
                PageSize   = 10,
                Tag        = "tag"
            };

            var sut2 = new EntryCriteria
            {
                PageNumber = 2,
                PageSize   = 10,
                Tag        = "tag",
                Category   = "cat"
            };

            // act
            var hashcode1 = sut1.GetHashCode();
            var hashcode2 = sut2.GetHashCode();

            // assert
            Assert.That(hashcode1, Is.Not.EqualTo(hashcode2));
        }
Ejemplo n.º 6
0
        public void GetBlogEntries_ByCategoryLimited()
        {
            var blog          = TestUtil.CreateNewBlog(TestContentRoot);
            var categoryAlpha = TestUtil.CreateNewCategory(blog, "Alpha");
            var categoryBeta  = TestUtil.CreateNewCategory(blog, "Beta");
            var entryLuna     = TestUtil.CreateNewEntry(blog, "Luna", categories: new[] { categoryBeta.ID }, entryDate: new DateTime(2012, 3, 1));
            var entryDeimos   = TestUtil.CreateNewEntry(blog, "Deimos", categories: new[] { categoryAlpha.ID, categoryBeta.ID }, entryDate: new DateTime(2012, 3, 2));
            var entryPhobos   = TestUtil.CreateNewEntry(blog, "Phobos", categories: new[] { categoryBeta.ID, categoryAlpha.ID }, entryDate: new DateTime(2012, 3, 3));
            var entryAdrastea = TestUtil.CreateNewEntry(blog, "Adrastea", categories: new[] { categoryBeta.ID, categoryAlpha.ID }, entryDate: new DateTime(2012, 3, 4));

            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 1,
                Category   = categoryAlpha.ID.ToString()
            };

            var results = manager.GetBlogEntries(blog, criteria, ListOrder.Descending);
            var ids     = from result in results.Results select result.Uri.ItemID;

            Assert.That(ids, Is.EqualTo(new[] { entryAdrastea.ID }));
        }
Ejemplo n.º 7
0
        public void GetSet_ForResultOrderWhichHasBeenSet_ReturnsNull()
        {
            // arrange
            var sut      = new EntrySearchCache();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 5
            };

            var list = new List <Entry>
            {
                new Entry
                {
                    Author = "admin",
                    Title  = "lorem"
                }
            };

            var results = new SearchResults <Entry>(list, false);

            // act
            sut.Set(criteria, ListOrder.Descending, results);
            var result = sut.Get(criteria, ListOrder.Ascending);

            // assert
            Assert.That(result, Is.Null);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the list of entries for the specified <see cref="EntryCriteria"/>.
        /// </summary>
        /// <param name="criteria">The criteria used to search for the entries.</param>
        /// <param name="resultOrder">The ordering of the results.</param>
        /// <returns>The list of entries for the criteria, or null if the criteria has not been cached.</returns>
        public SearchResults <Entry> Get(EntryCriteria criteria, ListOrder resultOrder)
        {
            Assert.ArgumentNotNull(criteria, nameof(criteria));

            var key = new EntrySearchCacheKey(criteria, resultOrder);

            return((SearchResults <Entry>)_cache.GetValue(key));
        }
Ejemplo n.º 9
0
        public void Equals(EntryCriteria criteria, object other, bool expected)
        {
            // act
            var result = criteria.Equals(other);

            // assert
            Assert.That(result, Is.EqualTo(expected));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Sets the list of entries for the specified <see cref="EntryCriteria"/>.
        /// </summary>
        /// <param name="criteria">The criteria used to search for the entries.</param>
        /// <param name="resultOrder">The ordering of the results.</param>
        /// <param name="entries">The entries for the search criteria.</param>
        public void Set(EntryCriteria criteria, ListOrder resultOrder, SearchResults <Entry> entries)
        {
            Assert.ArgumentNotNull(criteria, nameof(criteria));
            Assert.ArgumentNotNull(entries, nameof(entries));

            var key = new EntrySearchCacheKey(criteria, resultOrder);

            _cache.Add(key, entries);
        }
 public int DeleteEntries(EntryCriteria criteria)
 {
   int count;
   foreach (AtomEntry e in GetEntries(criteria, 0, int.MaxValue, out count))
   {
     DeleteEntry(e.Id);
   }
   return count;
 }
Ejemplo n.º 12
0
        public void GetHashCode_NothingSet_ReturnsZero()
        {
            // arrange
            var sut = new EntryCriteria();

            // act
            var hashcode = sut.GetHashCode();

            // assert
            Assert.That(hashcode, Is.EqualTo(0));
        }
Ejemplo n.º 13
0
        public override bool Equals(object obj)
        {
            if (obj is EntrySearchCacheKey)
            {
                var other = obj as EntrySearchCacheKey;

                return
                    (EntryCriteria.Equals(other.EntryCriteria) &&
                     ResultOrder.Equals(other.ResultOrder));
            }

            return(false);
        }
Ejemplo n.º 14
0
        public XmlRpcStruct[] getRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {
            Authenticate(username, password);

            var blog = GetBlog(blogid);

            if (blog != null)
            {
                var criteria = new EntryCriteria
                {
                    PageNumber = 1,
                    PageSize   = numberOfPosts
                };

                var entryList = EntryManager.GetBlogEntries(blog, criteria, ListOrder.Descending).Results.ToArray();

                var posts = new List <XmlRpcStruct>();

                //Populate structure with post entities
                foreach (var entry in entryList)
                {
                    var item = Database.GetItem(entry.Uri);
                    if (item == null)
                    {
                        continue;
                    }

                    var entryItem = new EntryItem(item);

                    var rpcstruct = new XmlRpcStruct
                    {
                        { "title", entryItem.Title.Raw },
                        { "link", GetItemAbsoluteUrl(entryItem) },
                        { "description", entryItem.Content.Text },
                        { "pubDate", entryItem.EntryDate.DateTime },
                        { "guid", entryItem.ID.ToString() },
                        { "postid", entryItem.ID.ToString() },
                        { "keywords", entryItem.Tags.Raw },
                        { "author", entryItem.InnerItem.Statistics.CreatedBy }
                    };

                    posts.Add(rpcstruct);
                }

                return(posts.ToArray());
            }

            return(new XmlRpcStruct[0]);
        }
Ejemplo n.º 15
0
        public void Get_ForCriteriaNotSet_ReturnsNull()
        {
            // arrange
            var sut      = new EntrySearchCache();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 5
            };

            // act
            var result = sut.Get(criteria, ListOrder.Descending);

            // assert
            Assert.That(result, Is.Null);
        }
Ejemplo n.º 16
0
        public void GetBlogEntries_LimitEntriesNegativeLimit()
        {
            var blog          = TestUtil.CreateNewBlog(TestContentRoot);
            var entryLuna     = TestUtil.CreateNewEntry(blog, "Luna", entryDate: new DateTime(2012, 3, 1));
            var entryDeimos   = TestUtil.CreateNewEntry(blog, "Deimos", entryDate: new DateTime(2012, 3, 2));
            var entryPhobos   = TestUtil.CreateNewEntry(blog, "Phobos", entryDate: new DateTime(2012, 3, 3));
            var entryAdrastea = TestUtil.CreateNewEntry(blog, "Adrastea", entryDate: new DateTime(2012, 3, 4));

            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = -7
            };
            var results = manager.GetBlogEntries(blog, criteria, ListOrder.Descending);

            Assert.That(results.Results, Is.Empty);
        }
Ejemplo n.º 17
0
        public void ClearCache_EntriesPreviouslySet_EntriesNoLongerSet()
        {
            // arrange
            var innerCache = new Cache <EntrySearchCacheKey>("test cache", 500)
            {
                Enabled = true
            };

            var cacheManager = Mock.Of <BaseCacheManager>(x =>
                                                          x.GetNamedInstance <EntrySearchCacheKey>(It.IsAny <string>(), It.IsAny <long>(), true) == innerCache
                                                          );

            var sut      = new EntrySearchCache(cacheManager);
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 5
            };

            var list = new List <Entry>
            {
                new Entry
                {
                    Author = "admin",
                    Title  = "lorem"
                }
            };

            var results = new SearchResults <Entry>(list, false);

            sut.Set(criteria, ListOrder.Descending, results);

            // act
            sut.ClearCache();

            // assert
            var result = sut.Get(criteria, ListOrder.Descending);

            Assert.That(result, Is.Null);
        }
Ejemplo n.º 18
0
        public void GetBlogEntries_ByTagWithSpace()
        {
            var blog        = TestUtil.CreateNewBlog(TestContentRoot);
            var entryLuna   = TestUtil.CreateNewEntry(blog, "Luna", tags: "wheeljack, prowl", entryDate: new DateTime(2012, 3, 1));
            var entryDeimos = TestUtil.CreateNewEntry(blog, "Deimos", tags: "prowl, orion pax, cliffjumper", entryDate: new DateTime(2012, 3, 2));
            var entryPhobos = TestUtil.CreateNewEntry(blog, "Phobos", tags: "wheeljack, hot rod, orion pax", entryDate: new DateTime(2012, 3, 3));

            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 10,
                Tag        = "orion pax"
            };

            var results = manager.GetBlogEntries(blog, criteria, ListOrder.Descending);
            var ids     = from result in results.Results select result.Uri.ItemID;

            Assert.That(ids, Is.EqualTo(new[] { entryPhobos.ID, entryDeimos.ID }));
        }
Ejemplo n.º 19
0
        public void GetBlogEntries_ByInvalidTag()
        {
            var blog          = TestUtil.CreateNewBlog(TestContentRoot);
            var entryLuna     = TestUtil.CreateNewEntry(blog, "Luna", tags: "wheeljack, prowl", entryDate: new DateTime(2012, 3, 1));
            var entryDeimos   = TestUtil.CreateNewEntry(blog, "Deimos", tags: "prowl, orion pax, cliffjumper", entryDate: new DateTime(2012, 3, 2));
            var entryPhobos   = TestUtil.CreateNewEntry(blog, "Phobos", tags: "wheeljack, hot rod, orion pax", entryDate: new DateTime(2012, 3, 3));
            var entryAdrastea = TestUtil.CreateNewEntry(blog, "Adrastea", tags: "wheeljack, hot rod, orion pax, prowl", entryDate: new DateTime(2012, 3, 4));

            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 10,
                Tag        = "blurr"
            };

            var results = manager.GetBlogEntries(blog, criteria, ListOrder.Descending);

            Assert.That(results.Results, Is.Empty);
        }
Ejemplo n.º 20
0
        public void GetSet_ForCriteriaWhichHasBeenSet_ReturnsListPreviouslySet()
        {
            // arrange
            var innerCache = new Cache <EntrySearchCacheKey>("test cache", 500)
            {
                Enabled = true
            };

            var cacheManager = Mock.Of <BaseCacheManager>(x =>
                                                          x.GetNamedInstance <EntrySearchCacheKey>(It.IsAny <string>(), It.IsAny <long>(), true) == innerCache
                                                          );

            var sut      = new EntrySearchCache(cacheManager);
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 5
            };

            var list = new List <Entry>
            {
                new Entry
                {
                    Author = "admin",
                    Title  = "lorem"
                }
            };

            var results = new SearchResults <Entry>(list, false);

            // act
            sut.Set(criteria, ListOrder.Descending, results);
            var result = sut.Get(criteria, ListOrder.Descending);

            // assert
            Assert.That(result.Results, Is.EquivalentTo(list));
        }
Ejemplo n.º 21
0
        public void GetBlogEntries_InDateRangeLimited()
        {
            var blog          = TestUtil.CreateNewBlog(TestContentRoot);
            var entryLuna     = TestUtil.CreateNewEntry(blog, "Luna", entryDate: new DateTime(2014, 10, 1));
            var entryDeimos   = TestUtil.CreateNewEntry(blog, "Deimos", entryDate: new DateTime(2014, 11, 1));
            var entryPhobos   = TestUtil.CreateNewEntry(blog, "Phobos", entryDate: new DateTime(2014, 12, 1));
            var entryAdrastea = TestUtil.CreateNewEntry(blog, "Adrastea", entryDate: new DateTime(2015, 1, 1));

            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber  = 1,
                PageSize    = 2,
                MinimumDate = new DateTime(2014, 11, 1),
                MaximumDate = new DateTime(2015, 1, 20)
            };

            var results = manager.GetBlogEntries(blog, criteria, ListOrder.Descending);
            var ids     = from result in results.Results select result.Uri.ItemID;

            Assert.That(ids, Is.EqualTo(new[] { entryAdrastea.ID, entryPhobos.ID }));
        }
Ejemplo n.º 22
0
        public void GetBlogEntries_ByInvalidCategory()
        {
            var blog          = TestUtil.CreateNewBlog(TestContentRoot);
            var categoryAlpha = TestUtil.CreateNewCategory(blog, "Alpha");
            var categoryBeta  = TestUtil.CreateNewCategory(blog, "Beta");

            TestUtil.CreateNewEntry(blog, "Luna", categories: new[] { categoryBeta.ID }, entryDate: new DateTime(2012, 3, 1));
            TestUtil.CreateNewEntry(blog, "Deimos", categories: new[] { categoryAlpha.ID, categoryBeta.ID }, entryDate: new DateTime(2012, 3, 2));
            TestUtil.CreateNewEntry(blog, "Phobos", categories: new[] { categoryBeta.ID, categoryAlpha.ID }, entryDate: new DateTime(2012, 3, 3));
            TestUtil.CreateNewEntry(blog, "Adrastea", categories: new[] { categoryBeta.ID, categoryAlpha.ID }, entryDate: new DateTime(2012, 3, 4));
            TestUtil.UpdateIndex();

            var manager  = new EntryManager();
            var criteria = new EntryCriteria
            {
                PageNumber = 1,
                PageSize   = 10,
                Category   = ID.NewID.ToString()
            };

            var results = manager.GetBlogEntries(blog, criteria, ListOrder.Descending);

            Assert.That(results.Results, Is.Empty);
        }
Ejemplo n.º 23
0
        public void GetHashCode_SameProperties_SameHashCode()
        {
            // arrange
            var sut1 = new EntryCriteria
            {
                PageNumber = 2,
                PageSize   = 10,
                Tag        = "tag"
            };

            var sut2 = new EntryCriteria
            {
                PageNumber = 2,
                PageSize   = 10,
                Tag        = "tag"
            };

            // act
            var hashcode1 = sut1.GetHashCode();
            var hashcode2 = sut2.GetHashCode();

            // assert
            Assert.That(hashcode1, Is.EqualTo(hashcode2));
        }
    public int GetEntriesCount(EntryCriteria criteria)
    {
      var key = criteria.ToString();
      if (HttpRuntime.Cache.Get(key) != null) return (int)HttpRuntime.Cache.Get(key);
      
      int count;
      GetEntries(criteria, 0, int.MaxValue, out count);

      //cache it
      HttpRuntime.Cache.Insert(key, count, GetCacheDependency(criteria),
          Cache.NoAbsoluteExpiration, TimeSpan.FromDays(30), CacheItemPriority.High, null);
      return count;
    }
 public int GetEntriesCount(EntryCriteria criteria)
 {
   int count;
   GetEntries(criteria, 0, int.MaxValue, out count);
   return count;
 }
 public System.Web.Caching.CacheDependency GetCacheDependency(EntryCriteria criteria)
 {
   throw new NotImplementedException();
 }
Ejemplo n.º 27
0
        protected virtual IList <EntryItem> GetEntries()
        {
            SearchResults <Entry> results = null;
            string tag    = QueryString["tag"];
            string sort   = QueryString["sort"];
            var    author = QueryString["author"];

            var criteria = new EntryCriteria
            {
                PageSize   = TotalToShow,
                PageNumber = (StartIndex / TotalToShow) + 1
            };

            if (!string.IsNullOrEmpty(tag))
            {
                criteria.Tag = tag;
                results      = EntryManager.GetBlogEntries(CurrentBlog, criteria, ListOrder.Descending);
            }
            else if (author != null)
            {
                // todo: need to implement entry criteria for author
                results = EntryManager.GetBlogEntries(CurrentBlog, EntryCriteria.AllEntries, ListOrder.Descending);

                var authorEntries = results.Results.GroupBy(item => AuthorsCore.GetUserFullName(item.Author))
                                    .FirstOrDefault(items => AuthorsCore.GetAuthorIdentity(items.Key) == author).ToList();

                results = new SearchResults <Entry>(authorEntries, false);
            }
            else if (TemplateManager.TemplateIsOrBasedOn(Context.Item, Settings.CategoryTemplateIds))
            {
                criteria.Category = Context.Item.Name;
                results           = EntryManager.GetBlogEntries(CurrentBlog, criteria, ListOrder.Descending);
            }
            else if (!string.IsNullOrEmpty(sort))
            {
                var algorithm = InterestingEntriesCore.GetAlgororithmFromString(sort, InterestingEntriesAlgorithm.Custom);
                if (algorithm != InterestingEntriesAlgorithm.Custom)
                {
                    var core     = new InterestingEntriesCore(EntryManager, algorithm);
                    var maxCount = TotalToShow + (TotalToShow * StartIndex) + 1;
                    return(core.GetEntries(CurrentBlog, maxCount));
                }
                else
                {
                    results = SearchResults <Entry> .Empty;
                }
            }
            else
            {
                results = EntryManager.GetBlogEntries(CurrentBlog, criteria, ListOrder.Descending);
            }

            var entryItems = new List <EntryItem>();

            foreach (var entry in results.Results)
            {
                var item = Database.GetItem(entry.Uri);
                if (item != null)
                {
                    entryItems.Add(new EntryItem(item));
                }
            }

            ShowViewMoreLink = results.HasMoreResults;

            return(entryItems);
        }
 public IEnumerable<AtomEntry> GetEntries(EntryCriteria criteria, int pageIndex, int pageSize, out int totalEntries)
 {
   throw new NotImplementedException();
 }
Ejemplo n.º 29
0
 public override int GetHashCode()
 {
     return(EntryCriteria.GetHashCode() + ResultOrder.GetHashCode());
 }
        public IEnumerable<AtomEntry> GetEntries(EntryCriteria criteria, int pageIndex, int pageSize, out int totalEntries)
        {
            IQueryable<Domain.AtomEntry> entries = GetEntries();

            if (criteria.StartDate.HasValue)
            {
                entries = entries.Where(entry => (entry.Published.GetValueOrDefault().DateTime >= criteria.StartDate));
            }

            if (criteria.EndDate.HasValue)
            {
                entries = entries.Where(entry => (entry.Published.GetValueOrDefault().DateTime <= criteria.EndDate));
            }

            if (!string.IsNullOrEmpty(criteria.WorkspaceName))
            {
                entries = entries.FilterByWorkspace(criteria.WorkspaceName);
            }

            if (!string.IsNullOrEmpty(criteria.CollectionName))
            {
                entries = entries.FilterByCollection(criteria.CollectionName);
            }

            if (! criteria.Authorized)
            {
                entries = entries.FilterByVisible();
            }

            if (!string.IsNullOrEmpty(criteria.CategoryTerm))
            {
                entries = entries.FilterByCategories(criteria.CategoryTerm, criteria.CategoryScheme);
            }

            if (!string.IsNullOrEmpty(criteria.PersonName))
            {
                entries = entries.FilterByPersonName(criteria.PersonName, criteria.PersonType);
            }


            if (criteria.EntryId != null)
            {
                // filter by annotations
                entries = FilterResultsByAnnotations(entries, criteria.EntryId, criteria.Deep);
            }

            if (!string.IsNullOrEmpty(criteria.SearchTerm))
            {
                entries = entries.FilterBySearchTerm(criteria.SearchTerm);
            }

            switch (criteria.SortMethod)
            {
                case SortMethod.EditDesc:
                    entries = entries.OrderByDescending(entry => entry.Edited);
                    break;

                case SortMethod.DateAsc:
                    entries = entries.OrderBy(entry => entry.Published);
                    break;

                case SortMethod.DateDesc:
                    entries = entries.OrderByDescending(entry => entry.Published);
                    break;
                case SortMethod.Default:
                    // no sorting
                    break;

                default:
                    // unhandled option
                    throw new ArgumentException("criteria.SortMethod");
            }

            totalEntries = entries.Count();

            entries = entries.Page(pageSize, pageIndex);

            return entries;
        }
 public int GetEntriesCount(EntryCriteria criteria)
 {
   throw new NotImplementedException();
 }
Ejemplo n.º 32
0
        /// <summary>
        /// Gets blog entries for the given blog which meet the criteria.
        /// </summary>
        /// <param name="blogRootItem">The root item of the blog to retrieve the entries for.</param>
        /// <param name="criteria">The criteria the entries should meet.</param>
        /// <param name="resultOrder">The ordering of the results.</param>
        /// <returns>The entries matching the criteria.</returns>
        public virtual SearchResults <Entry> GetBlogEntries(Item blogRootItem, EntryCriteria criteria, ListOrder resultOrder)
        {
            if (blogRootItem == null || criteria == null || criteria.PageNumber <= 0 || criteria.PageSize <= 0)
            {
                return(SearchResults <Entry> .Empty);
            }

            var cachedEntries = EntryCache?.Get(criteria, resultOrder);

            if (cachedEntries != null)
            {
                return(cachedEntries);
            }

            var customBlogItem = (from templateId in Settings.BlogTemplateIds
                                  where TemplateManager.TemplateIsOrBasedOn(blogRootItem, templateId)
                                  select(BlogHomeItem) blogRootItem).FirstOrDefault();

            if (customBlogItem == null)
            {
                customBlogItem = (from templateId in Settings.BlogTemplateIds
                                  let item = blogRootItem.FindAncestorByTemplate(templateId, TemplateManager)
                                             where item != null
                                             select(BlogHomeItem) item).FirstOrDefault();
            }

            if (customBlogItem == null)
            {
                return(SearchResults <Entry> .Empty);
            }

            var blogSettings = BlogSettingsResolver.Resolve(customBlogItem);

            using (var context = CreateSearchContext(blogRootItem))
            {
                var builder = PredicateBuilder.Create <EntryResultItem>(searchItem =>
                                                                        searchItem.TemplateId == blogSettings.EntryTemplateID &&
                                                                        searchItem.Paths.Contains(customBlogItem.ID) &&
                                                                        searchItem.Language.Equals(customBlogItem.InnerItem.Language.Name, StringComparison.InvariantCulture)
                                                                        );

                // Tag
                if (!string.IsNullOrEmpty(criteria.Tag))
                {
                    builder = builder.And(i => i.Tags.Contains(criteria.Tag));
                }

                // Categories
                if (!string.IsNullOrEmpty(criteria.Category))
                {
                    var categoryItem = ManagerFactory.CategoryManagerInstance.GetCategory(customBlogItem, criteria.Category);

                    // If the category is unknown, don't return any results.
                    if (categoryItem == null)
                    {
                        return(SearchResults <Entry> .Empty);
                    }

                    builder = builder.And(i => i.Category.Contains(categoryItem.ID));
                }

                if (criteria.MinimumDate != null)
                {
                    builder = builder.And(i => i.EntryDate >= criteria.MinimumDate);
                }

                if (criteria.MaximumDate != null)
                {
                    builder = builder.And(i => i.EntryDate <= criteria.MaximumDate);
                }

                var indexresults = context.GetQueryable <EntryResultItem>().Where(builder);

                if (resultOrder == ListOrder.Descending)
                {
                    indexresults = indexresults.OrderByDescending(item => item.EntryDate)
                                   .ThenByDescending(item => item.CreatedDate)
                                   .ThenByDescending(item => item.Title);
                }
                else
                {
                    indexresults = indexresults.OrderBy(item => item.EntryDate)
                                   .ThenBy(item => item.CreatedDate)
                                   .ThenBy(item => item.Title);
                }

                indexresults = indexresults.Skip(criteria.PageSize * (criteria.PageNumber - 1))
                               .Take(criteria.PageSize < int.MaxValue ? criteria.PageSize + 1 : criteria.PageSize);

                var entries = indexresults.Select(resultItem =>
                                                  // Keep field access inline so the query analyzer can see which fields will be used.
                                                  new Entry
                {
                    Uri       = resultItem.Uri,
                    Title     = string.IsNullOrWhiteSpace(resultItem.Title) ? resultItem.Name : resultItem.Title,
                    Tags      = resultItem.Tags != null ? resultItem.Tags : Enumerable.Empty <string>(),
                    EntryDate = resultItem.EntryDate
                }
                                                  ).ToList();


                var hasMore = entries.Count > criteria.PageSize;

                var entriesPage = entries.Take(criteria.PageSize).ToList();
                var results     = new SearchResults <Entry>(entriesPage, hasMore);

                EntryCache?.Set(criteria, resultOrder, results);

                return(results);
            }
        }
 public CacheDependency GetCacheDependency(EntryCriteria criteria)
 {
   //TODO: only get a dependency on specific files in entry criteria
   string[] paths = criteria.EntryId != null ?
             pathResolver.GetEntryFilePaths(criteria.EntryId, criteria.Annotations, criteria.Deep) :
     pathResolver.GetEntryFilePaths(criteria.WorkspaceName, criteria.CollectionName, criteria.Annotations, criteria.Deep);
   return new CacheDependency(paths);
 }