public virtual void Test_GetPageOfEntitiesWithReference_EmptyReferencedEntityID_NotFound() { using (LogGroup logGroup = LogGroup.Start("Testing the index retrieval of entities that don't have any references on a particular property.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); Guid userID = user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; TestRole role = new TestRole(); Guid roleID = role.ID = Guid.NewGuid(); role.Name = "Test Role"; // Assign a user to the role role.Users = new TestUser[] { user }; DataAccess.Data.Saver.Save(user); DataAccess.Data.Saver.Save(role); PagingLocation location = new PagingLocation(0, 10); string sortExpression = "UsernameAscending"; TestRole[] foundRoles = DataAccess.Data.Indexer.GetPageOfEntitiesWithReference <TestRole>("Users", typeof(TestUser), Guid.Empty, location, sortExpression); Assert.IsNotNull(foundRoles, "The found roles object returned was null."); Assert.AreEqual(0, foundRoles.Length, "Invalid number of roles found."); } }
public void Test_Index_PageIndex0_Size1() { TestArticle article = new TestArticle(); article.ID = Guid.NewGuid(); article.Title = "Test Title"; TestArticle article2 = new TestArticle(); article2.ID = Guid.NewGuid(); article2.Title = "Test Title 2"; DataAccess.Data.Saver.Save(article); DataAccess.Data.Saver.Save(article2); PagingLocation location = new PagingLocation(0, 1); IIndexStrategy strategy = IndexStrategy.New <TestArticle>(location, "TitleAscending", false); strategy.TypeName = "TestArticle"; strategy.EnablePaging = true; TestArticle[] foundArticles = Collection <TestArticle> .ConvertAll(strategy.Index()); Assert.IsNotNull(foundArticles); Assert.AreEqual(2, location.AbsoluteTotal, "Absolute total invalid."); Assert.AreEqual(1, foundArticles.Length, "Invalid number of test articles found."); }
public void Test_Index_PageIndex1_Size2() { TestArticle article = new TestArticle(); article.ID = Guid.NewGuid(); article.Title = "Test Title"; TestArticle article2 = new TestArticle(); article2.ID = Guid.NewGuid(); article2.Title = "Test Title 2"; DataAccess.Data.Saver.Save(article); DataAccess.Data.Saver.Save(article2); PagingLocation location = new PagingLocation(1, 2); IIndexStrategy strategy = IndexStrategy.New <TestArticle>(location, "TitleAscending", false); strategy.TypeName = "TestArticle"; strategy.EnablePaging = true; TestArticle[] foundArticles = Collection <TestArticle> .ConvertAll(strategy.Index()); Assert.IsNotNull(foundArticles); // There should be no articles found because it's requesting a page outside the available range Assert.AreEqual(0, foundArticles.Length, "Invalid number of test articles found."); }
public void Test_GetPageOfEntitiesWithReference_Page1_PageSize1_SortAscending() { TestCategory category = new TestCategory(); category.ID = Guid.NewGuid(); category.Name = "Test Category"; TestArticle article1 = new TestArticle(); article1.ID = Guid.NewGuid(); article1.Title = "Article C"; article1.Categories = new TestCategory[] { category }; TestArticle article2 = new TestArticle(); article2.ID = Guid.NewGuid(); article2.Title = "Article B"; article2.Categories = new TestCategory[] { category }; TestArticle article3 = new TestArticle(); article3.ID = Guid.NewGuid(); article3.Title = "Article A"; article3.Categories = new TestCategory[] { category }; DataAccess.Data.Saver.Save(category); DataAccess.Data.Saver.Save(article3); DataAccess.Data.Saver.Save(article2); DataAccess.Data.Saver.Save(article1); string[] titles = new String[] { article1.Title, article2.Title, article3.Title }; PagingLocation pagingLocation = new PagingLocation(0, 1); string sortExpression = "TitleAscending"; TestArticle[] entities = DataAccess.Data.Indexer.GetPageOfEntitiesWithReference <TestArticle>("Categories", typeof(TestCategory), category.ID, pagingLocation, sortExpression); Assert.IsNotNull(entities); foreach (TestArticle a in entities) { Assert.Greater(Array.IndexOf(titles, a.Title), -1, "The title of one of the retrieved entities doesn't match any of those expected."); } Assert.AreEqual(1, entities.Length, "Invalid number found."); Assert.AreEqual(article3.Title, entities[0].Title, "Sorting failed #1."); //Assert.AreEqual(article2.Title, entities[1].Title, "Sorting failed #2."); //Assert.AreEqual(article1.Title, entities[1].Title, "Sorting failed #3."); Assert.AreEqual(3, pagingLocation.AbsoluteTotal, "Invalid total"); }
/// <summary> /// Creates a new strategy for indexing a single page of entities the specified type. /// </summary> static public IIndexStrategy New <T>(PagingLocation location, string sortExpression) { IIndexStrategy strategy = StrategyState.Strategies.Creator.NewIndexer(typeof(T).Name); strategy.EnablePaging = true; strategy.Location = location; strategy.SortExpression = sortExpression; return(strategy); }
/// <summary> /// Creates a new strategy for indexing a single page of entities the specified type. /// </summary> static public IIndexStrategy New(PagingLocation location, string typeName, string sortExpression, bool requireAuthorisation) { IIndexStrategy strategy = StrategyState.Strategies.Creator.NewIndexer(typeName); strategy.EnablePaging = true; strategy.Location = location; strategy.RequireAuthorisation = requireAuthorisation; strategy.SortExpression = sortExpression; return(strategy); }
public static IndexController New(IControllable container, PagingLocation location) { container.CheckCommand(); IndexController controller = ControllerState.Controllers.Creator.NewIndexer(container.Command.TypeName); controller.Container = container; controller.EnablePaging = true; controller.Indexer.Location = location; return(controller); }
public void Test_IsInPage_PageSize10_Pre() { using (LogGroup logGroup = LogGroup.Start("Testing the IsInPage function on an item that appears on a earlier page.", LogLevel.Debug)) { int i = 9; int pageIndex = 1; int pageSize = 10; bool isInPage = new PagingLocation(pageIndex, pageSize).IsInPage(i); Assert.IsFalse(isInPage, "Returned true when it shouldn't have."); } }
public void Test_IsInPage_PageSize10_In_End() { using (LogGroup logGroup = LogGroup.Start("Testing the IsInPage function on an item that appears on the specified page.", LogLevel.Debug)) { int i = 9; int pageIndex = 0; int pageSize = 10; bool isInPage = new PagingLocation(pageIndex, pageSize).IsInPage(i); Assert.IsTrue(isInPage, "Returned false when it should have been true."); } }
public void Test_IsInPage_PageSize10_In_Start() { using (LogGroup logGroup = LogGroup.Start("Testing the IsInPage function on an item that appears on the specified page.", LogLevel.Debug)) { int i = 10; int pageIndex = 1; int pageSize = 10; bool isInPage = new PagingLocation(pageIndex, pageSize).IsInPage(i); Assert.IsTrue(isInPage, "Returned false when it should have been true."); } }
public void Test_IsInPage_PageSize10_Post() { using (LogGroup logGroup = LogGroup.Start("Testing the IsInPage function on an item that appears on a later page.", LogLevel.Debug)) { int i = 11; int pageIndex = 0; int pageSize = 10; bool isInPage = new PagingLocation(pageIndex, pageSize).IsInPage(i); Assert.IsFalse(isInPage, "Returned true when it shouldn't have."); } }
public void Test_Index_Paged_Page1() { List <TestRecord> list = new List <TestRecord>(); for (int i = 0; i < 30; i++) { TestRecord record = new TestRecord(); record.ID = Guid.NewGuid(); record.Text = "Record " + i; DataAccess.Data.Saver.Save(record); list.Add(record); } if (DataAccess.Data == null) { throw new InvalidOperationException("Data provider has not been initialized. Run setup."); } BaseIndexProjection page = new BaseIndexProjection("Index", typeof(TestRecord), false); PagingLocation location = new PagingLocation(0, 20); IndexController controller = IndexController.New(page, location); if (controller == null) { throw new Exception("Controller is null."); } controller.CurrentPageIndex = 0; controller.Index(); Assert.IsNotNull(controller.DataSource, "The DataSource of the controller wasn't set."); IEntity[] entities = (IEntity[])controller.DataSource; Assert.AreEqual(20, entities.Length, "DataSource count mismatch."); foreach (TestRecord record in list) { DataAccess.Data.Deleter.Delete(record); } }
public virtual void Test_GetEntitiesPageMatchReference() { using (LogGroup logGroup = LogGroup.Start("Testing the GetEntitiesPageMatchReference function to ensure it finds entities properly.", NLog.LogLevel.Debug)) { TestArticle article = new TestArticle(); article.ID = Guid.NewGuid(); article.Title = "Test Article"; TestCategory category = new TestCategory(); category.ID = Guid.NewGuid(); category.Name = "Test Category"; TestArticle article2 = new TestArticle(); article2.ID = Guid.NewGuid(); article2.Title = "Test Article 2"; TestCategory category2 = new TestCategory(); category2.ID = Guid.NewGuid(); category2.Name = "Test Category 2"; article.Categories = new TestCategory[] { category, category2 }; article2.Categories = new TestCategory[] { category, category2 }; DataAccess.Data.Saver.Save(category2); DataAccess.Data.Saver.Save(category); DataAccess.Data.Saver.Save(article); DataAccess.Data.Saver.Save(article2); PagingLocation location = new PagingLocation(0, 10); IEntity[] results = DataAccess.Data.Indexer.GetPageOfEntitiesWithReference <TestArticle>("Categories", typeof(TestCategory), category.ID, location, "TitleAscending"); Assert.IsNotNull(results, "The results were null."); Assert.AreEqual(2, location.AbsoluteTotal, "The absolute total count is incorrect."); if (results != null) { Assert.AreEqual(2, results.Length, "Incorrect number of results found."); IEntity entity = results[0]; Assert.AreEqual(article.GetType().FullName, entity.GetType().FullName, "The types don't match."); } } }
public void Test_IsInPage_PageSize1_In() { using (LogGroup logGroup = LogGroup.Start("Testing the IsInPage function with a page size of 1 on an item that appears on the specified page.", LogLevel.Debug)) { int i = 1; int pageIndex = 1; int pageSize = 1; PagingLocation location = new PagingLocation(pageIndex, pageSize); bool isInPage = location.IsInPage(i); Assert.IsTrue(isInPage, "Returned false when it should have been true."); } }
public void Test_Index_PrepareIndex() { List <TestRecord> list = new List <TestRecord>(); for (int i = 0; i < 30; i++) { TestRecord record = new TestRecord(); record.ID = Guid.NewGuid(); record.Name = "Record " + i; record.Text = "Record " + i; DataAccess.Data.Saver.Save(record); list.Add(record); } if (DataAccess.Data == null) { throw new InvalidOperationException("Data provider has not been initialized. Run setup."); } BaseIndexProjection page = new BaseIndexProjection("Index", typeof(TestRecord), false); PagingLocation location = new PagingLocation(0, 20); IndexController controller = IndexController.New(page, location); if (controller == null) { throw new Exception("Controller is null."); } controller.SortExpression = "NameAscending"; IEntity[] entities = controller.PrepareIndex(); Assert.IsNotNull(entities, "entities == null"); Assert.AreEqual(20, entities.Length, "Entity count mismatch."); Assert.AreEqual(30, controller.AbsoluteTotal, "Absolute count mismatch."); foreach (TestRecord record in list) { DataAccess.Data.Deleter.Delete(record); } }
protected virtual IEntity[] GetPage(IObjectSet objectSet, PagingLocation location) { int i = 0; List <IEntity> list = new List <IEntity>(); // Loop through each index in the object set for (i = 0; i < objectSet.Count; i++) { // If it's not in the current page then skip it if (location.IsInPage(i)) { // Add the entity to the collection list.Add((IEntity)objectSet[i]); } } location.AbsoluteTotal = i; return(list.ToArray()); }
/// <summary> /// Retrieves the specified page of objects from the provided IObjectSet. /// </summary> /// <param name="type">The type of entities to retrieve.</param> /// <param name="propertyName">The name of the property to query for.</param> /// <param name="propertyValue">The value of the property to query for.</param> /// <param name="location">The paging location to filter the query by.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the objects retrieved.</returns> public override T[] GetPageOfEntities <T>(string propertyName, object propertyValue, PagingLocation location, string sortExpression) { return(Collection <T> .ConvertAll(GetPageOfEntities(typeof(T), propertyName, propertyValue, location, sortExpression))); }
public void Test_GetEntitiesPage_Page1_PageSize1_SortDescending() { TestArticle article1 = new TestArticle(); article1.ID = Guid.NewGuid(); article1.Title = "Article C"; TestArticle article2 = new TestArticle(); article2.ID = Guid.NewGuid(); article2.Title = "Article B"; TestArticle article3 = new TestArticle(); article3.ID = Guid.NewGuid(); article3.Title = "article A"; List <string> titles = new List <string>(); titles.Add(article1.Title); titles.Add(article2.Title); titles.Add(article3.Title); List <Guid> ids = new List <Guid>(); ids.Add(article1.ID); ids.Add(article2.ID); ids.Add(article3.ID); DataAccess.Data.Saver.Save(article2); DataAccess.Data.Saver.Save(article3); DataAccess.Data.Saver.Save(article1); PagingLocation pagingLocation = new PagingLocation(0, 1); string sortExpression = "TitleDescending"; TestArticle[] entities = DataAccess.Data.Indexer.GetPageOfEntities <TestArticle>(pagingLocation, sortExpression); Assert.IsNotNull(entities); List <string> titlesFound = new List <string>(); List <Guid> idsFound = new List <Guid>(); foreach (TestArticle a in entities) { Assert.IsFalse(idsFound.Contains(a.ID), "The returned entities list includes more than one entity with the same ID'."); Assert.IsFalse(titlesFound.Contains(a.Title), "The returned entities list includes more than one entity with the same title: '" + a.Title + "'."); Assert.IsTrue(titles.Contains(a.Title), "The title of one of the retrieved entities doesn't match any of those expected."); Assert.IsTrue(ids.Contains(a.ID), "The ID of one of the retrieved entities doesn't match any of those expected."); titlesFound.Add(a.Title); idsFound.Add(a.ID); } Assert.AreEqual(1, entities.Length, "Invalid number found."); Assert.AreEqual(article1.Title, entities[0].Title, "Sorting failed #1."); //Assert.AreEqual(article2.Title, entities[1].Title, "Sorting failed #2."); //Assert.AreEqual(article3.Title, entities[2].Title, "Sorting failed #3."); }
public abstract T[] GetPageOfEntities <T>(string fieldName, object fieldValue, PagingLocation location, string sortExpression) where T : IEntity;
/// <summary> /// Retrieves a single page (at the specified location) of the specified type of entities from the corresponding data store. /// </summary> /// <param name="type">The type of entities to retrieve.</param> /// <param name="location">The coordinates of the page being retrieved.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the entities, including only the current page.</returns> public override IEntity[] GetPageOfEntities(Type type, PagingLocation location, string sortExpression) { // Create the collection List <IEntity> entities = new List <IEntity>(); using (LogGroup logGroup = LogGroup.Start("Retrieving a page of entities.", NLog.LogLevel.Debug)) { if (type == null) { throw new ArgumentNullException("type"); } if (location == null) { throw new ArgumentNullException("location"); } // Output the parameters to the log LogWriter.Debug("Type: " + type.ToString()); LogWriter.Debug("Page index: " + location.PageIndex); LogWriter.Debug("Page size: " + location.PageSize); LogWriter.Debug("Sort expression: " + sortExpression); // Get the corresponding data store Db4oDataStore store = (Db4oDataStore)GetDataStore(type); if (store == null) { throw new Exception("Can't find data store."); } if (store.ObjectContainer == null) { throw new Exception("store.ObjectContainer is null"); } // Create the query object IQuery query = store.ObjectContainer.Query(); // Constrain the query to the specified type query.Constrain(type); // Apply the sorting to the query ApplySorting(query, type, sortExpression); // Execute the query and get the object set IObjectSet os = query.Execute(); if (os == null) { throw new Exception("IObjectSet is null"); } int i = 0; // Loop through each index in the object set for (i = 0; i < os.Count; i++) { // ONLY execute during debug because it slows the indexing down if (new ModeDetector().IsDebug) { LogWriter.Debug("At absolute position " + i + ": " + ((IEntity)os[i]).ToString()); } // If it's not in the current page then skip it if (location.IsInPage(i)) { // Add the entity to the collection entities.Add((IEntity)os[i]); } } // Set the absolute total to the paging location // This is the total count including items on ALL pages, not just the current one location.AbsoluteTotal = i; LogWriter.Debug("Absolute count: " + location.AbsoluteTotal.ToString()); LogWriter.Debug("Entities count (single page): " + entities.Count.ToString()); } // Return the entities return(Release(entities.ToArray())); }
/// <summary> /// Retrieves a single page (at the specified location) of the specified type of entities from the corresponding data store. /// </summary> /// <param name="location">The coordinates of the page being retrieved.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the entities, including only the current page.</returns> public override T[] GetPageOfEntities <T>(PagingLocation location, string sortExpression) { // Pass this call through to another overload // Convert/cast the entities on the way return(Collection <T> .ConvertAll(GetPageOfEntities(typeof(T), location, sortExpression))); }
/// <summary> /// Retrieves the specified page of objects from the provided IObjectSet. /// </summary> /// <param name="type">The type of entities to retrieve.</param> /// <param name="filterGroup">The group to filter the query by.</param> /// <param name="location">The paging location to filter the query by.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the objects retrieved.</returns> public override IEntity[] GetPageOfEntities(Type type, IDataFilterGroup filterGroup, PagingLocation location, string sortExpression) { Db4oDataStore store = (Db4oDataStore)GetDataStore(type); Collection <IEntity> list = new Collection <IEntity>(); IObjectSet os = null; if (store.DoesExist) { Predicate matches = new MatchFilterGroupPredicate(filterGroup); os = store.ObjectContainer.Query(matches, new DynamicComparer( type, sortExpression)); list.AddRange(GetPage(os, location)); } return(Release((IEntity[])list.ToArray(type))); }
/// <summary> /// Retrieves the specified page of objects from the provided IObjectSet. /// </summary> /// <param name="filterGroup">The group to filter the query by.</param> /// <param name="location">The paging location to filter the query by.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the objects retrieved.</returns> public override T[] GetPageOfEntities <T>(IDataFilterGroup filterGroup, PagingLocation location, string sortExpression) { return(Collection <T> .ConvertAll(GetPageOfEntities(typeof(T), filterGroup, location, sortExpression))); }
/// <summary> /// Retrieves the specified page of objects from the provided IObjectSet. /// </summary> /// <param name="type">The type of entities to retrieve.</param> /// <param name="propertyName">The name of the property to query for.</param> /// <param name="propertyValue">The value of the property to query for.</param> /// <param name="location">The paging location to filter the query by.</param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the objects retrieved.</returns> public override IEntity[] GetPageOfEntities(Type type, string propertyName, object propertyValue, PagingLocation location, string sortExpression) { IQuery query = ((Db4oDataStore)GetDataStore(type)).ObjectContainer.Query(); query.Constrain(type); query.Descend(propertyName).Constrain(propertyValue); ApplySorting(query, type, sortExpression); IObjectSet os = query.Execute(); ArrayList page = new ArrayList(); page.Add(GetPage(os, location)); return(Release((IEntity[])page.ToArray(type))); }
public abstract IEntity[] GetPageOfEntities(Type type, PagingLocation location, string sortExpression);
public abstract T[] GetPageOfEntities <T>(IDataFilterGroup filterGroup, PagingLocation location, string sortExpression) where T : IEntity;
public abstract T[] GetPageOfEntitiesWithReference <T>(string propertyName, Type referencedEntityType, Guid referencedEntityID, PagingLocation location, string sortExpression) where T : IEntity;
public abstract IEntity[] GetPageOfEntities(Type type, string fieldName, object fieldValue, PagingLocation location, string sortExpression);
public abstract T[] GetPageOfEntities <T>(PagingLocation location, string sortExpression) where T : IEntity;
/// <summary> /// Retrieves the specified page of entities from the data store. /// </summary> /// <param name="propertyName">The name of the property containing the reference.</param> /// <param name="referencedEntities">An array of entities to check the reference to.</param> /// <param name="location"></param> /// <param name="sortExpression">The sort expression to apply before retrieving the page.</param> /// <returns>An array of the objects retrieved.</returns> public override T[] GetPageOfEntitiesWithReference <T>(string propertyName, IEntity[] referencedEntities, PagingLocation location, string sortExpression) { if (referencedEntities.Length == 0) { return new T[] {} } ; Collection <T> page = new Collection <T>(); //using (LogGroup logGroup = LogGroup.Start("Querying the data store based on the provided parameters.", NLog.LogLevel.Debug)) //{ //LogWriter.Debug("Property name: " + propertyName); //LogWriter.Debug("Referenced entity ID: " + referencedEntityID); if (location == null) { throw new ArgumentNullException("location"); } Type type = typeof(T); string sortPropertyName = sortExpression.Replace("Ascending", "").Replace("Descending", ""); Db4oDataStore store = (Db4oDataStore)GetDataStore(type); if (store == null) { throw new ArgumentException("Can't find data store for type '" + type.Name + "'."); } IObjectContainer container = store.ObjectContainer; if (container == null) { throw new Exception("No object container for store '" + store.Name + "'."); } int i = 0; PropertyInfo property = typeof(T).GetType().GetProperty(propertyName); if (referencedEntities.Length > 0) { Type referencedEntityType = referencedEntities.GetType().GetElementType(); Predicate matches = new MatchReferencesPredicate(Provider, typeof(T), propertyName, referencedEntityType, Collection <IEntity> .GetIDs(referencedEntities)); IObjectSet os = store.ObjectContainer.Query(matches, new DynamicComparer( type, sortExpression)); page.AddRange(GetPage(os, location)); } //LogWriter.Debug("Absolute total objects: " + location.AbsoluteTotal); //} return(Release((T[])page.ToArray())); }
public abstract IEntity[] GetPageOfEntities(Type type, IDataFilterGroup filterGroup, PagingLocation location, string sortExpression);