Ejemplo n.º 1
0
        private void OwnedChildrenList_OnRemove(object sender, NamedEventArgs e)
        {
            INamedObjectList oContainer   = (INamedObjectList)sender;
            IEntityBase      oChildEntity = e.NamedObject as IEntityBase;

            if (oChildEntity == null)
            {
                return;
            }

            using (AutoTreeChangeNotify oChangeNotify = new AutoTreeChangeNotify(
                       this,
                       EntityTreeChange.ChildRemove,
                       this,
                       oChildEntity,
                       EntityRelation.Owner,
                       null))
            {
                //
                //	The object oChild that we are it's owner is detached from the tree.
                //	1. All links to this object should be removed, i.e it should be removed
                //     from all containers listed in m_ParentContainers.
                //	2. Reset UniqueParentLink of all linked children of the removed object.
                //

                // Remove all links to child object

                // TODO: !!! Revert to INamedObjectList when RemoveObject will be part
                // of INamedObjectList interface
                foreach (INamedObjectList oParentContainer in oChildEntity.ParentContainers)
                {
                    try
                    {
                        // We don't want to remove from owner (our) container, since
                        // This operation is already done
                        if (oParentContainer != oContainer)
                        {
                            oParentContainer.Remove(oChildEntity.Name);
                        }
                    }
                    catch (Exception oExc)
                    {
                        Log.WriteError(
                            "Object [{0}] was not removed from parent container " +
                            " [{1}]", oChildEntity, oParentContainer);

                        Log.WriteException(oExc);
                    }
                }

                // Detach all linked children of the removed object
                oChildEntity.DetachAllChildren();

                // Remove links from child object to parents
                RemoveUniqueParentLinkIfNeeded(oChildEntity);

                // Reset child "OwnerParent" field
                SetObjectOwnerParent(
                    oChildEntity,
                    null);
            }
        }