Exemple #1
0
        /// <summary>
        /// Map entity-set URL to entity-set name.
        /// </summary>
        /// <param name="entitySetURL">An entity-set URL.</param>
        /// <returns>Returns the related entity-set name.</returns>
        public static string MapEntitySetURLToEntitySetName(this string entitySetURL)
        {
            if (string.IsNullOrEmpty(entitySetURL))
            {
                throw new ArgumentNullException(string.Format(ParamNullErrorMsgPattern, "entitySetURL"));
            }

            if (!IsSpecifiedEntitySetURLExist(entitySetURL))
            {
                throw new ArgumentException(string.Format(ParamNotFoundErrorMsgPattern, "entity-set", "URL", "entitySetURL"));
            }

            JObject serviceRoot = JObject.Parse(ServiceStatus.GetInstance().ServiceDocument);
            var     entries     = JsonParserHelper.GetEntries(serviceRoot);

            if (null != entries)
            {
                foreach (var entry in entries)
                {
                    if (null != entry["url"] && entitySetURL == entry["url"].Value <string>() &&
                        null != entry["kind"] && "EntitySet" == entry["kind"].Value <string>())
                    {
                        return(null != entry["name"] ? entry["name"].Value <string>() : string.Empty);
                    }
                }
            }

            return(string.Empty);
        }
Exemple #2
0
        /// <summary>
        /// Verify whether an entity-set URL is existent in the service.
        /// </summary>
        /// <param name="entitySetURL">An entity-set URL.</param>
        /// <returns>Returns true if exist, otherwise false.</returns>
        public static bool IsSpecifiedEntitySetURLExist(this string entitySetURL)
        {
            if (string.IsNullOrEmpty(entitySetURL))
            {
                return(false);
            }

            JObject serviceRoot = JObject.Parse(ServiceStatus.GetInstance().ServiceDocument);
            var     entries     = JsonParserHelper.GetEntries(serviceRoot);

            if (null == entries || !entries.Any())
            {
                return(false);
            }

            var entry = entries
                        .Where(e => "EntitySet" == e["kind"].Value <string>() && entitySetURL == e["url"].Value <string>())
                        .Select(e => e);

            try
            {
                return(null != entry && entry.Any());
            }
            catch
            {
                return(false);
            }
        }
        /// <summary>
        /// Whether specified Annoatation exist.
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="specifiedAnnoatation">The specified Annoatation</param>
        /// <returns>true if exist; false otherwise</returns>
        public static bool IsSpecifiedAnnotationExist(ServiceContext context, string specifiedAnnoatation)
        {
            JObject allobject;

            context.ResponsePayload.TryToJObject(out allobject);
            bool isExist = false;

            // If PayloadType is Feed, verify as below.
            if (context.PayloadType.Equals(RuleEngine.PayloadType.Feed))
            {
                var entries = JsonParserHelper.GetEntries(allobject);
                foreach (JObject entry in entries)
                {
                    var jProps = entry.Children();

                    foreach (JProperty jProp in jProps)
                    {
                        // Whether specified Annoatation exist in response.
                        if (jProp.Name.Equals(specifiedAnnoatation))
                        {
                            isExist = true;
                            break;
                        }
                        else
                        {
                            isExist = false;
                        }
                    }

                    if (isExist)
                    {
                        break;
                    }
                }
            }
            else
            {
                var jProps = allobject.Children();

                foreach (JProperty jProp in jProps)
                {
                    // Whether specified Annoatation exist in response.
                    if (jProp.Name.Equals(specifiedAnnoatation))
                    {
                        isExist = true;
                        break;
                    }
                    else
                    {
                        isExist = false;
                    }
                }
            }

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