Пример #1
0
        private static List <ResourceInstanceProperty> CloneObjectToResourceInstanceProperties(IEnumerable <ResourceProperty> properties, object o)
        {
            List <ResourceInstanceProperty> instanceProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty resProperty in properties)
            {
                if (resProperty.IsNavigation)
                {
                    continue;
                }
                object propertyObject = o.GetType().InvokeMember(resProperty.Name, System.Reflection.BindingFlags.GetProperty, null, o, new object[] { });
                ResourceInstanceProperty resourceInstanceProperty = null;
                if (resProperty.Type is ComplexType)
                {
                    ComplexResourceInstance complexInstance = CloneObjectToComplexResourceInstance(resProperty.Type as ComplexType, propertyObject);
                    resourceInstanceProperty = new ResourceInstanceComplexProperty(resProperty.Type.Name, resProperty.Name, complexInstance);
                }
                else
                {
                    resourceInstanceProperty = new ResourceInstanceSimpleProperty(resProperty.Name, new NodeValue(propertyObject, resProperty.Type));
                }
                instanceProperties.Add(resourceInstanceProperty);
            }
            return(instanceProperties);
        }
Пример #2
0
        public void VisitResourceInstanceComplexProperty(ResourceInstanceComplexProperty complexPropertyNode, XmlElement parentNode)
        {
            XmlElement propertyNodeElement = CreateComplexPropertyNode(complexPropertyNode);

            if (complexPropertyNode.ComplexResourceInstance != null)
            {
                foreach (ResourceInstanceSimpleProperty simpleProperty in complexPropertyNode.ComplexResourceInstance.Properties.OfType <ResourceInstanceSimpleProperty>())
                {
                    VisitResourceInstanceSimpleProperty(simpleProperty, propertyNodeElement);
                }
                foreach (ResourceInstanceComplexProperty complexProperty in complexPropertyNode.ComplexResourceInstance.Properties.OfType <ResourceInstanceComplexProperty>())
                {
                    VisitResourceInstanceComplexProperty(complexProperty, propertyNodeElement);
                }
            }

            if (parentNode == null)
            {
                AddNamespacesToTopElement(propertyNodeElement);
                document.AppendChild(propertyNodeElement);
            }
            else
            {
                parentNode.AppendChild(propertyNodeElement);
            }
        }
Пример #3
0
        private XmlElement CreateComplexPropertyNode(ResourceInstanceComplexProperty complexPropertyNode)
        {
            XmlElement propertyNodeElement = CreateDataWebElement(complexPropertyNode.Name);

            if (complexPropertyNode.IncludeTypeMetadataHint)
            {
                XmlAttribute typeAttribute = CreateDataMetadataAttribute("type");
                typeAttribute.Value = this.Workspace.ContextNamespace + "." + complexPropertyNode.TypeName;
                propertyNodeElement.Attributes.Append(typeAttribute);
            }

            if (complexPropertyNode.ComplexResourceInstance == null)
            {
                XmlAttribute isnullAttribute = CreateDataWebMetadataAttribute("null");
                isnullAttribute.Value = "true";
                propertyNodeElement.Attributes.Append(isnullAttribute);
            }
            return(propertyNodeElement);
        }
Пример #4
0
        public ResourceInstanceProperty CreateRandomResourceInstanceProperty()
        {
            ResourceInstanceProperty instanceProperty;

            if (this.IsComplexType)
            {
                ComplexType ct = (ComplexType)this.Type;
                if (ct.Facets.AbstractType)
                {
                    ct = ct.DerivedTypes.Where(t => !t.Facets.AbstractType).Choose();
                }

                ComplexResourceInstance cri = ResourceInstanceUtil.CreateComplexResourceInstance(ct);
                return(instanceProperty = new ResourceInstanceComplexProperty(ct.Name, this.Name, cri));
            }
            else
            {
                return(this.CreateRandomResourceSimpleInstanceProperty());
            }
        }
Пример #5
0
        public static ComplexResourceInstance CreateComplexResourceInstance(ComplexType type)
        {
            List <ResourceInstanceProperty> instanceProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty childProperty in type.Properties)
            {
                ResourceInstanceProperty resourceInstanceProperty = null;
                if (childProperty.IsComplexType)
                {
                    resourceInstanceProperty = new ResourceInstanceComplexProperty(childProperty.Type.Name, childProperty.Name, CreateComplexResourceInstance((ComplexType)childProperty.Type));
                }
                else
                {
                    NodeValue nodeValue = Resource.CreateValue(childProperty);
                    resourceInstanceProperty = new ResourceInstanceSimpleProperty(childProperty.Name, nodeValue);
                }
                instanceProperties.Add(resourceInstanceProperty);
            }
            ComplexResourceInstance complexInstance = new ComplexResourceInstance(type.Name, instanceProperties.ToArray());

            return(complexInstance);
        }
