Esempio n. 1
0
        public virtual async Task UpdateAsync(TTree tree, Action <TTree> childrenAction = null)
        {
            CheckSameName(tree);

            var oldFullName = tree.FullName;

            if (tree.ParentId.HasValue)
            {
                var parent = await _generalTreeRepository.FirstOrDefaultAsync(EqualId(tree.ParentId.Value));

                Check.NotNull(parent, nameof(parent));

                tree.FullName = parent.FullName + _generalTreeConfiguration.Hyphen + tree.Name;
            }
            else
            {
                tree.FullName = tree.Name;
            }

            var children = await GetChildrenAsync(tree.Id, true);

            foreach (var child in children)
            {
                child.FullName = GeneralTreeCodeGenerate.MergeFullName(tree.FullName,
                                                                       GeneralTreeCodeGenerate.RemoveParentCode(child.FullName, oldFullName));

                childrenAction?.Invoke(child);
            }
        }
Esempio n. 2
0
        private void TraverseTree(TTree parent, ICollection <TTree> children)
        {
            if (children == null || !children.Any())
            {
                return;
            }

            children.ForEach((tree, index) =>
            {
                //CheckSameName
                if (children.Count(x => x.Name == tree.Name) > 1)
                {
                    throw new UserFriendlyException(_generalTreeConfiguration.ExceptionMessageFactory.Invoke(tree));
                }

                tree.Code = index == 0
                    ? GeneralTreeCodeGenerate.MergeCode(parent.Code, GeneralTreeCodeGenerate.CreateCode(1))
                    : GeneralTreeCodeGenerate.GetNextCode(children.ElementAt(index - 1).Code);

                tree.Level    = tree.Code.Split('.').Length;
                tree.FullName = parent.FullName + _generalTreeConfiguration.Hyphen + tree.Name;
            });

            children.ForEach(tree => { TraverseTree(tree, tree.Children); });
        }
        public virtual async Task MoveAsync(TPrimaryKey id, TPrimaryKey?parentId)
        {
            var tree = await _generalTreeRepository.GetAsync(id);

            if (tree.ParentId.Equals(parentId))
            {
                return;
            }

            //Should find children before Code change
            var children = await GetChildrenAsync(id, true);

            //Store old code and full name of Tree
            var oldCode     = tree.Code;
            var oldFullName = tree.FullName;

            //Move Tree
            tree.Code = await GetNextChildCodeAsync(parentId);

            tree.Level    = tree.Code.Split('.').Length;
            tree.ParentId = parentId;
            tree.FullName = await GetChildFullNameAsync(parentId, tree.Name);

            CheckSameName(tree);

            //Update Children Codes and FullName
            foreach (var child in children)
            {
                child.Code = GeneralTreeCodeGenerate.MergeCode(tree.Code,
                                                               GeneralTreeCodeGenerate.RemoveParentCode(child.Code, oldCode));
                child.FullName = GeneralTreeCodeGenerate.MergeFullName(tree.FullName,
                                                                       GeneralTreeCodeGenerate.RemoveParentCode(child.FullName, oldFullName));
                child.Level = child.Code.Split('.').Length;
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     Get next child code
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private async Task <string> GetNextChildCodeAsync(TPrimaryKey?parentId)
        {
            var lastChild =
                _generalTreeRepository.GetAll()
                .Where(Equal(parentId, "ParentId"))
                .OrderByDescending(x => x.Code)
                .FirstOrDefault();

            if (lastChild != null)
            {
                //Get the next code
                return(GeneralTreeCodeGenerate.GetNextCode(lastChild.Code));
            }

            //Generate a code
            var parentCode = parentId != null ? await GetCodeAsync(parentId.Value) : null;

            return(GeneralTreeCodeGenerate.MergeCode(parentCode, GeneralTreeCodeGenerate.CreateCode(1)));
        }