示例#1
0
        /// <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);
        }