Esempio n. 1
0
#pragma warning disable CA1801  // Remove unused parameter
#pragma warning disable IDE0060 // Remove unused parameter
        /// <summary>
        /// Get data from DAWA DAGI WebApi and bootload database.
        /// </summary>
        /// <param name="transactionInfo">Dummy - not used.</param>
        /// <returns>Count of all rows.</returns>
        /// <typeparam name="T">Any DAGI entity class in the JOInformatik.DawaReplication.DataAccess namespace.</typeparam>
        public static int Dagi <T>(DawaProcessInfo transactionInfo, DateTime starttime)
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore CA1801  // Remove unused parameter
            where T : DAGIBase
        {
            var    methodName = LoggingUtils.GetMethodName();
            string entityName = typeof(T).Name;

            Logger?.Info($"{methodName}: Processing entity {entityName}");

            var entityNameApi   = entityName.Substring(6).ToLowerInvariant(); // Get rid of starting "DAGI__"
            int counter         = 0;
            var itemList        = new List <T>();
            var syncDeletesTime = DateTime.Now;
            var stopwatch       = System.Diagnostics.Stopwatch.StartNew();

            using (var httpClient = new HttpClient())
            {
                // Always use "&noformat" for increased performance:
                var stream = httpClient.GetStreamAsync($"{DawaApiUri}{entityNameApi}?format=geojson&srid={(int)KoordinatsystemSrid.ETRS89}&noformat").Result;
                stream.ReadTimeout = ReadTimeoutInSeconds * 1000;
                var fileStream = LocalDataHelper.CreateTempFile(entityName);
                try
                {
                    // Remove geojson header and save to file. Read from same file and stop when reaching end of array(list of objects).
                    for (int x = 0; x < DKStedDataStartPos; x++)
                    {
                        stream.ReadByte();
                    }

                    stream.CopyTo(fileStream);
                    stream.Close();
                    fileStream.Close();
                }
                catch
                {
                    // If something goes wrong, make sure to close filestream, so we can try to write to the file again.
                    stream.Close();
                    fileStream.Close();
                    throw;
                }
            }

            var fileLocation = LocalDataHelper.RenameTempFile(entityName);

            Console.Write("Done saving to file.");

            using (var reader = new JsonTextReader(new StreamReader(fileLocation)))
            {
                DBContext.Database.BeginTransaction();
                try
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.EndArray)
                        {
                            break;
                        }

                        if (reader.TokenType == JsonToken.StartObject)
                        {
                            var item             = JObject.Load(reader);
                            T   rootItemAsObject = JsonConvert.DeserializeObject <T>(item.ToString(Formatting.None), UserJsonSerializerSettings);
                            T   itemAsObject     = JsonConvert.DeserializeObject <T>(item["properties"].ToString(Formatting.None), UserJsonSerializerSettings);

                            itemAsObject.SetEntityFields(item);
                            itemAsObject.Geometry = rootItemAsObject.Geometry;
                            itemList.Add(itemAsObject);

                            counter += 1;

                            if (itemList.Count % DKStedBulkSize == 0)
                            {
                                DBContext.BulkInsertOrUpdate(itemList);
                                itemList.Clear();
                            }
                        }
                    }

                    Logger?.Info($"{methodName}: Total fetched: {counter}");

                    DBContext.BulkInsertOrUpdate(itemList);
                    DagiStedHelper.DeleteOldRows(DBContext, entityName, syncDeletesTime);

                    var finishtime = EntityStateHelper.SetEntityStateDone(DBContext, entityName, true, -1, counter);
                    EntityStateHelper.SetEntityStateHistoryDone(DBContext, entityName, true, starttime, finishtime, -1, itemList.Count, 0);
                    if (UseMSApplicationInsights)
                    {
                        TelemetryHelper.AddTelemetryForEntity(EntityProcessMode.Dagi, entityName, stopwatch);
                    }

                    DBContext.Database.CommitTransaction();
                }
                catch (Exception ex)
                {
                    if (DBContext.Database.CurrentTransaction != null)
                    {
                        DBContext.Database.RollbackTransaction();
                    }

                    var exception  = ex.InnerException ?? ex;
                    var finishtime = EntityStateHelper.SetEntityStateDone(DBContext, entityName, false, -1, counter, exception.Message);
                    EntityStateHelper.SetEntityStateHistoryDone(DBContext, entityName, false, starttime, finishtime, -1, null, null, exception.Message);
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    throw;
                }
            }

            // Delete temporary file.
            LocalDataHelper.RemoveTempFile(fileLocation);

            return(counter);
        }