// TODO: Change no internet connection handling

        /// <summary>
        /// Fetches and stores all data from a specific Eurostat dataset.
        /// </summary>
        /// <param name="table">Representation of the dataset. Has information needed to fetch.</param>
        /// <returns></returns>
        private async Task FetchAndStore(EuroStatTable table)
        {
            List <Nace> naces = await databaseStore.getAllNaces();

            int iterationCount = GetFetchIterationsCount(naces);

            for (int i = 0; i < iterationCount; i++)
            {
                string url = GetEuroStatURL(table, i, naces, StartYear, EndYear);
                try
                {
                    HttpResponseMessage response = await client.GetAsync(url);

                    if (response.IsSuccessStatusCode)
                    {
                        string jsonString = response.Content.ReadAsStringAsync().Result;
                        List <NaceRegionData> EurostatNRData = await JSONConverter.Convert(jsonString, table.attributeName);

                        await databaseStore.addNaceRegionData(EurostatNRData);
                    }
                    else
                    {
                        await handleStatusCodeNotSuccess((int)response.StatusCode, response, table, url);
                    }
                }
                catch (System.Net.Http.HttpRequestException e)
                {
                    _logger.LogWarning("No internet connection, check your network configuration and try again" + "\nError trace: " + e);
                    throw new Exception();
                }
            }
        }
Пример #2
0
        public async Task <String> PopulateDatabase()
        {
            List <Nace> savedNaces = await ds.getAllNaces();

            List <Region> savedRegions = await ds.getAllRegions();

            List <EuroStatTable> savedTables = await ds.getAllEuroStatTables();

            foreach (Nace nace in baseData.getAllNaces())
            {
                try
                {
                    if (!savedNaces.Contains(nace))
                    {
                        await ds.createNace(nace);
                    }
                } catch (Exception exception)
                {
                    _logger.LogError(exception.ToString());
                    return(exception.ToString());
                }
            }
            foreach (Region region in baseData.getAllRegions())
            {
                try
                {
                    if (!savedRegions.Contains(region))
                    {
                        await ds.createRegion(region);
                    }
                } catch (Exception exception)
                {
                    _logger.LogError(exception.ToString());
                    return(exception.ToString());
                }
            }
            foreach (EuroStatTable table in baseData.getAllEuroStatTables())
            {
                try
                {
                    if (!savedTables.Contains(table))
                    {
                        await ds.createEuroStatTable(table);
                    }
                } catch (Exception exception)
                {
                    _logger.LogError(exception.ToString());
                    return(exception.ToString());
                }
            }
            return("The base data was added successfully");
        }
        /// <summary>
        /// Converts a JSON-file on string format to a list of corresponding NaceRegionData objects.
        /// </summary>
        /// <param name="jsonString">A JSON object represented as a string</param>
        /// <param name="attributeName">The name of the datafield. Used to find and set the correct property in NaceRegionObjects</param>
        /// <returns></returns>
        public async Task <List <NaceRegionData> > Convert(string jsonString, string attributeName)
        {
            un = new EuroStatJSONUnNester(jsonString);
            List <NaceRegionData> nrdList = new List <NaceRegionData>();
            List <String>         naceRegionYearFields  = new List <String>();
            List <int>            numberOfItemsInFields = new List <int>();


            if (!un.IsValidDataset())
            {
                return(nrdList);
            }

            List <Region> regions = await databaseStore.getAllRegions();

            List <Nace> naces = await databaseStore.getAllNaces();


            foreach (KeyValuePair <String, String> entry in un.GetValues())
            {
                List <int> totalElementsList = un.GetFields().ConvertAll(field => field.totalElements);
                List <int> indexes           = findIndexesOfFields(int.Parse(entry.Key), totalElementsList);

                int naceId   = indexes[un.GetFieldIndex("nace")];
                int regionId = indexes[un.GetFieldIndex("region")];
                int yearId   = indexes[un.GetFieldIndex("year")];

                string naceCode   = un.GetNaceCode(naceId: naceId);
                string regionCode = un.GetRegionCode(regionId: regionId);
                int    year       = un.GetYear(yearId: yearId);

                double propValue = double.Parse(entry.Value, CultureInfo.InvariantCulture);

                Nace   nace   = naces.Find(nace => nace.naceCode == naceCode);
                Region region = regions.Find(region => region.regionCode == regionCode);

                if (nace != null && region != null)
                {
                    NaceRegionData nrd = new NaceRegionData()
                    {
                        naceId = nace.naceId, regionId = region.regionId, year = year
                    };
                    Type         type = nrd.GetType();
                    PropertyInfo prop = type.GetProperty(attributeName);
                    prop.SetValue(nrd, propValue, null);
                    nrdList.Add(nrd);
                }
            }
            return(nrdList);
        }
 public async Task <ActionResult <IEnumerable <Nace> > > GetAll()
 {
     return(await databaseStore.getAllNaces());
 }