Ejemplo n.º 1
0
        public async Task<List<Property>> GetResourceProperties(Resource resource)
        {
            var client = new AtomPubClient();
            var res = DataCache.Instance.Lookup<SyndicationItem>(resource.Identity.AbsoluteUri);
            if (res == null)
            {
                res = await client.RetrieveResourceAsync(resource.Identity);
                DataCache.Instance.Cache(resource.Identity.AbsoluteUri, res);
            }
         
            // process simple properties
            var properties = new List<Property>();

            if (res.Content.Type.Equals("application/xml"))
            {
                var xml = res.Content.Xml;
                foreach (var cn in xml.FirstChild.ChildNodes)
                {
                    try
                    {
                        if (cn.FirstChild == null || cn.FirstChild.NodeValue == null) continue;
                        var val = cn.FirstChild.NodeValue.ToString();
                        if (string.IsNullOrEmpty(val)) continue;
                        var property = new Property(this, null);
                        property.PropertyName = cn.LocalName.ToString();
                        property.PropertyValue = val;
                        property.IsLiteral = true;
                        if (IsPropertyVisible(resource.Type.Identity.ToString(), property.PropertyName)) properties.Add(property);
                    }
                    catch (Exception ex)
                    {
                        var msg = ex.Message;
                    }
                }
            }
            else
            {
                var propertyElements = res.ElementExtensions;
                foreach (var item in propertyElements)
                {
                    if (item.NodeName.ToLower().Equals("properties"))
                    {
                        foreach (var prop in item.ElementExtensions)
                        {
                            if (string.IsNullOrEmpty(prop.NodeValue)) continue;
                            if (prop.ElementExtensions.Count > 0)
                            {
                                foreach (var innerprop in prop.ElementExtensions)
                                {
                                    if (string.IsNullOrEmpty(innerprop.NodeValue)) continue;
                                    var property = new Property(this, null)
                                    {
                                        PropertyName = prop.NodeName + innerprop.NodeName,
                                        PropertyValue = innerprop.NodeValue,
                                        IsLiteral = true
                                    };
                                    properties.Add(property);
                                }
                            }
                            else
                            {
                                var property = new Property(this, null)
                                                   {
                                                       PropertyName = prop.NodeName,
                                                       PropertyValue = prop.NodeValue,
                                                       IsLiteral = true
                                                   };
                                properties.Add(property);
                            }
                        }
                    }
                }
            }
            
            // process link properties
            var links = res.Links.Where(l => l.Relationship.StartsWith(OdataRelationshipRelTypePrefix));
            foreach (var syndicationLink in links)
            {
                // property name
                var propertyName = syndicationLink.Relationship.Substring(OdataRelationshipRelTypePrefix.Length);

                // need to check if we need to load an entry or a feed
                if (syndicationLink.MediaType.ToLower().Contains("type=entry"))
                {
                    try
                    {
                        var relatedResource = await client.RetrieveResourceAsync(syndicationLink.Uri);
                        var property = new Property(this, null)
                                           {
                                               PropertyName = propertyName,
                                               PropertyValue = syndicationLink.Uri.ToString(),
                                               PropertyValueName = relatedResource.Title.Text
                                           };
                        properties.Add(property);
                    }
                    catch (Exception)
                    {
                        // sometimes we get bad data from odata services.
                    }
                }
                else
                {
                    try
                    {
                        AtomPubClient relatedResourceClient = new AtomPubClient();
                        // always ask for four and show three, if we get four then we can have a show all property.
                        var entries = await client.RetrieveFeedAsync(new Uri(syndicationLink.Uri.ToString() + "?$top=4")); 

                        foreach (var entry in entries.Items.Take(3))
                        {
                            var property = new Property(this, null);
                            property.PropertyName = propertyName;
                            property.PropertyValue = entry.EditUri.AbsoluteUri;
                            property.PropertyValueName = entry.Title.Text;
                            properties.Add(property);
                        }

                        if (entries.Items.Count > 3)
                        {
                            var property = new Property(this, null);
                            property.PropertyName = "";
                            property.IsCollectionProperty = true;
                            property.PropertyValue = syndicationLink.Uri.ToString();
                            property.PropertyValueName = "See all related " + propertyName;
                            properties.Add(property);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return properties;
        }
Ejemplo n.º 2
0
        public async Task<List<Resource>> GetResources(Uri collectionUri, ResourceType type)
        {
            var entries = DataCache.Instance.Lookup<SyndicationFeed>(collectionUri.AbsoluteUri);
            if (entries == null)
            {
                AtomPubClient client = new AtomPubClient();
                entries = await client.RetrieveFeedAsync(collectionUri);
                DataCache.Instance.Cache(collectionUri.AbsoluteUri, entries);                    
            }

            var resources = new List<Resource>();
            var resourceIndex = new Dictionary<string, Resource>();
            foreach (var entry in entries.Items)
            {
                var resource = new Resource(this, type);
                resource.Identity = new Uri(entry.Id);
                resource.Title = entry.Title.Text;
                resources.Add(resource);
            }

            // check for a link to more entries            
            var nextLink = entries.Links.FirstOrDefault(l => l.Relationship.Equals("next"));
            if (nextLink != null)
            {
                var nextResourceType = new ResourceType(this);
                nextResourceType.Identity = new Uri("http://www.brightstardb.com/databrowser/core/next");

                var nextResource = new Resource(this, nextResourceType);
                nextResource.Identity = nextLink.Uri;
                nextResource.Title = "More...";

                resources.Add(nextResource);
            }

            return resources;
        }