EntityCategory.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Wayne.Lib.Log
  4. {
  5. /// <summary>
  6. /// This class wraps an IIdentifiableEntity and a Category to be used as a
  7. /// key in e.g. Dictionaries and Lists.
  8. /// </summary>
  9. public class EntityCategory
  10. {
  11. #region Fields
  12. private DateTime lastTouched = DateTime.Now;
  13. private string categoryName;
  14. private string entityName;
  15. private string entityAncestryName;
  16. private string entityCategoryName;
  17. private string entityAncestryCategoryName;
  18. private List<IdentifableValues> ancestors;
  19. private int? hashCode;
  20. #endregion
  21. #region Construction
  22. /// <summary>
  23. /// Construction.
  24. /// </summary>
  25. /// <param name="entity">The entity.</param>
  26. /// <param name="category">The category.</param>
  27. internal EntityCategory(IIdentifiableEntity entity, object category)
  28. {
  29. Entity = entity;
  30. Category = category;
  31. if (category != null)
  32. CategoryString = category.ToString();
  33. else
  34. CategoryString = string.Empty;
  35. }
  36. #endregion
  37. #region Properties
  38. /// <summary>
  39. /// The entity.
  40. /// </summary>
  41. public IIdentifiableEntity Entity { get; private set; }
  42. /// <summary>
  43. /// The category.
  44. /// </summary>
  45. public object Category { get; private set; }
  46. /// <summary>
  47. /// The category as a string.
  48. /// </summary>
  49. public string CategoryString { get; private set; }
  50. /// <summary>
  51. /// A date time that specifies when the object was last touched.
  52. /// </summary>
  53. public DateTime LastTouched
  54. {
  55. get { return lastTouched; }
  56. }
  57. #endregion
  58. #region Methods
  59. /// <summary>
  60. /// ToString.
  61. /// </summary>
  62. /// <returns></returns>
  63. public override string ToString()
  64. {
  65. return GetName(EntityLogKind.Ancestors, false);
  66. }
  67. /// <summary>
  68. /// Equals
  69. /// </summary>
  70. /// <param name="obj"></param>
  71. /// <returns></returns>
  72. public override bool Equals(object obj)
  73. {
  74. EntityCategory otherEntityCategory = obj as EntityCategory;
  75. if (otherEntityCategory != null)
  76. {
  77. return IdentifiableEntity.Equals(Entity, otherEntityCategory.Entity) &&
  78. CategoryString.Equals(otherEntityCategory.CategoryString);
  79. }
  80. return false;
  81. }
  82. /// <summary>
  83. /// Equals
  84. /// </summary>
  85. /// <param name="entity"></param>
  86. /// <param name="category"></param>
  87. /// <returns></returns>
  88. public bool Equals(IIdentifiableEntity entity, object category)
  89. {
  90. if (entity == null)
  91. return false;
  92. return IdentifiableEntity.Equals(Entity, entity) && CategoryString.Equals(category.ToString());
  93. }
  94. /// <summary>
  95. /// GetHashCode
  96. /// </summary>
  97. /// <returns></returns>
  98. public override int GetHashCode()
  99. {
  100. if (!hashCode.HasValue)
  101. hashCode = Entity.GetHashCode() ^ CategoryString.GetHashCode();
  102. return hashCode.Value;
  103. }
  104. /// <summary>
  105. /// Touch the entity category.
  106. /// </summary>
  107. public void Touch()
  108. {
  109. lastTouched = DateTime.Now;
  110. }
  111. /// <summary>
  112. /// Get the log-name.
  113. /// </summary>
  114. /// <param name="entityLogKind">In which detail the id-entity should be presented.</param>
  115. /// <param name="suppressCategory">Should the category be suppressed or not.</param>
  116. /// <returns></returns>
  117. public string GetName(EntityLogKind entityLogKind, bool suppressCategory)
  118. {
  119. switch (entityLogKind)
  120. {
  121. ///////////////////////////////////////////////
  122. case EntityLogKind.None:
  123. if (suppressCategory)
  124. return string.Empty;
  125. if (categoryName == null)
  126. {
  127. if (string.IsNullOrEmpty(CategoryString))
  128. categoryName = string.Empty;
  129. else
  130. categoryName = "(" + CategoryString + ")";
  131. }
  132. return categoryName;
  133. ///////////////////////////////////////////////
  134. case EntityLogKind.Entity:
  135. if (suppressCategory)
  136. {
  137. if (entityName == null)
  138. entityName = IdentifiableEntity.ToString(Entity);
  139. return entityName;
  140. }
  141. if (entityCategoryName == null)
  142. {
  143. if (string.IsNullOrEmpty(CategoryString))
  144. entityCategoryName = IdentifiableEntity.ToString(Entity);
  145. else
  146. entityCategoryName = IdentifiableEntity.ToString(Entity) + "(" + CategoryString + ")";
  147. }
  148. return entityCategoryName;
  149. ///////////////////////////////////////////////
  150. case EntityLogKind.Ancestors:
  151. if (suppressCategory)
  152. {
  153. if (entityAncestryName == null)
  154. entityAncestryName = IdentifiableEntity.ToString(Entity, true);
  155. return entityAncestryName;
  156. }
  157. if (entityAncestryCategoryName == null)
  158. {
  159. if (string.IsNullOrEmpty(CategoryString))
  160. entityAncestryCategoryName = IdentifiableEntity.ToString(Entity, true);
  161. else
  162. entityAncestryCategoryName = IdentifiableEntity.ToString(Entity, true) + "(" + CategoryString + ")";
  163. }
  164. return entityAncestryCategoryName;
  165. ///////////////////////////////////////////////
  166. }
  167. return string.Empty;
  168. }
  169. internal IdentifableValues[] GetAncestors()
  170. {
  171. if (ancestors == null)
  172. {
  173. ancestors = new List<IdentifableValues>();
  174. foreach (var value in IdentifiableEntity.GetAncestorArray(Entity))
  175. {
  176. ancestors.Add(new IdentifableValues(value));
  177. }
  178. }
  179. return ancestors.ToArray();
  180. }
  181. #endregion
  182. }
  183. internal class IdentifableValues : IIdentifiableEntity
  184. {
  185. public int Id { get; private set; }
  186. public string EntityType { get; private set; }
  187. public string FullEntityName { get; set; }
  188. public string EntitySubType { get; private set; }
  189. public IIdentifiableEntity ParentEntity { get; private set; }
  190. public IdentifableValues(IIdentifiableEntity entity)
  191. {
  192. Id = entity.Id;
  193. EntityType = entity.EntityType;
  194. EntitySubType = entity.EntitySubType;
  195. }
  196. }
  197. }