コード例 #1
0
        /// <summary>
        /// Get the template of an specified entity.
        /// </summary>
        /// <param name="entitySetName">The entity-type short name.</param>
        /// <param name="actualEntityTypeShortName">The actual entity-type short name.</param>
        /// <returns>Returns the entity template.</returns>
        private JObject GetEntityDataTemplate(string entitySetName, string actualEntityTypeShortName = null)
        {
            if (string.IsNullOrEmpty(entitySetName) && !entitySetName.IsSpecifiedEntitySetNameExist())
            {
                return(null);
            }

            // Map 'entity-set name' to 'entity-type short name'.
            string entityTypeShortName = entitySetName.MapEntitySetNameToEntityTypeShortName();
            string targetShortName     = string.IsNullOrEmpty(actualEntityTypeShortName) ? entityTypeShortName : actualEntityTypeShortName;

            if (string.IsNullOrEmpty(entityTypeShortName))
            {
                throw new Exception("Failed to convert entity-set name to entity-type short name.");
            }

            var     keyProperties = MetadataHelper.GetKeyProperties(this.metadataDoc, entityTypeShortName);
            JObject entity        = null;

            // The multiple-key entities are very complex to construct, so the program only filter the single-key entities.
            if (null != keyProperties && 1 == keyProperties.Count())
            {
                var keyProperty = keyProperties.First();

                if (!KeyPropertyTypes.Contains(keyProperty.PropertyType))
                {
                    return(entity);
                }

                // Convert 'entity-set name' to 'entity-set URL'.
                string entitySetURL = entitySetName.MapEntitySetNameToEntitySetURL();

                if (string.IsNullOrEmpty(entitySetURL))
                {
                    throw new Exception("Failed to convert to entity-set name to entity-set URL.");
                }

                string url  = string.Format("{0}/{1}", this.rootURL.TrimEnd('/'), entitySetURL);
                var    resp = WebHelper.Get(new Uri(url), Constants.V4AcceptHeaderJsonFullMetadata, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, null);

                if (null != resp && HttpStatusCode.OK == resp.StatusCode)
                {
                    JObject feed;
                    resp.ResponsePayload.TryToJObject(out feed);
                    if (null != feed)
                    {
                        var entries = JsonParserHelper.GetEntries(feed);

                        if (null != entries && entries.Any())
                        {
                            // If the current entity-type is a derived type, the program will get the entity with derived type from the feed.
                            // Otherwise, it will get the first entity from the feed.
                            entity = !string.IsNullOrEmpty(actualEntityTypeShortName) && actualEntityTypeShortName.IsSpecifiedEntityTypeShortNameExist() ?
                                     this.GetDerivedEntity(entries, actualEntityTypeShortName) : entries.First as JObject;

                            // Set the new key value for the selected entity.
                            object keyValTemp = IntKeyPropertyTypes.Contains(keyProperty.PropertyType)
                                ? this.GetMaxEntityKey(entries, keyProperty.PropertyName)
                                : entity[keyProperty.PropertyName];
                            object keyVal = this.GenerateEntityID(keyProperty.PropertyType, keyValTemp);
                            entity[keyProperty.PropertyName] = new JValue(keyVal);
                            string pattern = "Edm.String" == keyProperty.PropertyType ? "{0}('{1}')" : "{0}({1})";
                            entity[Constants.V4OdataId] = new JValue(string.Format(pattern, url, keyVal.ToString()));

                            string serviceNamespace = this.GetNamespace(targetShortName, "EntityType");
                            entity[Constants.V4OdataType] = new JValue(string.Format("#{0}.{1}", serviceNamespace, targetShortName));
                        }
                    }
                }
            }

            return(entity);
        }