public static Tuple <List <ResultRow>, List <string> > MapSourceRowsToTargetRows( List <string> listVersions, Dictionary <string, Dictionary <DateTime, string> > versionMapping, List <string> versionColumnsPresent, List <SourceRow> sourceRows) { // Convert source rows to target rows Dictionary <uint, List <ResultRow> > resultRowsByCustomerId = new Dictionary <uint, List <ResultRow> >(sourceRows.Count); foreach (SourceRow sourceRow in sourceRows) { List <ResultRow> resultRowsPerCustomer; if (!resultRowsByCustomerId.TryGetValue(sourceRow.CustomerNumber, out resultRowsPerCustomer)) { resultRowsPerCustomer = new List <ResultRow>(1); resultRowsByCustomerId.Add(sourceRow.CustomerNumber, resultRowsPerCustomer); } ResultRow resultRow = new ResultRow { CustomerNumber = sourceRow.CustomerNumber, PerformanceFullDate = sourceRow.PerformanceDate, PerformanceDate = sourceRow.PerformanceDate.ToString("MM/dd") }; string version = CsvParsingHelper.DetermineVersion( sourceRow.CustomerNumber, !string.IsNullOrEmpty(sourceRow.List1), !string.IsNullOrEmpty(sourceRow.List2), !string.IsNullOrEmpty(sourceRow.List3), !string.IsNullOrEmpty(sourceRow.List4), !string.IsNullOrEmpty(sourceRow.List5), listVersions); resultRow.Version = version; if (versionMapping != null && versionMapping.TryGetValue(version, out Dictionary <DateTime, string> dateMapping) && dateMapping.TryGetValue(resultRow.PerformanceFullDate, out string mappedValue)) { resultRow[version] = mappedValue; } resultRow.YearsSubscribed = sourceRow.YearsSubscribed; resultRow.LetterSalutation = sourceRow.LetterSalutation; resultRow.FullName = CsvParsingHelper.ConstructFullName(sourceRow.FirstName, sourceRow.LastName); CsvParsingHelper.DetermineLocation(sourceRow, resultRow); resultRowsPerCustomer.Add(resultRow); } List <ResultRow> resultRows = new List <ResultRow>(resultRowsByCustomerId.Count); foreach (List <ResultRow> resultRowsPerCustomer in resultRowsByCustomerId.Values) { resultRowsPerCustomer.Sort(new ResultRowImportanceComparer()); resultRows.Add(resultRowsPerCustomer[0]); } resultRows.Sort(new ResultRowLocationComparer()); // Output target rows into a new CSV List <string> targetRows; if (versionColumnsPresent != null) { targetRows = new List <string>(ResultRow.BaseRows.Count + versionColumnsPresent.Count); targetRows.AddRange(ResultRow.BaseRows); targetRows.AddRange(versionColumnsPresent); } else { targetRows = ResultRow.BaseRows; } return(new Tuple <List <ResultRow>, List <string> >(resultRows, targetRows)); }
public static async Task Main(string[] args) { if (args == null || args.Length < 1) { Program.PrintHelp(error: "Missing required arguments"); return; } if (args.Length > 4) { Program.PrintHelp(error: "Too many arguments"); return; } string sourceFile = null; string targetFile = null; string listVersionNameFile = null; string versionMappingFile = null; for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg.StartsWith("/?")) { Program.PrintHelp(); return; } if (arg.StartsWith("/s:")) { sourceFile = arg.Substring(3); } else if (arg.StartsWith("/t:")) { targetFile = arg.Substring(3); } else if (arg.StartsWith("/v:")) { listVersionNameFile = arg.Substring(3); } else if (arg.StartsWith("/m:")) { versionMappingFile = arg.Substring(3); } else { Program.PrintHelp($"Unknown argument {arg}"); return; } } if (string.IsNullOrEmpty(sourceFile) || !File.Exists(sourceFile)) { Program.PrintHelp($"Source file {sourceFile} doesn't exist"); return; } if (targetFile == null) { targetFile = Path.Combine(Path.GetDirectoryName(sourceFile), Path.GetFileNameWithoutExtension(sourceFile) + ".sorted.csv"); } if (File.Exists(targetFile)) { Program.PrintHelp($"Target file {targetFile} already exists"); return; } if (!string.IsNullOrEmpty(listVersionNameFile) && !File.Exists(listVersionNameFile)) { Program.PrintHelp($"List version name file {listVersionNameFile} doesn't exist"); return; } if (string.IsNullOrEmpty(versionMappingFile) || !File.Exists(versionMappingFile)) { Program.PrintHelp($"Version-mapping file {versionMappingFile} doesn't exist"); return; } try { // Parse list version-name file: List <string> listVersions = CsvParsingHelper.DefaultListVersions; if (!string.IsNullOrEmpty(listVersionNameFile)) { listVersions = await CsvParsingHelper.GetListVersions(listVersionNameFile); } // Parse version mapping file: Dictionary <string, Dictionary <DateTime, string> > versionMapping = null; List <string> versionColumnsPresent = null; if (!string.IsNullOrEmpty(versionMappingFile)) { (versionMapping, versionColumnsPresent) = await CsvParsingHelper.GetVersionMappings(versionMappingFile, listVersions); } // Parse source rows from the CSV List <SourceRow> sourceRows = await CsvParsingHelper.GetSourceRows(sourceFile); (List <ResultRow> rows, List <string> headers) = CsvParsingHelper.MapSourceRowsToTargetRows(listVersions, versionMapping, versionColumnsPresent, sourceRows); await CsvParsingHelper.WriteResultRows(targetFile, rows, headers); } catch (SeatcardSorterException ex) { Program.PrintHelp(ex.ToString()); } }
private static void DetermineLocation(SourceRow sourceRow, ResultRow resultRow) { if (string.IsNullOrEmpty(sourceRow.Section) || string.IsNullOrEmpty(sourceRow.Location)) { resultRow.Level = CsvParsingHelper.ErrorString; resultRow.Location = CsvParsingHelper.ErrorString + sourceRow.Location; return; } Match sectionMatch = CsvParsingHelper.sourceSectionParser.Match(sourceRow.Section); if (!sectionMatch.Success) { throw new SeatcardSorterException($"Couldn't parse section for customer_no {sourceRow.CustomerNumber}"); } MatchCollection locationMatches = CsvParsingHelper.sourceLocationParser.Matches(sourceRow.Location); if (locationMatches.Count < 1) { throw new SeatcardSorterException($"Couldn't parse location for customer_no {sourceRow.CustomerNumber}"); } string sectionLevel = sectionMatch.Groups[1].Value; resultRow.Section = sectionMatch.Groups[3].Value; if (locationMatches.Count > 2) { resultRow.Level = CsvParsingHelper.ErrorString; resultRow.Location = CsvParsingHelper.ErrorString + sourceRow.Location; return; } if (locationMatches.Count == 1) { Match locationMatch = locationMatches[0]; string locationLevel = locationMatch.Groups[2].Value; string row = locationMatch.Groups[4].Value; string seat = locationMatch.Groups[5].Value; if (!uint.TryParse(seat, out uint seatNumber)) { throw new SeatcardSorterException($"Couldn't parse location seat number for customer_no {sourceRow.CustomerNumber}"); } if (!CsvParsingHelper.LevelsMatch(locationLevel, sectionLevel)) { resultRow.Level = CsvParsingHelper.ErrorString; resultRow.Location = CsvParsingHelper.ErrorString + sourceRow.Location; } else { resultRow.Level = locationLevel; resultRow.Row = row; resultRow.SeatNumber = seatNumber; resultRow.Location = CsvParsingHelper.GenerateLocation(resultRow.Level, resultRow.Section, resultRow.Row, resultRow.SeatNumber); } } else // if (locationMatches.Count == 2) { Match firstLocationMatch = locationMatches[0]; Match secondLocationMatch = locationMatches[1]; string firstLocationLevel = firstLocationMatch.Groups[2].Value; string secondLocationLevel = secondLocationMatch.Groups[2].Value; char firstAisle = firstLocationMatch.Groups[3].Value[0]; char secondAisle = secondLocationMatch.Groups[3].Value[0]; int aisleDifference = firstAisle - secondAisle; if (!string.Equals(firstLocationLevel, secondLocationLevel) || !CsvParsingHelper.LevelsMatch(firstLocationLevel, sectionLevel) || aisleDifference < -1 || aisleDifference > 1) { resultRow.Level = CsvParsingHelper.ErrorString; resultRow.Location = CsvParsingHelper.ErrorString + sourceRow.Location; } else { string row = firstLocationMatch.Groups[4].Value; string seat = firstLocationMatch.Groups[5].Value; uint seatNumber; if (!uint.TryParse(seat, out seatNumber)) { throw new SeatcardSorterException($"Couldn't parse location seat number for customer_no {sourceRow.CustomerNumber}"); } resultRow.Level = firstLocationLevel; resultRow.Row = row; resultRow.SeatNumber = seatNumber; resultRow.Location = CsvParsingHelper.GenerateLocation(resultRow.Level, resultRow.Section, resultRow.Row, resultRow.SeatNumber); } } }