PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.Core/JobManagement/ExportJob.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 270 lines | 197 code | 18 blank | 55 comment | 16 complexity | dced666b113882dd1ee963f47423ebc2 MD5 | raw file
  1. /* This file is part of DI Construction Kit.
  2. *
  3. * DI Construction Kit is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * DI Construction Kit is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with DI Construction Kit. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. using System;
  18. using System.Xml;
  19. using System.Collections;
  20. using System.Collections.Generic;
  21. using System.Data;
  22. using System.IO;
  23. using System.Text;
  24. using System.Diagnostics;
  25. using System.Reflection;
  26. namespace DICK.Core.JobManagement
  27. {
  28. /// <summary>
  29. /// This job type is used when producing export files with a template engine.
  30. /// Implements the IScriptContainer interface that is used as a base for all job types.
  31. /// </summary>
  32. public class ExportJob : IDotNetJob
  33. {
  34. #region Fields
  35. public string SelectionQuery { get; set; }
  36. public string SelectionQueryType { get; set; }
  37. public DICKAssembly MyAssembly
  38. {
  39. get;
  40. set;
  41. }
  42. #endregion
  43. //Convenience handle to the Engine singleton instance
  44. private Engine engine = Engine.GetInstance();
  45. /// <summary>
  46. /// Populates the job parameters from the given xml datastructure
  47. /// </summary>
  48. /// <param name="jobid">The identifier of the job</param>
  49. /// <param name="jobdata">The xml structure that contains the parameters of the job.</param>
  50. protected void InitializeJob(string jobID, XmlElement jobdata)
  51. {
  52. SelectionQuery = XmlTools.ParseString(jobdata, "SelectionQuery", "QUERY_NOT_FOUND");
  53. SelectionQueryType = XmlTools.ParseString(jobdata, "SelectionQueryType", "ADO");
  54. foreach (XmlElement target in jobdata.SelectNodes("ExportTarget"))
  55. {
  56. ExportTarget et = TargetFactory.GetTarget(target);
  57. this.Targets.Add(et);
  58. }
  59. if (dassembly != null)
  60. {
  61. initialized = true;
  62. }
  63. else
  64. {
  65. engine.LogDebug("ExportJob.Initialize: DASSEMBLY IS NULL!! Jobid:" + jobID + " jobdata:" + jobdata != null ? jobdata.ToString() : "NULL!");
  66. }
  67. }
  68. /// <summary>
  69. /// Overrides the standard ToString() method. Used for producing output about system configuration information.
  70. /// </summary>
  71. /// <returns></returns>
  72. public string ToString()
  73. {
  74. DateTime dt;
  75. StringBuilder sb = new StringBuilder();
  76. sb.AppendLine("Job ID:" + JobID);
  77. sb.AppendLine("Job Description:" + JobDescription);
  78. sb.AppendLine("B1 Required:" + B1Required);
  79. sb.AppendLine("Script:" + ScriptPath);
  80. sb.AppendLine("Class Name:" + ClassName);
  81. sb.AppendLine("Method:" + Method);
  82. sb.AppendLine("Language:" + ScriptLanguage);
  83. sb.AppendLine("Selection Query:" + SelectionQuery);
  84. sb.AppendLine("Selection Query Type:" + SelectionQueryType);
  85. foreach (ExportTarget target in Targets)
  86. {
  87. sb.AppendLine("EXPORT TARGET DEFINITION");
  88. sb.AppendLine(target.ToString());
  89. }
  90. //this.Targets
  91. return sb.ToString();
  92. }
  93. public List<ExportTarget> Targets = new List<ExportTarget>();
  94. /// <summary>
  95. /// Empty constructor for enabling runtime building of new ExportJob instances without a parameter data structure.
  96. /// </summary>
  97. protected ExportJob() { }
  98. /// <summary>
  99. /// The constructor for building an ExportJob from a parameter data structure.
  100. /// </summary>
  101. /// <param name="jobid">The identifier of the job</param>
  102. /// <param name="expdata">The parameter data for the job</param>
  103. public ExportJob(string jobid, XmlElement expdata)
  104. : base(jobid, expdata)
  105. {
  106. InitializeJob(jobid, expdata);
  107. }
  108. //**** EXPORT ****//
  109. //The method called from command line version of DICK
  110. public MessageToken DoExport()
  111. {
  112. List<string> exportkeys = new List<string>();
  113. PopulateExportKeyList(exportkeys);
  114. MessageToken result = DoExportByKeys(exportkeys);
  115. return result;
  116. }
  117. /// <summary>
  118. /// Used to export a number of objects based on the given list of keys (for instance itemcode for items and cardcode for business partners).
  119. /// </summary>
  120. /// <param name="keys">The list of identifier keys</param>
  121. /// <returns>MessageToken</returns>
  122. public MessageToken DoExportByKeys(List<string> keys)
  123. {
  124. MessageToken result = new MessageToken();
  125. int successcounter = 0;
  126. int failurecounter = 0;
  127. if (!initialized)
  128. {
  129. result.AddTextToBody("Job " + jobID + " not properly initialized!");
  130. result.OK = false;
  131. return result;
  132. }
  133. else
  134. {
  135. DICKContext initcontext = GetBatchInitContext();
  136. try
  137. {
  138. InitializeTargets(initcontext);
  139. engine.LogDebug("Looping through keys...");
  140. foreach (string key in keys)
  141. {
  142. ProcessTargetBody(ref successcounter, ref failurecounter, initcontext, key);
  143. }
  144. FinalizeTargets(result, initcontext);
  145. }
  146. catch (System.Exception ex)
  147. {
  148. result.OK = false;
  149. result.AddTextToBody("Exception during export: \r" + ex.Message);
  150. engine.LogError("Exception during export: \r" + ex.Message);
  151. failurecounter++;
  152. }
  153. finally
  154. {
  155. Targets.Clear();
  156. Targets = null;
  157. }
  158. }
  159. result.AddTextToBody("Documents processed successfully:" + successcounter);
  160. result.AddTextToBody("Documents with problems:" + failurecounter);
  161. return result;
  162. }
  163. private void FinalizeTargets(MessageToken result, DICKContext initcontext)
  164. {
  165. foreach (ExportTarget target in Targets)
  166. {
  167. if (target.IsBatch)
  168. {
  169. target.Finalize(initcontext);
  170. //For batch preview targets, we do the concatenation of results here
  171. if (target is PreviewTarget)
  172. {
  173. PreviewTarget pt = (PreviewTarget)target;
  174. result.AddPayload(pt.PayLoad.ToString());
  175. }
  176. }
  177. }
  178. }
  179. private void ProcessTargetBody(ref int successcounter, ref int failurecounter, DICKContext initcontext, string key)
  180. {
  181. //INSTANTIATE OBJECT FROM SCRIPT
  182. if (dassembly == null)
  183. {
  184. engine.LogError("EXPORTJOB.EXPORTBYKEYS: DASSEMBLY IS NULL!!");
  185. throw new DICK.Core.DICKException();
  186. }
  187. object o = dassembly.InvokeInstanceMethod(className, "GetContext", new object[] { key });
  188. if (o != null)
  189. {
  190. DICKContext context = (DICKContext)o;
  191. //We set a link to the batch context within each individual context object.
  192. context.ParentContext = initcontext;
  193. //PASS THE RETRIEVED CONTEXT OBJECT TO EACH TARGET
  194. foreach (ExportTarget target in Targets)
  195. {
  196. //IF JOB IS BATCH AND INITCONTEXT IS NOT INITIALIZED ==> BYPASS
  197. if (!(initcontext == null && target.IsBatch))
  198. {
  199. target.Process(context);
  200. }
  201. else
  202. {
  203. engine.LogError("ExportJob.ExportByKeys: BATCH NOT INITIATED ==> BYPASSING MERGE OF INDIVIDUAL TARGETS.");
  204. }
  205. }
  206. successcounter++;
  207. }
  208. else
  209. {
  210. engine.LogError("EXPORTJOB.EXPORTBYKEYS: Context object is null! ");
  211. failurecounter++;
  212. }
  213. }
  214. private void InitializeTargets(DICKContext initcontext)
  215. {
  216. foreach (ExportTarget target in Targets)
  217. {
  218. if (target.IsBatch)
  219. {
  220. target.Initialize(initcontext);
  221. }
  222. }
  223. engine.LogDebug("All targets initialized properly for exportjob " + jobID);
  224. }
  225. public DICKContext GetBatchInitContext()
  226. {
  227. engine.LogDebug("ExportJob calling GetBatchInitContext from script...");
  228. if (dassembly != null)
  229. {
  230. DICKContext c = dassembly.InvokeStaticMethod(className, "GetBatchInitContext", new object[] { }) as DICKContext;
  231. return c;
  232. }
  233. else
  234. {
  235. engine.LogError("DASSEMBLY IS NULL WHILE CALLING GETBATCHINITCONTEXT FROM EXPORTJOB !!");
  236. throw new DICKException();
  237. }
  238. }
  239. /// <summary>
  240. /// Overridden ín B1ExportJob so that the data is retrieved from B1 instead of ADO.NET but the rest of the code is unchanged.
  241. /// </summary>
  242. /// <param name="exportkeys"></param>
  243. protected virtual void PopulateExportKeyList(List<string> exportkeys)
  244. {
  245. DataTable dt = QueryToolsADO.QueryToDataTable(this.SelectionQuery);
  246. foreach (DataRow row in dt.Rows)
  247. {
  248. exportkeys.Add(row[0].ToString());
  249. }
  250. }
  251. }
  252. }