/// <summary> Get PT book text in USX, or throw if can't. </summary> public string GetBookText(UserSecret userSecret, string ptProjectId, int bookNum) { ScrText scrText = ScrTextCollection.FindById(GetParatextUsername(userSecret), ptProjectId); if (scrText == null) { throw new DataNotFoundException("Can't get access to cloned project."); } string usfm = scrText.GetText(bookNum); return(UsfmToUsx.ConvertToXmlString(scrText, bookNum, usfm, false)); }
/// <summary> /// Creates an internal test project. /// </summary> /// <param name="sourceId">The source project/resource identifier.</param> /// <param name="targetId">The target project identifier. /// This is the project that will reference this project/resource a source.</param> /// <returns>THe task</returns> /// <exception cref="DataNotFoundException"> /// The target project does not exist /// or /// The user does not exist. /// </exception> /// <remarks> /// This is only to be used on test runs! /// </remarks> internal async Task CreateInternalTestProjectAsync(string sourceId, string targetId) { if (!this._testProjectCollection.Any(p => p.ParatextId == sourceId) && !string.IsNullOrWhiteSpace(sourceId)) { // Create the test project SFProject testProject = new SFProject { Id = ObjectId.GenerateNewId().ToString(), ParatextId = sourceId, }; this._testProjectCollection.Add(testProject); // Load the source project from the target project's source directory (it is not moved in test mode) ScrText?scrText = SourceScrTextCollection.FindById("admin", targetId); if (scrText != null) { // Create the test text objects for all of the books foreach (int bookNum in scrText.Settings.BooksPresentSet.SelectedBookNumbers) { string usfm = scrText.GetText(bookNum); string bookText = UsfmToUsx.ConvertToXmlString(scrText, bookNum, usfm, false); var usxDoc = XDocument.Parse(bookText); Dictionary <int, ChapterDelta> deltas = this._deltaUsxMapper.ToChapterDeltas(usxDoc).ToDictionary(cd => cd.Number); var chapters = new List <Chapter>(); foreach (KeyValuePair <int, ChapterDelta> kvp in deltas) { this._testTextDataIdCollection.Add(TextData.GetTextDocId(testProject.Id, bookNum, kvp.Key)); } } } else { Program.Log($"Test Mode Error Migrating TextData For {sourceId} - Could Not Load From Filesystem!"); } // See that at least one user in the target project has permission to create the source project var targetProject = this._realtimeService.QuerySnapshots <SFProject>().FirstOrDefault(p => p.ParatextId == targetId); if (targetProject == null) { throw new DataNotFoundException("The target project does not exist"); } // Get the highest ranked users for this project, that probably have source access string[] userIds = targetProject.UserRoles .Where(ur => ur.Value == SFProjectRole.Administrator || ur.Value == SFProjectRole.Translator) .OrderBy(ur => ur.Value) .Select(ur => ur.Key) .ToArray(); bool someoneCanAccessSourceProject = false; foreach (string userId in userIds) { Attempt <UserSecret> userSecretAttempt = await _userSecrets.TryGetAsync(userId); if (!userSecretAttempt.TryResult(out UserSecret userSecret)) { throw new DataNotFoundException("The user does not exist."); } // We check projects first, in case it is a project IReadOnlyList <ParatextProject> ptProjects = await _paratextService.GetProjectsAsync(userSecret); if (ptProjects.Any(p => p.ParatextId == sourceId)) { someoneCanAccessSourceProject = true; break; } } if (!someoneCanAccessSourceProject) { Program.Log($"Test Mode Error Creating {sourceId} - Nobody In The Target Project Has Access!"); } } }
private UsxDocument GetUsxDocumentForBook(int bookNum) { return(new UsxDocument(UsfmToUsx.ConvertToXmlDocument(UnderlyingScrText, bookNum, UnderlyingScrText.GetText(bookNum)))); }