private void InsertOrUpdateMenu(IMenuRepository menuRepository, int index, Menu menu, MenuLevel level, Guid? parentId)
        {
            // sort start with 1

            // a1, check the paramters
            //  1, check the level and parentId
            //      1a, when level = parent, the parentId = null
            //      2a, when level != parent and the parentId = null, throw exception.
            // a2, check the index
            //  1, index > 0

            // a1, when level = parent
            //  1, find all menus that level = parent
            //      1a, if the index > menus count, set the sort = (countOfMenu + 1)
            //      1b, if the index <= menus count, set the sort = (countOfMenu + 1), then set the sort that large then index add one
            // a2, when level = child
            //  1, find all menus that parentId = input parentId
            //      1a, when the index > menus count, set the sort = (countOfMenu + 1)
            //      1b, when the index <= menus count, set the sort = (countOfMenu + 1), then set the sort that large then index add one

            ISpecification<Menu> spec = null;
            var existMenu = menuRepository.Exist(Specification<Menu>.Eval(m => m.ID == menu.ID));

            if (level == MenuLevel.Parent)
            {
                spec = Specification<Menu>.Eval(m => m.Level == MenuLevel.Parent);
            }

            if (level == MenuLevel.Children)
            {
                if (!parentId.HasValue)
                    throw new ArgumentNullException("parentId", "When menuLevel is children, the parentId must be not null.");

                spec = Specification<Menu>.Eval(m => m.Level == MenuLevel.Children && m.ParentId == parentId);
            }

            var menus = this.GetMenus(menuRepository, spec, menu.ID, existMenu);
            
            // if the menu not exist, add the menu (the menu has been atteched).
            if (!existMenu)
            {
                var sort = this.InsertSortMenu(menus, index);
                menu.SetSort(sort);
                menuRepository.Add(menu);
            }
            else
            {
                var sort = this.MoveSortMenu(menus, menu.Sort, index);
                menu.SetSort(sort);
                menuRepository.Update(menu);
                foreach (var item in menus)
                    menuRepository.Update(item);
            }
        }
        public void CreateMenu_Test()
        {
            var menu = new Menu("TestMenu", "My test menu", "My test menu description", null, MenuLevel.Parent, 1, null, "*****@*****.**");

            //using (var repositoryContext = new EntityFrameworkRepositoryContext(new RmsDbContext("rms")))
            //{
            //    var context = repositoryContext.Context.Set<Menu>();
            //    context.Add(menu);
            //    repositoryContext.Commit();
            //}

            using (var myContext = new RmsDbContext("rms"))
            {
                myContext.Context.Set<Menu>().Add(menu);
                myContext.Context.SaveChanges();
            }
        }
        public void CreateMenu(MenuDto menu)
        {
            var m_menu = new Menu(menu.MenuName, menu.DisplayName, menu.Description, menu.ParentId,
                (MenuLevel) menu.Level, menu.Sort, menu.ActionsId, menu.CreatedBy);

            this._menuDomainService.AddOrUpdateMenu(this._menuRepository, m_menu);
        }
 public void AddOrUpdateMenu(IMenuRepository menuRepository, Menu menu)
 {
     this.InsertOrUpdateMenu(menuRepository, menu.Sort, menu, menu.Level, menu.ParentId);
 }