示例#1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            var thisTreeItemId = Guid.Parse(context.Request["thisTreeItemId"]);

            using (var db = new OrganizerEntities())
            {
                var thisTreeItem = db.TreeItems.Find(thisTreeItemId);
                CustomMembershipProvider.ValidateAccessToTreeItem_ReturnActiveAuthEntity(thisTreeItem, db, true);
                var thisTreeItemChildren = new List <TreeItem>();
                GetTreeItems.GetChildTreeItemsReadonlyHtml(thisTreeItemId, db, thisTreeItemChildren);
                var newItem = new TreeItem
                {
                    CreatedUtc      = DateTime.UtcNow,
                    Id              = Guid.NewGuid(),
                    LastModifiedUtc = DateTime.UtcNow,
                    ParentId        = thisTreeItem.Id,
                    NextSiblingId   = thisTreeItemChildren.Count != 0 ? (Guid?)thisTreeItemChildren[0].Id : null,
                    AutoLoadNestedChildrenIfNotRoot = true
                };
                db.TreeItems.Add(newItem);
                db.SaveChanges();

                context.Response.Write(ServerSideProcedures.DataObjectToXml(newItem));
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            var id = Guid.Parse(context.Request["treeItemId"]);

            using (var db = new OrganizerEntities())
            {
                var item = db.TreeItems.Find(id);
                CustomMembershipProvider.ValidateAccessToTreeItem_ReturnActiveAuthEntity(item, db, true);
                if (item.ParentId == null)
                {
                    throw new Exception("Can't delete root node");
                }

                var children = new List <TreeItem>();
                GetTreeItems.GetChildTreeItemsReadonlyHtml(id, db, children, false, true, null, true);
                for (int i = children.Count - 1; i >= 0; i--) // need to delete in this order to avoid FK errors
                {
                    db.TreeItems.Remove(children[i]);
                    db.SaveChanges();
                }

                var previousSibling = db.TreeItems.FirstOrDefault(x => x.NextSiblingId == id);
                if (previousSibling != null)
                {
                    previousSibling.NextSiblingId = item.NextSiblingId;
                    db.SaveChanges();
                }

                db.TreeItems.Remove(item);
                db.SaveChanges();
            }

            context.Response.ContentType = "text/plain";
            context.Response.Write("OK");
        }
