public AdditionDeletionFileSummary ProcessFile(string country, HttpPostedFile file) { var returned = new AdditionDeletionFileSummary(); var fileData = new byte[file.ContentLength]; file.InputStream.Read(fileData, 0, file.ContentLength); var rawStringAdditionDeletionDataHolders = new List <RawAdditionDeletionDataHolder>(); using (var ms = new MemoryStream(fileData)) { var sr = new StreamReader(ms); var headerLine = sr.ReadLine(); if (string.IsNullOrEmpty(headerLine)) { returned.FilePaseMessage = "Invalid File"; return(returned); } var columns = headerLine.Split(','); if (columns[0] != "Location") { returned.FilePaseMessage = "Invalid Header"; return(returned); } while (!sr.EndOfStream) { var dataRow = sr.ReadLine(); if (dataRow == null) { continue; } var splitDataRow = dataRow.Split(','); if (splitDataRow.Count() != 6) { continue; } var rawData = new RawAdditionDeletionDataHolder { Location = splitDataRow[0].Trim(), CarGroup = splitDataRow[1].Trim(), Day = splitDataRow[2].Trim(), Time = splitDataRow[3].Trim(), Number = splitDataRow[4].Trim(), AddDel = splitDataRow[5].Trim() }; rawStringAdditionDeletionDataHolders.Add(rawData); } } ParseRawData(rawStringAdditionDeletionDataHolders, returned, country); return(returned); }
private void ParseRawData(IEnumerable <RawAdditionDeletionDataHolder> rawData, AdditionDeletionFileSummary adfs, string country) { adfs.Additions = new List <ResAddition>(); adfs.Deletions = new List <ResDeletion>(); Dictionary <string, int> validLocations; Dictionary <string, int> validCarGroups; using (var dataAccess = new AdditionDeletionDataAccess()) { validLocations = dataAccess.GetValidLocationsForCountry(country); validCarGroups = dataAccess.GetValidCarGroups(country); } foreach (var rd in rawData) { if (!validLocations.ContainsKey(rd.Location)) { adfs.RowsSkipped++; continue; } //var groupId = dataAccess.GetCarGroupId(location.country, rd.CarGroup); if (!validCarGroups.ContainsKey(rd.CarGroup)) { adfs.RowsSkipped++; continue; } DateTime repDate; var parseSucceeded = DateTime.TryParse(string.Format("{0} {1}", rd.Day, rd.Time), out repDate); if (!parseSucceeded) { adfs.RowsSkipped++; continue; } int number; parseSucceeded = int.TryParse(rd.Number, out number); if (!parseSucceeded) { adfs.RowsSkipped++; continue; } if (rd.AddDel == AdditionIdentifier) { var rAdd = new ResAddition { LocId = validLocations[rd.Location], CarGrpId = validCarGroups[rd.CarGroup], RepDate = repDate, Value = number }; adfs.Additions.Add(rAdd); adfs.ValidRows++; } else if (rd.AddDel == DeletionIdentifier) { var rDel = new ResDeletion { LocId = validLocations[rd.Location], CarGrpId = validCarGroups[rd.CarGroup], RepDate = repDate, Value = number }; adfs.Deletions.Add(rDel); adfs.ValidRows++; } else { adfs.RowsSkipped++; } } }