public void ResourceSanity()
 {
     var resource = new Resource(null);
     Assert.AreEqual(0, resource.Data().Count);
 }
예제 #2
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;
        }