public void GetAtomEntriesNotNullTest()
        {
            MakeTestCollections(false);

            IAtomEntryRepository repository = GetEntryRepository();

            int totalEntries;
            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                Authorized = true
            };

            IEnumerable<AtomSite.Domain.AtomEntry> results = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(results);
        }
    public virtual AtomFeed GetFeedBySearch(Id collectionId, string term, int pageIndex, int pageSize)
    {
      LogService.Info("AtomPubService.GetFeedBySearch collectionId={0} term={1} pageIndex={2}", collectionId, term, pageIndex);
      Auth(collectionId, AuthAction.GetFeed);

      AppCollection c = AppServiceRepository.GetService().GetCollection(collectionId);

      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = collectionId.Workspace,
        CollectionName = collectionId.Collection,
        SearchTerm = term,
        Authorized = AuthorizeService.IsAuthorized(GetUser(), collectionId.ToScope(), AuthAction.GetEntryOrMedia)
      };
      int total;
      //search annotations?

      AtomFeed feed = AtomFeed.BuildFeed(c, AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total),
        total);//, pageIndex, pageSize, RouteFunc, "AtomPubFeed", "AtomPubCollectionIndex", false);

      feed.Subtitle = new AtomSubtitle { Text = "Search for '" + term + "'" };
      SetLinks(feed);
      return feed;
    }
    public AtomFeed GetFeedBySearch(string workspace, string term, int pageIndex, int pageSize)
    {
      LogService.Info("AtomPubService.GetFeedBySearch workspace={0} term={1} pageIndex={2}", workspace, term, pageIndex);
      AuthorizeService.Auth(new Scope() { Workspace = workspace }, AuthAction.GetFeed);

      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = workspace,
        SearchTerm = term,
        Authorized = AuthorizeService.IsAuthorized(GetUser(), new Scope() { Workspace = workspace }, AuthAction.GetEntryOrMedia)
      };
      int total;
      //search annotations?

      AtomFeed feed = new AtomFeed();
      feed.Generator = new AtomGenerator { Text = "atomsite.net" };
      AppWorkspace w = AppServiceRepository.GetService().GetWorkspace(workspace);
      feed.Title = w.Title;
      feed.Updated = DateTimeOffset.UtcNow;
      feed.Entries = AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total);
      feed.TotalResults = total;

      //use newest updated date as feed updated date
      if (feed.Entries.Count() > 0)
        feed.Updated = feed.Entries.Max().Updated;

      feed.Subtitle = new AtomSubtitle { Text = "Search for '" + term + "'" };
      SetLinks(feed);
      return feed;
    }
    public virtual AtomFeed GetFeedByCategory(Id collectionId, string term, Uri scheme, int pageIndex, int pageSize)
    {
      LogService.Info("AtomPubService.GetFeedByCategory collectionId={0}, term={1}", collectionId, term);
      Auth(collectionId, AuthAction.GetFeed);

      AppCollection c = AppServiceRepository.GetService().GetCollection(collectionId);
      //TODO: support external categories
      AtomCategory category = c.Categories.SelectMany(cats => cats.Categories).Where(cat => cat.Term == term &&
          cat.Scheme == scheme).SingleOrDefault();
      if (category == null) throw new ResourceNotFoundException("category", term);

      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = collectionId.Workspace,
        CollectionName = collectionId.Collection,
        CategoryTerm = term,
        CategoryScheme = scheme,
        Authorized = AuthorizeService.IsAuthorized(GetUser(), collectionId.ToScope(), AuthAction.GetEntryOrMedia)
      };
      int total;

      AtomFeed feed = AtomFeed.BuildFeed(c, AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total),
        total);//, pageIndex, pageSize, RouteFunc, "AtomPubFeed", "AtomPubCollectionIndex", false);

      feed.Subtitle = new AtomSubtitle { Text = "Browsing " + category.ToString() };
      SetLinks(feed);
      return feed;
    }
    public virtual AtomFeed GetFeedByPerson(Id collectionId, string personName, int pageIndex, int pageSize)
    {
      LogService.Info("AtomPubService.GetFeedByPerson collectionId={0}, personName={1}", collectionId, personName);
      Auth(collectionId, AuthAction.GetFeed);

      AppCollection c = AppServiceRepository.GetService().GetCollection(collectionId);
      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = collectionId.Workspace,
        CollectionName = collectionId.Collection,
        PersonName = personName,
        PersonType = "person",
        Authorized = AuthorizeService.IsAuthorized(GetUser(), collectionId.ToScope(), AuthAction.GetEntryOrMedia)
      };
      int total;

      AtomFeed feed = AtomFeed.BuildFeed(c, AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total),
        total);//, pageIndex, pageSize, RouteFunc, "AtomPubFeed", "AtomPubCollectionIndex", false);
      feed.Subtitle = new AtomSubtitle() { Text = personName };
      SetLinks(feed);
      return feed;
    }
    public virtual AtomFeed GetFeedByDate(Id collectionId, DateTime startDate, DateTime endDate, int pageIndex, int pageSize)
    {
      LogService.Info("AtomPubService.GetFeedByDate collectionId={0}, startDate={1}, endDate={2}", collectionId, startDate, endDate);
      Auth(collectionId, AuthAction.GetFeed);

      AppCollection c = AppServiceRepository.GetService().GetCollection(collectionId);
      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = collectionId.Workspace,
        CollectionName = collectionId.Collection,
        StartDate = startDate,
        EndDate = endDate,
        Authorized = AuthorizeService.IsAuthorized(GetUser(), collectionId.ToScope(), AuthAction.GetEntryOrMedia)
      };
      int total;
      AtomFeed feed = AtomFeed.BuildFeed(c, AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total),
        total);//, pageIndex, pageSize, RouteFunc, "AtomPubFeed", "AtomPubCollectionIndex", false);
      if (startDate.DayOfYear == 1 && endDate.DayOfYear > 354)
      {
        feed.Subtitle = new AtomSubtitle() { Text = "For the year " + startDate.Year.ToString("0000") };
      }
      else if (startDate.Day == 1 && endDate.Day == DateTime.DaysInMonth(endDate.Year, endDate.Month))
      {
        feed.Subtitle = new AtomSubtitle() { Text = "For " + startDate.ToString("MMMM yyyy") };
      }
      else if (startDate.Day == endDate.Day)
      {
        feed.Subtitle = new AtomSubtitle() { Text = "For " + startDate.ToString("D") };
      }
      else
      {
        feed.Subtitle = new AtomSubtitle()
        {
          Text = "From " + startDate.ToShortDateString() + " to " +
            endDate.ToShortDateString()
        };
      }
      SetLinks(feed);
      return feed;
    }
        public void GetEntriesAprovedAuthorizedTest()
        {
            MakeTestCollections(true);

            DateTime now = DateTime.Now;

            DateTime oneDayAgo = now.AddDays(-1);

            // make an entry for today 
            AtomEntry entry1 = TestDataHelper.MakeTestEntry(now, TestCollection1Name);

            // and an entry for yesterday
            AtomEntry entry2 = TestDataHelper.MakeTestEntry(oneDayAgo, TestCollection1Name);
            entry2.Control.Approved = false;

            IAtomEntryRepository repository = GetEntryRepository();
            repository.CreateEntry(entry1);
            repository.CreateEntry(entry2);

            //now retrieve entries with authorized
            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                Authorized = true
            };

            int totalEntries;
            IEnumerable<AtomEntry> entries = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entries);
            Assert.AreEqual(2, entries.Count());

            // now get them without auth
            criteria.Authorized = false;
            IEnumerable<AtomEntry> entriesNotAuth = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesNotAuth);
            Assert.AreEqual(1, entriesNotAuth.Count());

            DataTester.AssertEntriesEqual(entry1, entriesNotAuth.First());
        }
 public AtomFeed GetAnnotations(string workspace, bool deep, int pageIndex, int pageSize)
 {
   LogService.Info("AnnotateService.GetAnnotations workspace={0} deep={1}", workspace, deep);
   EntryCriteria criteria = new EntryCriteria()
   {
     Annotations = true,
     WorkspaceName = workspace,
     SortMethod = SortMethod.Default,
     Authorized = AuthorizeService.IsAuthorized(GetUser(), new Scope() { Workspace = workspace }, AuthAction.GetEntryOrMedia),
     Deep = deep
   };
   return GetAnnotations(criteria, pageIndex, pageSize);
 }
        public void AddEntryWorkspaceAndCollectionMismatchTest()
        {
            MakeTestCollections(false);

            IAtomEntryRepository repository = GetEntryRepository();

            // count the entries before adding one
            int totalEntries;

            // create an entry
            AtomSite.Domain.AtomEntry entry = TestDataHelper.MakeTestEntry(TestCollection1Name);

            // save the entry
            repository.CreateEntry(entry);

            // should be retrieved
            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                Authorized = true
            };

            IEnumerable<AtomSite.Domain.AtomEntry> entriesFound = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsTrue(entriesFound.ContainsId(entry.Id), "entry not retrieved");

            // should not be retrieved
            criteria = new EntryCriteria
            {
                WorkspaceName = Guid.NewGuid().ToString(),
                CollectionName = TestCollection1Name,
                Authorized = true
            };

            // sql repository returns empty list, file provider throws an exception
            IEnumerable<AtomSite.Domain.AtomEntry> entriesNotFound;
            try
            {
                entriesNotFound = repository.GetEntries(criteria, 0, PageSize, out totalEntries);
            }
            catch (Exception)
            {
                entriesNotFound = new List<AtomSite.Domain.AtomEntry>();
            }
            
            Assert.IsFalse(entriesNotFound.ContainsId(entry.Id), "Entry retrieved with wrong workspace");

            // should not be retrieved
            criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = Guid.NewGuid().ToString(),
                Authorized = true
            };

            try
            {
                entriesNotFound = repository.GetEntries(criteria, 0, PageSize, out totalEntries);
            }
            catch (Exception)
            {
                entriesNotFound = new List<AtomSite.Domain.AtomEntry>();
            }

            Assert.IsFalse(entriesNotFound.ContainsId(entry.Id), "Entry retrieved with wrong collection");
        }
        public void EntrySearchCriteriaTest()
        {
            MakeTestCollections(false);

            // make an entry 
            AtomEntry entry = TestDataHelper.MakeTestEntry(DateTime.Now, TestCollection1Name);

            string correctGuid = Guid.NewGuid().ToString();
            string otherGuid = Guid.NewGuid().ToString();

            // save the entry
            entry.Content = new AtomContent
                 {
                     Type = "text",
                     Text = "some entry text " + correctGuid + ""
                 };

            IAtomEntryRepository repository = GetEntryRepository();
            repository.CreateEntry(entry);

            // get entries
            EntryCriteria criteria = new EntryCriteria
             {
                 WorkspaceName = TestWorkspaceName,
                 CollectionName = TestCollection1Name,
                 Authorized = true,
                 SearchTerm = correctGuid
             };

            int totalEntries;
            IEnumerable<AtomEntry> searchEnties = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.AreEqual(1, searchEnties.Count());
            Assert.IsTrue(searchEnties.ContainsId(entry.Id));

            // try the wrong guid
            criteria.SearchTerm = otherGuid;
            searchEnties = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.AreEqual(0, searchEnties.Count());
        }
        public void GetEntriesByCategoryTest()
        {
            MakeTestCollections(true);

            DateTime now = DateTime.Now;

            DateTime oneDayAgo = now.AddDays(-1);
            DateTime twoDaysAgo = now.AddDays(-2);

            // make two categories
            AtomCategory category1 = GetCategory(true);
            AtomCategory category2 = GetCategory(false);


            // make an entry for today 
            AtomEntry entry1 = TestDataHelper.MakeTestEntry(now, TestCollection1Name);
            // in category 1
            entry1.Categories = entry1.Categories.Add(category1);

            // and an entry for yesterday
            AtomEntry entry2 = TestDataHelper.MakeTestEntry(oneDayAgo, TestCollection1Name);
            // in category 2
            entry2.Categories = entry2.Categories.Add(category2);

            // and the day before yesteday
            AtomEntry entry3 = TestDataHelper.MakeTestEntry(twoDaysAgo, TestCollection1Name);

            // in both categories
            entry3.Categories = entry3.Categories.Add(category1, category2);

            IAtomEntryRepository repository = GetEntryRepository();
            repository.CreateEntry(entry1);
            repository.CreateEntry(entry2);
            repository.CreateEntry(entry3);

            //now retrieve entries by category 1

            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                CategoryScheme = category1.Scheme,
                CategoryTerm = category1.Term,
                Authorized = true,                
            };

            int totalEntries;
            IEnumerable<AtomEntry> entriesPerson1 = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesPerson1);
            Assert.AreEqual(2, entriesPerson1.Count());

            // should contain entries 1 and 3
            Assert.IsTrue(entriesPerson1.ContainsId(entry1.Id), "Entry 1 not found");
            Assert.IsTrue(entriesPerson1.ContainsId(entry3.Id), "Entry 3 not found");


            // now get them with category 2
            criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                CategoryScheme = category2.Scheme,
                CategoryTerm = category2.Term,
                Authorized = true,
            };

            IEnumerable<AtomEntry> entriesPerson2 = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesPerson2);
            Assert.AreEqual(2, entriesPerson2.Count());

            // should contain entries 2 and 3
            Assert.IsTrue(entriesPerson2.ContainsId(entry2.Id), "Entry 2 not found");
            Assert.IsTrue(entriesPerson2.ContainsId(entry3.Id), "Entry 3 not found");
        }
        public void GetEntriesByPersonTest()
        {
            MakeTestCollections(false);

            DateTime now = DateTime.Now;

            DateTime oneDayAgo = now.AddDays(-1);
            DateTime twoDaysAgo = now.AddDays(-2);

            // make two people
            AtomPerson person1 = TestDataHelper.MakeTestPerson();

            AtomPerson person2 = TestDataHelper.MakeTestPerson();

            // make an entry for today 
            AtomEntry entry1 = TestDataHelper.MakeTestEntry(now, TestCollection1Name);
            // person 1 is the author
            entry1.Authors = entry1.Authors.Add(person1);

            // and an entry for yesterday
            AtomEntry entry2 = TestDataHelper.MakeTestEntry(oneDayAgo, TestCollection1Name);
            // person 2 is the author
            entry2.Authors = entry2.Authors.Add(person2);

            // and the day before yesteday
            AtomEntry entry3 = TestDataHelper.MakeTestEntry(twoDaysAgo, TestCollection1Name);

            // join authors - both people
            entry3.Authors = entry1.Authors.Add(person1, person2);


            IAtomEntryRepository repository = GetEntryRepository();
            repository.CreateEntry(entry1);
            repository.CreateEntry(entry2);
            repository.CreateEntry(entry3);

            //now retrieve entries by person 1

            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                PersonName = person1.Name,
                PersonType = person1.Type,
                Authorized = true,
            };

            int totalEntries;
            IEnumerable<AtomEntry> entriesPerson1 = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesPerson1);
            Assert.AreEqual(2, entriesPerson1.Count());

            // should contain entries 1 and 3
            Assert.IsTrue(entriesPerson1.ContainsId(entry1.Id), "Entry 1 not found");
            Assert.IsTrue(entriesPerson1.ContainsId(entry3.Id), "Entry 3 not found");


            // now get them with person 2
            criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                PersonName = person2.Name,
                PersonType = person2.Type,
                Authorized = true
            };

            IEnumerable<AtomEntry> entriesPerson2 = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesPerson2);
            Assert.AreEqual(2, entriesPerson2.Count());

            // should contain entries 2 and 3
            Assert.IsTrue(entriesPerson2.ContainsId(entry2.Id), "Entry 2 not found");
            Assert.IsTrue(entriesPerson2.ContainsId(entry3.Id), "Entry 3 not found");
        }
        public void GetEntriesByDateTest()
        {
            MakeTestCollections(true);

            DateTime now = DateTime.Now;

            DateTime oneMonthAgo = now.AddMonths(-1);
            DateTime twoMonthAgo = oneMonthAgo.AddMonths(-1);

            AtomEntry entry1 = TestDataHelper.MakeTestEntry(now, TestCollection1Name);
            AtomEntry entry2 = TestDataHelper.MakeTestEntry(oneMonthAgo, TestCollection1Name);
            AtomEntry entry3 = TestDataHelper.MakeTestEntry(twoMonthAgo, TestCollection1Name);

            IAtomEntryRepository repository = GetEntryRepository();
            repository.CreateEntry(entry1);
            repository.CreateEntry(entry2);
            repository.CreateEntry(entry3);

            //now retrieve recent entries
            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                StartDate = now.AddDays(-1),
                EndDate = now.AddDays(1),
                Authorized = true
            };

            int totalEntries;
            IEnumerable<AtomEntry> entriesAfter = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.AreEqual(1, entriesAfter.Count(), "recent entries");
            DataTester.AssertEntriesEqual(entry1, entriesAfter.First());

            // a month back - two entries
            criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                StartDate = oneMonthAgo.AddDays(-1),
                EndDate = now.AddDays(1),
                Authorized = true
            };

            IEnumerable<AtomEntry> oneMonthEntries = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.AreEqual(2, oneMonthEntries.Count(), "one month of entries");
            DataTester.AssertEntriesEqual(entry1, oneMonthEntries.First());
            DataTester.AssertEntriesEqual(entry2, oneMonthEntries.Skip(1).First());


            criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                StartDate = twoMonthAgo.AddDays(-1),
                EndDate = now.AddDays(1),
                Authorized = true
            };

            // two months back - three entries
            IEnumerable<AtomEntry> twoMonthEntries = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.AreEqual(3, twoMonthEntries.Count(), "two month of entries");
            DataTester.AssertEntriesEqual(entry1, twoMonthEntries.First());
            DataTester.AssertEntriesEqual(entry2, twoMonthEntries.Skip(1).First());
            DataTester.AssertEntriesEqual(entry3, twoMonthEntries.Skip(2).First());

        }
    public virtual AtomFeed GetCollectionFeed(Id collectionId, int pageIndex)
    {
      LogService.Info("AtomPubService.GetCollectionFeed collectionId={0} pageIndex={1}", collectionId, pageIndex);
      Auth(collectionId, AuthAction.GetCollectionFeed);
      AppCollection c = AppServiceRepository.GetService().GetCollection(collectionId);
      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = collectionId.Workspace,
        CollectionName = collectionId.Collection,
        Authorized = true,
        SortMethod = SortMethod.EditDesc //according to spec, collection feeds should be ordered by edited
      };
      int total;
      AtomFeed feed = AtomFeed.BuildFeed(c, AtomEntryRepository.GetEntries(criteria, pageIndex, c.PageSize, out total),
        total);//, pageIndex, c.PageSize, RouteFunc, "AtomPubCollection", "AtomPubCollectionIndex", true);
      SetLinks(feed);

      var url = new UrlHelper(Container.GetInstance<RequestContext>());
      feed.Links = feed.Links.Concat(url.GetPagingLinks("AtomPubCollection", feed.Id, null, feed.TotalResults ?? 0, pageIndex, c.PageSize,
        Atom.ContentTypeFeed, AbsoluteMode.Force));
      return feed;
    }
    /// <summary>
    /// Gets a consolidated feed of all collections in a workspace.
    /// </summary>
    /// <param name="workspaceName"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public virtual AtomFeed GetFeed(string workspaceName, int pageIndex, int pageSize)
    {
      LogService.Info("AtomPubService.GetFeed: workspace={0}", workspaceName);
      AuthorizeService.Auth(new Scope() { Workspace = workspaceName }, AuthAction.GetFeed);
      AppService svc = AppServiceRepository.GetService();
      AtomFeed feed = new AtomFeed();
      feed.Generator = new AtomGenerator { Text = "atomsite.net" };
      feed.Id = new Uri("urn:" + Guid.NewGuid().ToString());
      feed.Title = new AtomText() { Text = svc.Base.Authority };
      int grandTotal = 0;

      if (!string.IsNullOrEmpty(workspaceName) || svc.Workspaces.Count() == 1)
      {
        //fill info from workspace
        feed.Title = svc.Workspaces.First().Title;
        feed.Subtitle = svc.Workspaces.First().Subtitle;
        //TODO: other props
      }
      
      //build feed for entire service or a single workspace
      var collections = svc.Workspaces.Where(w => w.Name == workspaceName || workspaceName == null)
        .SelectMany(w => w.Collections)
        .Where(c => c.SyndicationOn && c.Visible);

      List<AtomEntry> entries = new List<AtomEntry>();
      foreach (AppCollection coll in collections)
      {
        if (AuthorizeService.IsAuthorized(GetUser(), coll.Id.ToScope(), AuthAction.GetFeed))
        {
          int total;
          EntryCriteria criteria = new EntryCriteria()
          {
            WorkspaceName = coll.Id.Workspace,
            CollectionName = coll.Id.Collection,
            Authorized = AuthorizeService.IsAuthorized(GetUser(), coll.Id.ToScope(), AuthAction.GetEntryOrMedia),
          };
          entries.AddRange(AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total));
          grandTotal += total;
        }
      }
      feed.TotalResults = grandTotal;
      entries.OrderByDescending(e => e.Date);      
      entries.Take(pageSize);
      feed.Entries = entries;
      SetLinks(feed);
      return feed;
    }
 /// <summary>
 /// Get entries by the given criteria.
 /// </summary>
 /// <remarks>This method will likely deprecate many of the specific feed based functions.</remarks>
 /// <param name="criteria"></param>
 /// <param name="pageIndex"></param>
 /// <param name="pageSize"></param>
 /// <returns></returns>
 public IPagedList<AtomEntry> GetEntries(EntryCriteria criteria, int pageIndex, int pageSize)
 {
   LogService.Info("AtomPubService.GetEntries");
   AuthorizeService.Auth(new Scope() { Workspace = criteria.WorkspaceName, Collection = criteria.CollectionName },
     AuthAction.GetFeed);
   
   criteria.Authorized = true;//TODO: AuthorizeService.IsAuthorized(GetUser(), coll.Id, AuthAction.GetEntryOrMedia);
   int total;      
   var entries = AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total);
   foreach (AtomEntry entry in entries) SetLinks(entry);
   PagedList<AtomEntry> pagedList = new PagedList<AtomEntry>(entries, pageIndex, pageSize, total);
   return pagedList;
 }
    public virtual AtomFeed GetFeed(Id collectionId, int pageIndex, int pageSize)
    {
      //<atom:link rel="alternate" type="text/html" href="https://atomsite.net/blog.xhtml" />

      LogService.Info("AtomPubService.GetFeed collectionId={0}", collectionId);
      Auth(collectionId, AuthAction.GetFeed);
      AppCollection c = AppServiceRepository.GetService().GetCollection(collectionId);
      EntryCriteria criteria = new EntryCriteria()
      {
        WorkspaceName = collectionId.Workspace,
        CollectionName = collectionId.Collection,
        Authorized = AuthorizeService.IsAuthorized(GetUser(), collectionId.ToScope(), AuthAction.GetEntryOrMedia),
      };
      int total;
      AtomFeed feed = AtomFeed.BuildFeed(c, AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total),
        total);//, pageIndex, pageSize, RouteFunc, "AtomPubFeed", "AtomPubCollectionIndex", true);

      var url = new UrlHelper(Container.GetInstance<RequestContext>());
      feed.Links = feed.Links.Concat(url.GetPagingLinks("AtomPubFeed", feed.Id, null, feed.TotalResults ?? 0, 
        pageIndex, pageSize,Atom.ContentTypeFeed, AbsoluteMode.Force));

      SetLinks(feed);
      return feed;
    }
 protected virtual AtomFeed GetAnnotations(EntryCriteria criteria, int pageIndex, int pageSize)
 {
   AtomFeed feed = new AtomFeed();
   feed.Subtitle = new AtomText(Atom.AtomNs + "subtitle") { Text = "Annotations" };
   if (criteria.EntryId != null) //get annotations for an entry
   {
     AtomEntry parent = AtomEntryRepository.GetEntry(criteria.EntryId);
     feed.Title = parent.Title;
     feed.Id = parent.Id;
     //feed.Authors = parent.Authors;
     //feed.Contributors = parent.Contributors;
   }
   else if (criteria.CollectionName != null) //get annotations for a collection
   {
     AppCollection coll = AppService.GetCollection(criteria.WorkspaceName, criteria.CollectionName);
     feed.Id = coll.Id;
     feed.Title = coll.Title;
     //TODO: feed.Authors = coll.Authors;
     //TODO: feed.Contributors = coll.Contributors;
   }
   else if (criteria.WorkspaceName != null) //get annotations for an entire workspace
   {
     AppWorkspace ws = AppService.GetWorkspace(criteria.WorkspaceName);
     feed.Title = ws.Title;
     //TODO: feed.Authors = ws.Authors;
     //TODO: feed.Contributors = ws.Contributors;
   }
   else
   {
     feed.Title = new AtomTitle { Text = "Annotations" };
   }
   int total;
   feed.Entries = AtomEntryRepository.GetEntries(criteria, pageIndex, pageSize, out total);
   feed.TotalResults = total;
   //foreach (AtomEntry e in feed.Entries)
   //{
   //  e.UpdateLinks(this.UrlHelper.RouteIdUri);
   //}
   return feed;
 }
        public void GetEntriesSortOrderThreeEntriesTest()
        {
            MakeTestCollections(true);

            DateTime now = DateTime.Now;

            DateTime oneDayAgo = now.AddDays(-1);
            DateTime twoDaysAgo = now.AddDays(-2);

            // make an entry for today 
            AtomEntry entry1 = TestDataHelper.MakeTestEntry(now, TestCollection1Name);
            entry1.Edited = now.AddHours(1);

            // and an entry for yesterday
            AtomEntry entry2 = TestDataHelper.MakeTestEntry(oneDayAgo, TestCollection1Name);
            // created yesterday, edited after the others
            entry2.Edited = now.AddHours(3);

            // third entry, day before yesterday
            AtomEntry entry3 = TestDataHelper.MakeTestEntry(twoDaysAgo, TestCollection1Name);
            // created yesterday, edited in the middle
            entry2.Edited = now.AddHours(2);


            IAtomEntryRepository repository = GetEntryRepository();
            repository.CreateEntry(entry1);
            repository.CreateEntry(entry2);
            repository.CreateEntry(entry3);

            //now retrieve recent entries with no sort order
            EntryCriteria criteria = new EntryCriteria
            {
                WorkspaceName = TestWorkspaceName,
                CollectionName = TestCollection1Name,
                Authorized = true
            };

            int totalEntries;
            IEnumerable<AtomEntry> entries = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entries);
            Assert.IsTrue(entries.Count() > 0);
            DataTester.AssertEntriesEqual(entry1, entries.First());
            DataTester.AssertEntriesEqual(entry2, entries.Skip(1).First());
            DataTester.AssertEntriesEqual(entry3, entries.Skip(2).First());

            // now get them in edit order - the second entry should now be first
            criteria.SortMethod = SortMethod.EditDesc;

            IEnumerable<AtomEntry> entriesEditSort = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesEditSort);
            Assert.IsTrue(entriesEditSort.Count() > 0);

            DataTester.AssertEntriesEqual(entry2, entriesEditSort.First());
            DataTester.AssertEntriesEqual(entry3, entriesEditSort.Skip(2).First());
            DataTester.AssertEntriesEqual(entry1, entriesEditSort.Skip(1).First());

            // date ascending order
            criteria.SortMethod = SortMethod.DateAsc;

            IEnumerable<AtomEntry> entriesSortDateAsc = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesSortDateAsc);
            Assert.IsTrue(entriesSortDateAsc.Count() > 0);

            DataTester.AssertEntriesEqual(entry3, entriesSortDateAsc.First());
            DataTester.AssertEntriesEqual(entry2, entriesSortDateAsc.Skip(1).First());
            DataTester.AssertEntriesEqual(entry1, entriesSortDateAsc.Skip(2).First());

            // date descending order
            criteria.SortMethod = SortMethod.DateDesc;
            IEnumerable<AtomEntry> entriesSortDateDesc = repository.GetEntries(criteria, 0, PageSize, out totalEntries);

            Assert.IsNotNull(entriesSortDateDesc);
            Assert.IsTrue(entriesSortDateDesc.Count() > 0);

            DataTester.AssertEntriesEqual(entry1, entriesSortDateDesc.First());
            DataTester.AssertEntriesEqual(entry2, entriesSortDateDesc.Skip(1).First());
            DataTester.AssertEntriesEqual(entry3, entriesSortDateDesc.Skip(2).First());
        }