async void ReplaceDocumentCollectionAsync(object resource, RequestOptions requestOptions) { try { var originalCollection = (DocumentCollection)Tag; //Update collection if necessary var updatedCollection = resource as DocumentCollection; originalCollection.IndexingPolicy = (IndexingPolicy)updatedCollection.IndexingPolicy.Clone(); ResourceResponse <DocumentCollection> response; using (PerfStatus.Start("ReplaceDocumentCollection")) { response = await _client.ReplaceDocumentCollectionExAsync(originalCollection, requestOptions); } Program.GetMain().SetResultInBrowser(null, "Replace DocumentCollection succeed!", false, response.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
private async void FillWithChildren() { try { FeedResponse <Database> databases; using (PerfStatus.Start("ReadDatabaseFeed")) { databases = await _client.ReadDatabaseFeedAsync(); } //databases.Sort((first, second) => string.Compare(((Document)first).Id, ((Document)second).Id, StringComparison.Ordinal)); //databases = databases.Sort() var dbNodeList = databases.Select(db => new DatabaseNode(_client, db)).ToList(); dbNodeList.Sort((first, second) => string.Compare((first).Text, (second).Text, StringComparison.Ordinal)); foreach (var databaseNode in dbNodeList) { Nodes.Add(databaseNode); } Program.GetMain().SetResponseHeaders(databases.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateUDFAsync(object resource, RequestOptions requestOptions) { try { var udf = resource as UserDefinedFunction; ResourceResponse <UserDefinedFunction> newudf; using (PerfStatus.Start("CreateUDF")) { newudf = await _client.CreateUserDefinedFunctionAsync((Parent.Tag as DocumentCollection).GetLink(_client), udf, requestOptions); } Nodes.Add(new ResourceNode(_client, newudf.Resource, ResourceType.UserDefinedFunction)); // set the result window var json = newudf.Resource.ToString(); Program.GetMain().SetResultInBrowser(json, null, false, newudf.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateAttachmentAsync(object resource, RequestOptions requestOptions) { try { var text = resource as string; var attachment = (Attachment)JsonConvert.DeserializeObject(text, typeof(Attachment)); ResourceResponse <Attachment> rr; using (PerfStatus.Start("CreateAttachment")) { rr = await _client.CreateAttachmentAsync((Tag as Resource).GetLink(_client), attachment, requestOptions); } var json = rr.Resource.ToString(); SetResultInBrowser(json, null, false, rr.ResponseHeaders); Nodes.Add(new ResourceNode(_client, rr.Resource, ResourceType.Attachment)); } catch (AggregateException e) { SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }
public void FillWithChildren() { try { var collnode = (DocumentCollectionNode)Parent; FeedResponse <UserDefinedFunction> sps; using (PerfStatus.Start("ReadUdfFeed")) { sps = _client.ReadUserDefinedFunctionFeedAsync((collnode.Tag as DocumentCollection).GetLink(_client)).Result; } foreach (var sp in sps) { var node = new ResourceNode(_client, sp, ResourceType.UserDefinedFunction); Nodes.Add(node); } Program.GetMain().SetResponseHeaders(sps.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateTriggerAsync(object triggerObject, RequestOptions requestOptions) { try { var trigger = triggerObject as Trigger; ResourceResponse <Trigger> newtrigger; using (PerfStatus.Start("CreateTrigger")) { newtrigger = await _client.CreateTriggerAsync((Parent.Tag as DocumentCollection).GetLink(_client), trigger, requestOptions); } Nodes.Add(new ResourceNode(_client, newtrigger.Resource, ResourceType.Trigger)); // set the result window var json = newtrigger.Resource.ToString(); Program.GetMain().SetResultInBrowser(json, null, false, newtrigger.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
public async void FillWithChildren() { try { FeedResponse <DocumentCollection> colls; var db = (Database)Tag; var dbId = db.Id; using (PerfStatus.Start("ReadDocumentCollectionFeed")) { colls = await _client.ReadDocumentCollectionFeedAsync(db.GetLink(_client)); } foreach (var coll in colls) { var node = new DocumentCollectionNode(_client, coll, dbId); Nodes.Add(node); } Program.GetMain().SetResponseHeaders(colls.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
public void FillWithChildren() { try { FeedResponse <Conflict> feedConflicts; using (PerfStatus.Start("ReadConflictsFeed")) { feedConflicts = _client.ReadConflictFeedAsync((Parent.Tag as DocumentCollection).GetLink(_client)).Result; } foreach (var sp in feedConflicts) { var node = new ResourceNode(_client, sp, ResourceType.Conflict); Nodes.Add(node); } Program.GetMain().SetResponseHeaders(feedConflicts.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateDocumentCollectionAsync(object resource, RequestOptions requestOptions) { try { var coll = resource as DocumentCollection; var db = (Database)Tag; var dbId = db.Id; ResourceResponse <DocumentCollection> newcoll; using (PerfStatus.Start("CreateDocumentCollection")) { newcoll = await _client.CreateDocumentCollectionAsync(db.GetLink(_client), coll, requestOptions); } // set the result window var json = JsonConvert.SerializeObject(newcoll.Resource, Formatting.Indented); Program.GetMain().SetResultInBrowser(json, null, false, newcoll.ResponseHeaders); Nodes.Add(new DocumentCollectionNode(_client, newcoll.Resource, dbId)); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateUserAsync(object resource, RequestOptions requestOptions) { try { var user = (User)JsonConvert.DeserializeObject(resource as string, typeof(User)); ResourceResponse <User> newUser; using (PerfStatus.Start("CreateUser")) { newUser = await _client.CreateUserAsync((Parent.Tag as Database).GetLink(_client), user, requestOptions); } Nodes.Add(new ResourceNode(_client, newUser.Resource, ResourceType.User)); // set the result window var json = newUser.Resource.ToString(); Program.GetMain().SetResultInBrowser(json, null, false, newUser.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
public void FillWithChildren() { try { FeedResponse <User> sps; using (PerfStatus.Start("ReadUser")) { sps = _client.ReadUserFeedAsync((Parent.Tag as Database).GetLink(_client)).Result; } foreach (var sp in sps) { var node = new ResourceNode(_client, sp, ResourceType.User); Nodes.Add(node); } Program.GetMain().SetResponseHeaders(sps.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreatePermissionAsync(object resource, RequestOptions requestOptions) { try { var permission = JsonSerializable.LoadFrom <Permission>(new MemoryStream(Encoding.UTF8.GetBytes(resource as string))); ResourceResponse <Permission> newtpermission; using (PerfStatus.Start("CreatePermission")) { newtpermission = await _client.CreatePermissionAsync((Parent.Tag as Resource).GetLink(_client), permission, requestOptions); } Nodes.Add(new ResourceNode(_client, newtpermission.Resource, ResourceType.Permission)); // set the result window var json = newtpermission.Resource.ToString(); Program.GetMain().SetResultInBrowser(json, null, false, newtpermission.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateStoredProcedureAsync(object resource, RequestOptions requestOptions) { try { var sp = resource as StoredProcedure; ResourceResponse <StoredProcedure> newsp; using (PerfStatus.Start("CreateStoredProcedure")) { newsp = await _client.CreateStoredProcedureAsync((Parent.Tag as DocumentCollection).GetLink(_client), sp, requestOptions); } Nodes.Add(new ResourceNode(_client, newsp.Resource, ResourceType.StoredProcedure)); // set the result window var json = newsp.Resource.ToString(); Program.GetMain().SetResultInBrowser(json, null, false, newsp.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateDatabaseAsync(object resource, RequestOptions requestOptions) { try { var text = resource as string; var db = (Database)JsonConvert.DeserializeObject(text, typeof(Database)); ResourceResponse <Database> newdb; using (PerfStatus.Start("CreateDatabase")) { newdb = await _client.CreateDatabaseAsync(db, requestOptions); } Nodes.Add(new DatabaseNode(_client, newdb.Resource)); // set the result window var json = JsonConvert.SerializeObject(newdb.Resource, Formatting.Indented); Program.GetMain().SetResultInBrowser(json, null, false, newdb.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
public void FillWithChildren() { try { var docs = new List <dynamic>(); NameValueCollection responseHeaders = null; var dc = (DocumentCollection)Tag; var dcId = dc.Id; using (PerfStatus.Start("ReadDocumentFeed")) { var feedReader = CreateDocumentQuery("Select * from c", new FeedOptions { EnableCrossPartitionQuery = true }); while (feedReader.HasMoreResults && docs.Count() < 100) { FeedResponse <Document> response = feedReader.ExecuteNextAsync <Document>().Result; docs.AddRange(response); responseHeaders = response.ResponseHeaders; } } var host = _client.ServiceEndpoint.Host; string customDocumentDisplayIdentifier; string sortField; bool reverseSort; var useCustom = _customDocumentListDisplayManager.GetCustomDocumentDisplayIdentifier(docs, host, dc.Id, _databaseId, out customDocumentDisplayIdentifier, out sortField, out reverseSort); DocumentHelper.SortDocuments(useCustom, docs, sortField, reverseSort); foreach (var doc in docs) { if (useCustom) { var displayText = _customDocumentListDisplayManager.GetDisplayText(true, doc, customDocumentDisplayIdentifier); var node = new ResourceNode(_client, doc, ResourceType.Document, dc.PartitionKey, displayText, dataBaseId: _databaseId, documentCollectionId: dcId); Nodes.Add(node); } else { var node = new ResourceNode(_client, doc, ResourceType.Document, dc.PartitionKey, dataBaseId: _databaseId, documentCollectionId: dcId); Nodes.Add(node); } } Program.GetMain().SetResponseHeaders(responseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void myMenuItemAttachmentFromFile_Click(object sender, EventArgs eventArg) { var ofd = new OpenFileDialog(); var dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { var filename = ofd.FileName; // // todo: present the dialog for Slug name and Content type // Program.GetMain().SetLoadingState(); try { using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { var mediaOptions = new MediaOptions() { ContentType = "application/octet-stream", Slug = Path.GetFileName(ofd.FileName) }; ResourceResponse <Attachment> rr; var document = ((Document)Tag); var collection = ((DocumentCollection)Parent.Tag); var requestOptions = GetRequestOptions(); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } using (PerfStatus.Start("CreateAttachment")) { rr = await _client.CreateAttachmentAsync((Tag as Document).SelfLink + "/attachments", stream, mediaOptions, requestOptions); } var json = rr.Resource.ToString(); SetResultInBrowser(json, null, false, rr.ResponseHeaders); Nodes.Add(new ResourceNode(_client, rr.Resource, ResourceType.Attachment)); } } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } } }
async void QueryOfferAsync(object resource, RequestOptions requestOptions) { try { var queryText = resource as string; // text is the querytext. var q = _client.CreateOfferQuery(queryText).AsDocumentQuery(); FeedResponse <Database> r; using (PerfStatus.Start("QueryOffer")) { r = await q.ExecuteNextAsync <Database>(); } // set the result window string text = null; if (r.Count > 1) { text = string.Format(CultureInfo.InvariantCulture, "Returned {0} Offers", r.Count); } else { text = string.Format(CultureInfo.InvariantCulture, "Returned {0} Offer", r.Count); } var jsonarray = "["; var index = 0; foreach (dynamic d in r) { index++; // currently Query.ToString() has Formatting.Indented, but the public release doesn't have yet. jsonarray += d.ToString(); if (index == r.Count) { jsonarray += "]"; } else { jsonarray += ",\r\n"; } } Program.GetMain().SetResultInBrowser(jsonarray, text, true, r.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void ExecuteStoredProcedureAsync(object resource, RequestOptions requestOptions) { try { var text = resource as string; var inputParamters = new List <string>(); if (!string.IsNullOrEmpty(text)) { using (var sr = new StringReader(text)) { string line; while ((line = sr.ReadLine()) != null) { if (!string.IsNullOrEmpty(line)) { inputParamters.Add(line); } } //while } //usi } var dynamicInputParams = new dynamic[inputParamters.Count]; for (var i = 0; i < inputParamters.Count; i++) { var inputParamter = inputParamters[i]; var jTokenParam = JToken.Parse(inputParamter); var dynamicParam = Helper.ConvertJTokenToDynamic(jTokenParam); dynamicInputParams[i] = dynamicParam; } StoredProcedureResponse <dynamic> rr; using (PerfStatus.Start("ExecuateStoredProcedure")) { rr = await _client.ExecuteStoredProcedureAsync <dynamic>((Tag as Resource).GetLink(_client), requestOptions, dynamicInputParams); } string executeResult = rr.Response.ToString(); SetResultInBrowser(null, executeResult, true, rr.ResponseHeaders); } catch (AggregateException e) { SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }
public async Task InvokeCreateDocumentsFromFolder() { var ofd = new OpenFileDialog { Multiselect = true }; var dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { var status = string.Format(CultureInfo.InvariantCulture, "Create {0} documents in collection\r\n", ofd.FileNames.Length); // Read the files foreach (var filename in ofd.FileNames) { // right now assume every file is JSON content var jsonText = File.ReadAllText(filename); var fileRootName = Path.GetFileName(filename); var document = JsonConvert.DeserializeObject(jsonText); try { using (PerfStatus.Start("CreateDocument")) { var newdocument = await _client.CreateDocumentAsync((Tag as DocumentCollection).GetLink(_client), document, Program.GetMain().GetRequestOptions()); status += string.Format(CultureInfo.InvariantCulture, "Succeed adding {0} \r\n", fileRootName); } } catch (DocumentClientException ex) { status += string.Format(CultureInfo.InvariantCulture, "Failed adding {0}, statusCode={1} \r\n", fileRootName, ex.StatusCode); } catch (Exception ex) { status += string.Format(CultureInfo.InvariantCulture, "Failed adding {0}, unknown exception \r\n", fileRootName, ex.Message); } Program.GetMain().SetResultInBrowser(null, status, false); } } }
async void myMenuItemRenderMedia_Click(object sender, EventArgs eventArg) { var guidFileName = Guid.NewGuid().ToString(); string fileName; // let's guess the contentype. var attachment = Tag as Attachment; if (string.Compare(attachment.ContentType, "application/octet-stream", StringComparison.OrdinalIgnoreCase) == 0) { // get the extension from attachment.Id var index = attachment.Id.LastIndexOf('.'); fileName = guidFileName + attachment.Id.Substring(index); } else if (attachment.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase)) { // treat as image. fileName = guidFileName + ".gif"; } else { fileName = guidFileName + ".txt"; } fileName = Path.Combine(SystemInfoProvider.LocalApplicationDataPath, fileName); try { MediaResponse rr; using (PerfStatus.Start("DownloadMedia")) { rr = await _client.ReadMediaAsync(attachment.MediaLink); } using (var fileStream = File.Create(fileName)) { rr.Media.CopyTo(fileStream); } SetResultInBrowser(null, "It is saved to " + fileName, true); Program.GetMain().RenderFile(fileName); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }
async void myMenuItemDownloadMedia_Click(object sender, EventArgs eventArg) { var attachment = Tag as Attachment; // Get the filenanme from attachment.Id var index = attachment.Id.LastIndexOf('\\'); var fileName = attachment.Id; if (index > 0) { fileName = fileName.Substring(index + 1); } var ofd = new SaveFileDialog { FileName = fileName }; var dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { var saveFile = ofd.FileName; Program.GetMain().SetLoadingState(); try { MediaResponse rr; using (PerfStatus.Start("DownloadMedia")) { rr = await _client.ReadMediaAsync(attachment.MediaLink); } using (var fileStream = File.Create(saveFile)) { rr.Media.CopyTo(fileStream); } SetResultInBrowser(null, "It is saved to " + saveFile, true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } } }
async void DeleteDocumentCollectionAsync(object resource, RequestOptions requestOptions) { try { var coll = (DocumentCollection)Tag; ResourceResponse <DocumentCollection> newcoll; using (PerfStatus.Start("DeleteDocumentCollection")) { newcoll = await _client.DeleteDocumentCollectionAsync(coll.GetLink(_client), Program.GetMain().GetRequestOptions()); } Program.GetMain().SetResultInBrowser(null, "Delete DocumentCollection succeed!", false, newcoll.ResponseHeaders); Remove(); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void myMenuItemReadDatabase_Click(object sender, EventArgs eArgs) { try { ResourceResponse <Database> database; using (PerfStatus.Start("ReadDatabase")) { database = await _client.ReadDatabaseAsync(((Database)Tag).GetLink(_client), Program.GetMain().GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(database.Resource, Formatting.Indented); Program.GetMain().SetResultInBrowser(json, null, false, database.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void DeleteDatabaseAsync(object resource, RequestOptions requestOptions) { try { var db = (Database)Tag; ResourceResponse <Database> newdb; using (PerfStatus.Start("DeleteDatabase")) { newdb = await _client.DeleteDatabaseAsync(db.GetLink(_client), requestOptions); } Program.GetMain().SetResultInBrowser(null, "Delete database succeed!", false, newdb.ResponseHeaders); Remove(); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void CreateDocumentAsync(object resource, RequestOptions requestOptions) { try { var document = JsonConvert.DeserializeObject(resource as string); var dc = Tag as DocumentCollection; var dcId = dc.Id; ResourceResponse <Document> newdocument; using (PerfStatus.Start("CreateDocument")) { newdocument = await _client.CreateDocumentAsync(dc.GetLink(_client), document, requestOptions); } var hostName = _client.ServiceEndpoint.Host; var displayText = _customDocumentListDisplayManager.GetDisplayText(newdocument.Resource, hostName, dcId, _databaseId); Nodes.Add( new ResourceNode(_client, newdocument.Resource, ResourceType.Document, dc.PartitionKey, displayText) ); // set the result window var json = newdocument.Resource.ToString(); json = DocumentHelper.RemoveInternalDocumentValues(json); Program.GetMain().SetResultInBrowser(json, null, false, newdocument.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
async void myMenuItemReadDatabaseAccount_Click(object sender, EventArgs eArgs) { try { DatabaseAccount databaseAccount; using (PerfStatus.Start("ReadDatabase")) { databaseAccount = await _client.GetDatabaseAccountAsync(); } // set the result window var json = JsonConvert.SerializeObject(databaseAccount, Formatting.Indented); Tag = databaseAccount; Program.GetMain().SetResultInBrowser(json, null, false); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
public void FillWithChildren() { try { var document = ((Document)Tag); var collection = ((DocumentCollection)Parent.Tag); var options = new FeedOptions(); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { options.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } FeedResponse <Attachment> attachments; using (PerfStatus.Start("ReadAttachmentFeed")) { attachments = _client.ReadAttachmentFeedAsync(document.GetLink(_client), options).Result; } foreach (var attachment in attachments) { var node = new ResourceNode(_client, attachment, ResourceType.Attachment); Nodes.Add(node); } Program.GetMain().SetResponseHeaders(attachments.ResponseHeaders); } catch (AggregateException e) { SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }
async void DeleteResourceAsync(object resource, RequestOptions requestOptions) { try { if (_resourceType == ResourceType.Document) { var doc = (Document)Tag; ResourceResponse <Document> rr; var collection = ((DocumentCollection)Parent.Tag); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(doc, collection.PartitionKey)); } using (PerfStatus.Start("DeleteDocument")) { rr = await _client.DeleteDocumentAsync(doc.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete Document succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.StoredProcedure) { var sp = (StoredProcedure)Tag; ResourceResponse <StoredProcedure> rr; using (PerfStatus.Start("DeleteStoredProcedure")) { rr = await _client.DeleteStoredProcedureAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete StoredProcedure succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.User) { var sp = (User)Tag; ResourceResponse <User> rr; using (PerfStatus.Start("DeleteUser")) { rr = await _client.DeleteUserAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete User succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Trigger) { var sp = (Trigger)Tag; ResourceResponse <Trigger> rr; using (PerfStatus.Start("DeleteTrigger")) { rr = await _client.DeleteTriggerAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete Trigger succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.UserDefinedFunction) { var sp = (UserDefinedFunction)Tag; ResourceResponse <UserDefinedFunction> rr; using (PerfStatus.Start("DeleteUDF")) { rr = await _client.DeleteUserDefinedFunctionAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete UserDefinedFunction succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Permission) { var sp = (Permission)Tag; ResourceResponse <Permission> rr; using (PerfStatus.Start("DeletePermission")) { rr = await _client.DeletePermissionAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete Permission succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Attachment) { var sp = (Attachment)Tag; ResourceResponse <Attachment> rr; var document = ((Document)Parent.Tag); var collection = ((DocumentCollection)Parent.Parent.Tag); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } using (PerfStatus.Start("DeleteAttachment")) { rr = await _client.DeleteAttachmentAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete Attachment succeed!", false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Conflict) { var sp = (Conflict)Tag; ResourceResponse <Conflict> rr; using (PerfStatus.Start("DeleteConlict")) { rr = await _client.DeleteConflictAsync(sp.GetLink(_client), requestOptions); } SetResultInBrowser(null, "Delete Conflict succeed!", false, rr.ResponseHeaders); } // Remove the node. Remove(); } catch (AggregateException e) { SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }
async void ReplaceResourceAsync(object resource, RequestOptions requestOptions) { try { string json = null; if (_resourceType == ResourceType.Document) { var text = resource as string; var doc = (Document)JsonConvert.DeserializeObject(text, typeof(Document)); var tagAsDoc = (Tag as Document); doc.SetReflectedPropertyValue("AltLink", tagAsDoc.GetAltLink()); ResourceResponse <Document> rr; var hostName = _client.ServiceEndpoint.Host; using (PerfStatus.Start("ReplaceDocument")) { rr = await _client.ReplaceDocumentAsync(doc.GetLink(_client), doc, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = _customDocumentListDisplayManager.GetDisplayText(rr.Resource, hostName, _documentCollectionId, _databaseId); // set the result window SetResultInBrowser(DocumentHelper.RemoveInternalDocumentValues(json), null, false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.StoredProcedure) { var input = resource as StoredProcedure; var sp = Tag as StoredProcedure; sp.Body = input.Body; if (!string.IsNullOrEmpty(input.Id)) { sp.Id = input.Id; } ResourceResponse <StoredProcedure> rr; using (PerfStatus.Start("ReplaceStoredProcedure")) { rr = await _client.ReplaceStoredProcedureExAsync(sp, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = rr.Resource.Id; // set the result window SetResultInBrowser(json, null, false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.User) { var text = resource as string; var sp = (User)JsonConvert.DeserializeObject(text, typeof(User)); sp.SetReflectedPropertyValue("AltLink", (Tag as User).GetAltLink()); ResourceResponse <User> rr; using (PerfStatus.Start("ReplaceUser")) { rr = await _client.ReplaceUserExAsync(sp, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = rr.Resource.Id; // set the result window SetResultInBrowser(json, null, false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Trigger) { var input = resource as Trigger; var sp = Tag as Trigger; sp.Body = input.Body; if (!string.IsNullOrEmpty(input.Id)) { sp.Id = input.Id; } ResourceResponse <Trigger> rr; using (PerfStatus.Start("ReplaceTrigger")) { rr = await _client.ReplaceTriggerExAsync(sp, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = rr.Resource.Id; // set the result window SetResultInBrowser(json, null, false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.UserDefinedFunction) { var input = resource as UserDefinedFunction; var sp = Tag as UserDefinedFunction; sp.Body = input.Body; if (!string.IsNullOrEmpty(input.Id)) { sp.Id = input.Id; } ResourceResponse <UserDefinedFunction> rr; using (PerfStatus.Start("ReplaceUDF")) { rr = await _client.ReplaceUserDefinedFunctionExAsync(sp, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = rr.Resource.Id; // set the result window SetResultInBrowser(json, null, false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Permission) { var text = resource as string; var sp = JsonSerializable.LoadFrom <Permission>(new MemoryStream(Encoding.UTF8.GetBytes(text))); sp.SetReflectedPropertyValue("AltLink", (Tag as Permission).GetAltLink()); ResourceResponse <Permission> rr; using (PerfStatus.Start("ReplacePermission")) { rr = await _client.ReplacePermissionExAsync(sp, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = rr.Resource.Id; // set the result window SetResultInBrowser(json, null, false, rr.ResponseHeaders); } else if (_resourceType == ResourceType.Attachment) { var text = resource as string; var sp = (Attachment)JsonConvert.DeserializeObject(text, typeof(Attachment)); sp.SetReflectedPropertyValue("AltLink", (Tag as Attachment).GetAltLink()); ResourceResponse <Attachment> rr; var document = ((Document)Parent.Tag); var collection = ((DocumentCollection)Parent.Parent.Tag); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } using (PerfStatus.Start("ReplaceAttachment")) { rr = await _client.ReplaceAttachmentExAsync(sp, requestOptions); } json = rr.Resource.ToString(); Tag = rr.Resource; Text = rr.Resource.Id; // set the result window SetResultInBrowser(json, null, false, rr.ResponseHeaders); } } catch (AggregateException e) { SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }
async void myMenuItemRead_Click(object sender, EventArgs eventArg) { try { switch (_resourceType) { case ResourceType.Offer: { ResourceResponse <Offer> rr; using (PerfStatus.Start("ReadOffer")) { rr = await _client.ReadOfferAsync(((Resource)Tag).SelfLink); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.Document: { var document = (Document)Tag; var collection = (DocumentCollection)Parent.Tag; var requestOptions = GetRequestOptions(); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } ResourceResponse <Document> rr; using (PerfStatus.Start("ReadDocument")) { rr = await _client.ReadDocumentAsync(document.GetLink(_client), requestOptions); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); json = DocumentHelper.RemoveInternalDocumentValues(json); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.Conflict: { ResourceResponse <Conflict> rr; using (PerfStatus.Start("ReadConflict")) { rr = await _client.ReadConflictAsync(((Resource)Tag).GetLink(_client), GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.Attachment: { ResourceResponse <Attachment> rr; var document = ((Document)Tag); var collection = ((DocumentCollection)Parent.Tag); var requestOptions = GetRequestOptions(); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } using (PerfStatus.Start("ReadAttachment")) { rr = await _client.ReadAttachmentAsync(document.GetLink(_client), requestOptions); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.User: { ResourceResponse <User> rr; using (PerfStatus.Start("ReadUser")) { rr = await _client.ReadUserAsync(((Resource)Tag).GetLink(_client), GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.Permission: { ResourceResponse <Permission> rr; using (PerfStatus.Start("ReadPermission")) { rr = await _client.ReadPermissionAsync(((Resource)Tag).GetLink(_client), GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.StoredProcedure: { ResourceResponse <StoredProcedure> rr; using (PerfStatus.Start("ReadStoredProcedure")) { rr = await _client.ReadStoredProcedureAsync(((Resource)Tag).GetLink(_client), GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.Trigger: { ResourceResponse <Trigger> rr; using (PerfStatus.Start("ReadTrigger")) { rr = await _client.ReadTriggerAsync(((Resource)Tag).GetLink(_client), GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; case ResourceType.UserDefinedFunction: { ResourceResponse <UserDefinedFunction> rr; using (PerfStatus.Start("ReadUDF")) { rr = await _client.ReadUserDefinedFunctionAsync(((Resource)Tag).GetLink(_client), GetRequestOptions()); } // set the result window var json = JsonConvert.SerializeObject(rr.Resource, Formatting.Indented); SetResultInBrowser(json, null, false, rr.ResponseHeaders); } break; default: throw new ArgumentException("Unsupported resource type " + _resourceType); } } catch (AggregateException e) { SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { SetResultInBrowser(null, e.ToString(), true); } }