Пример #6
0
        protected virtual string Visit(ExpNode caller, ExpNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node is KeyedResourceInstance && (!(node is AssociationResourceInstance)))
            {
                KeyedResourceInstance e = (KeyedResourceInstance)node;
                _resourceType = this.GetResourceType(e);

                _typeStack.Push(_resourceType);

                string properties = null;
                if (this.RequestVerb == RequestVerb.Post &&
                    e.ResourceInstanceKey != null)
                {
                    foreach (ResourceInstanceProperty resourceProperty in e.ResourceInstanceKey.KeyProperties)
                    {
                        if (properties != null)
                        {
                            properties += "," + this.Visit(e, resourceProperty);
                        }
                        else
                        {
                            properties = this.Visit(e, resourceProperty);
                        }
                    }
                }
                foreach (ResourceInstanceProperty resourceProperty in e.Properties)
                {
                    if (properties != null)
                    {
                        properties += "," + this.Visit(e, resourceProperty);
                    }
                    else
                    {
                        properties = this.Visit(e, resourceProperty);
                    }
                }

                _resourceType = _typeStack.Pop();


                string typeAttribute = null;

                //if( Workspace.DataLayerProviderKind == DataLayerProviderKind.NonClr )
                //    typeAttribute = String.Format("type:\"{0}\" ", e.ResourceTypeName);
                //else
                typeAttribute = String.Format("type:\"{0}.{1}\" ", this.Workspace.ContextNamespace, e.TypeName);

                string uriAttribute   = null;
                string metadataString = null;
                if (e.ResourceInstanceKey != null && this.RequestVerb != RequestVerb.Post)
                {
                    uriAttribute = String.Format("uri:\"{0}\"", CreateCanonicalUri(e.ResourceInstanceKey));
                }

                if (uriAttribute != null)
                {
                    if (e.IncludeTypeMetadataHint)
                    {
                        metadataString = String.Format("__metadata: {{ {0}, {1}}}", typeAttribute, uriAttribute);
                    }
                    else
                    {
                        metadataString = String.Format("__metadata: {{ {0} }}", uriAttribute);
                    }
                }
                else
                {
                    if (e.IncludeTypeMetadataHint)
                    {
                        metadataString = String.Format("__metadata: {{ {0} }}", typeAttribute);
                    }
                }

                //if (uriAttribute != null && e.IncludeTypeMetadataHint)
                //    metadataString = String.Format("__metadata: {{ {0}, {1}}}", typeAttribute, uriAttribute);
                //else if (e.IncludeTypeMetadataHint && uriAttribute == null)
                //    metadataString = String.Format("__metadata: {{ {0} }}", typeAttribute);

                string payload = null;
                if (properties == null)
                {
                    payload = ApplyErrors(node, String.Format("{{{0}}}", metadataString));
                }
                else
                if (metadataString != null)
                {
                    payload = ApplyErrors(node, String.Format("{{{0}, {1}}}", metadataString, properties));
                }
                else
                {
                    payload = ApplyErrors(node, String.Format("{{{0}}}", properties));
                }
                return(payload);
            }
            else if (node is AssociationResourceInstance)
            {
                AssociationResourceInstance e = (AssociationResourceInstance)node;

                if (e.Operation == AssociationOperation.Add)
                {
                    string uri     = CreateCanonicalUri(e.ResourceInstanceKey);
                    string payload = null;
                    if (caller == null)
                    {
                        payload = "{ uri:\"" + uri + "\"}";
                    }
                    else if (e.IncludeTypeInBind)
                    {
                        payload = String.Format("__metadata: {{ uri:\"{0}\", type:\"{1}.{2}\"}}", uri, this.Workspace.ContextNamespace, e.ResourceInstanceKey.ResourceTypeName);
                    }
                    else
                    {
                        payload = String.Format("__metadata: {{ uri:\"{0}\"}}", uri);
                    }
                    return(ApplyErrors(node, payload));
                }
                else
                {
                    string navString = null;
                    if (caller != null)
                    {
                        navString = e.Name + ": null";
                    }
                    else
                    {
                        navString = "{ uri: null }";
                    }
                    return(ApplyErrors(node, navString));
                }
            }

            /*
             * else if (node is ResourceInstanceKey)
             * {
             * ResourceInstanceKey e = (ResourceInstanceKey)node;
             * if (RequestVerb == RequestVerb.Post)
             * {
             *  string payload = String.Format("__metadata: {{ type:\"{0}.{1}\" }}", this.Workspace.ContextNamespace, e.ResourceTypeName);
             *  if (e.KeyProperties != null)
             *  {
             *      foreach (ResourceInstanceProperty resourceProperty in e.KeyProperties)
             *      {
             *          payload += String.Format(", {0}", this.Visit(e, resourceProperty));
             *      }
             *  }
             *  return ApplyErrors(node, payload);
             * }
             * else if (RequestVerb == RequestVerb.Put)
             * {
             *  string keyValues = WriteCommaDelimitedKeyValues(e);
             *
             *  string payloadUri = String.Format("/{0}({1})", e.ResourceSetName, keyValues);
             *  return ApplyErrors(node, String.Format("__metadata: {{uri:\"{0}\" }}", payloadUri));
             * }
             * throw new ArgumentException("Request Verb is incorrect can't build an update payload:" + this.RequestVerb.ToString());
             * }*/
            else if (node is ResourceInstanceSimpleProperty)
            {
                ResourceInstanceSimpleProperty e = (ResourceInstanceSimpleProperty)node;
                Type   clrType = e.ClrType;
                object val     = e.PropertyValue;

                if (val != null)
                {
                    clrType = val.GetType();
                }

                if (e.CreateDollarValue)
                {
                    if (val == null)
                    {
                        return(null);
                    }
                    else if (clrType == typeof(byte[]))
                    {
                        return((new System.Text.UTF8Encoding()).GetString((byte[])val));
                    }
                    else
                    {
                        return(AstoriaUnitTests.Data.TypeData.XmlValueFromObject(val));
                    }
                }

                string jsonStringValue;

                if (clrType == typeof(DateTime) && val is DateTime)
                {
                    if (e.UseTickCountForJsonDateTime)
                    {
                        jsonStringValue = "'" + JsonPrimitiveTypesUtil.GetJsonDateTimeStringValue((DateTime)val) + "'";
                    }
                    else
                    {
                        jsonStringValue = JsonPrimitiveTypesUtil.DateTimeToString(val);
                    }
                }
                else
                {
                    jsonStringValue = JsonPrimitiveTypesUtil.PrimitiveToString(val, clrType);

                    if (clrType == typeof(double) || clrType == typeof(float))
                    {
                        // PrimitiveToString will lose the trailing .0 if its a whole number
                        long temp;
                        if (long.TryParse(jsonStringValue, out temp))
                        {
                            jsonStringValue += ".0";
                        }
                    }
                }

                if (caller == null)
                {
                    return("{" + ApplyErrors(node, String.Format("{0}: {1}", e.Name, jsonStringValue)) + "}");
                }
                else
                {
                    return(ApplyErrors(node, String.Format("{0}: {1}", e.Name, jsonStringValue)));
                }
            }
            else if (node is ResourceInstanceComplexProperty)
            {
                ResourceInstanceComplexProperty e = (ResourceInstanceComplexProperty)node;
                string properties = null;

                if (e.IncludeTypeMetadataHint)
                {
                    properties += String.Format("__metadata: {{ type:\"{0}.{1}\" }}", this.Workspace.ContextNamespace, e.TypeName);
                }

                foreach (ResourceInstanceProperty resourceProperty in e.ComplexResourceInstance.Properties)
                {
                    if (string.IsNullOrEmpty(properties))
                    {
                        properties += this.Visit(e, resourceProperty);
                    }
                    else
                    {
                        properties += "," + this.Visit(e, resourceProperty);
                    }
                }
                string results = null;
                if (caller == null)
                {
                    results = "{" + e.Name + ": {" + properties + "}" + "}";
                }
                else
                {
                    results = e.Name + ": {" + properties + "}";
                }
                return(ApplyErrors(node, results));
            }
            else if (node is ResourceInstanceNavRefProperty)
            {
                ResourceInstanceNavRefProperty e = (ResourceInstanceNavRefProperty)node;
                AssociationResourceInstance    associationResourceInstance = e.TreeNode as AssociationResourceInstance;
                string navString = null;
                if ((associationResourceInstance != null && associationResourceInstance.Operation != AssociationOperation.Remove))
                {
                    navString  = e.Name + ": {";
                    navString += this.Visit(e, e.TreeNode);
                    navString += "}";
                }
                else if (associationResourceInstance != null && associationResourceInstance.Operation == AssociationOperation.Remove)
                {
                    if (caller != null)
                    {
                        navString = e.Name + ": null";
                    }
                    else
                    {
                        navString = "null";
                    }
                }
                else
                {
                    navString  = e.Name + ": ";
                    navString += this.Visit(e, e.TreeNode);
                }
                return(ApplyErrors(node, navString));
            }
            else if (node is ResourceInstanceNavColProperty)
            {
                ResourceInstanceNavColProperty e = (ResourceInstanceNavColProperty)node;

                //string navString = String.Format("{0}: {", e.Name);
                string navString = e.Name + ": [";
                foreach (NamedNode namedNode in e.Collection.NodeList)
                {
                    if (!(namedNode is AssociationResourceInstance))
                    {
                        navString += this.Visit(e, namedNode) + ",";
                    }
                    else
                    {
                        navString += "{" + this.Visit(e, namedNode) + "},";
                    }
                }
                navString  = navString.TrimEnd(',');
                navString += "]";

                return(ApplyErrors(node, navString));
            }

            else
            {
                throw new Exception("Unknown node type: " + node.GetType());
            }
        }
