Example #1
0
        /// <summary>
        /// Generate a new ID for an new created entity.
        /// </summary>
        /// <param name="keyPropertyType">The type of key property.</param>
        /// <param name="refID">The reference ID.
        /// Note: Generally, this ID is the largest one in the entity-set.</param>
        /// <returns>Return a new ID.</returns>
        private object GenerateEntityID(string keyPropertyType, object refID = null)
        {
            if (!KeyPropertyTypes.Contains(keyPropertyType))
            {
                return(null);
            }

            if ("Edm.Guid" != keyPropertyType && null == refID)
            {
                return(null);
            }

            object result = null;

            switch (keyPropertyType)
            {
            case "Edm.Int16":
                result = Convert.ToInt16(refID) + keyIncrement;
                break;

            case "Edm.Int32":
                result = Convert.ToInt32(refID) + keyIncrement;
                break;

            case "Edm.Int64":
                result = Convert.ToInt64(refID) + keyIncrement;
                break;

            case "Edm.Guid":
                result = Guid.NewGuid();
                break;

            case "Edm.String":
                result = string.Format(RefID, Guid.NewGuid().ToString());
                break;

            default:
                break;
            }

            keyIncrement++;

            return(result);
        }
        /// <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);
        }