Esempio n. 1
0
        public HttpResponseMessageWrapper <Item> InsertItem(HttpRequestMessage req)
        {
            Operation      operation = null;
            HttpStatusCode code      = AuthenticateUser(req);

            if (code != HttpStatusCode.OK)
            {   // user not authenticated
                return(ReturnResult <Item>(req, operation, code));
            }

            // get the new item from the message body
            Item clientItem = null;

            code = ProcessRequestBody <Item>(req, out clientItem, out operation);
            if (code != HttpStatusCode.OK)  // error encountered processing body
            {
                return(ReturnResult <Item>(req, operation, code));
            }

            if (clientItem.ParentID == Guid.Empty)
            {   // parent ID is an empty guid, make it null instead so as to not violate ref integrity rules
                clientItem.ParentID = null;
            }

            try
            {
                Folder folder = this.StorageContext.Folders.Single <Folder>(tl => tl.ID == clientItem.FolderID);
                if (folder.UserID != CurrentUser.ID)
                {   // requested folder does not belong to the authenticated user, return 403 Forbidden
                    TraceLog.TraceError("Folder of Entity does not belong to current user)");
                    return(ReturnResult <Item>(req, operation, HttpStatusCode.Forbidden));
                }

                // fill out the ID's for any FieldValues that travelled with the item
                if (clientItem.FieldValues != null)
                {
                    foreach (var fv in clientItem.FieldValues)
                    {
                        if (fv.ItemID == null || fv.ItemID == Guid.Empty)
                        {
                            fv.ItemID = clientItem.ID;
                        }
                    }
                }

                // fill out the timestamps if they aren't set (null, or MinValue.Date, allowing for DST and timezone issues)
                DateTime now = DateTime.UtcNow;
                if (clientItem.Created == null || clientItem.Created.Date == DateTime.MinValue.Date)
                {
                    clientItem.Created = now;
                }
                if (clientItem.LastModified == null || clientItem.LastModified.Date == DateTime.MinValue.Date)
                {
                    clientItem.LastModified = now;
                }

                ItemProcessor ip = ItemProcessor.Create(CurrentUser, StorageContext, clientItem.ItemTypeID);
                if (ip != null)
                {   // do itemtype-specific processing
                    ip.ProcessCreate(clientItem);
                }

                try
                {   // add the new item to the database
                    var item = this.StorageContext.Items.Add(clientItem);
                    int rows = this.StorageContext.SaveChanges();
                    if (rows < 1 || item == null)
                    {
                        TraceLog.TraceError("Internal Server Error (database operation did not succeed)");
                        return(ReturnResult <Item>(req, operation, HttpStatusCode.InternalServerError));  // return 500 Internal Server Error
                    }
                    else
                    {
                        // invoke any workflows associated with this item
                        if (folder.Name.StartsWith("$") == false)
                        {
                            WorkflowHost.WorkflowHost.InvokeWorkflowForOperation(this.StorageContext, null, operation);
                        }
                        TraceLog.TraceInfo("Created");
                        return(ReturnResult <Item>(req, operation, item, HttpStatusCode.Created));     // return 201 Created
                    }
                }
                catch (Exception ex)
                {   // check for the condition where the item is already in the database
                    // in that case, return 202 Accepted; otherwise, return 500 Internal Server Error
                    try
                    {
                        var dbItem = this.StorageContext.Items.Single(t => t.ID == clientItem.ID);
                        if (dbItem.Name == clientItem.Name)
                        {
                            TraceLog.TraceInfo("Accepted (entity already in database) : Exception[" + ex.Message + "]");
                            return(ReturnResult <Item>(req, operation, dbItem, HttpStatusCode.Accepted));
                        }
                        else
                        {
                            TraceLog.TraceException("Error inserting entity", ex);
                            return(ReturnResult <Item>(req, operation, HttpStatusCode.InternalServerError));
                        }
                    }
                    catch (Exception)
                    {   // item not inserted - return 500 Internal Server Error
                        TraceLog.TraceException("Error inserting entity", ex);
                        return(ReturnResult <Item>(req, operation, HttpStatusCode.InternalServerError));
                    }
                }
            }
            catch (Exception ex)
            {   // folder not found - return 404 Not Found
                TraceLog.TraceException("Resource not found (Folder)", ex);
                return(ReturnResult <Item>(req, operation, HttpStatusCode.NotFound));
            }
        }