Esempio n. 1
0
        private static ResourceInstanceComplexProperty ConvertComplexPayloadObjectToComplexProperty(ResourceProperty rp, PayloadComplexProperty payloadComplexProperty)
        {
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty childProperty in (rp.Type as ComplexType).Properties.OfType <ResourceProperty>())
            {
                if (childProperty.IsComplexType)
                {
                    PayloadComplexProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadComplexProperty;
                    properties.Add(ConvertComplexPayloadObjectToComplexProperty(childProperty, fromPayload));
                }
                else
                {
                    PayloadSimpleProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadSimpleProperty;
                    if (fromPayload != null)
                    {
                        ResourceInstanceProperty newProperty = null;
                        object val = CommonPayload.DeserializeStringToObject(fromPayload.Value, childProperty.Type.ClrType, false, fromPayload.ParentObject.Format);
                        newProperty = new ResourceInstanceSimpleProperty(childProperty.Name, new NodeValue(val, childProperty.Type));
                        properties.Add(newProperty);
                    }
                }
            }
            ComplexResourceInstance complexResourceInstance = new ComplexResourceInstance(rp.Type.Name, properties.ToArray());

            return(new ResourceInstanceComplexProperty(rp.Type.Name, rp.Name, complexResourceInstance));
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public ResourceInstanceSimpleProperty CreateResourceSimpleInstanceProperty(NodeValue nodeValue)
        {
            if (this.IsNavigation == true)
            {
                throw new InvalidOperationException("Needs to be a simple property");
            }
            ResourceInstanceSimpleProperty resourceInstanceSimpleProperty = new ResourceInstanceSimpleProperty(this.Name, nodeValue);

            return(resourceInstanceSimpleProperty);
        }
Esempio n. 4
0
        internal static KeyedResourceInstance CreateKeyedResourceInstanceFromPayloadObject(ResourceContainer container, ResourceType resourceType, PayloadObject payloadObject)
        {
            List <ResourceInstanceProperty> properties    = new List <ResourceInstanceProperty>();
            List <ResourceInstanceProperty> keyProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty property in resourceType.Properties.OfType <ResourceProperty>())
            {
                if (property.IsNavigation)
                {
                    continue;
                }

                if (property.IsComplexType)
                {
                    PayloadComplexProperty fromPayload = payloadObject[property.Name] as PayloadComplexProperty;
                    properties.Add(ConvertComplexPayloadObjectToComplexProperty(property, fromPayload));
                }
                else
                {
                    string stringValue;
                    if (payloadObject.PayloadProperties.Any(p => p.Name == property.Name))
                    {
                        PayloadSimpleProperty fromPayload = payloadObject[property.Name] as PayloadSimpleProperty;
                        stringValue = fromPayload.Value;
                    }
                    else
                    {
                        if (!payloadObject.CustomEpmMappedProperties.TryGetValue(property.Name, out stringValue))
                        {
                            stringValue = null;
                        }
                    }

                    ResourceInstanceProperty newProperty = null;
                    object val = CommonPayload.DeserializeStringToObject(stringValue, property.Type.ClrType, false, payloadObject.Format);
                    newProperty = new ResourceInstanceSimpleProperty(property.Name, new NodeValue(val, property.Type));

                    if (property.PrimaryKey != null)
                    {
                        keyProperties.Add(newProperty);
                    }
                    else
                    {
                        properties.Add(newProperty);
                    }
                }
            }

            ResourceInstanceKey key = new ResourceInstanceKey(container, resourceType, keyProperties.ToArray());

            return(new KeyedResourceInstance(key, properties.ToArray()));
        }
Esempio n. 5
0
        public void VisitResourceInstanceSimpleProperty(ResourceInstanceSimpleProperty simplePropertyNode, XmlElement parentNode)
        {
            XmlElement simplePropertyElement = null;

            simplePropertyElement = CreateDataWebElement(simplePropertyNode.Name);

            if (simplePropertyNode.IncludeTypeMetadataHint)
            {
                XmlAttribute typeAttribute = CreateDataMetadataAttribute("type");
#if !ClientSKUFramework
                if (simplePropertyNode.ClrType == typeof(byte[]) || simplePropertyNode.ClrType == typeof(System.Data.Linq.Binary))
                {
                    typeAttribute.Value = "Edm.Binary";
                }
                else
#endif
                typeAttribute.Value = simplePropertyNode.ClrType.ToString().Replace("System.", "Edm.");
                simplePropertyElement.Attributes.Append(typeAttribute);
            }
            //TODO, need to do the correct serialization per type
            //TODO: What do we do if null?
            if (simplePropertyNode.PropertyValue != null)
            {
                string xmlValue = TypeData.XmlValueFromObject(simplePropertyNode.PropertyValue);
                simplePropertyElement.InnerText = xmlValue;
                if (simplePropertyNode.PropertyValue is string)
                {
                    if ((simplePropertyNode.PropertyValue as string).ToCharArray().Any(c => Char.IsWhiteSpace(c)))
                    {
                        XmlAttribute space = document.CreateAttribute("xml", "space", null);
                        space.Value = "preserve";
                        simplePropertyElement.Attributes.Append(space);
                    }
                }
            }
            else
            {
                XmlAttribute isnullAttribute = CreateDataWebMetadataAttribute("null");
                isnullAttribute.Value = "true";
                simplePropertyElement.Attributes.Append(isnullAttribute);
            }

            if (parentNode == null)
            {
                AddNamespacesToTopElement(simplePropertyElement);
                document.AppendChild(simplePropertyElement);
            }
            else
            {
                parentNode.AppendChild(simplePropertyElement);
            }
        }
Esempio n. 6
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);
        }
Esempio n. 7
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());
            }
        }
Esempio n. 8
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);
            }
        }