Example #1
0
        /// <summary>
        /// Rolls back a data source to an earlier version.
        /// </summary>
        /// <param name="jobId">The job ID from a previous data source upload. To find the job ID for the data source version you want to restore, query for information about a data source using the showAllVersions parameter.</param>
        /// <param name="dataSourceName">Name of a data source.</param>
        /// <param name="masterKey">A Bing Maps key that has full control of the data source.</param>
        /// <returns>A data flow job which contains the status of the rollback job.</returns>
        public async Task <DataflowJob> RollbackDataSource(string jobId, string dataSourceName, string masterKey)
        {
            DataflowJob result = new DataflowJob();

            try
            {
                if (string.IsNullOrWhiteSpace(jobId))
                {
                    throw new Exception("Job id not specified.");
                }

                if (string.IsNullOrWhiteSpace(dataSourceName))
                {
                    throw new Exception("Data source name not specified.");
                }

                if (string.IsNullOrWhiteSpace(masterKey))
                {
                    throw new Exception("Master key not specified.");
                }

                var request = string.Format("https://spatial.virtualearth.net/REST/v1/Dataflows/DataSourceRollback/{0}/{1}?output=json&key={2}&clientApi=SDSToolkit", jobId, dataSourceName, masterKey);

                var jobs = await DownloadDataflowJobs(request);

                if (jobs != null && jobs.Count > 0 &&
                    string.Compare(jobs[0].Description, "DataSourceRollback", StringComparison.OrdinalIgnoreCase) == 0 &&
                    jobs[0].Links != null && jobs[0].Links.Length > 0)
                {
                    string statusUrl = string.Empty;
                    foreach (var l in jobs[0].Links)
                    {
                        if (string.Compare(l.Role, "self", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            statusUrl = l.Url;
                            break;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(statusUrl))
                    {
                        result = await MonitorStatus(new Uri(statusUrl));
                    }
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                result.Status       = "Aborted";
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Publishes a staged data source to production.
        /// </summary>
        /// <param name="accessId">A unique id used by the Bing Spatial Data Services to identify a data source.</param>
        /// <param name="dataSourceName">The name of a data source.</param>
        /// <param name="masterKey">A Bing Maps key that has full control of the data source.</param>
        /// <returns>The final data flow job when the publishing process completes or is aborted.</returns>
        public async Task <DataServiceJob> PublishStagedDataSource(string accessId, string dataSourceName, string masterKey)
        {
            DataflowJob result = new DataflowJob();

            try
            {
                ValidateProperties(accessId, dataSourceName, masterKey);

                string request = string.Format("https://spatial.virtualearth.net/REST/v1/data/{0}/{1}/$commit?output=json&key={2}&clientApi=SDSToolkit", accessId, dataSourceName, masterKey);

                var jobs = await DownloadDataServiceJobs(request);

                if (jobs != null && jobs.Count > 0 &&
                    string.Compare(jobs[0].Description, "DataSourcePublishFromStaged", StringComparison.OrdinalIgnoreCase) == 0 &&
                    jobs[0].Links != null && jobs[0].Links.Length > 0)
                {
                    string statusUrl = string.Empty;
                    foreach (var l in jobs[0].Links)
                    {
                        if (string.Compare(l.Role, "self", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            statusUrl = l.Url;
                            break;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(statusUrl))
                    {
                        result = await MonitorStatus(new Uri(statusUrl));
                    }
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                result.Status       = "Aborted";
            }

            return(result);
        }
Example #3
0
        private async Task <DataflowJob> MonitorStatus(Uri statusUrl)
        {
            DataflowResponse statusDetails;
            DataflowJob      job = null;
            var ser = new DataContractJsonSerializer(typeof(DataflowResponse));
            int failedMoinitorRequests = 0;

            do
            {
                try
                {
                    using (var s = await ServiceHelper.GetStreamAsync(statusUrl))
                    {
                        statusDetails = ser.ReadObject(s) as DataflowResponse;

                        if (statusDetails != null &&
                            statusDetails.ResourceSets != null &&
                            statusDetails.ResourceSets.Length > 0 &&
                            statusDetails.ResourceSets[0] != null &&
                            statusDetails.ResourceSets[0].Resources != null &&
                            statusDetails.ResourceSets[0].Resources.Length > 0 &&
                            statusDetails.ResourceSets[0].Resources[0] != null)
                        {
                            job = statusDetails.ResourceSets[0].Resources[0] as DataflowJob;
                            if (job.Status == "Aborted")
                            {
                                var sb = new StringBuilder();
                                sb.AppendLine("Job was aborted due to an error.");
                                if (statusDetails.ErrorDetails != null)
                                {
                                    foreach (var e in statusDetails.ErrorDetails)
                                    {
                                        sb.AppendLine(e);
                                    }
                                }

                                job.ErrorMessage = sb.ToString();
                            }
                        }

                        if (job == null || job.Status.Equals("Pending"))
                        {
                            await Task.Delay(_statusUpdateInterval);
                        }
                    }
                }
                catch
                {
                    failedMoinitorRequests++;

                    //If it failed to get the status more than 3 times then stop monitoring.
                    if (failedMoinitorRequests > 3)
                    {
                        job.Status = "Unknown - Lost connection.";
                        break;
                    }
                }
            }while (job != null && job.Status.Equals("Pending"));

            return(job);
        }
Example #4
0
        /// <summary>
        /// Uploads KML and SHP files streams as a data source.
        /// </summary>
        /// <param name="dataSourceStream">A file stream for a data source that is in KML or SHP file format.</param>
        /// <param name="format">Data Source file Format.</param>
        /// <param name="loadOperation">The type of operation to perform when uploading a data source.</param>
        /// <param name="info">Information about the datasource.</param>
        /// <param name="setPublic">A boolean value indicating if the data source should be made public or not.</param>
        /// <returns>The final data flow job when the upload process completes or is aborted.</returns>
        public async Task <DataflowJob> Upload(Stream dataSourceStream, DataSourceFormat format, LoadOperation loadOperation, BasicDataSourceInfo info, bool setPublic)
        {
            var result = new DataflowJob();

            try
            {
                if (string.IsNullOrWhiteSpace(info.MasterKey))
                {
                    throw new Exception("A valid Bing Maps key must be specified as a master key for the data source.");
                }

                if (format != DataSourceFormat.KML && format != DataSourceFormat.SHP)
                {
                    var dataSource = new DataSource(info);

                    if (await dataSource.ReadAsync(dataSourceStream, format))
                    {
                        return(await Upload(dataSource, loadOperation, setPublic));
                    }
                    else
                    {
                        throw new Exception("Unable to read data source file.");
                    }
                }

                //Handle KML and SHP files.

                string request = string.Format("https://spatial.virtualearth.net/REST/v1/Dataflows/LoadDataSource?loadOperation={0}&dataSourceName={1}&setPublic={2}&input={3}&output=json&key={4}&clientApi=SDSToolkit",
                                               loadOperation,
                                               info.DataSourceName,
                                               (setPublic) ? 1 : 0,
                                               (format == DataSourceFormat.KML) ? "kml" : "shp",
                                               info.MasterKey);

                if (!string.IsNullOrWhiteSpace(info.QueryKey))
                {
                    request += "&queryKey=" + info.QueryKey;
                }

                if (!string.IsNullOrWhiteSpace(info.Description))
                {
                    request += "&description=" + Uri.EscapeDataString(info.Description);
                }

                ReportUploadStatus("Creating upload job.");
                var dataflowJobLocation = await CreateUploadJob(request, dataSourceStream, format);

                var statusUrl = new Uri(dataflowJobLocation + "?output=json&key=" + info.MasterKey);

                ReportUploadStatus("Upload job created. Monitoring status.");
                result = await MonitorStatus(statusUrl);

                ReportUploadStatus("Upload job " + result.Status);
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                result.Status       = "Aborted";
                ReportUploadStatus("Upload aborted.\r\n" + ex.Message);
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// Uploads a data source to the Bing Spatial Data Services.
        /// </summary>
        /// <param name="dataSource">The data source that is to be uploaded.</param>
        /// <param name="loadOperation">The type of operation to perform when uploading a data source.</param>
        /// <param name="setPublic">A boolean value indicating if the data source should be made public or not.</param>
        /// <param name="skipEmptyLocations">A boolean value indicating if rows that don't have locaiton information (latitude, longitude, or Geography column) should be uploaded or not. If all rows do not have location information an error will occur and the upload aborted.</param>
        /// <returns>The final data flow job when the upload process completes or is aborted.</returns>
        public async Task <DataflowJob> Upload(DataSource dataSource, LoadOperation loadOperation, bool setPublic, bool skipEmptyLocations)
        {
            var result = new DataflowJob();

            try
            {
                if (string.IsNullOrWhiteSpace(dataSource.Info.MasterKey))
                {
                    throw new Exception("A valid Bing Maps key must be specified as a master key for the data source.");
                }

                var validation = await dataSource.Validate();

                if (validation.Errors != null && validation.Errors.Count > 0)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("Data source failed validation process.");
                    foreach (var e in validation.Errors)
                    {
                        sb.AppendLine(e);
                    }

                    throw new Exception(sb.ToString());
                }

                if (!validation.AllRowsHaveLocationInfo && !skipEmptyLocations)
                {
                    throw new Exception("Not all rows of the data source contain location information.");
                }

                string request = string.Format("https://spatial.virtualearth.net/REST/v1/Dataflows/LoadDataSource?loadOperation={0}&dataSourceName={1}&setPublic={2}&input=xml&output=json&key={3}&clientApi=SDSToolkit",
                                               loadOperation,
                                               dataSource.Info.DataSourceName,
                                               (setPublic) ? 1 : 0,
                                               dataSource.Info.MasterKey);

                if (!string.IsNullOrWhiteSpace(dataSource.Info.QueryKey))
                {
                    request += "&queryKey=" + dataSource.Info.QueryKey;
                }

                if (!string.IsNullOrWhiteSpace(dataSource.Info.AccessId) && (loadOperation == LoadOperation.Incremental || loadOperation == LoadOperation.IncrementalStaging))
                {
                    request += "&accessId=" + dataSource.Info.AccessId;
                }

                if (!string.IsNullOrWhiteSpace(dataSource.Info.Description))
                {
                    request += "&description=" + Uri.EscapeDataString(dataSource.Info.Description);
                }

                ReportUploadStatus("Creating upload job.");
                var dataflowJobLocation = await CreateUploadJob(request, dataSource, skipEmptyLocations);

                var statusUrl = new Uri(dataflowJobLocation + "?output=json&key=" + dataSource.Info.MasterKey);

                ReportUploadStatus("Upload job created. Monitoring status.");
                result = await MonitorStatus(statusUrl);

                ReportUploadStatus("Upload job " + result.Status);
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                result.Status       = "Aborted";
                ReportUploadStatus("Upload aborted.\r\n" + ex.Message);
            }

            return(result);
        }