KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationtype > PublicationType


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.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 JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34
35 public class PublicationType {
36     private final String JavaDoc name;
37     private final String JavaDoc label;
38     private final String JavaDoc startResource;
39     private final Map defaultProperties;
40     private final PartDecider partDecider;
41     private final PublicationProcess publicationProcess;
42
43     PublicationType(String JavaDoc name, String JavaDoc label, String JavaDoc 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 JavaDoc publicationOutputName, String JavaDoc publicationOutputLabel,
58                             Map customProperties, Map bookMetadata, Repository repository, ServiceManager serviceManager,
59                             Context avalonContext, String JavaDoc daisyContextPath, String JavaDoc daisyCocoonPath, Locale locale,
60                             PublicationLog publicationLog) throws Exception JavaDoc {
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         // Note: publication tasks may modify the properties
68

69         if (properties.containsKey("publishDate")) {
70             String JavaDoc pattern = (String JavaDoc)properties.get("publishDate");
71             SimpleDateFormat JavaDoc dateFormat;
72             if (pattern.trim().length() == 0) {
73                 // do nothing
74
} else {
75                 if (pattern.equals("date")) {
76                     dateFormat = (SimpleDateFormat JavaDoc)DateFormat.getDateInstance(DateFormat.LONG, locale);
77                 } else if (pattern.equals("dateTime")) {
78                     dateFormat = (SimpleDateFormat JavaDoc)DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
79                 } else {
80                     dateFormat = (SimpleDateFormat JavaDoc)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 JavaDoc[] locations = new String JavaDoc[] { 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 JavaDoc 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