예제 #1
0
        public void CleanUpDatabase()
        {
            // Find:
            // - conferences w/o participants
            // - participants w/o streams
            // - streams w/o bytes
            ArrayList confs = new ArrayList(), parts = new ArrayList(), streams = new ArrayList();

            #region Depth-first-search of tree
            // By collecting a full list of the conferences, participants, and streams to be deleted
            //  we can more effeciently delete the objects by doing quick bulk deletes from the database,
            //  rather than transacting for each individual delete.
            foreach (TreeNode yearNode in Nodes)
            {
                foreach (TreeNode monthNode in yearNode.Nodes)
                {
                    foreach (TreeNode dateNode in monthNode.Nodes)
                    {
                        foreach (TreeNode confNode in dateNode.Nodes)
                        {
                            if (!confNode.Nodes.GetEnumerator().MoveNext())  // check for emptiness
                            {
                                confs.Add(confNode);
                            }
                            else
                            {
                                foreach (TreeNode partNode in confNode.Nodes)
                                {
                                    if (!partNode.Nodes.GetEnumerator().MoveNext())
                                    {
                                        parts.Add(partNode);
                                    }
                                    else
                                    {
                                        foreach (TreeNode streamNode in partNode.Nodes)
                                        {
                                            if ((streamNode.Tag as Stream).Bytes <= 1)
                                            {
                                                streams.Add(streamNode);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            #region Transact on DB for Delete:
            // - conferences
            int[] confIDs = new int[confs.Count];
            for (int cnt = 0; cnt < confIDs.Length; ++cnt)
            {
                confIDs[cnt] = ((confs[cnt] as TreeNode).Tag as Conference).ConferenceID;
            }
            DBHelper.DeleteConferences(confIDs);

            // - participants
            int[] partIDs = new int[parts.Count];
            for (int cnt = 0; cnt < partIDs.Length; ++cnt)
            {
                partIDs[cnt] = ((parts[cnt] as TreeNode).Tag as Participant).ParticipantID;
            }
            DBHelper.DeleteParticipants(partIDs);

            // - streams
            int[] streamIDs = new int[streams.Count];
            for (int cnt = 0; cnt < streamIDs.Length; ++cnt)
            {
                streamIDs[cnt] = ((streams[cnt] as TreeNode).Tag as Stream).StreamID;
            }
            DBHelper.DeleteStreams(streamIDs);
            #endregion

            // Delete from UI
            RemoveNodes(confs);
            RemoveNodes(parts);
            RemoveNodes(streams);

            // Find Conferences w/o end times
            // (for simplicity, just get them from the DB again)
            Conference[] allConfs = DBHelper.GetConferences();
            foreach (Conference conf in allConfs)
            {
                if (conf.End == DateTime.MinValue)
                {
                    DBHelper.CreateConferenceEndTime(conf.ConferenceID);
                }
            }
        }