/// <summary> /// Gets the entity. /// </summary> /// <param name="entityRef">The entity reference.</param> /// <returns></returns> public JsonEntity GetEntity(EntityRef entityRef) { JsonEntity jsonEntity = Entities.FirstOrDefault(p => p.Id == entityRef.Id); if (jsonEntity == null) { jsonEntity = new JsonEntity(entityRef, this); Entities.Add(jsonEntity); } return(jsonEntity); }
/// <summary> /// Initializes a new instance of the <see cref="JsonEntityData" /> class. /// </summary> /// <param name="entityData">The entity data.</param> /// <param name="context">The context.</param> public JsonEntityData(EntityData entityData, JsonEntityQueryResult context) { Id = 0; if (entityData == null) { return; } Id = entityData.Id.Id; JsonEntity jsonEntity = context.GetEntity(entityData.Id); jsonEntity.DataState = entityData.DataState; IEnumerable <EntityRef> newTypeIds = entityData.TypeIds.Where(p => !jsonEntity.TypeIds.Contains(p.Id)); jsonEntity.TypeIds.AddRange(newTypeIds.Select(p => context.GetEntityRef(p).Id)); foreach (FieldData f in entityData.Fields) { JsonFieldData existingField = jsonEntity.Fields.FirstOrDefault(p => p.FieldId == f.FieldId.Id); if (existingField == null) { var jsonField = new JsonFieldData(f, context); jsonEntity.Fields.Add(jsonField); } } foreach (RelationshipData r in entityData.Relationships) { JsonRelationshipData existing = jsonEntity.Relationships.FirstOrDefault(p => p.RelTypeId.Id == r.RelationshipTypeId.Id && p.RelTypeId.Alias == r.RelationshipTypeId.Alias && p.IsReverse == r.IsReverse); if (existing == null) { // need to stick a placeholder relationship in place before recursively calling to set up the related entity data var tempRel = new JsonRelationshipData(r, null); jsonEntity.Relationships.Add(tempRel); // add this var newRel = new JsonRelationshipData(r, context); // before doing this // now swap out the temp with the actual jsonEntity.Relationships.Remove(tempRel); jsonEntity.Relationships.Add(newRel); } else if (existing.Instances.Select(p => p.Entity).Except(r.Instances.Select(p => p.Entity.Id.Id)).Any( )) { //throw new InvalidOperationException("Wasn't expecting diff relationship instances for same rel type on a given object...."); EventLog.Application.WriteWarning("Wasn't expecting diff relationship instances for same rel type on a given object...."); } } }
/// <summary> /// Gets the entity data. /// </summary> /// <param name="id">The identifier.</param> /// <param name="cache">The cache.</param> /// <param name="refsCache">The refs cache.</param> /// <returns></returns> /// <exception cref="System.ArgumentException"> /// </exception> public EntityData GetEntityData(long id, Dictionary <long, EntityData> cache = null, Dictionary <long, EntityRef> refsCache = null) { if (cache == null) { cache = new Dictionary <long, EntityData>( ); } if (cache.ContainsKey(id)) { return(cache[id]); } long entityId = id; Dictionary <long, EntityRef> refs = refsCache; if (refs == null) { refs = new Dictionary <long, EntityRef>( ); foreach (JsonEntityRef er in EntityRefs) { if (!refs.ContainsKey(er.Id)) { refs.Add(er.Id, er.ToEntityRef( )); } } } var entityData = new EntityData( ); if (refs.ContainsKey(entityId)) { entityData.Id = refs[entityId]; } cache[id] = entityData; JsonEntity entity = Entities.FirstOrDefault(p => p.Id == entityId); if (entity == null) { EventLog.Application.WriteTrace(DumpEntity(entityData)); return(entityData); } entityData.DataState = entity.DataState; // Types if (entity.TypeIds.Any(p => !refs.ContainsKey(p))) { throw new ArgumentException( string.Format("typeIds missing from entityRefs: {0}", string.Join(",", entity.TypeIds.Where(p => !refs.ContainsKey(p))))); } entityData.TypeIds = entity.TypeIds.Select(p => refs[p]).ToList( ); // Fields if (entity.Fields.Any(p => !refs.ContainsKey(p.FieldId))) { throw new ArgumentException( string.Format("fieldIds missing from entityRefs: {0}", string.Join(",", entity.Fields.Where(p => !refs.ContainsKey(p.FieldId))))); } entityData.Fields = entity.Fields.Select(p => new FieldData { FieldId = refs[p.FieldId], Value = new TypedValue(p.Value) // TODO : Yikes! Use type-based string conversion, because this will break stuff }).ToList( ); entityData.Relationships = new List <RelationshipData>( ); foreach (JsonRelationshipData relationship in entity.Relationships) { var relData = new RelationshipData( ); entityData.Relationships.Add(relData); relData.RelationshipTypeId = relationship.RelTypeId.ToEntityRef( ); // refs[relationship.relTypeId.id]; relData.IsReverse = relationship.IsReverse; relData.RemoveExisting = relationship.RemoveExisting; relData.DeleteExisting = relationship.DeleteExisting; relData.AutoCardinality = relationship.AutoCardinality; relData.Instances = new List <RelationshipInstanceData>( ); foreach (JsonRelationshipInstanceData inst in relationship.Instances) { if (inst.Entity != 0) { relData.Instances.Add(new RelationshipInstanceData { Entity = GetEntityData(inst.Entity, cache, refs), RelationshipInstanceEntity = inst.RelEntity != 0 ? GetEntityData(inst.RelEntity, cache, refs) : null, DataState = inst.DataState }); } } } //EventLog.Application.WriteTrace( DumpEntity( entityData ) ); return(entityData); }