PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/SurveyControls/SurveyQuestion/SQBindingContext.cs

http://coffee.codeplex.com
C# | 166 lines | 152 code | 14 blank | 0 comment | 24 complexity | 2bee090dd2c2e54dfb3ae303b7691e50 MD5 | raw file
  1. using System.Web.UI;
  2. using System.Xml;
  3. using Coffee.DataMappers;
  4. namespace Coffee
  5. {
  6. public class SQBindingField
  7. {
  8. public string ControlType;
  9. public string Target;
  10. public string PropertyName;
  11. }
  12. public class SQBindingContext
  13. {
  14. public SQBindingField Current;
  15. public SQBindingField Previous;
  16. XmlNode configuration;
  17. public SQBindingContext(XmlNode configuration)
  18. {
  19. this.configuration = configuration;
  20. Current = GetBindingField(configuration, "current");
  21. Previous = GetBindingField(configuration, "previous");
  22. }
  23. public void ReadProperties(IDataMapper mapper, Control previous, Control current)
  24. {
  25. Reflection.SetProperty(
  26. Previous.Target,
  27. previous,
  28. mapper.Previous.GetProperty(Previous.PropertyName)
  29. );
  30. Reflection.SetProperty(
  31. Current.Target,
  32. current,
  33. mapper.Current.GetProperty(Current.PropertyName)
  34. );
  35. }
  36. public void WriteProperties(IDataMapper mapper, object current)
  37. {
  38. XmlAttribute dataType;
  39. bool IsNullOrEmpty = false;
  40. string curs = string.Empty;
  41. if (current.GetType() == typeof(string))
  42. {
  43. curs = (string)current;
  44. IsNullOrEmpty = string.IsNullOrEmpty(curs);
  45. }
  46. if ((dataType = configuration.Attributes["datatype"]) != null)
  47. {
  48. if (dataType.Value == "integer")
  49. {
  50. if (current.GetType() == typeof(string))
  51. {
  52. if (IsNullOrEmpty)
  53. {
  54. mapper.Current.SetProperty(Current.PropertyName, null);
  55. }
  56. else
  57. {
  58. mapper.Current.SetProperty(Current.PropertyName, int.Parse(curs.Trim()));
  59. }
  60. }
  61. else
  62. {
  63. mapper.Current.SetProperty(Current.PropertyName, current);
  64. }
  65. }
  66. else if (dataType.Value == "decimal")
  67. {
  68. if (current.GetType() == typeof(string))
  69. {
  70. if (IsNullOrEmpty)
  71. {
  72. mapper.Current.SetProperty(Current.PropertyName, null);
  73. }
  74. else
  75. {
  76. mapper.Current.SetProperty(Current.PropertyName, decimal.Parse(curs.Trim()));
  77. }
  78. }
  79. else
  80. {
  81. mapper.Current.SetProperty(Current.PropertyName, current);
  82. }
  83. }
  84. else
  85. {
  86. if (IsNullOrEmpty)
  87. {
  88. mapper.Current.SetProperty(Current.PropertyName, null);
  89. }
  90. else
  91. {
  92. mapper.Current.SetProperty(Current.PropertyName, current);
  93. }
  94. }
  95. }
  96. else
  97. {
  98. if (IsNullOrEmpty)
  99. {
  100. mapper.Current.SetProperty(Current.PropertyName, null);
  101. }
  102. else
  103. {
  104. mapper.Current.SetProperty(Current.PropertyName, current);
  105. }
  106. }
  107. }
  108. SQBindingField GetBindingField(XmlNode configuration, string name)
  109. {
  110. SQBindingField field = new SQBindingField();
  111. string ctlId = configuration.Attributes["id"].Value;
  112. string ctlType = configuration.Attributes["type"].Value;
  113. string defaultType = string.Empty;
  114. switch (name)
  115. {
  116. case "current":
  117. switch (ctlType)
  118. {
  119. case "TextBox": defaultType = "Text"; break;
  120. case "DropDownList": defaultType = "SelectedValue"; break;
  121. }
  122. break;
  123. case "previous":
  124. switch (ctlType)
  125. {
  126. case "TextBox": defaultType = "Text"; break;
  127. case "DropDownList": defaultType = "Text"; break;
  128. }
  129. break;
  130. }
  131. XmlNode binding = configuration.SelectSingleNode("binding/" + name);
  132. if (binding != null)
  133. {
  134. if (binding.Attributes["type"] != null)
  135. {
  136. field.Target = binding.Attributes["type"].Value;
  137. }
  138. else
  139. {
  140. field.Target = defaultType;
  141. }
  142. field.PropertyName = binding.SelectSingleNode("text()").Value;
  143. }
  144. else
  145. {
  146. field.Target = defaultType;
  147. field.PropertyName = ctlId;
  148. }
  149. return field;
  150. }
  151. }
  152. }