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

/DICK.B1/IronPython.DICK/Other.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 313 lines | 264 code | 35 blank | 14 comment | 16 complexity | e7832370ac0d0599466a824027ee30e9 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Runtime.CompilerServices;
  21. using System.Runtime.InteropServices;
  22. using System.Reflection;
  23. using System.Text;
  24. using System.Threading;
  25. using DICK.Core;
  26. using DICK.Core.JobManagement;
  27. using DICK.B1;
  28. using SAPbobsCOM;
  29. using Microsoft.Scripting;
  30. using Microsoft.Scripting.Actions;
  31. using Microsoft.Scripting.Runtime;
  32. using Microsoft.Scripting.Utils;
  33. using IronPython.Compiler;
  34. using IronPython.Runtime;
  35. using IronPython.Runtime.Binding;
  36. using IronPython.Runtime.Exceptions;
  37. using IronPython.Runtime.Operations;
  38. using IronPython.Runtime.Types;
  39. #if CLR2
  40. using Microsoft.Scripting.Math;
  41. using Complex = Microsoft.Scripting.Math.Complex64;
  42. #else
  43. using System.Numerics;
  44. #endif
  45. [assembly: PythonModule("__builtin__", typeof(IronPython.Modules.Builtin))]
  46. namespace IronPython.Modules {
  47. public static partial class Builtin {
  48. private static CompanyClass getsession()
  49. {
  50. B1Engine engine = B1Engine.GetInstance();
  51. return engine.session;
  52. }
  53. public static IronPython.Runtime.List getservices()
  54. {
  55. return getservices(B1Engine.LISTCASE.CASE_UPPER);
  56. }
  57. public static IronPython.Runtime.List getservices(B1Engine.LISTCASE listcase)
  58. {
  59. IronPython.Runtime.List list = new IronPython.Runtime.List();
  60. foreach (string servicename in B1Engine.GetInstance().B1ServiceList.Keys)
  61. {
  62. ProcessName(listcase, list, servicename);
  63. }
  64. return list;
  65. }
  66. public static IronPython.Runtime.List getbusinessobjects()
  67. {
  68. return getbusinessobjects(B1Engine.LISTCASE.CASE_UPPER);
  69. }
  70. public static IronPython.Runtime.List getbusinessobjects(B1Engine.LISTCASE listcase)
  71. {
  72. IronPython.Runtime.List list = new IronPython.Runtime.List();
  73. foreach (string objectname in B1Engine.GetInstance().B1ObjectList.Keys)
  74. {
  75. ProcessName(listcase, list, objectname);
  76. }
  77. return list;
  78. }
  79. private static void ProcessName(B1Engine.LISTCASE listcase, IronPython.Runtime.List list, string objectname)
  80. {
  81. switch (listcase)
  82. {
  83. case B1Engine.LISTCASE.CASE_LOWER:
  84. list.AddNoLockNoDups(objectname.ToLower());
  85. break;
  86. case B1Engine.LISTCASE.CASE_UPPER:
  87. list.AddNoLockNoDups(objectname.ToUpper());
  88. break;
  89. case B1Engine.LISTCASE.CASE_MIXED:
  90. if (!objectname.isupper() && !objectname.islower())
  91. list.AddNoLockNoDups(objectname);
  92. break;
  93. case B1Engine.LISTCASE.CASE_ALL:
  94. list.AddNoLockNoDups(objectname);
  95. break;
  96. default:
  97. list.AddNoLockNoDups(objectname);
  98. break;
  99. }
  100. }
  101. public static object getservice(SAPbobsCOM.ServiceTypes servicetype, object datainterface)
  102. {
  103. CompanyClass session = getsession();
  104. try
  105. {
  106. SAPbobsCOM.CompanyService companyservice;
  107. companyservice = session.GetCompanyService();
  108. object svc = companyservice.GetBusinessService(servicetype);
  109. string expr = servicetype.ToString() + ".GetDataInterface(" + datainterface.ToString() + ")";
  110. return B1Engine.GetInstance().ExecutePython(expr);
  111. }
  112. catch (Exception ex)
  113. {
  114. return ex.Message;
  115. }
  116. }
  117. public static object getservice(SAPbobsCOM.ServiceTypes servicetype)
  118. {
  119. CompanyClass session = getsession();
  120. try
  121. {
  122. SAPbobsCOM.CompanyService companyservice;
  123. companyservice = session.GetCompanyService();
  124. return companyservice.GetBusinessService(servicetype);
  125. }
  126. catch (Exception ex)
  127. {
  128. return ex.Message;
  129. }
  130. }
  131. public static int getlasterrorcode()
  132. {
  133. CompanyClass session = getsession();
  134. return session.GetLastErrorCode();
  135. }
  136. public static int getlasterrorcode(CompanyClass session)
  137. {
  138. return session.GetLastErrorCode();
  139. }
  140. public static string getlasterrordescription()
  141. {
  142. CompanyClass session = getsession();
  143. return session.GetLastErrorDescription();
  144. }
  145. public static string getlasterrordescription(CompanyClass session)
  146. {
  147. return session.GetLastErrorDescription();
  148. }
  149. public static string getlasterror()
  150. {
  151. CompanyClass session = getsession();
  152. return getlasterror(session);
  153. }
  154. public static string getlasterror(CompanyClass session)
  155. {
  156. string errcode = session.GetLastErrorCode().ToString();
  157. string errtext = session.GetLastErrorDescription();
  158. string err = errcode;
  159. if (errtext != null && !errtext.Trim().Equals(""))
  160. {
  161. err += " " + errtext;
  162. }
  163. return err;
  164. }
  165. public static void release(object o)
  166. {
  167. if (o != null)
  168. {
  169. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o);
  170. }
  171. }
  172. public static Recordset query(string query)
  173. {
  174. CompanyClass session = getsession();
  175. Recordset rs = session.GetBusinessObject(BoObjectTypes.BoRecordset) as Recordset;
  176. rs.DoQuery(query);
  177. return rs;
  178. }
  179. public static Recordset query(string query, CompanyClass session)
  180. {
  181. Recordset rs = session.GetBusinessObject(BoObjectTypes.BoRecordset) as Recordset;
  182. rs.DoQuery(query);
  183. return rs;
  184. }
  185. public static IEnumerable<object> inspect(object o)
  186. {
  187. Type t = o.GetType();
  188. MemberInfo[] mis=t.GetMembers();
  189. foreach(MemberInfo mi in mis)
  190. {
  191. yield return mi.Name;
  192. }
  193. }
  194. public static IEnumerable<object> browse(object o)
  195. {
  196. if (o is Recordset)
  197. {
  198. Recordset rs = (Recordset)o;
  199. if (rs.Fields.Count == 1)
  200. {
  201. return new ColumnBrowser(null, rs);
  202. }
  203. else
  204. {
  205. return new RowBrowser(rs);
  206. }
  207. }
  208. if(o is IronPython.Runtime.List)
  209. {
  210. return new PythonListBrowser((IronPython.Runtime.List)o);
  211. }
  212. else
  213. {
  214. return new Browser(o);
  215. }
  216. }
  217. public static IEnumerable<object> browse(BoObjectTypes objtype, SAPbobsCOM.Recordset rs)
  218. {
  219. object o = get(objtype);
  220. return new BOBSObjectRecordSetBrowser(o, rs);
  221. }
  222. public static IEnumerable<object> browse(BoObjectTypes objtype, SAPbobsCOM.Recordset rs, CompanyClass session)
  223. {
  224. object o = session.GetBusinessObject(objtype);
  225. return new BOBSObjectRecordSetBrowser(o, rs);
  226. }
  227. public static IEnumerable<object> browse(SAPbobsCOM.Recordset rs, string columnname)
  228. {
  229. return new ColumnBrowser(columnname, rs);
  230. }
  231. public static ExportJob getexportjob(string jobname)
  232. {
  233. return Engine.GetInstance().GetExportJob(jobname);
  234. }
  235. public static B1ImportJob getimportjob(string jobname)
  236. {
  237. return B1Engine.GetInstance().GetImportJob(jobname);
  238. }
  239. public static GenericJob getgenericjob(string jobname)
  240. {
  241. return Engine.GetInstance().GetGenericJob(jobname);
  242. }
  243. public static object get(string objectname)
  244. {
  245. B1Engine engine=B1Engine.GetInstance();
  246. if(engine.pythoninitialized)
  247. {
  248. object handle = B1Engine.GetInstance().scope.GetVariable(objectname.ToUpper());
  249. if (handle != null && handle.GetType().Equals(typeof(BoObjectTypes)))
  250. {
  251. return get((BoObjectTypes)handle);
  252. }
  253. else
  254. {
  255. engine.LogError("Could not retrieve handle for " + objectname);
  256. return null;
  257. }
  258. }
  259. else
  260. {
  261. engine.LogError("Python Engine not initialized !!!");
  262. return null;
  263. }
  264. }
  265. public static DICKContext export(string jobname, DICKContext context, params String[] args)
  266. {
  267. B1Engine b1engine = B1Engine.GetInstance();
  268. ExportJob job=b1engine.GetExportJob(jobname);
  269. List<String> keys=new List<String>();
  270. foreach(String arg in args)
  271. {
  272. keys.Add(arg);
  273. }
  274. job.DoExportByKeys(keys);
  275. return context;
  276. }
  277. }
  278. }