/// <summary>
        /// Find those Task Assistant workbooks in the database that are not being used by
        /// any step that launches ArcMap
        /// </summary>
        /// <returns>The total number of orphaned items found</returns>
        private int UpdateOrphanedTaWorkbooks()
        {
            SortedList <string, string> unusedItems = new SortedList <string, string>();
            IJTXDatabase3      wmxDb     = this.WmxDatabase;
            IJTXConfiguration3 configMgr = wmxDb.ConfigurationManager as IJTXConfiguration3;
            IJTXTaskAssistantWorkflowRecordSet allItems = configMgr.TaskAssistantWorkflowRecords;

            // First check to see if there are even any TA workbooks in the database
            if (allItems.Count > 0)
            {
                Dictionary <string, string> usedTypes = new Dictionary <string, string>();

                // Search all the step types for Task Assistant workbooks currently in use
                IJTXStepTypeSet allStepTypes = wmxDb.ConfigurationManager.StepTypes;
                for (int i = 0; i < allStepTypes.Count; i++)
                {
                    IJTXStepType2 stepType = allStepTypes.get_Item(i) as IJTXStepType2;

                    // Skip over unused step types
                    if (m_unusedStepTypes.Keys.Contains(stepType.ID))
                    {
                        continue;
                    }

                    // Examine the remaining step types
                    for (int j = 0; j < stepType.Arguments.Length; j++)
                    {
                        string stepArg = stepType.Arguments[j].ToString();
                        if (stepArg.StartsWith(C_WORKBOOK_FLAG))
                        {
                            string suffix = stepArg.Substring(C_WORKBOOK_FLAG.Length);
                            suffix            = suffix.Trim(new char[] { '"' });
                            usedTypes[suffix] = null;
                        }
                    }
                }

                // Loop over all the Task Assistant workbooks, looking for anything
                // that we didn't identify as "in use"
                for (int i = 0; i < allItems.Count; i++)
                {
                    IJTXTaskAssistantWorkflowRecord item = allItems.get_Item(i);
                    if (!usedTypes.ContainsKey(item.Alias))
                    {
                        m_unusedTaWorkbooks[item.Alias] = null;
                    }
                }
            }

            return(m_unusedTaWorkbooks.Count);
        }
        /// <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 map documents embedded in the database that are not being
        /// referenced in any way
        /// </summary>
        /// <returns>The total number of orphaned items found</returns>
        private int UpdateOrphanedMapDocuments()
        {
            SortedList <string, int> unusedItems = new SortedList <string, int>();
            IJTXDatabase3            wmxDb       = this.WmxDatabase;
            IJTXConfiguration3       configMgr   = wmxDb.ConfigurationManager as IJTXConfiguration3;
            IJTXConfigurationEdit2   configEdit  = wmxDb.ConfigurationManager as IJTXConfigurationEdit2;

            IJTXMapSet allMaps = configMgr.JTXMaps;
            Dictionary <string, int> allMapNames = new Dictionary <string, int>();

            for (int i = 0; i < allMaps.Count; i++)
            {
                IJTXMap map = allMaps.get_Item(i);
                allMapNames[map.Name] = map.ID;
            }

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

            // Find the map types that are associated with job types
            IJTXJobTypeSet allJobTypes = configMgr.JobTypes;

            for (int i = 0; i < allJobTypes.Count; i++)
            {
                // TODO: Skip orphaned job types

                IJTXJobType3 jobType = allJobTypes.get_Item(i) as IJTXJobType3;
                if (jobType.AOIMap != null)
                {
                    usedItems[jobType.AOIMap.Name] = jobType.AOIMap.ID;
                }
                if (jobType.JobMap != null)
                {
                    usedItems[jobType.JobMap.Name] = jobType.JobMap.ID;
                }
            }

            // If necessary, find the map types launched by custom steps.  Look for
            // the "/mxd:" argument as an identifier.
            IJTXStepTypeSet allStepTypes = wmxDb.ConfigurationManager.StepTypes;

            for (int i = 0; i < allStepTypes.Count; i++)
            {
                IJTXStepType2 stepType = allStepTypes.get_Item(i) as IJTXStepType2;

                // Skip orphaned step types
                if (m_unusedStepTypes.Keys.Contains(stepType.ID))
                {
                    continue;
                }

                for (int j = 0; j < stepType.Arguments.Length; j++)
                {
                    string stepArg = stepType.Arguments[j].ToString();
                    if (stepArg.StartsWith(C_MAP_DOC_FLAG))
                    {
                        string suffix = stepArg.Substring(C_MAP_DOC_FLAG.Length);
                        suffix = suffix.Trim(new char[] { '"' });
                        if (allMapNames.Keys.Contains(suffix))
                        {
                            usedItems[suffix] = allMapNames[suffix];
                        }
                    }
                }
            }

            // Add in the map document that's used as the template map document
            // (if one exists)
            IJTXConfigurationProperties configProps = this.WmxDatabase.ConfigurationManager as IJTXConfigurationProperties;
            string mapIdStr = configProps.GetProperty(Constants.JTX_PROPERTY_MAPVIEW_MAP_GUID);

            if (mapIdStr != null && !mapIdStr.Equals(string.Empty))
            {
                for (int i = 0; i < allMaps.Count; i++)
                {
                    IJTXMap        tempMap   = allMaps.get_Item(i);
                    IJTXIdentifier tempMapId = tempMap as IJTXIdentifier;
                    if (tempMapId.GUID.Equals(mapIdStr))
                    {
                        usedItems[tempMap.Name] = tempMap.ID;
                        break;
                    }
                }
            }

            // Loop over all the map documents in the DB, looking for anything
            // that we didn't identify as "in use"
            foreach (string name in allMapNames.Keys)
            {
                if (!usedItems.ContainsKey(name))
                {
                    m_unusedMapDocs[name] = allMapNames[name];
                }
            }

            return(m_unusedMapDocs.Count);
        }
        /// <summary>
        /// Helper function that runs all of those checks that operate on each step in
        /// every workflow; intended to make the checks slightly more efficient by running
        /// through them all at once rather than looping through all of the elements
        /// multiple times
        /// </summary>
        /// <param name="msgs">Add any GP messages to this object</param>
        /// <param name="errorCount">Counter used to track the number of problems found</param>
        /// <param name="logFileWriter">Object used to write error descriptions to a text file</param>
        private void ExecuteWorkflowStepChecks(IGPMessages msgs, ref int errorCount, StreamWriter logFileWriter)
        {
            // Only continue executing this function if needed
            if (!m_flagInvalidStepAssign &&
                !m_flagUnassignedSteps &&
                !m_flagZeroPctSteps &&
                !m_flagDifferingStepNames)
            {
                return;
            }

            IJTXConfiguration3     configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;
            IJTXConfigurationEdit2 configEdit = configMgr as IJTXConfigurationEdit2;

            // Put the items into a sorted list in order to make the output easier to
            // read/follow
            IJTXWorkflowSet allWorkflows = configMgr.Workflows;
            SortedList <string, IJTXWorkflow> allWorkflowsSorted = new SortedList <string, IJTXWorkflow>();

            for (int i = 0; i < allWorkflows.Count; i++)
            {
                allWorkflowsSorted[allWorkflows.get_Item(i).Name] = allWorkflows.get_Item(i);
            }

            // Iterate through each item
            foreach (IJTXWorkflow workflow in allWorkflowsSorted.Values)
            {
                IJTXWorkflowConfiguration workflowCfg = workflow as IJTXWorkflowConfiguration;
                int[] allStepIds = workflowCfg.GetAllSteps();
                foreach (int j in allStepIds)
                {
                    IJTXStep3 step       = workflowCfg.GetStep(j) as IJTXStep3;
                    string    assignedTo = step.AssignedTo;

                    // Check for any default step types with an invalid step assignment
                    if (m_flagInvalidStepAssign)
                    {
                        if (step.AssignedType == jtxAssignmentType.jtxAssignmentTypeUser && configMgr.GetUser(assignedTo) == null)
                        {
                            string message = "Workflow '" + workflow.Name + "', step '" +
                                             step.StepName + "' assigned to unknown user '" + assignedTo + "'";
                            RecordMessage(message, msgs, logFileWriter);
                            errorCount++;
                        }
                        else if (step.AssignedType == jtxAssignmentType.jtxAssignmentTypeGroup && configMgr.GetUserGroup(assignedTo) == null)
                        {
                            string message = "Workflow '" + workflow.Name + "', step '" +
                                             step.StepName + "' assigned to unknown group '" + assignedTo + "'";
                            RecordMessage(message, msgs, logFileWriter);
                            errorCount++;
                        }
                    }

                    // Check for any steps that have not been assigned to a group or user
                    if (m_flagUnassignedSteps)
                    {
                        if (step.AssignedType == jtxAssignmentType.jtxAssignmentTypeUnassigned)
                        {
                            string message = "Workflow '" + workflow.Name + "', step '" +
                                             step.StepName + "' is unassigned";
                            RecordMessage(message, msgs, logFileWriter);
                            errorCount++;
                        }
                    }

                    // Check for any steps whose "post-complete" percentage is 0
                    if (m_flagZeroPctSteps)
                    {
                        if (step.DefaultPercComplete < double.Epsilon)
                        {
                            string message = "Workflow '" + workflow.Name + "', step '" +
                                             step.StepName + "' sets percent complete to 0";
                            RecordMessage(message, msgs, logFileWriter);
                            errorCount++;
                        }
                    }

                    // Check for any steps whose descriptions in a workflow does not match
                    // the underlying step type name
                    if (m_flagDifferingStepNames)
                    {
                        IJTXStepType2 stepType = configMgr.GetStepTypeByID(step.StepTypeID) as IJTXStepType2;
                        if (!step.StepName.Equals(stepType.Name))
                        {
                            string message = "Workflow '" + workflow.Name + "', step name '" +
                                             step.StepName + "' does not match step type name '" + stepType.Name + "'";
                            RecordMessage(message, msgs, logFileWriter);
                            errorCount++;
                        }
                    }
                } // end for each step
            }     // end for each workflow
        }