示例#3
0
        public void ProcessRequest(HttpContext context)
        {
            using (var reader = new StreamReader(context.Request.InputStream))
            {
                var xml = XDocument.Parse(reader.ReadToEnd());
                reader.Close();

                var id = Guid.Parse(xml.Root.Elements().First(e => (string)e.Attribute("name") == "Id").Value);
                using (var db = new OrganizerEntities())
                {
                    var item = db.TreeItems.Find(id);
                    if (item == null)
                    {
                        throw new Exception("item is not found by ID = " + id);
                    }
                    CustomMembershipProvider.ValidateAccessToTreeItem_ReturnActiveAuthEntity(item, db, true);
                    ServerSideProcedures.XmlToItem(item, xml.Root, "LastModifiedUtc", "IsSelected", "Id", "ParentId", "NextSiblingId");
                    item.LastModifiedUtc = DateTime.UtcNow;
                    db.SaveChanges();
                }

                context.Response.ContentType = "text/plain";
                context.Response.Write("OK");
            }
        }
        public static string RenderTreeItemsReadonly(OrganizerEntities db, TreeItem rootItem)
        {
            var itemsHtml = new StringBuilder();
            var items     = new List <TreeItem>();

            if (rootItem != null)
            {
                itemsHtml.AppendFormat("<h4>{0}</h4>", rootItem.Text);
                items.Add(rootItem);
                GetChildTreeItemsReadonlyHtml(rootItem.Id, db, items, true, true, itemsHtml);
            }

            return(itemsHtml.ToString());
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";

            var acceptEncoding = context.Request.Headers["Accept-Encoding"];

            if (!string.IsNullOrEmpty(acceptEncoding))
            {
                // The two common compression formats in web are GZip and Deflate
                if (acceptEncoding.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    // Read the response using a GZip compressor ,   and replace the output with compressed result
                    context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                    // Tell the client the ouput they got is compressed in GZip
                    context.Response.AppendHeader("Content-Encoding", "gzip");
                }
                else if (acceptEncoding.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    // Read the response using a Deflate compressor ,   and replace the output with compressed result
                    context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
                    // Tell the client the ouput they got is compressed in Deflate
                    context.Response.AppendHeader("Content-Encoding", "deflate");
                }
            }


            var        rootTreeItemId = Guid.Parse(context.Request["rootTreeItemId"]);
            var        result         = new List <TreeItem>();
            AuthEntity user           = null;

            using (var db = new OrganizerEntities())
            {
                var item = db.TreeItems.FirstOrDefault(x => x.Id == rootTreeItemId);
                if (item != null)
                {
                    user = CustomMembershipProvider.ValidateAccessToTreeItem_ReturnActiveAuthEntity(item, db, false);
                    user.LastActiveAtUtc = DateTime.UtcNow;
                    db.SaveChanges();

                    result.Add(item);
                    GetChildTreeItemsReadonlyHtml(item.Id, db, result, true, true);
                }
            }
            if (result.Count != 0 && user != null)
            {
                context.Response.Write(ServerSideProcedures.ItemsListToXml(result[0], result,
                                                                           null// (x) => new Dictionary<string, string> {{"IsSelected", ((TreeItem)x).Id == user.SelectedTreeItemId ? "True" : "False"}}
                                                                           ));
            }
        }
        public static void GetChildTreeItemsReadonlyHtml(Guid parentId, OrganizerEntities db, List <TreeItem> result, bool fixNextSiblingId = false, bool recursive = true, StringBuilder htmlOutput = null, bool getAllItems = false)
        {
#if DEBUG2
            if (result.Count > 100)
            {
                return;
            }
#endif
            var items = SortSiblings(db.TreeItems.Where(x => x.ParentId == parentId).ToList(), fixNextSiblingId, db);
            result.AddRange(items);
            if (htmlOutput != null)
            {
                htmlOutput.Append("<ul>");
            }

            foreach (var item in items)
            {
                if (getAllItems || item.ShareRead != false)
                {
                    if (htmlOutput != null)
                    {
                        htmlOutput.AppendFormat("<li>{0}", RemovePrivateText(item.Text));
                    }
                    if (recursive)
                    {
                        if (getAllItems || item.AutoLoadNestedChildrenIfNotRoot)
                        {
                            GetChildTreeItemsReadonlyHtml(item.Id, db, result, fixNextSiblingId, true, htmlOutput);
                        }
                        else
                        {
                            if (db.TreeItems.Any(x => x.ParentId == item.Id))
                            {
                                item.Text += " [...children]";
                            }
                        }
                    }
                    if (htmlOutput != null)
                    {
                        htmlOutput.Append("</li>");
                    }
                }
            }
            if (htmlOutput != null)
            {
                htmlOutput.Append("</ul>");
            }
        }
        /// <summary>
        ///  sorts items according to NextSiblingId
        /// </summary>
        static LinkedList <TreeItem> SortSiblings(List <TreeItem> list, bool fixNextSiblingId = false, OrganizerEntities db = null)
        {
            var r = new LinkedList <TreeItem>();

            var nonEndingItemsByNextSiblingId = new Dictionary <Guid, TreeItem>();

            foreach (var treeItem in list)
            {
                if (treeItem.NextSiblingId == null || nonEndingItemsByNextSiblingId.ContainsKey(treeItem.NextSiblingId.Value))
                {
                    r.AddLast(treeItem);
                }
                else
                {
                    nonEndingItemsByNextSiblingId.Add(treeItem.NextSiblingId.Value, treeItem);
                }
            }

            if (r.First != null)
            {
                for (var enumeratedEndingItem = r.First;;)
                {
                    var searchedNextSiblingId = enumeratedEndingItem.Value.Id;
                    var nextSiblingItemNode   = enumeratedEndingItem;

_search_previous_sibling:
                    TreeItem foundTreeItem;
                    if (nonEndingItemsByNextSiblingId.TryGetValue(searchedNextSiblingId, out foundTreeItem))
                    {
                        nonEndingItemsByNextSiblingId.Remove(searchedNextSiblingId);
                        nextSiblingItemNode   = r.AddBefore(nextSiblingItemNode, foundTreeItem);
                        searchedNextSiblingId = foundTreeItem.Id;
                        goto _search_previous_sibling;
                    }

                    if (enumeratedEndingItem.Next == null)
                    {
                        break;
                    }
                    enumeratedEndingItem = enumeratedEndingItem.Next;
                }
            }
            foreach (var item in nonEndingItemsByNextSiblingId.Values)
            {
                r.AddLast(item);
            }

            if (fixNextSiblingId)
            {
                if (db == null)
                {
                    throw new ArgumentNullException();
                }
                TreeItem previousTI = null;
                foreach (var treeItem in r)
                {
                    if (previousTI != null && previousTI.NextSiblingId != treeItem.Id)
                    {
                        previousTI.NextSiblingId = treeItem.Id;
                        db.SaveChanges();
                    }
                    previousTI = treeItem;
                }
                if (previousTI != null && previousTI.NextSiblingId.HasValue) // last item must have NextSiblingId = null
                {
                    previousTI.NextSiblingId = null;
                    db.SaveChanges();
                }
            }

            return(r);
        }
        public void ProcessRequest(HttpContext context)
        {
            var  moveType = context.Request["moveType"];
            var  from     = Guid.Parse(context.Request["from"]);
            var  toStr    = context.Request["to"];
            Guid?to;

            if (!String.IsNullOrEmpty(toStr))
            {
                to = Guid.Parse(toStr);
            }
            else
            {
                to = null;
            }
            using (var db = new OrganizerEntities())
            {
                var fromItem = db.TreeItems.Find(from);
                CustomMembershipProvider.ValidateAccessToTreeItem_ReturnActiveAuthEntity(fromItem, db, true);
                TreeItem toItem = null;
                if (to.HasValue)
                {
                    toItem = db.TreeItems.Find(to);
                    CustomMembershipProvider.ValidateAccessToTreeItem_ReturnActiveAuthEntity(toItem, db, true);
                }

                switch (moveType)
                {
                case "top":
                {
                    var children = new List <TreeItem>();
                    GetTreeItems.GetChildTreeItemsReadonlyHtml(fromItem.ParentId.Value, db, children, false, false);
                    var firstChild = children.FirstOrDefault();

                    var previousSibling = db.TreeItems.FirstOrDefault(x => x.NextSiblingId == from);
                    if (previousSibling != null)
                    {
                        previousSibling.NextSiblingId = fromItem.NextSiblingId;
                        db.SaveChanges();
                    }

                    fromItem.NextSiblingId = firstChild.Id;
                    db.SaveChanges();
                }
                break;

                case "bottom":
                {
                    var children = new List <TreeItem>();
                    GetTreeItems.GetChildTreeItemsReadonlyHtml(fromItem.ParentId.Value, db, children, false, false);
                    var lastChild = children.LastOrDefault();

                    var previousSibling = db.TreeItems.FirstOrDefault(x => x.NextSiblingId == from);
                    if (previousSibling != null)
                    {
                        previousSibling.NextSiblingId = fromItem.NextSiblingId;
                        db.SaveChanges();
                    }

                    if (lastChild != null)
                    {
                        lastChild.NextSiblingId = fromItem.Id;
                    }
                    fromItem.NextSiblingId = null;
                    db.SaveChanges();
                }
                break;

                case "children":
                {
                    var children = new List <TreeItem>();
                    GetTreeItems.GetChildTreeItemsReadonlyHtml(to.Value, db, children, false, false);
                    var lastChild = children.LastOrDefault();

                    var previousSibling = db.TreeItems.FirstOrDefault(x => x.NextSiblingId == from);
                    if (previousSibling != null)
                    {
                        previousSibling.NextSiblingId = fromItem.NextSiblingId;
                        db.SaveChanges();
                    }
                    fromItem.ParentId      = toItem.Id;
                    fromItem.NextSiblingId = null;
                    if (lastChild != null)
                    {
                        lastChild.NextSiblingId = fromItem.Id;
                    }
                    db.SaveChanges();
                }
                break;

                case "siblings":
                {
                    var previousSibling = db.TreeItems.FirstOrDefault(x => x.NextSiblingId == from);
                    if (previousSibling != null)
                    {
                        previousSibling.NextSiblingId = fromItem.NextSiblingId;
                        db.SaveChanges();
                    }

                    fromItem.ParentId      = toItem.ParentId;
                    fromItem.NextSiblingId = toItem.NextSiblingId;
                    toItem.NextSiblingId   = fromItem.Id;

                    db.SaveChanges();
                }
                break;

                default: throw new ArgumentException("invalid moveType: " + moveType);
                }

                db.SaveChanges();
            }

            context.Response.ContentType = "text/plain";
            context.Response.Write("OK");
        }