public static JsonEntityQueryResult GetJsonEntityQueryResult(ICollection <EntityRef> entityRefs, ICollection <string> requests) { #pragma warning disable 618 var svc = new EntityInfoService( ); #pragma warning restore 618 var entityDataList = new List <EntityData>( ); for (int i = 0; i < entityRefs.Count; ++i) { EntityRef entityRef = entityRefs.ElementAt(i); string request = i < requests.Count ? requests.ElementAt(i) : requests.Last( ); var rqObj = Factory.RequestParser.ParseRequestQuery(request); EntityData entityData = svc.GetEntityData(entityRef, rqObj); entityDataList.Add(entityData); } if (entityDataList.Count(p => p != null) == 0) { throw new HttpResponseException(HttpStatusCode.NotFound); } var result = new JsonEntityQueryResult(entityDataList); return(result); }
private HttpResponseMessage <JsonEntityQueryResult> GetEntityData(ICollection <EntityRef> entityRefs, ICollection <string> requests) { try { Stopwatch sw = Stopwatch.StartNew( ); JsonEntityQueryResult result = GetJsonEntityQueryResult(entityRefs, requests); result.Extra = "" + sw.ElapsedMilliseconds; return(new HttpResponseMessage <JsonEntityQueryResult>(result)); } catch (HttpResponseException) { throw; } catch (PlatformSecurityException) { throw; } catch (Exception e) { if (e is ArgumentException) // would be better if there was a more specific exception for 'not found' { throw new HttpResponseException(HttpStatusCode.NotFound); } EventLog.Application.WriteError("caught exception: " + e.Message); throw new InvalidOperationException("caught exception " + e.GetType( ).Name, e); } }
/// <summary> /// Initializes a new instance of the <see cref="JsonEntity" /> class. /// </summary> /// <param name="entityRef">The entity reference.</param> /// <param name="context">The context.</param> public JsonEntity(EntityRef entityRef, JsonEntityQueryResult context) : this() { JsonEntityRef eid = context.GetEntityRef(entityRef); if (!string.IsNullOrEmpty(entityRef.Alias) && eid.Alias != entityRef.Alias) { EventLog.Application.WriteWarning("EntityRef with id {0} using different aliases \"{1}\" <> \"{2}\"", eid.Id, eid.Alias, entityRef.Alias); } Id = eid.Id; }
/// <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> /// Resolves the ids. /// </summary> /// <param name="idMap">The identifier map.</param> public void ResolveIds(Dictionary <long, long> idMap) { long id0 = Id; Id = JsonEntityQueryResult.ResolveId(Id, idMap); if (Id != id0 && DataState == DataState.Create) { DataState = DataState.Unchanged; EventLog.Application.WriteTrace("Resolved id from {0} to {1} so changed dataState from Create to Unchanged", id0, Id); } TypeIds = TypeIds.Select(p => JsonEntityQueryResult.ResolveId(p, idMap)).ToList( ); Fields.ForEach(p => { p.FieldId = JsonEntityQueryResult.ResolveId(p.FieldId, idMap); }); Relationships.ForEach(p => p.ResolveIds(idMap)); }
/// <summary> /// Initializes a new instance of the <see cref="JsonRelationshipData" /> class. /// </summary> /// <param name="relationshipData">The relationship data.</param> /// <param name="context">The context.</param> public JsonRelationshipData(RelationshipData relationshipData, JsonEntityQueryResult context) { RelTypeId = new JsonEntityRef { Id = relationshipData.RelationshipTypeId.Id, NameSpace = relationshipData.RelationshipTypeId.Namespace, Alias = relationshipData.RelationshipTypeId.Alias }; IsReverse = relationshipData.IsReverse; IsLookup = relationshipData.IsLookup; RemoveExisting = relationshipData.RemoveExisting; DeleteExisting = relationshipData.DeleteExisting; AutoCardinality = relationshipData.AutoCardinality; Instances = new List <JsonRelationshipInstanceData>( ); if (context != null) { foreach (RelationshipInstanceData instance in relationshipData.Instances) { Instances.Add(new JsonRelationshipInstanceData(instance, context)); } } }
/// <summary> /// Handles cloning data. /// </summary> /// <param name="entityData">The entity data.</param> /// <returns>The id of the cloned entity</returns> private IHttpActionResult HandleCloneAndUpdate(JsonEntityQueryResult entityData) { // resolve all entity ids above our 'known id hi watermark' that actually do exist entityData.ResolveIds(); long id = entityData.Ids.FirstOrDefault(); long cloneId = -1; if (id >= JsonEntityQueryResult.BaseNewId) { return(BadRequest("Cannot clone a temporary entity.")); } EventLog.Application.WriteTrace("Cloning entity " + id); EntityData newEntityData = entityData.GetEntityData(id); DatabaseContext.RunWithRetry(() => { using (DatabaseContext context = DatabaseContext.GetContext(true)) { #pragma warning disable 618 var svc = new EntityInfoService(); #pragma warning restore 618 var clonedIds = svc.CloneAndUpdateEntity(newEntityData); if (!clonedIds.TryGetValue(id, out cloneId)) { cloneId = -1; } context.CommitTransaction(); } }); return(Ok(cloneId)); }
/// <summary> /// Resolves the ids. /// </summary> /// <param name="idMap">The identifier map.</param> public void ResolveIds(Dictionary <long, long> idMap) { Entity = JsonEntityQueryResult.ResolveId(Entity, idMap); RelEntity = JsonEntityQueryResult.ResolveId(RelEntity, idMap); }
/// <summary> /// Initializes a new instance of the <see cref="JsonRelationshipInstanceData" /> class. /// </summary> /// <param name="relInstanceData">The relative instance data.</param> /// <param name="context">The context.</param> public JsonRelationshipInstanceData(RelationshipInstanceData relInstanceData, JsonEntityQueryResult context) { Entity = new JsonEntityData(relInstanceData.Entity, context).Id; if (relInstanceData.RelationshipInstanceEntity != null) { RelEntity = new JsonEntityData(relInstanceData.RelationshipInstanceEntity, context).Id; } DataState = relInstanceData.DataState; }
/// <summary> /// Merges the specified json entity query result. /// </summary> /// <param name="jsonEntityQueryResult">The json entity query result.</param> public void Merge(JsonEntityQueryResult jsonEntityQueryResult) { }
/// <summary> /// Initializes a new instance of the <see cref="JsonFieldData" /> class. /// </summary> /// <param name="field">The field.</param> /// <param name="context">The context.</param> public JsonFieldData(FieldData field, JsonEntityQueryResult context) { FieldId = context.GetEntityRef(field.FieldId).Id; Value = field.Value.ValueString; TypeName = field.Value.Type.GetDisplayName( ); }
/// <summary> /// Resolves the ids. /// </summary> /// <param name="idMap">The identifier map.</param> public void ResolveIds(Dictionary <long, long> idMap) { RelTypeId.Id = JsonEntityQueryResult.ResolveId(RelTypeId.Id, idMap); Instances.ForEach(p => p.ResolveIds(idMap)); }
/// <summary> /// Handles post data. /// </summary> /// <param name="entityData">The entity data.</param> /// <param name="returnMap">if set to <c>true</c> [return map].</param> /// <returns></returns> private HttpResponseMessage HandlePost(JsonEntityQueryResult entityData, bool returnMap) { Stopwatch sw = Stopwatch.StartNew( ); long t1; // resolve all entity ids above our 'known id hi watermark' that actually do exist entityData.ResolveIds( ); long id = entityData.Ids.FirstOrDefault( ); IDictionary <long, IEntity> map = null; if (id >= JsonEntityQueryResult.BaseNewId) { // create EventLog.Application.WriteTrace("Creating entity " + id); EntityData newEntityData = entityData.GetEntityData(id); t1 = sw.ElapsedMilliseconds; DatabaseContext.RunWithRetry(() => { using (DatabaseContext context = DatabaseContext.GetContext(true)) { #pragma warning disable 618 var svc = new EntityInfoService(); #pragma warning restore 618 map = svc.CreateEntityGetMap(newEntityData); IEntity entity = map[newEntityData.Id.Id]; id = entity == null ? -1 : entity.Id; context.CommitTransaction(); } }); EventLog.Application.WriteTrace("EntityPost create took {0} msec ({1} to de-json)", sw.ElapsedMilliseconds, t1); } else { map = new Dictionary <long, IEntity>( ); EventLog.Application.WriteTrace("Updating entity " + id); EntityData newEntityData = entityData.GetEntityData(id); t1 = sw.ElapsedMilliseconds; DatabaseContext.RunWithRetry(() => { using (DatabaseContext context = DatabaseContext.GetContext(true)) { #pragma warning disable 618 var svc = new EntityInfoService(); #pragma warning restore 618 svc.UpdateEntity(newEntityData); context.CommitTransaction(); } }); EventLog.Application.WriteTrace("EntityPost update took {0} msec ({1} to de-json)", sw.ElapsedMilliseconds, t1); } HttpResponseMessage httpResponseMessage; if (returnMap) { ///// // Create a custom response message so the infrastructure framework doesn't serialize it twice. ///// httpResponseMessage = new HttpResponseMessage { Content = new StringContent(DictionaryToJson(map)) }; httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); } else { httpResponseMessage = new HttpResponseMessage <long>(id); } return(httpResponseMessage); }
public HttpResponseMessage Post([FromBody] JsonEntityQueryResult entityData) { return(HandlePost(entityData, false)); }
public IHttpActionResult PostCloneAndUpdate([FromBody] JsonEntityQueryResult entityData) { return(HandleCloneAndUpdate(entityData)); }