コード例 #1
0
        /// <summary>
        /// Parse URL, get special tags and replace with latest available date
        /// Supported :
        ///     [currentmonth]
        ///     [previousmonth]
        ///     [lastyearmonth]
        ///     [currentyear]
        ///     [lastyear]
        /// </summary>
        private void DetectPeriods()
        {
            if (!string.IsNullOrEmpty(_tableId) && (_url.Contains("Perioden") || _url.Contains("Periods")) && (_url.Contains("previousmonth") || _url.Contains("currentmonth") || _url.Contains("lastyearmonth") || _url.Contains("currentyear") || _url.Contains("lastyear")))
            {
                // English Periods data differs from Dutch
                string periodUrl = _url.Contains("Periods") ? string.Format("{0}{1}/Periods?$format=json", _baseUrl, _tableId) : string.Format("{0}{1}/Perioden?$format=json", _baseUrl, _tableId);

                string        json          = GetJson(periodUrl);
                PropertyItems propertyItems = Deserialize <PropertyItems>(json);

                if ((propertyItems != null) && (propertyItems.Items != null) && (propertyItems.Items.Any()))
                {
                    List <PropertyItem> searchItems = propertyItems.Items.OrderByDescending(x => x.Key).Where(p => p.Key.Contains("MM")).ToList();

                    if (searchItems.Any())
                    {
                        string currentmonth = searchItems[0].Key;
                        _url = _url.Replace("[currentmonth]", currentmonth);

                        if (propertyItems.Items.Count > 1)
                        {
                            string previousmonth = searchItems[1].Key;
                            _url = _url.Replace("[previousmonth]", previousmonth);
                        }

                        int year = Convert.ToInt32(currentmonth.Substring(0, 4));
                        year = year - 1;

                        string lastyearmonth = currentmonth.Replace(currentmonth.Substring(0, 4), year.ToString());
                        _url = _url.Replace("[lastyearmonth]", lastyearmonth);
                    }

                    searchItems = propertyItems.Items.OrderByDescending(x => x.Key).Where(p => p.Key.Contains("JJ")).ToList();

                    if (searchItems.Any())
                    {
                        _url = _url.Replace("[currentyear]", searchItems[0].Key);

                        if (propertyItems.Items.Count > 1)
                        {
                            _url = _url.Replace("[previousyear]", searchItems[1].Key);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Could not parse period table {0}", periodUrl);
                }
            }
        }
コード例 #2
0
        public SeriesTableViewModel GetTable()
        {
            if (string.IsNullOrEmpty(_tableId))
            {
                return(null);
            }

            PropertyItems regions = null;

            string             json      = GetJson(string.Format("{0}Tables?$format=json&$filter=(Identifier%20eq%20%27{1}%27)", _catalogUrl, _tableId));
            TableInfoViewModel tableInfo = Deserialize <TableInfoViewModel>(json);

            json = GetJson(_url);
            SeriesTableViewModel seriesTableViewModel = Deserialize <SeriesTableViewModel>(json);

            string propertiesUrl = string.Format("{0}{1}/DataProperties?$format=json", _baseUrl, _tableId);

            json = GetJson(propertiesUrl);
            DataPropertiesViewModel properties = Deserialize <DataPropertiesViewModel>(json);

            if ((tableInfo == null) || (properties == null) || (seriesTableViewModel == null))
            {
                return(null);
            }

            // Parse properties
            var regionAvailable = properties.DataProperties.FirstOrDefault(s => s.Datatype == "GeoDimension");

            if (regionAvailable != null)
            {
                string regionKey = regionAvailable.Key;

                json    = GetJson(string.Format("{0}{1}/{2}?$format=json", _baseUrl, _tableId, regionKey));
                regions = Deserialize <PropertyItems>(json);
            }

            foreach (DataColumn col in seriesTableViewModel.Data.Columns)
            {
                var propertyColumn = properties.DataProperties.FirstOrDefault(s => s.Key == col.ColumnName);

                if (propertyColumn != null)
                {
                    col.ColumnName = string.Format("{0} {1}", propertyColumn.Title, string.IsNullOrEmpty(propertyColumn.Unit) ? string.Empty : string.Format("({0})", propertyColumn.Unit));

                    if (propertyColumn.Type == "TimeDimension")
                    {
                        foreach (DataRow row in seriesTableViewModel.Data.Rows)
                        {
                            string periode = row[col.ColumnName].ToString();
                            row[col.ColumnName] = ParsePeriod(periode);
                        }
                    }

                    if ((propertyColumn.Type == "GeoDimension") && (regions != null))
                    {
                        foreach (DataRow row in seriesTableViewModel.Data.Rows)
                        {
                            string region     = row[col.ColumnName].ToString();
                            var    regionName = regions.Items.FirstOrDefault(x => x.Key == region);
                            if (regionName != null)
                            {
                                row[col.ColumnName] = regionName.Title;
                            }
                        }
                    }
                }
            }

            if (_transpose)
            {
                seriesTableViewModel.Data = Transpose(seriesTableViewModel.Data);
            }

            if ((tableInfo.TableInfos != null) && (tableInfo.TableInfos.Any()))
            {
                seriesTableViewModel.TableInfo = tableInfo.TableInfos[0];
            }
            seriesTableViewModel.TableId = _tableId;

            return(seriesTableViewModel);
        }