PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/AppLib/CE.InsightsBulk.LoadData/ActionItemFile.cs

https://bitbucket.org/jeffmccommas/acex
C# | 236 lines | 215 code | 9 blank | 12 comment | 1 complexity | fff3dce52047195bc675aab609f9ef52 MD5 | raw file
  1. using CE.InsightsBulk.Infrastructure;
  2. using CE.InsightsBulk.Infrastructure.DataAccess;
  3. using CE.InsightsBulk.Infrastructure.Interfaces;
  4. using CE.InsightsBulk.Infrastructure.SharedClasses;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13. using System.Xml.Schema;
  14. using Aclara.UFx.StatusManagement;
  15. using CE.InsightsBulk.Model;
  16. namespace CE.InsightsBulk.LoadData
  17. {
  18. public class ActionItemFile : IBulkFile
  19. {
  20. private readonly int _clientId;
  21. private readonly IConfig _config;
  22. private readonly ILogger _log;
  23. private const string PremiseNode_Customerid = "CustomerId";
  24. private const string PremiseNode_Accountid = "AccountId";
  25. private const string PremiseNode_PremiseId = "PremiseId";
  26. private const string ActionItemNode_ActionKey = "ActionKey";
  27. private const string ActionItemNode_SubActionKey = "SubActionKey";
  28. private const string ActionItemNode_Status = "Status";
  29. private const string ActionItemNode_StatusDate = "StatusDate";
  30. private const string ActionItemNode_Source = "Source";
  31. private const string ActionKeyData_ActionData = "ActionData";
  32. private const string ActionKeyData_ActionValue = "ActionValue";
  33. /// <summary>
  34. /// Constructor
  35. /// </summary>
  36. /// <param name="o"></param>
  37. /// <param name="config"></param>
  38. /// <param name="log"></param>
  39. public ActionItemFile(Options o, IConfig config, ILogger log)
  40. {
  41. _clientId = Convert.ToInt32(o.ClientId);
  42. _config = config;
  43. _log = log;
  44. }
  45. /// <summary>
  46. /// Process the Action Item file
  47. /// </summary>
  48. /// <param name="theFile">The file to be processed</param>
  49. /// <param name="statusList">The Status</param>
  50. /// <returns>The tracking id</returns>
  51. public string ProcessFile(FileInfo theFile, out StatusList statusList)
  52. {
  53. var trackingid = "";
  54. statusList = new StatusList();
  55. var nodeCounter = 0;
  56. var totalNodeCounter = 0;
  57. var processedNodeCounter = 0;
  58. var attributeTable = new List<ActionItem>();
  59. var da = new BulkDataAccess();
  60. var daMeta = new MetaDataAccess();
  61. try
  62. {
  63. statusList.Add(new Status(DateTime.Now.ToString(CultureInfo.InvariantCulture) + " Processing File " + theFile.FullName,
  64. StatusTypes.StatusSeverity.Informational));
  65. _log.Info(trackingid, $"Processing File{theFile.FullName} ");
  66. DateTime trackingIdDate;
  67. Helpers.SetTrackingData(theFile.Name, out trackingid, out trackingIdDate);
  68. var xsdFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" +
  69. _config.InputXSD;
  70. // This logic sets-up a custom iteration using linq and yield. Each element is traversed forward-only, greatly saving resources.
  71. var getCustomerElements = InputFileXMLStream.StreamData(theFile.FullName).Select(el => el);
  72. var schemas = new XmlSchemaSet();
  73. schemas.Add(Constants.FileNamespace, XmlReader.Create(new StreamReader(xsdFile)));
  74. foreach (var element in getCustomerElements)
  75. {
  76. //Validate the node
  77. var errors = false;
  78. var errStatusList = new StatusList();
  79. var doc = new XDocument(new XElement(Constants.RootNode));
  80. var xElement = doc.Element(Constants.RootNode);
  81. xElement?.Add(element);
  82. doc.Validate(schemas, (o, e) =>
  83. {
  84. errStatusList.Add(new Status(e.Message, StatusTypes.StatusSeverity.Error));
  85. string elementData = string.Concat(element.Elements());
  86. daMeta.LogErrorsByInputType(
  87. Helpers.ExtractElements(elementData, BulkLoadConstants.RunType.ActionItem, _clientId,
  88. trackingid), e.Message, BulkLoadConstants.RunType.ActionItem,
  89. _config.ImportSpecificLoggingOn);
  90. _log.ErrorException(trackingid,
  91. new InsightsBulkLoadException(
  92. $"Validation Error for file {theFile.FullName} data: {elementData}", e.Exception));
  93. errors = true;
  94. }, true);
  95. if (nodeCounter == 0)
  96. {
  97. //Create the datatable for storage
  98. attributeTable = new List<ActionItem>();
  99. }
  100. // Main Call
  101. if (!errors)
  102. {
  103. nodeCounter++;
  104. var xmlNode = element.GetXmlNode();
  105. ProcessNode(trackingid, trackingIdDate, xmlNode, attributeTable);
  106. }
  107. else
  108. {
  109. statusList.AddRange(errStatusList);
  110. errors = false;
  111. errStatusList.Clear();
  112. }
  113. totalNodeCounter++;
  114. if (attributeTable.Count >= _config.MaxBatchSize)
  115. {
  116. //WriteOutdata
  117. processedNodeCounter += nodeCounter;
  118. var results = da.WriteActionKeyData(attributeTable, ref statusList);
  119. nodeCounter = 0;
  120. if (results != attributeTable.Count)
  121. {
  122. statusList.Add(new Status("Not all data was inserted", StatusTypes.StatusSeverity.Error));
  123. }
  124. attributeTable.Clear();
  125. }
  126. }
  127. if (attributeTable.Count > 0)
  128. {
  129. processedNodeCounter += nodeCounter;
  130. var results = da.WriteActionKeyData(attributeTable, ref statusList);
  131. if (results != attributeTable.Count)
  132. {
  133. statusList.Add(new Status("Not all data was inserted", StatusTypes.StatusSeverity.Error));
  134. _log.ErrorException(trackingid, new InsightsBulkLoadException("Not all data was inserted"));
  135. }
  136. }
  137. statusList.Add(
  138. new Status(DateTime.Now.ToString(CultureInfo.InvariantCulture) + " Processed " + processedNodeCounter + " customers out of " +
  139. totalNodeCounter, StatusTypes.StatusSeverity.Informational));
  140. _log.Info(trackingid,$"Processed {processedNodeCounter} customers out of {totalNodeCounter}");
  141. }
  142. catch (Exception ex)
  143. {
  144. statusList.Add(new Status(ex, StatusTypes.StatusSeverity.Error));
  145. _log.ErrorException(trackingid, ex);
  146. }
  147. return trackingid;
  148. }
  149. /// <summary>
  150. /// Process the XML Node
  151. /// </summary>
  152. /// <param name="trackingid">The tracking Id</param>
  153. /// <param name="trackingdate">The tracking date</param>
  154. /// <param name="rootNode">The xml node</param>
  155. /// <param name="actionItemTable">The data table</param>
  156. private void ProcessNode(string trackingid, DateTime trackingdate, XmlNode rootNode,
  157. ICollection<ActionItem> actionItemTable)
  158. {
  159. //For some reason, customer is coming in as a child node, so just get all children
  160. var cNode = rootNode.FirstChild;
  161. if (!cNode.HasChildNodes)
  162. {
  163. return;
  164. }
  165. //Load the Premise Attribute Table
  166. foreach (XmlElement accountNode in cNode.ChildNodes)
  167. {
  168. if (!accountNode.HasChildNodes)
  169. {
  170. continue;
  171. }
  172. foreach (XmlElement premiseNode in accountNode.ChildNodes)
  173. {
  174. foreach (XmlElement actionNode in premiseNode.ChildNodes)
  175. {
  176. foreach (XmlElement actionDataNode in actionNode.ChildNodes)
  177. {
  178. var actionItem = new ActionItem
  179. {
  180. ClientId = _clientId,
  181. CustomerId = Helpers.GetText(cNode, PremiseNode_Customerid),
  182. AccountID = Helpers.GetText(accountNode, PremiseNode_Accountid),
  183. PremiseId = Helpers.GetText(premiseNode, PremiseNode_PremiseId),
  184. ActionKey = Helpers.GetText(actionNode, ActionItemNode_ActionKey),
  185. SubActionKey = Helpers.GetText(actionNode, ActionItemNode_SubActionKey),
  186. ActionData = Helpers.GetText(actionDataNode, ActionKeyData_ActionData),
  187. ActionDataValue = Helpers.GetText(actionDataNode, ActionKeyData_ActionValue),
  188. TrackingID = trackingid,
  189. TrackingDate = trackingdate,
  190. StatusId = Helpers.ConvertStatusToId(Helpers.GetText(actionNode, ActionItemNode_Status)),
  191. SourceId = Helpers.ConvertSourceToId(Helpers.GetText(actionNode, ActionItemNode_Source))
  192. };
  193. var strDateTime = Helpers.GetText(actionNode, ActionItemNode_StatusDate);
  194. actionItem.StatusDate = Convert.ToDateTime(strDateTime.Length == 0 ? "1900-01-01" : strDateTime);
  195. actionItemTable.Add(actionItem);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }