View Javadoc

1   package org.paneris.bibliomania;
2   
3   /**
4    * A TemplateContext utility. 
5    * This is a utility object that is pretty widely used.
6    * It provides shortcuts to frequently used functions.
7    *
8    * @see org.paneris.bibliomania.BibliomaniaDatabase
9    */
10  
11  public class Bib {
12    private BibliomaniaDatabase db;
13  
14    public Bib(BibliomaniaDatabase db) {
15      this.db = db;
16    }
17  
18    public BibliomaniaDatabase getDatabase() {
19      return db;
20    }
21  
22    /**
23     * We need to add the extension as this is used in a 
24     * template context so the TemplateEngine does not get a 
25     * chance to add an extension.
26     *   
27     * @param name with path and extension
28     * @return A name WebMacro can find in the CLASSPATH. 
29     */
30    public static String bibliomaniaTemplate(String name) {
31      return BibliomaniaServlet.bibliomaniaTemplate(name) + ".wm";
32    }
33    /**
34     * Return the URL for the root of the static content directory (set via
35     * the `StaticRootURL' setting in the Settings table, generally
36    `* /bibliomania-static').
37     */
38    public String getStaticRootURL() {
39      return db.getStaticRootURL();
40    }
41  
42    public String getContentStaticRootURL() {
43      return db.getContentStaticRootURL();
44    }
45  
46    public String getCacheRootURL() {
47      return db.getCacheRootURL();
48    }
49  
50    public String getHomepageURL() {
51      return db.getHomepageURL();
52    }
53  
54    /**
55     * 
56     * Access to static URLs defined here 
57     * 
58     */
59  
60    /**
61     * Return the URL for the logo frame
62     */
63    public String getLogoURL() {
64      return getStaticRootURL() + "/logo.html";
65    }
66  
67    public String getFaqURL() {
68      return getCacheRootURL() + "/Faq.html";
69    }
70  
71    public String getCommentURL() {
72      return "mailto:comments@boards.bibliomania.com";
73    }
74  
75    public String getTrolleyURL() {
76      return "/trolley";
77    }
78  
79    public String urlPrefixedReally(
80      String prefix,
81      SectionGroup area,
82      Section section,
83      Unit object,
84      Integer page) {
85      if (object != null && object instanceof Chapter) {
86        return addPage(
87          urlDirectoryPrefixedReally(prefix, area, section, object),
88          page)
89          .toString();
90      } else {
91        return urlDirectoryPrefixedReally(prefix, area, section, object)
92          .toString();
93      }
94  
95    }
96    public StringBuffer urlDirectoryPrefixedReally(
97      String prefix,
98      SectionGroup area,
99      Section section,
100     Unit object) {
101 
102     //Integer sectionid; 
103     Integer authorid, bookid, chapterid;
104 
105     if (object == null) {
106       //sectionid = null; 
107       authorid = bookid = chapterid = null;
108     } else {
109       if (object instanceof Chapter) {
110         Chapter chapter = (Chapter)object;
111         Book book = chapter.getBook();
112         chapterid = chapter.troid();
113         bookid = book.troid();
114         authorid = book.getAuthorTroid();
115         section = book.getSection();
116       } else if (object instanceof Book) {
117         Book book = (Book)object;
118         chapterid = null;
119         bookid = book.troid();
120         authorid = book.getAuthorTroid();
121         section = book.getSection();
122       } else if (object instanceof Author) {
123         Author author = (Author)object;
124         chapterid = null;
125         bookid = null;
126         authorid = author.troid();
127         if (section == null)
128           if ((section = author.getSection()) == null)
129             section = db.getSectionTable().getSectionObject(0); // FIXME
130       } else if (object instanceof Section) {
131         chapterid = null;
132         bookid = null;
133         authorid = null;
134         section = (Section)object;
135       } else {
136         chapterid = null;
137         bookid = null;
138         authorid = null;
139         section = null;
140         area = (SectionGroup)object;
141       }
142     }
143 
144     if (area == null)
145       area = section != null ? section.getGroup() : db.getReadSectionGroup();
146 
147     StringBuffer it = new StringBuffer();
148     it.append(prefix);
149     it.append('/');
150     if (area.getUrlprefix() != null) {
151       it.append(area.getUrlprefix());
152       it.append('/');
153     }
154     it.append(area.troid());
155     if (section != null) {
156       it.append('/');
157       it.append(section.troid());
158       if (authorid != null) {
159         it.append('/');
160         it.append(authorid);
161         if (bookid != null) {
162           it.append('/');
163           it.append(bookid);
164           if (chapterid != null) {
165             it.append('/');
166             it.append(chapterid);
167           }
168         }
169       }
170     }
171     return it;
172   }
173 
174   private StringBuffer addPage(StringBuffer url, Integer page) {
175     url.append('/');
176     if (page == null) {
177       url.append(1);
178     } else {
179       url.append(page.intValue());
180     }
181     url.append(".html");
182     return url;
183   }
184 
185   public String urlPrefixed(
186     String prefix,
187     SectionGroup area,
188     Section section,
189     Unit object,
190     Integer page) {
191     if (object != null && object instanceof Book) {
192       Book book = (Book)object;
193       Boolean hnfp = book.getHasnofrontpage();
194       if (hnfp != null && hnfp.booleanValue())
195         object = book.getChapter0();
196     }
197 
198     return urlPrefixedReally(prefix, area, section, object, page);
199   }
200 
201   public String url(
202     SectionGroup area,
203     Section section,
204     Unit object,
205     Integer page) {
206     return urlPrefixed(
207       ((BibliomaniaDatabase)object.getDatabase()).getCacheRootURL(),
208       area,
209       section,
210       object,
211       page);
212   }
213 
214   /**
215    * 
216    * Given a SectionGroup and any Unit
217    * return the url for that Unit within this 
218    * SectionGroup
219    * 
220    */
221   public String url(SectionGroup area, Unit object) {
222     return url(area, null, object, null);
223   }
224 
225   public String url(Chapter object, Integer page) {
226     return url(null, null, object, page);
227   }
228 
229   /**
230    * 
231    * Given a chapter and page return the url
232    * 
233    */
234   public String url(Chapter object, String page) {
235     return url(null, null, object, new Integer(page));
236   }
237 
238   /**
239    * 
240    * Given any object return its url
241    * 
242    */
243   public String url(Unit object) {
244     return url(null, object);
245   }
246 
247   /**
248    * 
249    * Given any object return its frameset url
250    * 
251    */
252   // FIXME hack, this naming policy needs to be centrally organised
253   public String framesetURL(Unit object) {
254 
255     String url = url(object);
256     if (url.endsWith(".html"))
257       url = url.substring(0, url.length() - 5);
258 
259     return url + "/frameset.html";
260   }
261 
262   /**
263    * 
264    * Given a chapter and page return 
265    * the frameset url
266    * 
267    */
268   public String framesetURL(Chapter chapter, String page) {
269 
270     String url = url(chapter, page);
271     if (url.endsWith(".html"))
272       url = url.substring(0, url.length() - 5);
273 
274     return url + "/frameset.html";
275   }
276 
277   // FIXME I don't think this is used
278   public String urlReally(Unit object) {
279     return urlPrefixedReally(
280       ((BibliomaniaDatabase)object.getDatabase()).getCacheRootURL(),
281       null,
282       null,
283       object,
284       null);
285   }
286 
287   /**
288    * 
289    * Given a Book id return the book
290    * 
291    */
292   public Book book(Integer troid) {
293     return (Book)db.getBookTable().getObject(troid);
294   }
295 
296   /**
297    * Given a chapter and an anchor 
298    * return an anchor url.
299    * 
300    */
301 
302   public String anchorURL(Chapter chapter, String anchor) {
303     Integer page = chapter.pageOfAnchor(anchor);
304     return page == null
305       ? url(null, chapter)
306       : url(null, null, chapter, page) + "#" + anchor;
307   }
308 
309   /**
310    * Walk up the content hierarchy until Book level is reached.  I.e.,
311    * return the book from which a chapter comes, or the object itself if it
312    * is a book, author, section or sectiongroup.
313    */
314 
315   public Unit atMostBook(Unit object) {
316     return object instanceof Chapter ? ((Chapter)object).getBook() : object;
317   }
318 
319   /**
320    * Walk up the content hierarchy until Author level is reached.  I.e.,
321    * return the author of a book or chapter, or the object itself if it is
322    * an author, section or sectiongroup.
323    */
324   public Unit atMostAuthor(Unit object) {
325     return object instanceof Chapter
326       ? ((Chapter)object).getBook().getAuthor()
327       : object instanceof Book
328       ? ((Book)object).getAuthor()
329       : object;
330   }
331 }