Exemplo n.º 1
0
        /// <summary>
        ///   Moves source under specified destination item.
        ///   If destination is inside of source tree it will throw exception.
        /// </summary>
        /// <param name="source"> Source item. </param>
        /// <param name="destination"> Destination item. </param>
        /// <param name="position"> <see cref="HierarchyMovePosition" /> value </param>
        public static void MoveTo(this IHierarchy source, IHierarchy destination, HierarchyMovePosition position)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            if (source.IsEqualOrChildOf(destination))
            {
                throw new HierarchyException("Source entity cannot be a parent or sibling to itself or its child.");
            }

            switch (position)
            {
            case HierarchyMovePosition.Over:
                source.Parent  = destination;
                source.Ordinal = destination.Children.Max(x => x.Ordinal) + 1;
                Reindex(destination.Parent.Children);
                return;

            case HierarchyMovePosition.After:
                source.Parent = destination.Parent;
                // todo: Ordinal property has to be set here
                if (destination.Parent != null)
                {
                    Reindex(destination.Parent.Children);
                }
                break;

            case HierarchyMovePosition.Before:
                source.Parent = destination.Parent;
                if (destination.Parent != null)
                {
                    Reindex(destination.Parent.Children);
                }
                // todo: Ordinal property has to be set here
                break;
            }
        }