/// <summary>
        /// Get species observation count.
        /// </summary>
        /// <returns>species observation count.</returns>
        public Int64 GetSpeciesObservationCount()
        {
            GbifDataSetsJson page = GetSpeciesObservationPage(0, 0);

            if (page.IsNull())
            {
                throw new ApplicationException("Faild to retrieve species observation count.");
            }

            return(page.Count);
        }
        /// <summary>
        /// Opens a new connection to the GBIF web service and tries to return processed update-rows for the "data-page" as requested by the dataSetKey combined with the pageNumber and pageSize parameters
        /// </summary>
        /// <param name="mappings"></param>
        /// <param name="context"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        private List <HarvestSpeciesObservation> GetSpeciesObservations(List <HarvestMapping> mappings, WebServiceContext context, Int64 pageNumber, Int64 pageSize)
        {
            var createdSpeciesObservations = new List <HarvestSpeciesObservation>();
            GbifDataSetsJson datasetData   = GetSpeciesObservationPage(pageNumber, pageSize);

            if (datasetData.IsNotNull())
            {
                // Convert and process species observations.
                foreach (Dictionary <string, object> speciesObservations in datasetData.Results)
                {
                    createdSpeciesObservations.Add(ProcessInstance.ProcessObservation(GetWebData(speciesObservations), mappings, context));
                }
            }

            return(createdSpeciesObservations);
        }
        /// <summary>
        /// Opens the file tries to return rows for the "data-page" as requested by the pageNumber and pageSize parameters
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        private GbifDataSetsJson GetSpeciesObservationPage(Int64 pageNumber, Int64 pageSize)
        {
            if (IsFileBased)
            {
                var startRow = pageNumber * pageSize + 1;
                var endRow   = (pageNumber + 1) * pageSize + 1;
                var columns  = HeaderRow.Split('\t');
                var page     = new GbifDataSetsJson
                {
                    Results      = new List <Dictionary <string, object> >(),
                    Count        = FileLineCount,
                    Limit        = pageSize,
                    Offset       = startRow,
                    EndOfRecords = endRow >= FileLineCount
                };

                foreach (var row in File.ReadLines(Filename).Skip((Int32)startRow).Take((Int32)pageSize))
                {
                    var resultRow = new Dictionary <string, object>();
                    var values    = row.Split('\t');

                    for (int i = 0; i < columns.Length; i++)
                    {
                        if (values[i].IsNotEmpty())
                        {
                            resultRow.Add(columns[i], values[i]);
                        }
                    }

                    page.Results.Add(resultRow);
                }

                if ((pageNumber <= 0) && page.Results.IsNotEmpty())
                {
                    // Remove first line in file.
                    // This line is expected to hold the column headers.
                    page.Results.RemoveAt(0);
                }

                return(page);
            }
            else
            {
                using (var client = new HttpClient {
                    BaseAddress = new Uri(WebServiceAddress), Timeout = new TimeSpan(0, TimeoutMinutes, 0)
                })
                {
                    // Add an Accept header for JSON format.
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var querystring = WebServiceQueryString
                                      .AddSimpleQueryParameter("offset", pageNumber * pageSize)
                                      .AddSimpleQueryParameter("limit", pageSize);

                    var dataSetResponse = client.GetAsync(querystring).Result;

                    if (dataSetResponse.IsSuccessStatusCode)
                    {
                        var response       = dataSetResponse.Content.ReadAsByteArrayAsync().Result;
                        var responseString = Encoding.UTF8.GetString(response, 0, response.Length);
                        return(new JavaScriptSerializer {
                            MaxJsonLength = int.MaxValue
                        }.Deserialize <GbifDataSetsJson>(responseString));
                    }
                }
            }

            return(null);
        }