Esempio n. 1
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode MoveAllChildren(
            INamespace @namespace,
            ref Result error
            )
        {
            CheckDisposed();

            if (@namespace == null)
            {
                error = "invalid namespace";
                return(ReturnCode.Error);
            }

            if (children == null)
            {
                error = String.Format(
                    "children not available in namespace \"{0}\"",
                    GetDisplayName());

                return(ReturnCode.Error);
            }

            //
            // NOTE: Grab all the children [that need to be moved] from the
            //       existing namespace.
            //
            IEnumerable <INamespace> newChildren = @namespace.GetAllChildren();

            if (newChildren == null)
            {
                return(ReturnCode.Ok); /* NOTE: Ok, no children. */
            }
            //
            // NOTE: Pass 1: Verify that no duplicate children exist.
            //
            foreach (INamespace child in newChildren)
            {
                if (child == null)
                {
                    continue;
                }

                if (children.ContainsKey(child.Name))
                {
                    error = String.Format(
                        "can't add \"{0}\": namespace already exists",
                        child.Name);

                    return(ReturnCode.Error);
                }
            }

            //
            // NOTE: Pass 2: Add all new children to our collection.
            //
            bool reAddThis = false;

            foreach (INamespace child in newChildren)
            {
                if (child == null)
                {
                    continue;
                }

                //
                // BUGFIX: We cannot be a child of ourself.  Therefore, we
                //         need to re-add ourself to the original namespace
                //         after clearing all the other children from it.
                //
                if (Object.ReferenceEquals(child, this))
                {
                    reAddThis = true;
                    continue;
                }

                children.Add(child.Name, child);
                child.Parent = this;
            }

            //
            // NOTE: Next, clear the children from the source namespace.
            //
            @namespace.ClearAllChildren();

            //
            // NOTE: Finally, if necessary, re-add this namespace to the
            //       original one.
            //
            if (reAddThis &&
                (@namespace.AddChild(this, ref error) != ReturnCode.Ok))
            {
                return(ReturnCode.Error);
            }

            return(ReturnCode.Ok);
        }