Пример #7
0
        private void CreateEntryElement(KeyedResourceInstance keyedResourceInstance, XmlNode parentNode)
        {
            currentResource = keyedResourceInstance;
            XmlElement entryElement = CreateBasicEntryElement(keyedResourceInstance, parentNode);

            //string relativeParentKey = null;
            //Add the Id if there is one

            if (this.RequestVerb != RequestVerb.Post)
            {
                if (keyedResourceInstance.ResourceInstanceKey != null)
                {
                    XmlElement idNode = CreateIdElement(keyedResourceInstance);
                    entryElement.AppendChild(idNode);
                }
            }


            ResourceType type = Workspace.ServiceContainer.ResourceTypes.Single(rt => rt.Name == keyedResourceInstance.TypeName);

            XmlElement propertiesNode = CreateDataWebMetadataElement("properties");

            IEnumerable <ResourceInstanceProperty> properties = keyedResourceInstance.Properties;

            if (this.RequestVerb == RequestVerb.Post && keyedResourceInstance.ResourceInstanceKey != null)
            {
                properties = keyedResourceInstance.ResourceInstanceKey.KeyProperties.Union(properties);
            }

            foreach (ResourceInstanceProperty property in properties)
            {
                if (property is ResourceInstanceSimpleProperty)
                {
                    ResourceInstanceSimpleProperty simpleResourceProperty = property as ResourceInstanceSimpleProperty;
                    VisitResourceInstanceSimpleProperty(simpleResourceProperty, propertiesNode);
                }
                else if (property is ResourceInstanceComplexProperty)
                {
                    ResourceInstanceComplexProperty complexResourceProperty = property as ResourceInstanceComplexProperty;

                    if (complexResourceProperty.ComplexResourceInstance == null)
                    {
                        VisitResourceInstanceComplexProperty(complexResourceProperty, propertiesNode);
                    }
                    else
                    {
                        VisitResourceInstanceComplexProperty(complexResourceProperty, propertiesNode);
                    }
                }
                else if (property is ResourceInstanceNavProperty)
                {
                    ResourceInstanceNavProperty navigationProperty = property as ResourceInstanceNavProperty;
                    VisitResourceNavigationProperty(navigationProperty, entryElement);
                }
            }

            if (propertiesNode.ChildNodes.Count > 0)
            {
                if (!type.Facets.HasStream)
                {
                    XmlElement   contentNode          = CreateAtomElement("content");
                    XmlAttribute contentTypeAttribute = CreateAtomAttribute("type");
                    contentTypeAttribute.Value = RequestUtil.RandomizeContentTypeCapitalization("application/xml");
                    contentNode.Attributes.Append(contentTypeAttribute);
                    contentNode.AppendChild(propertiesNode);
                    entryElement.AppendChild(contentNode);
                }
                else
                {
                    entryElement.AppendChild(propertiesNode);
                }
            }

            if (EntryElementCallback != null)
            {
                EntryElementCallback(entryElement);
            }
        }