Exemplo n.º 1
0
        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();

            Console.WriteLine("Reading postal info from crab");

            var connectionString = _configuration[CrabConnectionStringConfigKey];
            var commands         = new List <ImportPostalInformationFromCrab>();

            using (var conn = new SqlConnection(connectionString))
            {
                conn.Open();
                var fromDateTimeOffset = Instant.FromDateTimeOffset(DateTimeOffset.Now);
                using (var cmd = new SqlCommand("SELECT [subKantonId], [postkantonCode], [subkantonCode], [BeginDatum], [nisGemeenteCode], [gemeenteNaam] FROM odb.vwSubKantonCodes", conn))
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            commands.Add(
                                new ImportPostalInformationFromCrab(
                                    new PostalCode(reader.GetString(1)),
                                    new CrabSubCantonId(reader.GetInt32(0)),
                                    new CrabSubCantonCode(reader.GetString(2)),
                                    new NisCode(reader.GetString(4)),
                                    new CrabMunicipalityName(reader.GetString(5), null),
                                    new CrabLifetime(reader.GetDateTime(3).ToCrabLocalDateTime(), null),
                                    new CrabTimestamp(fromDateTimeOffset),
                                    new CrabOperator("VLM\\PostalRegistry.EventGenerator.CrabPostinfo"),
                                    CrabModification.Correction,
                                    CrabOrganisation.DePost));
                        }
                    }
                }
            }

            Console.WriteLine($"Generated {commands.Count} commands");
            Console.WriteLine("Press key to start import");
            Console.ReadKey();

            foreach (var importPostalInfoFromCrab in commands)
            {
                SendCrabImportCommand(importPostalInfoFromCrab.PostalCode, importPostalInfoFromCrab, importPostalInfoFromCrab.CreateCommandId());
            }

            Console.WriteLine("Finished");
        }
        /// <summary>
        /// Sets up and adds additional converters for PostalRegister to the JsonSerializerSettings
        /// </summary>
        /// <param name="source"></param>
        /// <returns>the updated JsonSerializerSettings</returns>
        public static JsonSerializerSettings ConfigureForPostalRegistry(this JsonSerializerSettings source)
        {
            var jsonSerializerSettings = JsonSerializerSettingsProvider.CreateSerializerSettings();

            source.ContractResolver = jsonSerializerSettings.ContractResolver;

            if (source.ContractResolver is DefaultContractResolver resolver)
            {
                resolver.NamingStrategy.ProcessDictionaryKeys = true;
            }

            source.DateFormatHandling = DateFormatHandling.IsoDateFormat;
            source.Converters.Add(new StringEnumConverter {
                CamelCaseText = true
            });

            //source.DateFormatString = "yyyy-MM-dd";
            //source.Converters.Add(new TrimStringConverter());
            //source.Converters.Add(new GuidConverter());

            return(source
                   .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
                   .WithIsoIntervalConverter());
        }