PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace.UserInterface/Utilities/EntityGraphUtilities.cs

https://bitbucket.org/VahidN/interlace
C# | 177 lines | 108 code | 40 blank | 29 comment | 3 complexity | f7a711dbba729e6a75b9db68492f29f5 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.Text;
  31. using SD.LLBLGen.Pro.ORMSupportClasses;
  32. using Interlace.Collections;
  33. #endregion
  34. namespace Interlace.Utilities
  35. {
  36. public static class EntityGraphUtilities
  37. {
  38. public static List<IEntityCollection2> GetAllMemberEntityCollections(IEnumerable<IEntity2> entities)
  39. {
  40. List<IEntityCollection2> collections = new List<IEntityCollection2>();
  41. foreach (IEntity2 entity in entities)
  42. {
  43. foreach (IEntityCollection2 collection in entity.GetMemberEntityCollections())
  44. {
  45. collections.Add(collection);
  46. }
  47. }
  48. return collections;
  49. }
  50. public static IEnumerable<IEntity2> GetAllEntitiesInGraph(IEntity2 rootObject)
  51. {
  52. Queue<IEntity2> seen = new Queue<IEntity2>();
  53. Set<IEntity2> visited = new Set<IEntity2>();
  54. seen.Enqueue(rootObject);
  55. while (seen.Count > 0)
  56. {
  57. IEntity2 current = seen.Dequeue();
  58. visited.UnionUpdate(current);
  59. // Add the connected entities to a temporary list:
  60. List<IEntity2> newEntities = new List<IEntity2>();
  61. newEntities.AddRange(current.GetDependentRelatedEntities());
  62. newEntities.AddRange(current.GetDependingRelatedEntities());
  63. foreach (IEntityCollection2 collection in current.GetMemberEntityCollections())
  64. {
  65. foreach (IEntity2 entity in collection) newEntities.Add(entity);
  66. }
  67. // Ensure they're visited:
  68. foreach (IEntity2 entity in newEntities)
  69. {
  70. if (!visited.Contains(entity)) seen.Enqueue(entity);
  71. }
  72. }
  73. return visited;
  74. }
  75. public static bool IsGraphDirty(IEntity2 rootObject)
  76. {
  77. EntityGraphIsDirtyVisitor visitor = new EntityGraphIsDirtyVisitor();
  78. VisitAllEntities(rootObject, visitor);
  79. return visitor.Result;
  80. }
  81. #region Debugging Helpers
  82. public static void PrintAllEntitiesInGraph(IEntity2 rootObject)
  83. {
  84. EntityGraphPrintingVisitor visitor = new EntityGraphPrintingVisitor(new string[] { });
  85. VisitAllEntities(rootObject, visitor);
  86. Console.Write(visitor.Result);
  87. }
  88. public static string ExamineAllEntitiesInGraph(IEntity2 rootObject, params string[] fieldsToDisplay)
  89. {
  90. EntityGraphPrintingVisitor visitor = new EntityGraphPrintingVisitor(fieldsToDisplay);
  91. VisitAllEntities(rootObject, visitor);
  92. return visitor.Result;
  93. }
  94. public static string ExamineDirtyEntities(IEntity2 rootObject)
  95. {
  96. EntityGraphModificationPrintingVisitor visitor = new EntityGraphModificationPrintingVisitor();
  97. VisitAllEntities(rootObject, visitor);
  98. return visitor.Result;
  99. }
  100. public static void PrintEntitiesWithMatchingProperty(IEntity2 rootObject, Type entityType, string propertyName, object propertyValue)
  101. {
  102. VisitAllEntities(rootObject, new EntityGraphSearchingVisitor(entityType, propertyName, propertyValue));
  103. }
  104. public static void VisitAllEntities(IEntity2 rootObject, EntityGraphVisitor visitor)
  105. {
  106. VisitAllEntitiesRecursion(new CactusStack<IEntity2>(rootObject), new Set<IEntity2>(), visitor);
  107. }
  108. static void VisitAllEntitiesRecursion(CactusStack<IEntity2> current, Set<IEntity2> visited, EntityGraphVisitor visitor)
  109. {
  110. visited.UnionUpdate(current.Value);
  111. visitor.VisitEntity(current);
  112. // Add the connected entities to a temporary list:
  113. List<IEntity2> newEntities = new List<IEntity2>();
  114. newEntities.AddRange(current.Value.GetDependentRelatedEntities());
  115. newEntities.AddRange(current.Value.GetDependingRelatedEntities());
  116. foreach (IEntityCollection2 collection in current.Value.GetMemberEntityCollections())
  117. {
  118. foreach (IEntity2 entity in collection) newEntities.Add(entity);
  119. }
  120. // Recurse into them:
  121. foreach (IEntity2 entity in newEntities)
  122. {
  123. if (!visited.Contains(entity))
  124. {
  125. visitor.BeginVisitingChildren(current);
  126. VisitAllEntitiesRecursion(current.Push(entity), visited, visitor);
  127. visitor.EndVisitingChildren(current);
  128. }
  129. else
  130. {
  131. visitor.VisitAlreadyVisitedEntity(current.Push(entity));
  132. }
  133. }
  134. }
  135. #endregion
  136. }
  137. }