Esempio n. 1
0
        /// <summary>
        /// Registers a 'member' entry for the relationship.
        /// </summary>
        private void RegisterRelationshipMember(JsonQueryResult result, EntityPackageContext entityPackageContext, RelationshipData rel)
        {
            var  members = result.Members;
            long relId   = rel.RelationshipTypeId.Id;

            // Determine if there is an existing member registered for this relationship.
            // And create one if necessary.
            JsonMember member;

            if (!members.TryGetValue(relId, out member))
            {
                member = new JsonMember();
                members.Add(relId, member);
            }

            // Then we ensure there is information for the member in the particular direction that we're interested in.
            JsonRelationshipMember relMember = rel.IsReverseActual ? member.Reverse : member.Forward;

            if (relMember == null)
            {
                relMember          = new JsonRelationshipMember();
                relMember.Alias    = GetAliasString(rel.RelationshipTypeId);
                relMember.IsLookup = rel.IsLookup;
                if (rel.IsReverseActual)
                {
                    member.Reverse = relMember;
                }
                else
                {
                    member.Forward = relMember;
                }
            }
            else if (relMember.Alias == null && rel.RelationshipTypeId.Alias != null)
            {
                relMember.Alias = GetAliasString(rel.RelationshipTypeId);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Store fields into the JSON entity structure.
        /// </summary>
        /// <param name="entityData">The entity being read.</param>
        /// <param name="entityPackageContext"></param>
        /// <param name="entityJson">JSON for holding member data for the individual entity.</param>
        /// <returns></returns>
        private void AppendFieldsToEntity(JsonQueryResult result, EntityData entityData, EntityPackageContext entityPackageContext,
                                          Dictionary <string, object> entityJson)
        {
            var members = result.Members;

            // Visit fields
            foreach (var field in entityData.Fields)
            {
                long fieldId = field.FieldId.Id;
                // Register the field member
                JsonMember member;
                if (members.TryGetValue(fieldId, out member))
                {
                    if (field.FieldId.Alias != null && member.Alias == null)
                    {
                        member.Alias = GetAliasString(field.FieldId);
                    }
                }
                else
                {
                    member          = new JsonMember();
                    member.Alias    = GetAliasString(field.FieldId);
                    member.DataType = DataTypeHelper.FromDatabaseType(field.Value.Type).ToString();
                    members.Add(fieldId, member);
                }

                object value = field.Value.Value;

                // Ensure empty strings are represented as nulls - this is by design!
                if (value is string && ((string)value).Length == 0)
                {
                    value = null;
                }

                // Store the data in the entity
                entityJson[fieldId.ToString(CultureInfo.InvariantCulture)] = value;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Store relationships into the JSON entity structure.
        /// </summary>
        /// <param name="entityData">The entity being read.</param>
        /// <param name="entityPackageContext"></param>
        /// <param name="entityJson">JSON for holding member data for the individual entity.</param>
        /// <returns></returns>
        private void AppendRelationshipsToEntity(JsonQueryResult result, EntityData entityData, EntityPackageContext entityPackageContext,
                                                 Dictionary <string, object> entityJson)
        {
            // Visit relationships
            foreach (var rel in entityData.Relationships)
            {
                // Register the relationship member metadata
                // Note: we need to first see if there is a container for the relationship in either direction
                RegisterRelationshipMember(result, entityPackageContext, rel);

                // Get relationship data.
                // Holds a mixture of either long or JsonRelationshipInstance
                var relResult = new List <object>();
                foreach (var relInst in rel.Instances)
                {
                    // If we have a relationship instance, send a complex object
                    // Otherwise just send the ID of the entity we're relating to.
                    if (relInst.RelationshipInstanceEntity != null)
                    {
                        var relInstResult = new JsonRelationshipInstance();
                        if (relInst.Entity != null)
                        {
                            relInstResult.EntityId = relInst.Entity.Id.Id;
                        }
                        relInstResult.RelationshipInstance = relInst.RelationshipInstanceEntity.Id.Id;
                        relResult.Add(relInstResult);
                        // Recursively visit the relationship instance entity
                        AppendEntityToResult(result, relInst.RelationshipInstanceEntity, entityPackageContext);
                    }
                    else if (relInst.Entity != null)
                    {
                        long relatedId = relInst.Entity.Id.Id;
                        relResult.Add(relatedId);
                    }

                    // Recursively visit the related entity
                    AppendEntityToResult(result, relInst.Entity, entityPackageContext);
                }

                // Store the data in the entity
                StoreRelationshipData(entityJson, rel, relResult);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Store an entity into the JSON result structure.
        /// </summary>
        private void AppendEntityToResult(JsonQueryResult result, EntityData entityData, EntityPackageContext entityPackageContext)
        {
            long entityKey = entityData.Id.Id;

            // Avoid circular loops within a query.
            if (entityPackageContext.EntitiesVisited.Contains(entityKey))
            {
                return;
            }
            entityPackageContext.EntitiesVisited.Add(entityKey);

            Dictionary <string, object> entityJson;

            if (!result.Entities.TryGetValue(entityKey, out entityJson))
            {
                entityJson = new Dictionary <string, object>();
                result.Entities.Add(entityKey, entityJson);
            }

            AppendFieldsToEntity(result, entityData, entityPackageContext, entityJson);

            AppendRelationshipsToEntity(result, entityData, entityPackageContext, entityJson);

            if (!entityJson.Any())
            {
                // Note: always add, then remove if not needed. Otherwise it's not present in the dictionary when we recurse,
                // which can lead to infinite loops.
                result.Entities.Remove(entityKey);
            }
        }