/// <summary>
        /// Find those step types in the database that are not being used by any workflow.
        /// </summary>
        /// <returns>The total number of orphaned items found</returns>
        private int UpdateOrphanedStepTypes()
        {
            Dictionary <int, int> usedStepTypes = new Dictionary <int, int>();
            IJTXDatabase3         wmxDb         = this.WmxDatabase;
            IJTXConfiguration3    configMgr     = wmxDb.ConfigurationManager as IJTXConfiguration3;
            IJTXWorkflowSet       workflows     = configMgr.Workflows;

            // Iterate through each workflow, building up the list of used steps
            for (int i = 0; i < workflows.Count; i++)
            {
                // Skip over those workflows that aren't in use
                IJTXWorkflow workflow = workflows.get_Item(i);
                if (m_unusedWorkflows.Keys.Contains(workflow.ID))
                {
                    continue;
                }

                // Examine the remaining workflows
                IJTXWorkflowConfiguration workflowCfg = workflows.get_Item(i) as IJTXWorkflowConfiguration;
                int[] stepIds = workflowCfg.GetAllSteps();
                foreach (int stepId in stepIds)
                {
                    int stepTypeId = workflowCfg.GetStep(stepId).StepTypeID;
                    usedStepTypes[stepTypeId] = stepTypeId;
                }
            }

            // Get the complete list of step types in the database
            IJTXStepTypeSet allStepTypeObjs = configMgr.StepTypes;

            // Loop over all of the step types.  For anything whose step type ID is not
            // contained in the "used" list, add it to the "unused" list.
            // If all of the items are used, don't bother trying to add to the
            // unused list.
            if (usedStepTypes.Count != allStepTypeObjs.Count)
            {
                for (int i = 0; i < allStepTypeObjs.Count; i++)
                {
                    IJTXStepType2 stepType   = allStepTypeObjs.get_Item(i) as IJTXStepType2;
                    int           stepTypeId = stepType.ID;
                    if (!usedStepTypes.ContainsKey(stepTypeId))
                    {
                        m_unusedStepTypes[stepTypeId] = stepType.Name;
                    }
                }
            }

            return(m_unusedStepTypes.Count);
        }
        /// <summary>
        /// Find those workflows in the database that are not being used by any job type.
        /// </summary>
        /// <returns>The total number of orphaned items found</returns>
        private int UpdateOrphanedWorkflows()
        {
            Dictionary <int, int> usedWorkflows = new Dictionary <int, int>();
            IJTXDatabase3         wmxDb         = this.WmxDatabase;
            IJTXConfiguration3    configMgr     = wmxDb.ConfigurationManager as IJTXConfiguration3;
            IJTXJobTypeSet        jobTypes      = configMgr.JobTypes;

            // Iterate through each item in the set, building up the list of used types
            for (int i = 0; i < jobTypes.Count; i++)
            {
                // TODO: Do not consider items that we know are not in use

                IJTXJobType3 type = jobTypes.get_Item(i) as IJTXJobType3;
                // job type don't always need to have a workflow
                if (type.Workflow != null)
                {
                    usedWorkflows[type.Workflow.ID] = type.Workflow.ID;
                }
            }

            // Get the complete list of this type of object in the database
            IJTXWorkflowSet workflows = configMgr.Workflows;

            // Loop through the complete list of this object type.  If any of the IDs
            // are not in the "used" list, add that object to the "unused" list.
            // If all of the items are used, don't bother trying to add to the
            // unused list.
            if (usedWorkflows.Count != workflows.Count)
            {
                for (int i = 0; i < workflows.Count; i++)
                {
                    IJTXWorkflow workflow = workflows.get_Item(i) as IJTXWorkflow;
                    if (!usedWorkflows.ContainsKey(workflow.ID))
                    {
                        m_unusedWorkflows[workflow.ID] = workflow.Name;
                    }
                }
            }

            return(m_unusedWorkflows.Count);
        }
        /// <summary>
        /// Find those users in the database who are not being referenced in any way
        /// </summary>
        /// <returns>The total number of orphaned items found</returns>
        private int UpdateOrphanedUsers()
        {
            SortedList <string, string> unusedItems = new SortedList <string, string>();
            IJTXDatabase3      wmxDb     = this.WmxDatabase;
            IJTXConfiguration3 configMgr = wmxDb.ConfigurationManager as IJTXConfiguration3;
            IJTXJobManager     jobMgr    = wmxDb.JobManager;
            IJTXUserSet        allUsers  = configMgr.Users;

            Dictionary <string, string> usedItems = new Dictionary <string, string>();

            // Get all of the users who are members of a group
            IJTXUserGroupSet allGroups = configMgr.UserGroups;

            for (int i = 0; i < allGroups.Count; i++)
            {
                IJTXUserGroup2 group = allGroups.get_Item(i) as IJTXUserGroup2;
                for (int j = 0; j < group.Users.Count; j++)
                {
                    IJTXUser3 user = group.Users.get_Item(j) as IJTXUser3;
                    usedItems[user.UserName] = user.FullName;
                }
            }

            // If necessary, add in the users who have jobs assigned to them
            if (usedItems.Count < allUsers.Count)
            {
                IJTXJobSet allJobs = jobMgr.GetAllJobs();
                for (int i = 0; i < allJobs.Count; i++)
                {
                    IJTXJob3 job = allJobs.get_Item(i) as IJTXJob3;
                    if (job.AssignedType == jtxAssignmentType.jtxAssignmentTypeUser)
                    {
                        IJTXUser3 user = configMgr.GetUser(job.AssignedTo) as IJTXUser3;

                        // It's possible for a user to have a job assigned, but have
                        // already been removed from the DB.  Throw an exception in
                        // this case, as the DB needs to be cleaned up.
                        if (user == null)
                        {
                            throw new WmauException(WmauErrorCodes.C_USER_NOT_FOUND_ERROR);
                        }
                        usedItems[user.UserName] = user.FullName;
                    }
                }
            }

            // If necessary, add in the users who have a job type's default assignment
            // set to them
            if (usedItems.Count < allUsers.Count)
            {
                IJTXJobTypeSet allJobTypes = configMgr.JobTypes;
                for (int i = 0; i < allJobTypes.Count; i++)
                {
                    // TODO: Exclude orphaned job types

                    IJTXJobType3 jobType = allJobTypes.get_Item(i) as IJTXJobType3;
                    if (jobType.DefaultAssignedType == jtxAssignmentType.jtxAssignmentTypeUser)
                    {
                        IJTXUser3 user = configMgr.GetUser(jobType.DefaultAssignedTo) as IJTXUser3;

                        // It's possible for a user to have a job assigned, but have
                        // already been removed from the DB.  Throw an exception in
                        // this case, as the DB needs to be cleaned up.
                        if (user == null)
                        {
                            throw new WmauException(WmauErrorCodes.C_USER_NOT_FOUND_ERROR);
                        }
                        usedItems[user.UserName] = user.FullName;
                    }
                }
            }

            // If necessary, add in the users who have steps assigned to them
            // by default
            if (usedItems.Count < allUsers.Count)
            {
                IJTXWorkflowSet allWorkflows = configMgr.Workflows;
                for (int i = 0; i < allWorkflows.Count; i++)
                {
                    // Skip over unused workflows
                    IJTXWorkflow workflow = allWorkflows.get_Item(i);
                    if (m_unusedWorkflows.Keys.Contains(workflow.ID))
                    {
                        continue;
                    }

                    // Examine the other items
                    IJTXWorkflowConfiguration workflowCfg = allWorkflows.get_Item(i) as IJTXWorkflowConfiguration;
                    int[] workflowStepIds = workflowCfg.GetAllSteps();
                    foreach (int j in workflowStepIds)
                    {
                        IJTXStep3 step = workflowCfg.GetStep(j) as IJTXStep3;
                        if (step.AssignedType == jtxAssignmentType.jtxAssignmentTypeUser)
                        {
                            IJTXUser3 user = configMgr.GetUser(step.AssignedTo) as IJTXUser3;

                            // It's possible for a user to have a job assigned, but have
                            // already been removed from the DB.  Throw an exception in
                            // this case, as the DB needs to be cleaned up.
                            if (user == null)
                            {
                                throw new WmauException(WmauErrorCodes.C_USER_NOT_FOUND_ERROR);
                            }
                            usedItems[user.UserName] = user.FullName;
                        }
                    }
                }
            }

            // Loop over all the users in the DB, looking for anything
            // that we didn't identify as "in use"
            for (int i = 0; i < allUsers.Count; i++)
            {
                IJTXUser3 item = allUsers.get_Item(i) as IJTXUser3;
                if (!usedItems.ContainsKey(item.UserName))
                {
                    m_unusedUsers[item.UserName] = item.FullName;
                }
            }

            return(m_unusedUsers.Count);
        }
        /// <summary>
        /// Find those status types in the database that are not being used by any step.
        /// </summary>
        /// <returns>The total number of orphaned items found</returns>
        private int UpdateOrphanedStatusTypes()
        {
            Dictionary <int, int> usedStatusTypes = new Dictionary <int, int>();
            IJTXDatabase3         wmxDb           = this.WmxDatabase;

            string[] coreStatusNames =
            {
                C_STATUS_CLOSED,
                C_STATUS_CREATED,
                C_STATUS_DONE_WORKING,
                C_STATUS_READY_TO_WORK,
                C_STATUS_WORKING
            };

            IJTXConfiguration3 configMgr = wmxDb.ConfigurationManager as IJTXConfiguration3;

            // Iterate through each job currently in the system, adding the status types used
            // by these jobs
            IJTXJobSet jobs = wmxDb.JobManager.GetAllJobs();

            for (int i = 0; i < jobs.Count; i++)
            {
                IJTXJob3 job = jobs.get_Item(i) as IJTXJob3;
                if (job.Status != null)
                {
                    usedStatusTypes[job.Status.ID] = job.Status.ID;
                }
            }

            // Iterate through each workflow, building up the list of used status types
            // based on the statuses that are assigned by each step
            IJTXWorkflowSet workflows = configMgr.Workflows;

            for (int i = 0; i < workflows.Count; i++)
            {
                // Skip over those workflows that aren't in use
                IJTXWorkflow workflow = workflows.get_Item(i);
                if (m_unusedWorkflows.Keys.Contains(workflow.ID))
                {
                    continue;
                }

                // Examine the remaining workflows
                IJTXWorkflowConfiguration workflowCfg = workflows.get_Item(i) as IJTXWorkflowConfiguration;
                int[] stepIds = workflowCfg.GetAllSteps();
                foreach (int stepId in stepIds)
                {
                    IJTXStep3 step = workflowCfg.GetStep(stepId) as IJTXStep3;
                    usedStatusTypes[step.StatusID] = step.StatusID;
                }
            }

            // Add the status types used by Workflow Manager itself
            foreach (string s in coreStatusNames)
            {
                IJTXStatus2 status = configMgr.GetStatus(s) as IJTXStatus2;

                // Avoid problems if someone deleted one of these mandatory types from the database
                if (status != null)
                {
                    int id = status.ID;
                    usedStatusTypes[id] = id;
                }
            }

            // Get the complete list of status types in the database
            IJTXStatusSet allStatusTypes = configMgr.Statuses;

            // Loop over all of the status types.  For anything whose ID is not contained
            // in the "used" list, add it to the "unused" list. If all of the items are
            // used, don't bother trying to add to the unused list.
            if (usedStatusTypes.Count != allStatusTypes.Count)
            {
                for (int i = 0; i < allStatusTypes.Count; i++)
                {
                    IJTXStatus2 statusType = allStatusTypes.get_Item(i) as IJTXStatus2;
                    if (!usedStatusTypes.ContainsKey(statusType.ID))
                    {
                        m_unusedStatusTypes[statusType.ID] = statusType.Name;
                    }
                }
            }

            return(m_unusedStatusTypes.Count);
        }