private static IEnumerable <ImportPostalInformationFromBPost> GenerateUpdateCommands( Dictionary <string, List <BPostData> > newByPostalCode, Dictionary <string, List <BPostData> > oldByPostalCode, CommandFactory commandFactory, BPostTimestamp timestamp) { // DETERMINE UPDATED var commands = new List <ImportPostalInformationFromBPost>(); var updateKeys = newByPostalCode.Keys.Intersect(oldByPostalCode.Keys); foreach (var updateKey in updateKeys) { var old = oldByPostalCode[updateKey]; var oldRoot = old.GetRootRecord(); var oldPostalNames = old.Select(x => x.PostalName).ToList(); var @new = newByPostalCode[updateKey]; var newRoot = @new.GetRootRecord(); var newPostalNames = @new.Select(x => x.PostalName).ToList(); if (oldRoot.IsSubMunicipality != newRoot.IsSubMunicipality || !string.Equals(oldRoot.Province, newRoot.Province, StringComparison.OrdinalIgnoreCase) || oldPostalNames.Any(x => !newPostalNames.Contains(x)) || newPostalNames.Any(x => !oldPostalNames.Contains(x))) { commands.Add(commandFactory.Create(@new, timestamp, Modification.Update)); } } Console.WriteLine($"Generated {commands.Count} update commands"); return(commands); }
private static IEnumerable <ImportPostalInformationFromBPost> GenerateDeleteCommands( Dictionary <string, List <BPostData> > oldByPostalCode, Dictionary <string, List <BPostData> > newByPostalCode, CommandFactory commandFactory, BPostTimestamp timestamp) { // DETERMINE DELETED var deletedKeys = oldByPostalCode.Keys.Except(newByPostalCode.Keys); var commands = deletedKeys.Select(key => commandFactory.Create(oldByPostalCode[key], timestamp, Modification.Delete)).ToList(); Console.WriteLine($"Generated {commands.Count} delete commands"); return(commands); }
private static IEnumerable <ImportPostalInformationFromBPost> GenerateNewCommands( Dictionary <string, List <BPostData> > newByPostalCode, Dictionary <string, List <BPostData> > oldByPostalCode, CommandFactory commandFactory, BPostTimestamp timestamp) { // DETERMINE NEW var newKeys = newByPostalCode.Keys.Except(oldByPostalCode.Keys); var commands = newKeys.Select(newKey => commandFactory.Create(newByPostalCode[newKey], timestamp, Modification.Insert)).ToList(); Console.WriteLine($"Generated {commands.Count} insert commands"); return(commands); }
private static List <ImportPostalInformationFromBPost> GenerateCommands( Dictionary <string, List <BPostData> > newByPostalCode, Dictionary <string, List <BPostData> > oldByPostalCode, CommandFactory commandFactory, BPostTimestamp timestamp) { var commands = new List <ImportPostalInformationFromBPost>(); commands.AddRange(GenerateNewCommands(newByPostalCode, oldByPostalCode, commandFactory, timestamp)); commands.AddRange(GenerateDeleteCommands(oldByPostalCode, newByPostalCode, commandFactory, timestamp)); commands.AddRange(GenerateUpdateCommands(newByPostalCode, oldByPostalCode, commandFactory, timestamp)); Console.WriteLine($"Generated {commands.Count} commands"); return(commands); }
public ImportPostalInformationFromBPost( PostalCode postalCode, List <PostalName> postalNames, bool?isSubMunicipality, Province province, BPostTimestamp timestamp, Modification modification) { PostalCode = postalCode; PostalNames = postalNames; IsSubMunicipality = isSubMunicipality; Province = province; Timestamp = timestamp; Modification = modification; }
public static void Main(string[] args) { var configureForPostalRegistry = JsonSerializerSettingsProvider.CreateSerializerSettings().ConfigureForPostalRegistry(); JsonConvert.DefaultSettings = () => configureForPostalRegistry; _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) .AddJsonFile($"appsettings.{Environment.MachineName.ToLowerInvariant()}.json", optional: true, reloadOnChange: false) .Build(); _importPath = string.IsNullOrEmpty(_configuration[ImportedPathConfigKey]) ? AppDomain.CurrentDomain.BaseDirectory + "\\imported" : _configuration[ImportedPathConfigKey]; Directory.CreateDirectory(_importPath); var importInstant = Instant.FromDateTimeOffset(DateTimeOffset.Now); var timestamp = new BPostTimestamp(importInstant); var dataToImport = DownloadPostalCodes(); var previouslyImportedData = GetPreviouslyImportedData(); var commandFactory = new CommandFactory(GetPostalNamesWithLanguage()); var oldByPostalCode = previouslyImportedData.GroupBy(x => x.PostalCode).ToDictionary(x => x.Key, x => x.ToList()); var newByPostalCode = dataToImport.GroupBy(x => x.PostalCode).ToDictionary(x => x.Key, x => x.ToList()); ImportCommands(GenerateCommands(newByPostalCode, oldByPostalCode, commandFactory, timestamp)); Console.WriteLine("Saving imported html to csv"); CsvWriterHelper.Export($"{_importPath}\\{importInstant.InUtc().ToString(FileDateFormat, CultureInfo.InvariantCulture)}.csv", dataToImport); Console.WriteLine("Finished"); Console.ReadKey(); }