public void AutoRefreshGivenQuery(string queryName, Document wordDocument, ISyncServiceDocumentModel documentModel, IConfiguration configuration)
        {
            //RegisterServices();

            _documentModel = documentModel;
            _wordDocument  = documentModel.WordDocument as Document;

            _configuration = configuration;
            _documentModel.Configuration.RefreshMappings();
            _documentModel.Configuration.ActivateMapping(_documentModel.MappingShowName);

            //Get the service
            GetService();

            //Search the workitems
            _workItems = GetWorkItemsIdsByQueryName(queryName);

            Find();

            while (IsFindingWorkItems)
            {
                Thread.CurrentThread.Join(50);
            }

            // Show how many work items were found
            ConsoleExtensionLogging.LogMessage(string.Format("{0}{1}", Resources.WorkItemsCountInformation, FoundWorkItems.Count), ConsoleExtensionLogging.LogLevel.Both);
            SyncServiceTrace.D((string.Format("{0}{1}", Resources.WorkItemsCountInformation, FoundWorkItems.Count)));

            Import();
            while (IsImporting)
            {
                Thread.CurrentThread.Join(50);
            }
        }
        /// <summary>
        ///  This method creates a word document with the GetWorkItems-Command
        /// </summary>
        /// <param name="documentConfiguration">The configuration</param>
        /// <param name="workItems">The string that identifies the work items that should be imported</param>
        /// <param name="type">The type of the import operation</param>
        public void CreateWorkItemDocument(DocumentConfiguration documentConfiguration, string workItems, string type)
        {
            try
            {
                _documentConfiguration = documentConfiguration;

                // Initialize the document
                Initialize();

                if (type.Equals("ByID"))
                {
                    _workItems = GetWorkItemsIdsByString(workItems);
                }
                else if (type.Equals("ByQuery"))
                {
                    _workItems = GetWorkItemsIdsByQueryName(workItems);
                }

                if (_workItems == null || _workItems.Count < 1)
                {
                    SyncServiceTrace.E(Resources.NoWorkItemsFoundInformation);
                    throw new Exception(Resources.NoWorkItemsFoundInformation);
                }

                // Get the work items that should be imported / This will come from the query settings
                Find();

                while (IsFindingWorkItems)
                {
                    Thread.CurrentThread.Join(50);
                }

                // Show how many work items were found
                ConsoleExtensionLogging.LogMessage(string.Format("{0}{1}", Resources.WorkItemsCountInformation, FoundWorkItems.Count), ConsoleExtensionLogging.LogLevel.Both);
                SyncServiceTrace.D(string.Format("{0}{1}", Resources.WorkItemsCountInformation, FoundWorkItems.Count));

                Import();

                while (IsImporting)
                {
                    Thread.CurrentThread.Join(50);
                }

                ConsoleExtensionLogging.LogMessage(Resources.WorkItemsImportedInformation, ConsoleExtensionLogging.LogLevel.Both);
                SyncServiceTrace.D(Resources.WorkItemsImportedInformation);

                FinalizeDocument();
            }
            catch (Exception e)
            {
                // close any open word applications form this method
                if (_wordApplication != null)
                {
                    _wordApplication.Quit();
                }
                SyncServiceTrace.LogException(e);
                // ReSharper disable once PossibleIntendedRethrow
                throw e;
            }
        }
        /// <summary>
        /// Convert a string to a list of work Items
        /// </summary>
        /// <param name="workitems"></param>
        /// <returns>ids of workitems</returns>
        private List <int> GetWorkItemsIdsByString(string workitems)
        {
            ConsoleExtensionLogging.LogMessage(Resources.GetWorkItemById, ConsoleExtensionLogging.LogLevel.Both);
            SyncServiceTrace.D(Resources.GetWorkItemById);

            var stringIDs = workitems.Split(new[] { ' ', ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
            int intValue;

            return(stringIDs.Where(x => int.TryParse(x, out intValue)).Select(int.Parse).ToList());
        }
        /// <summary>
        /// Finalize the document
        /// </summary>
        private void FinalizeDocument()
        {
            SyncServiceTrace.D(Resources.FinalizationOfDoc);
            UndoPreparationsDocumentForReportGeneration();
            _wordDocument.SaveAs(_fileName);

            ConsoleExtensionLogging.LogMessage(string.Format(Resources.FileSavedInformation, _fileName), ConsoleExtensionLogging.LogLevel.Both);
            SyncServiceTrace.D(string.Format(Resources.FileSavedInformation, _fileName));
            if (_documentConfiguration.Settings.CloseOnFinish)
            {
                _wordDocument.Close(true);
                _wordApplication.Quit();
            }

            ConsoleExtensionLogging.LogMessage(Resources.WordClosed, ConsoleExtensionLogging.LogLevel.Both);
            SyncServiceTrace.D(Resources.WordClosed);
        }
        /// <summary>
        /// Get all work item ids for a given query name
        /// </summary>
        /// <param name="query"></param>
        /// <returns>ids of workitems</returns>
        private List <int> GetWorkItemsIdsByQueryName(string query)
        {
            ConsoleExtensionLogging.LogMessage(Resources.GetWorkItemsByQueryName + query, ConsoleExtensionLogging.LogLevel.Both);
            SyncServiceTrace.D(Resources.GetWorkItemsByQueryName + query);
            var queryconfig = new QueryConfiguration
            {
                QueryPath    = query,
                ImportOption = QueryImportOption.SavedQuery
            };
            var adapter        = new Tfs2012SyncAdapter(_documentModel.TfsServer, _documentModel.TfsProject, queryconfig, _documentModel.Configuration);
            var intIDs         = new List <int>();
            var queryWorkItems = adapter.LoadWorkItemsFromSavedQuery(queryconfig);

            foreach (var queryWorkItem in queryWorkItems)
            {
                intIDs.Add(queryWorkItem.Id);
            }

            return(intIDs);
        }