1 16 package org.outerj.daisy.books.publisher.impl.publicationtype; 17 18 import org.outerj.daisy.books.publisher.impl.publicationprocess.PublicationProcess; 19 import org.outerj.daisy.books.publisher.impl.publicationprocess.PublicationContext; 20 import org.outerj.daisy.books.publisher.impl.bookmodel.Book; 21 import org.outerj.daisy.books.publisher.impl.util.PublicationLog; 22 import org.outerj.daisy.books.publisher.impl.dataretrieval.PartDecider; 23 import org.outerj.daisy.books.store.BookInstance; 24 import org.outerj.daisy.books.store.PublicationInfo; 25 import org.outerj.daisy.repository.Repository; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.avalon.framework.context.Context; 28 import org.apache.cocoon.i18n.BundleFactory; 29 import org.apache.cocoon.i18n.Bundle; 30 31 import java.util.*; 32 import java.text.DateFormat ; 33 import java.text.SimpleDateFormat ; 34 35 public class PublicationType { 36 private final String name; 37 private final String label; 38 private final String startResource; 39 private final Map defaultProperties; 40 private final PartDecider partDecider; 41 private final PublicationProcess publicationProcess; 42 43 PublicationType(String name, String label, String startResource, PublicationProcess publicationProcess, 44 PartDecider partDecider, Map defaultProperties) { 45 this.name = name; 46 this.label = label; 47 this.startResource = startResource; 48 this.publicationProcess = publicationProcess; 49 this.partDecider = partDecider; 50 this.defaultProperties = defaultProperties; 51 } 52 53 public PartDecider getPartDecider() { 54 return partDecider; 55 } 56 57 public void publishBook(Book book, BookInstance bookInstance, String publicationOutputName, String publicationOutputLabel, 58 Map customProperties, Map bookMetadata, Repository repository, ServiceManager serviceManager, 59 Context avalonContext, String daisyContextPath, String daisyCocoonPath, Locale locale, 60 PublicationLog publicationLog) throws Exception { 61 publicationLog.info("Starting publication for publication type \"" + name + "\" to publication output \"" + publicationOutputName + "\"."); 62 Date now = new Date(); 63 64 Map properties = new HashMap(); 65 properties.putAll(defaultProperties); 66 properties.putAll(customProperties); 67 69 if (properties.containsKey("publishDate")) { 70 String pattern = (String )properties.get("publishDate"); 71 SimpleDateFormat dateFormat; 72 if (pattern.trim().length() == 0) { 73 } else { 75 if (pattern.equals("date")) { 76 dateFormat = (SimpleDateFormat )DateFormat.getDateInstance(DateFormat.LONG, locale); 77 } else if (pattern.equals("dateTime")) { 78 dateFormat = (SimpleDateFormat )DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale); 79 } else { 80 dateFormat = (SimpleDateFormat )DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale); 81 dateFormat.applyPattern(pattern); 82 } 83 properties.put("publishDate", dateFormat.format(now)); 84 } 85 } 86 87 Bundle bundle; 88 BundleFactory bundleFactory = null; 89 try { 90 bundleFactory = (BundleFactory)serviceManager.lookup(BundleFactory.ROLE); 91 String [] locations = new String [] { daisyContextPath + "/books/publicationtypes/common/i18n", daisyContextPath + "/books/publicationtypes/" + name + "/i18n" }; 92 bundle = bundleFactory.select(locations, "messages", locale); 93 } finally { 94 if (bundleFactory != null) 95 serviceManager.release(bundleFactory); 96 } 97 98 PublicationContext publicationContext = new PublicationContext(book, bookInstance, name, publicationOutputName, 99 repository, serviceManager, avalonContext, daisyCocoonPath, daisyContextPath, locale, properties, 100 bookMetadata, bundle, publicationLog); 101 publicationProcess.run(publicationContext); 102 103 String bookPackage = publicationProcess.buildsPackage() ? "output/" + bookInstance.getName() + "-" + publicationOutputName + ".zip" : null; 104 105 bookInstance.addPublication(new PublicationInfo(publicationOutputName, publicationOutputLabel, startResource, 106 bookPackage, repository.getUserId(), now)); 107 } 108 } 109 | Popular Tags |