PageRenderTime 62ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/VahidN/interlace
C# | 166 lines | 105 code | 34 blank | 27 comment | 12 complexity | 83ce4db026e1642690a09f4e47ab8879 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.Text;
  30. using SD.LLBLGen.Pro.ORMSupportClasses;
  31. using Interlace.Collections;
  32. #endregion
  33. namespace Interlace.Utilities
  34. {
  35. internal class EntityGraphPrintingVisitor : EntityGraphVisitor
  36. {
  37. ReferenceLabeller _labeller;
  38. StringBuilder _builder;
  39. string[] _fieldsToDisplay;
  40. public EntityGraphPrintingVisitor(string[] fieldsToDisplay)
  41. {
  42. _labeller = new ReferenceLabeller();
  43. _builder = new StringBuilder();
  44. _fieldsToDisplay = fieldsToDisplay;
  45. if (_fieldsToDisplay == null) _fieldsToDisplay = new string[] { };
  46. }
  47. public string Result
  48. {
  49. get { return _builder.ToString(); }
  50. }
  51. public override void VisitEntity(CactusStack<IEntity2> current)
  52. {
  53. PrintCurrentEntity(_builder, current.Count - 1, current.Value, _labeller, _fieldsToDisplay);
  54. }
  55. public override void VisitAlreadyVisitedEntity(CactusStack<IEntity2> current)
  56. {
  57. PrintCurrentEntity(_builder, current.Count - 1, current.Value, _labeller, _fieldsToDisplay, "Already Visited");
  58. }
  59. internal static string GetValueDebugString(object value)
  60. {
  61. if (value == null) return "null";
  62. if (value is string) return string.Format("\"{0}\"", value);
  63. return string.Format("{0}", value);
  64. }
  65. internal static string GetFieldValuesDebugString(IEnumerable<IEntityField2> fields, bool includePreviousValues)
  66. {
  67. List<string> fieldStrings = new List<string>();
  68. foreach (IEntityField2 field in fields)
  69. {
  70. fieldStrings.Add(string.Format(includePreviousValues ? "{0}: {1} -> {2}" : "{0}: {2}",
  71. field.Name, GetValueDebugString(field.DbValue), GetValueDebugString(field.CurrentValue)));
  72. }
  73. return string.Join(", ", fieldStrings.ToArray());
  74. }
  75. internal static List<IEntityField2> GetFieldsFromNames(IEntity2 entity, IEnumerable<string> names)
  76. {
  77. List<IEntityField2> fields = new List<IEntityField2>();
  78. foreach (string fieldName in names)
  79. {
  80. IEntityField2 field = entity.Fields[fieldName];
  81. if (field != null && fields.IndexOf(field) == -1)
  82. {
  83. fields.Add(field);
  84. }
  85. }
  86. return fields;
  87. }
  88. internal static List<IEntityField2> GetDirtyFields(IEntity2 entity)
  89. {
  90. List<IEntityField2> fields = new List<IEntityField2>();
  91. foreach (IEntityField2 field in entity.Fields)
  92. {
  93. if (field.IsChanged) fields.Add(field);
  94. }
  95. return fields;
  96. }
  97. internal static string GetShortEntityName(IEntity2 entity)
  98. {
  99. Type entityType = entity.GetType();
  100. const string entitySuffix = "Entity";
  101. if (entityType.Name.EndsWith(entitySuffix))
  102. {
  103. return entityType.Name.Substring(0, entityType.Name.Length - entitySuffix.Length);
  104. }
  105. else
  106. {
  107. return string.Format("\"{0}\"", entityType.Name);
  108. }
  109. }
  110. internal static void PrintCurrentEntity(StringBuilder builder, int indent, IEntity2 current,
  111. ReferenceLabeller labeller, string[] fieldsToDisplay, params string[] notes)
  112. {
  113. // Build a list of primary key fields:
  114. List<IEntityField2> fields = new List<IEntityField2>();
  115. fields.AddRange(current.PrimaryKeyFields);
  116. fields.AddRange(GetFieldsFromNames(current, fieldsToDisplay));
  117. List<string> allNotes = new List<string>();
  118. allNotes.Add(labeller.Label(current));
  119. if (current.IsDirty) allNotes.Add("Dirty");
  120. allNotes.AddRange(notes);
  121. // Print the details:
  122. string leftPart = string.Format("{0} {1} ({2})",
  123. "".PadLeft(indent * 4),
  124. GetShortEntityName(current),
  125. GetFieldValuesDebugString(fields, false));
  126. string line = string.Format("{0} [{1}]",
  127. leftPart.PadRight(40),
  128. string.Join("; ", allNotes.ToArray()));
  129. builder.AppendLine(line);
  130. }
  131. }
  132. }