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);
                }
            }
        }
Beispiel #2
0
        public static object ExtractPartitionKeyValue(Document document, PartitionKeyDefinition partitionKeyDefinition)
        {
            if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0)
            {
                return(Undefined.Value);
            }

            return(partitionKeyDefinition.Paths.Select(path =>
            {
                string[] parts = PathParser.GetPathParts(path);
                Debug.Assert(parts.Length >= 1, "Partition key component definition path is invalid.");

                return DocumentAnalyzer.GetValueByPath <object>(JToken.FromObject(document), parts, Undefined.Value);
            }).ToArray().FirstOrDefault());
        }
        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);
            }
        }