public BookPage GetWithRenderingDocument(DocumentFile activeDocument) { var page = this; if (this.Document == activeDocument) { page = new BookPage(this.Document, this.Chapter); page.Active = true; page.SubPageActive = false; page.SubPages = this.SubPages; } else if (this.SubPages != null) { var subPages = this.SubPages.Select(p => p.GetWithRenderingDocument(activeDocument)).ToList(); if (subPages.Any(c => c.Active || c.SubPageActive)) { page = new BookPage(this.Document, this.Chapter); page.Active = false; page.SubPageActive = true; page.SubPages = subPages; } } return page; }
private IEnumerable<Book> ProcessExplicitOrder(ILookup<string, DocumentFile> documentsById) { var ordered = this.Documents.Where(d => d.Order > 0) .OrderBy(d => d.Order) .ToList(); var orderedGroupedByParent = ordered.GroupBy(d => d.ParentId); var documentsToChapter = new Dictionary<DocumentFile, BookPage>(); var books = new List<Book>(); foreach (var groupedDocuments in orderedGroupedByParent) { var parentDocument = documentsById[groupedDocuments.Key].FirstOrDefault(); // If there is no parent document or the parent document is not an ordered // document then this set of grouped documents must be a set of chapters in // a new book. if (parentDocument == null || parentDocument.Order == 0) { var chapters = new List<BookPage>(); foreach (var document in groupedDocuments) { BookPage chapter; if (!documentsToChapter.TryGetValue(document, out chapter)) { chapter = new BookPage(document, true); } Debug.Assert(chapter.Document == document); chapters.Add(chapter); documentsToChapter.Add(document, chapter); } var book = new Book(groupedDocuments.Key, chapters, parentDocument, null); books.Add(book); } else { BookPage chapter; // If the parent document has not been processed yet, make it a chapter page now. if (!documentsToChapter.TryGetValue(parentDocument, out chapter)) { chapter = new BookPage(parentDocument, true); documentsToChapter.Add(parentDocument, chapter); } else { // If the document is not already a chapter, convert the existing // page for the document into a sub-chapter. if (chapter.Document != parentDocument) { for (var i = 0; i < chapter.SubPages.Count; ++i) { var page = chapter.SubPages[i]; if (page.Document == parentDocument) { Debug.Assert(!page.Chapter); var subChapter = new BookPage(parentDocument, true); chapter.SubPages[i] = subChapter; documentsToChapter[parentDocument] = chapter; chapter = subChapter; break; } } } } foreach (var document in groupedDocuments) { chapter.SubPages.Add(new BookPage(document)); documentsToChapter.Add(document, chapter); } } } foreach (var book in books) { DocumentFile previous = null; foreach (var chapter in book.Chapters) { previous = ProcessBookAndChapterOrder(book, chapter, book.ParentDocument, previous); } } return books; }
private static DocumentFile ProcessBookAndChapterOrder(Book book, BookPage chapter, DocumentFile parent, DocumentFile previous) { var bookWithActiveDocument = book.GetBookWithRenderingDocument(chapter.Document); chapter.Document.Book = bookWithActiveDocument; chapter.Document.Chapter = AllChapters(bookWithActiveDocument).Where(c => c.Document == chapter.Document).Single(); previous = SetNextPreviousAndParent(previous, chapter.Document, parent); foreach (var page in chapter.SubPages) { previous = SetNextPreviousAndParent(previous, page.Document, chapter.Document); if (page.SubPages != null && page.SubPages.Any()) { previous = ProcessBookAndChapterOrder(book, page, chapter.Document, previous); } else { page.Document.Book = book.GetBookWithRenderingDocument(page.Document); } } return previous; }