1 16 package org.outerj.daisy.books.publisher.impl.dataretrieval; 17 18 import org.outerj.daisy.books.publisher.impl.bookmodel.Book; 19 import org.outerj.daisy.books.publisher.impl.bookmodel.SectionContainer; 20 import org.outerj.daisy.books.publisher.impl.bookmodel.Section; 21 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 22 import org.outerj.daisy.books.publisher.impl.util.PublicationLog; 23 import org.outerj.daisy.books.store.BookInstance; 24 import org.outerj.daisy.xmlutil.XmlSerializer; 25 import org.outerj.daisy.publisher.Publisher; 26 import org.outerj.daisy.repository.Repository; 27 import org.xml.sax.helpers.DefaultHandler ; 28 import org.outerx.daisy.x10Publisher.PublisherRequestDocument; 29 import org.outerx.daisy.x10Publisher.DocumentDocument; 30 import org.outerx.daisy.x10Publisher.PreparedDocumentsDocument; 31 32 import java.io.OutputStream ; 33 import java.util.Locale ; 34 35 38 public class BookDataRetrievalProcess { 39 private Repository repository; 40 private Publisher publisher; 41 private long dataBranchId; 42 private long dataLanguageId; 43 private String globalDataVersion; 44 private Locale locale; 45 private BookInstance bookInstance; 46 private Book book; 47 private PartDecider partDecider; 48 private BookDependencies bookDependencies = new BookDependencies(); 49 private PublicationLog publicationLog; 50 51 public BookDataRetrievalProcess(Repository repository, long dataBranchId, long dataLanguageId, Locale locale, 52 String dataVersion, Book book, BookInstance bookInstance, PartDecider partDecider, 53 PublicationLog publicationLog) { 54 this.repository = repository; 55 this.publisher = (Publisher)repository.getExtension("Publisher"); 56 this.dataBranchId = dataBranchId; 57 this.dataLanguageId = dataLanguageId; 58 this.book = book; 59 this.bookInstance = bookInstance; 60 this.locale = locale; 61 this.globalDataVersion = dataVersion; 62 this.publicationLog = publicationLog; 63 this.partDecider = partDecider; 64 } 65 66 public void run() throws Exception { 67 publicationLog.info("Starting retrieval of book data from repository"); 68 fetchResources(); 69 70 publicationLog.info("Storing book dependency list"); 72 OutputStream dependenciesStream = bookInstance.getResourceOutputStream(BookInstanceLayout.getDependenciesPath()); 73 try { 74 bookDependencies.store(dependenciesStream); 75 } finally { 76 dependenciesStream.close(); 77 } 78 79 publicationLog.info("Storing processed book definition"); 81 OutputStream bookDefProcessedStream = bookInstance.getResourceOutputStream(BookInstanceLayout.getProcessedBookDefinitionPath()); 82 try { 83 book.store(bookDefProcessedStream); 84 } finally { 85 bookDefProcessedStream.close(); 86 } 87 88 publicationLog.info("Ending data retrieval"); 89 } 90 91 private void fetchResources() throws Exception { 92 ImageHandler imageHandler = new ImageHandler(repository, bookInstance, bookDependencies, new DefaultHandler ()); 93 fetchSectionDocs(book, imageHandler); 94 } 95 96 private void fetchSectionDocs(SectionContainer sectionContainer, ImageHandler imageHandler) throws Exception { 97 Section[] sections = sectionContainer.getSections(); 98 for (int i = 0; i < sections.length; i++) { 99 if (sections[i].getDocumentId() != -1) { 100 long documentId = sections[i].getDocumentId(); 101 long branchId = sections[i].getBranchId() == -1 ? dataBranchId : sections[i].getBranchId(); 102 long languageId = sections[i].getLanguageId() == -1 ? dataLanguageId : sections[i].getLanguageId(); 103 String version = sections[i].getVersion() == null ? globalDataVersion : sections[i].getVersion(); 104 105 PublisherRequestDocument publisherRequestDocument = PublisherRequestDocument.Factory.newInstance(); 106 PublisherRequestDocument.PublisherRequest publisherRequestXml = publisherRequestDocument.addNewPublisherRequest(); 107 DocumentDocument.Document documentRequestXml = publisherRequestXml.addNewDocument(); 108 documentRequestXml.setId(documentId); 109 documentRequestXml.setBranch(String.valueOf(branchId)); 110 documentRequestXml.setLanguage(String.valueOf(languageId)); 111 documentRequestXml.setVersion(version); 112 PreparedDocumentsDocument.PreparedDocuments preparedDocsRequestXml = documentRequestXml.addNewPreparedDocuments(); 113 PreparedDocumentsDocument.PreparedDocuments.Context preparedDocsContextXml = preparedDocsRequestXml.addNewContext(); 114 preparedDocsContextXml.setBranch(String.valueOf(dataBranchId)); 115 preparedDocsContextXml.setLanguage(String.valueOf(dataLanguageId)); 116 117 String storePath = BookInstanceLayout.getDocumentStorePath(bookInstance, documentId, branchId, languageId); 118 OutputStream os = bookInstance.getResourceOutputStream(storePath); 119 try { 120 XmlSerializer serializer = new XmlSerializer(os); 127 imageHandler.setConsumer(serializer); 128 PartDownloadHandler partDownloadHandler = new PartDownloadHandler(partDecider, bookInstance, repository, imageHandler); 129 DependencyCollector dependencyCollector = new DependencyCollector(bookDependencies, partDownloadHandler); 130 PreparedDocumentsExtractor preparedDocumentsExtractor = new PreparedDocumentsExtractor(dependencyCollector); 131 publisher.processRequest(publisherRequestDocument, preparedDocumentsExtractor); 132 } finally { 133 os.close(); 134 } 135 136 sections[i].setBookStorePath(storePath); 137 } 138 fetchSectionDocs(sections[i], imageHandler); 139 } 140 } 141 142 } 143 | Popular Tags |