PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/DICK.B1/B1Browser.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 185 lines | 152 code | 20 blank | 13 comment | 12 complexity | 87bed39f4a1b1a9702da8b25b2c7eb7d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using System.Reflection;
  6. using SAPbobsCOM;
  7. namespace DICK.B1
  8. {
  9. public class PythonListBrowser : IEnumerable<object>
  10. {
  11. IronPython.Runtime.List list;
  12. public PythonListBrowser(IronPython.Runtime.List _list)
  13. {
  14. list = _list;
  15. }
  16. public int GetCount(object dummy)
  17. {
  18. return list.Count;
  19. }
  20. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
  21. public IEnumerator<object> GetEnumerator()
  22. {
  23. foreach (object o in list)
  24. {
  25. yield return o;
  26. }
  27. }
  28. }
  29. /// <summary>
  30. /// Used to browse a SAPbobsCOM "lines" object
  31. /// </summary>
  32. public class Browser : IEnumerable<object>
  33. {
  34. object lines;
  35. Type t;
  36. public Browser(object _lines)
  37. {
  38. lines = _lines;
  39. t = _lines.GetType();
  40. }
  41. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
  42. public int GetCount(object dummy)
  43. {
  44. try
  45. {
  46. object countobj = t.InvokeMember("Count", BindingFlags.GetProperty | BindingFlags.GetField, null, lines, new object[] { });
  47. int count = (int)countobj;
  48. return count;
  49. }
  50. catch (Exception ex)
  51. {
  52. B1Engine.GetInstance().LogDebug("Exception when accessing Count in B1Wrapper: " + ex.Message);
  53. throw;
  54. }
  55. }
  56. public IEnumerator<object> GetEnumerator()
  57. {
  58. object countobj = t.InvokeMember("Count", BindingFlags.GetProperty | BindingFlags.GetField, null, lines, new object[] { });
  59. int count = (int)countobj;
  60. for (int index = 0; index < count; index++)
  61. {
  62. t.InvokeMember("SetCurrentLine", BindingFlags.InvokeMethod, null, lines, new object[] { index });
  63. yield return lines;
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// Used to browse a set of SAPbobsCOM objects that all are of the specified type.
  69. /// </summary>
  70. public class BOBSObjectRecordSetBrowser : IEnumerable<object>
  71. {
  72. object o;
  73. Recordset rs;
  74. Type t;
  75. public BOBSObjectRecordSetBrowser(object _o, SAPbobsCOM.Recordset _rs)
  76. {
  77. o = _o;
  78. rs = _rs;
  79. t = o.GetType();
  80. }
  81. IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
  82. public IEnumerator<object> GetEnumerator()
  83. {
  84. bool originalAutoRelease = B1Engine.AutoRelease;
  85. B1Engine.AutoRelease = false;
  86. try
  87. {
  88. object browserobj = t.InvokeMember("Browser", BindingFlags.GetProperty | BindingFlags.GetField, null, o, new object[] { });
  89. DataBrowser br = (DataBrowser)browserobj;
  90. Type brt = br.GetType();
  91. brt.InvokeMember("Recordset", BindingFlags.SetField | BindingFlags.SetProperty, null, br, new object[] { rs });
  92. if (br != null && br.RecordCount > 0)
  93. {
  94. br.MoveFirst();
  95. while (!br.EoF)
  96. {
  97. yield return o;
  98. br.MoveNext();
  99. }
  100. }
  101. }
  102. finally
  103. {
  104. B1Engine.AutoRelease = originalAutoRelease;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Used to iterate through a single column from the specified dataset
  110. /// </summary>
  111. public class ColumnBrowser : IEnumerable<object>
  112. {
  113. string column;
  114. Recordset rs;
  115. public ColumnBrowser(string _column, Recordset _rs)
  116. {
  117. column = _column;
  118. rs = _rs;
  119. }
  120. IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
  121. public IEnumerator<object> GetEnumerator()
  122. {
  123. int columnindex = 0;
  124. if (rs.Fields.Count > 1 && column != null)
  125. {
  126. for (int i = 0; i < rs.Fields.Count; i++)
  127. {
  128. Field f = rs.Fields.Item(i);
  129. if (f.Name.ToUpper().Equals(column.ToUpper()))
  130. {
  131. columnindex = i;
  132. }
  133. }
  134. }
  135. rs.MoveFirst();
  136. while (!rs.EoF)
  137. {
  138. yield return rs.Fields.Item(columnindex).Value;
  139. rs.MoveNext();
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// Used to iterate through a recordset, returning a Fields object for each record.
  145. /// </summary>
  146. public class RowBrowser : IEnumerable<object>
  147. {
  148. Recordset rs;
  149. public RowBrowser(Recordset _rs)
  150. {
  151. rs = _rs;
  152. }
  153. IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
  154. public IEnumerator<object> GetEnumerator()
  155. //public IEnumerator<Fields> GetEnumerator()
  156. {
  157. rs.MoveFirst();
  158. while (!rs.EoF)
  159. {
  160. yield return rs.Fields;
  161. rs.MoveNext();
  162. }
  163. }
  164. }
  165. }