public int Create(ContextMenuProperties item)
        {
            Logger.Debug("Creating context menu item");
            if (item.Id >= 0 && FindItemById(item.Id) != null)
            {
                throw new Exception(string.Format("An item with the id {0} already exists", item.Id));
            }

            if (item.Id < 0)
            {
                item.Id = ++_id;
            }

            if (item.Contexts == null)
            {
                item.Contexts = new List <string> {
                    "page"
                };
            }

            if (string.IsNullOrEmpty(item.Title))
            {
                throw new Exception("Title is required");
            }

            ContextMenuProperties parent = null;

            if (item.ParentId >= 0)
            {
                parent = FindItemById(item.ParentId);
                if (parent == null)
                {
                    throw new Exception(string.Format("Parent item with id {0} does not exists", item.ParentId));
                }
            }

            if (parent != null)
            {
                if (parent.ChildItems == null)
                {
                    parent.ChildItems = new List <ContextMenuProperties>();
                }

                parent.ChildItems.Add(item);
            }
            else
            {
                lock (Lock)
                {
                    _items.Add(item);
                }
            }
            return(item.Id);
        }
 private bool VisitItem(ContextMenuProperties item, Predicate <ContextMenuProperties> shouldStop)
 {
     if (shouldStop(item))
     {
         return(true);
     }
     if (item.ChildItems != null && item.ChildItems.Count > 0)
     {
         return(VisitItems(item.ChildItems, shouldStop));
     }
     return(false);
 }
        private ContextMenuProperties FindItemById(int id)
        {
            ContextMenuProperties item = null;

            lock (_items)
            {
                VisitItems(_items, i =>
                {
                    if (i.Id == id)
                    {
                        item = i;
                        return(true);
                    }
                    return(false);
                });
            }
            return(item);
        }