/// <summary>
        ///     Gets the document stream for the specified entity identifier.
        /// </summary>
        /// <param name="entityId">The entity id.</param>
        /// <returns>The open file stream for the document stored in the file stream table.</returns>
        /// <remarks></remarks>
        private static RevisionData GetDocumentStreamForRevisionId(long entityId)
        {
            Stream stream = null;
            // Get the document revision
            var documentRevision = Entity.Get <DocumentRevision>(entityId);

            if (documentRevision == null)
            {
                EventLog.Application.WriteError("Unable to get the document revision stream, invalid document entityId {0}", entityId);
                return(null);
            }

            stream = FileRepositoryHelper.GetFileDataStreamForToken(Factory.DocumentFileRepository, documentRevision.FileDataHash);

            RevisionData result = new RevisionData
            {
                Stream        = stream,
                FileExtension = (documentRevision.FileExtension ?? "").ToLowerInvariant().Trim()
            };

            return(result);
        }
        /// <summary>
        ///     Worker thread method to generate a report document.
        /// </summary>
        /// <param name="reportSettings">The report-template settings.</param>
        /// <param name="taskInfo">The long-running task info object.</param>
        private static void GenerateDocumentWorker(ReportTemplateSettings reportSettings, LongRunningInfo taskInfo)
        {
            // Load report template resource
            var reportTemplate = Entity.Get <ReadiNow.Model.ReportTemplate>(reportSettings.ReportTemplate);

            if (reportTemplate == null)
            {
                throw new Exception("Report template could not be loaded.");
            }

            // Load the document resource
            if (reportTemplate.ReportTemplateUsesDocument == null)
            {
                throw new Exception("Report template has no associated template document.");
            }

            long docId = reportTemplate.ReportTemplateUsesDocument.Id;

            // Load the document stream
            RevisionData templateData   = GetLatestDocument(docId);
            Stream       templateStream = templateData.Stream;
            string       extension      = templateData.FileExtension;

            // Verify supported types
            if (extension != ".docx" && extension != ".dotx" && extension != ".docm" && extension != ".dotm")
            {
                throw new Exception("Report template has an unsupported file extension.");
            }

            // Verify resource types
            VerifyResourceTypes(reportSettings, reportTemplate);

            // Fill settings
            var settings = new GeneratorSettings
            {
                TimeZoneName = reportSettings.TimeZone
            };

            if (reportSettings.SelectedResources != null)
            {
                settings.SelectedResourceId = reportSettings.SelectedResources.First( ).Id;
            }

            // Generate the document
            var outputSteam = new MemoryStream( );

            Factory.DocumentGenerator.CreateDocument(templateStream, outputSteam, settings);
            outputSteam.Flush( );
            outputSteam.Position = 0;

            // Store file stream in the temporary table
            string token = PersistTemporaryFile(outputSteam);

            // Store the result into a document entity
            string filename   = string.Format("{0} {1:s}", reportTemplate.Name, DateTime.Now);
            long   documentId = StoreFileInEntity(outputSteam, filename, extension, token);

            // Return token that is the document identifier that is used for the download of the document file entity.
            taskInfo.ResultData = documentId.ToString(CultureInfo.InvariantCulture);
            LongRunningHelper.SaveLongRunningTaskInfo(taskInfo);
        }