Esempio n. 1
0
 /// <summary>
 /// Constructor. 
 /// </summary>
 /// <param name="name">Name of the resource. </param>
 /// <param name="isPropertyResource">If true, this resource is represented by a property. </param>
 public Resource(string name, bool isPropertyResource, Resource owner)
 {
     this.Name = name;
     Storage = new Dictionary<string, List<Entry>>();
     Resources = new List<Resource>();
     IsPropertyResource = isPropertyResource;
     Owner = owner;
 }
 public void ResourceSanityValid()
 {
     var resource = new Resource("Pet");
     Assert.AreEqual("Pet", resource.Name);
 }
 public void ResourceSanityNull()
 {
     var resource = new Resource(null);
     Assert.IsNull(resource.Name);
 }
 public void ResourceSanityEmpty()
 {
     var resource = new Resource("");
     Assert.AreEqual("", resource.Name);
 }
 public void ResourceSanity()
 {
     var resource = new Resource(null);
     Assert.AreEqual(0, resource.Data().Count);
 }
Esempio n. 6
0
        /// <summary>
        /// Add an entry to the database for the given path and pattern. That data will be validated as Json. 
        /// </summary>
        /// <param name="path">The path. ie: /Pet, /Pet/Dog</param>
        /// <param name="pattern">ie: /Pet, /Pet/{Kind}</param>
        /// <param name="propertyName">The property name (and its value) to use as the unique identifier in the object. Both the property name and contents are case sensitive. </param>
        /// <param name="data">The data to add. Can be null. </param>
        /// <param name="discriminator">Optional discriminator if multiple databases are allowed (ie: via the header)</param>
        /// <returns>true - Added; false - not added. </returns>
        public bool AddJson(string path, string pattern, string propertyName, string data, byte[] binaryContent, string discriminator)
        {
            if (discriminator == null) discriminator = "";

            var tokens = RouteParser.Parse(path, pattern).ToArray();
            if (tokens.Count() == 0) return false;

            JObject jobject = JsonConvert.DeserializeObject(data) as JObject;

            // 1. We need to 'walk' the path (and therefore the database) until we get to the Resource that will hold our value.
            //    As we walk the path, we create the resources we need to store the data as we go.
            // 2. Add the entry to the database.

            // 1.
            List<Resource> currentResourceList = GetResources(discriminator);
            Resource resource = null;
            string propertyValue = null;
            JToken jobjectPropertyValue = null;
            Resource owner = null;
            for (int offset = 0; offset < tokens.Length; offset++)
            {
                propertyValue = null;
                jobjectPropertyValue = null;

                var token = tokens[offset];
                if (token.Kind == RouteTokenKind.Resource)
                {
                    resource = currentResourceList.FirstOrDefault(f => string.Compare(token.Name, f.Name, false) == 0 && !f.IsPropertyResource);
                    if (resource == null)
                    {
                        resource = new Resource(token.Name, false, owner);
                        currentResourceList.Add(resource);
                    }
                    owner = resource;
                    currentResourceList = resource.Resources;
                    continue;
                }
                if (token.Kind == RouteTokenKind.Property)
                {
                    if (offset == tokens.Length - 1)
                    {
                        // The property at the end of the path is assumed to reference an existing object; so we are finished "walking"
                        continue;

                    }

                    var existingPropertyResourse = resource.Resources.FirstOrDefault(f => string.Compare(f.Name, token.Value, false) == 0 && f.IsPropertyResource);
                    if (existingPropertyResourse == null)
                    {
                        var nr = new Resource(token.Value, true, owner);
                        resource.Resources.Add(nr);

                        var jsonRaw = JsonConvert.DeserializeObject("{}") as JObject;
                        jsonRaw[token.Name] = token.Value;
                        var json = JsonConvert.SerializeObject(jsonRaw);

                        nr.Data().Add(new Entry() { Json = json, Bytes = binaryContent });

                        currentResourceList = resource.Resources.Last().Resources;
                    }
                    else
                    {
                        currentResourceList = existingPropertyResourse.Resources;
                    }
                }
            }

            if (null == resource)
            {
                return false;
            }

            // ASSERTION: We have found the resource we need to add the entry.
            if (propertyName != null)
            {
                jobjectPropertyValue = jobject[propertyName];
            }
            if (jobjectPropertyValue != null)
            {
                propertyValue = jobjectPropertyValue.ToString();
            }

            var existingPropertyIndex = FindIndexOf(resource, propertyName, propertyValue, discriminator);
            if (existingPropertyIndex != -1)
            {
                resource.Resources.RemoveAt(existingPropertyIndex);
            }

            var existingPropertyResourse2 = resource.Resources.FirstOrDefault(f => string.Compare(f.Name, propertyValue, false) == 0 && f.IsPropertyResource);
            if (existingPropertyResourse2 != null)
            {
                resource.Resources.Remove(existingPropertyResourse2);
            }

            var newResource = new Resource(propertyValue, true, owner);
            newResource.Data().Add(new Entry() { Json = data, Bytes = binaryContent });
            resource.Resources.Add(newResource);

            return true;
        }
Esempio n. 7
0
        /// <summary>
        /// Returns the 0-based index of the data in the resource where propertyName is equal to value (case sensitive). 
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="discriminator"></param>
        /// <returns></returns>
        protected int FindIndexOf(Resource resource, string propertyName, string value, string discriminator)
        {
            if (null == discriminator) discriminator = "";
            if (null == propertyName) return -1;

            foreach (var r in resource.Resources)
            {
                foreach (var d in r.Data())
                {
                    JObject jobject = JsonConvert.DeserializeObject(d.Json) as JObject;
                    if (null == jobject) continue;

                    var currentValue = jobject[propertyName];
                    if (currentValue != null && currentValue.ToString() == value)
                    {
                        return resource.Resources.IndexOf(r);
                    }
                    if (currentValue == null && value == null)
                    {
                        return resource.Resources.IndexOf(r);
                    }
                }
            }

            return -1;
        }
Esempio n. 8
0
 protected int FindIndexOf(Resource resource, string propertyName, string value)
 {
     return FindIndexOf(resource, propertyName, value, null);
 }