KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > dataretrieval > BookDataRetrievalProcess


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
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 JavaDoc;
33 import java.util.Locale JavaDoc;
34
35 /**
36  * The process that retrieves all the data needed for the book (documents & images).
37  */

38 public class BookDataRetrievalProcess {
39     private Repository repository;
40     private Publisher publisher;
41     private long dataBranchId;
42     private long dataLanguageId;
43     private String JavaDoc globalDataVersion;
44     private Locale JavaDoc 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 JavaDoc locale,
52             String JavaDoc 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 JavaDoc {
67         publicationLog.info("Starting retrieval of book data from repository");
68         fetchResources();
69
70         // Store dependencies
71
publicationLog.info("Storing book dependency list");
72         OutputStream JavaDoc dependenciesStream = bookInstance.getResourceOutputStream(BookInstanceLayout.getDependenciesPath());
73         try {
74             bookDependencies.store(dependenciesStream);
75         } finally {
76             dependenciesStream.close();
77         }
78
79         // Store processed book definition
80
publicationLog.info("Storing processed book definition");
81         OutputStream JavaDoc 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 JavaDoc {
92         ImageHandler imageHandler = new ImageHandler(repository, bookInstance, bookDependencies, new DefaultHandler JavaDoc());
93         fetchSectionDocs(book, imageHandler);
94     }
95
96     private void fetchSectionDocs(SectionContainer sectionContainer, ImageHandler imageHandler) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc storePath = BookInstanceLayout.getDocumentStorePath(bookInstance, documentId, branchId, languageId);
118                 OutputStream JavaDoc os = bookInstance.getResourceOutputStream(storePath);
119                 try {
120                     // Push parsing result through a pipeline like this:
121
// 1. leave only through the p:preparedDocuments element
122
// 2. collect IDs of all the prepared documents for the dependency information
123
// 3. download the parts specified in the downloadParts set
124
// 4. download referenced images
125
// 5. serialize result
126
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