public ActionResult ImportStep3() { //Display Results of Import CDRImportStep3VM cdrPostImportResult = new CDRImportStep3VM(); cdrPostImportResult = (CDRImportStep3VM)TempData["CdrPostImportResult"]; ClientSubUnit clientSubUnit = new ClientSubUnit(); clientSubUnit = clientSubUnitRepository.GetClientSubUnit(cdrPostImportResult.ClientSubUnitGuid); cdrPostImportResult.ClientSubUnit = clientSubUnit; return(View(cdrPostImportResult)); }
public ActionResult ImportStep2(CDRImportStep1VM preImportCheckResultVM) { //PreImport Check Results (check has passed) CDRImportStep2VM preImportCheckResult = new CDRImportStep2VM(); preImportCheckResult = preImportCheckResultVM.CDRImportStep2VM; //Do the Import, return results CDRImportStep3VM cdrPostImportResult = new CDRImportStep3VM(); cdrPostImportResult = clientSubUnitCDRRepository.Import( preImportCheckResult.FileBytes, preImportCheckResultVM.ClientSubUnitGuid, preImportCheckResultVM.DisplayName, preImportCheckResultVM.RelatedToDisplayName ); cdrPostImportResult.ClientSubUnitGuid = preImportCheckResultVM.ClientSubUnitGuid; TempData["CdrPostImportResult"] = cdrPostImportResult; //Pass Results to Next Page return(RedirectToAction("ImportStep3")); }
public CDRImportStep3VM Import(byte[] FileBytes, string clientSubUnitGuid, string displayName, string relatedToDisplayName = "") { System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); string fileToText = fileToText = enc.GetString(FileBytes); /* * Regex to match on each line (5 items separated by commas, followed by a new line - last 2 items are optional) * CDR Value, SourceSystemCode, ClientAccountNumber, CreditCardID (optional), RelatedToValue (optional) */ CDRImportStep3VM cdrPostImportResult = new CDRImportStep3VM(); // Create the xml document container, this will be used to store the data after the Regex checks XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null); doc.AppendChild(dec); XmlElement root = doc.CreateElement("CSUCDRs"); doc.AppendChild(root); ClientSubUnit clientSubUnit = new ClientSubUnit(); ClientSubUnitClientAccountRepository clientSubUnitClientAccountRepository = new ClientSubUnitClientAccountRepository(); ClientSubUnitRepository clientSubUnitRepository = new ClientSubUnitRepository(); clientSubUnit = clientSubUnitRepository.GetClientSubUnit(clientSubUnitGuid); //Counters are above loop, every time we call proc,we have to sum values (old+new) int deletedItemCount = 0; int addedItemCount = 0; //declare step flag for proc int step = 0; //Split the CSV into lines string[] lines = fileToText.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); //Max size of chunk int MaxSizeOfChunk = 10; //Lines buffer string[] linesBuffer; //begin loop for divide into chunks for (int j = 0; j < lines.Length; j += MaxSizeOfChunk) { int i = 0; //Set MaxSizeOfChunk for last iteration if (lines.Length - j < MaxSizeOfChunk) { MaxSizeOfChunk = lines.Length - j; } //Copy from source (lines), array size(chunk), into buffer array linesBuffer = new string[MaxSizeOfChunk]; Array.Copy(lines, j, linesBuffer, 0, MaxSizeOfChunk); //loop through CSV lines of buffer foreach (string line in linesBuffer) { if (i != j) //ignore first line with titles-we have to avoid first line, position (0,0) { string[] cells = line.Split(','); //extract the data items from the file string fileCostCentre = cells[0]; //CDRValue (Cost Centre) string fileSourceSystemCode = cells[1]; //SourceSystemCode string fileClientAccountNumber = cells[2]; //CWT Account No (Air/Rail) //Credit Card No field is an optional field on the .csv file string fileCreditCardNumber = string.Empty; if (cells.Length >= 4) { fileCreditCardNumber = cells[3] ?? ""; } //The RelatedToValue field is an optional field on the .csv file. If there is no CDR Display Name value entered in the Validate CDR field, //then the Import should not look for a RelatedToValue column on the .csv file string fileRelatedToValue = string.Empty; if (cells.Length >= 5) { fileRelatedToValue = cells[4] ?? ""; } //Build the XML Element for a CDR XmlElement xmlCDRItem = doc.CreateElement("CSUCDR"); //Populate the XML Element for a CDR //This XMLDoc is only used if the File Passes Validation XmlElement xmlSourceSystemCode = doc.CreateElement("SourceSystemCode"); xmlSourceSystemCode.InnerText = fileSourceSystemCode; xmlCDRItem.AppendChild(xmlSourceSystemCode); XmlElement xmlClientAccountNumber = doc.CreateElement("ClientAccountNumber"); xmlClientAccountNumber.InnerText = fileClientAccountNumber; xmlCDRItem.AppendChild(xmlClientAccountNumber); XmlElement xmlCostCentre = doc.CreateElement("CostCentre"); xmlCostCentre.InnerText = fileCostCentre; xmlCDRItem.AppendChild(xmlCostCentre); //Check for numerical credit card if (!string.IsNullOrEmpty(fileCreditCardNumber)) { int creditCardId; if (!Int32.TryParse(fileCreditCardNumber, out creditCardId)) { fileCreditCardNumber = ""; } XmlElement xmlCreditCardId = doc.CreateElement("CreditCardId"); xmlCreditCardId.InnerText = !string.IsNullOrEmpty(fileCreditCardNumber) ? fileCreditCardNumber : ""; xmlCDRItem.AppendChild(xmlCreditCardId); } if (!string.IsNullOrEmpty(fileRelatedToValue) && !string.IsNullOrEmpty(relatedToDisplayName)) { XmlElement xmlRelatedToDisplayName = doc.CreateElement("RelatedToDisplayName"); xmlRelatedToDisplayName.InnerText = !string.IsNullOrEmpty(relatedToDisplayName) ? relatedToDisplayName : ""; xmlCDRItem.AppendChild(xmlRelatedToDisplayName); XmlElement xmlRelatedToValue = doc.CreateElement("RelatedToValue"); xmlRelatedToValue.InnerText = !string.IsNullOrEmpty(fileRelatedToValue) ? fileRelatedToValue : ""; xmlCDRItem.AppendChild(xmlRelatedToValue); } //Attach the XML Element for a CDR to the Document root.AppendChild(xmlCDRItem); } i++; } //DB Check string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0]; //Extra param added to proc, send 0 if first page, 1 others(if it is first page in that case we delete in proc) step = (j == 0 ? 0 : 1); var output = (from n in db.spDesktopDataAdmin_UpdateClientSubUnitClientDefinedReferences_v1( clientSubUnit.ClientSubUnitGuid, displayName, System.Xml.Linq.XElement.Parse(doc.OuterXml), adminUserGuid, step) select n).ToList(); foreach (spDesktopDataAdmin_UpdateClientSubUnitClientDefinedReferences_v1Result message in output) { deletedItemCount += message.DeletedCount; addedItemCount += message.AddedCount; } //empty xml nodes into root, ready for next iteration root.RemoveAll(); } //end loop for divide into chunks cdrPostImportResult.ClientSubUnitGuid = clientSubUnitGuid; cdrPostImportResult.DisplayName = displayName; cdrPostImportResult.AddedItemCount = addedItemCount; cdrPostImportResult.DeletedItemCount = deletedItemCount; return(cdrPostImportResult); }