Coverage Report - org.paneris.bibliomania.Bib
 
Classes in this File Line Coverage Branch Coverage Complexity
Bib
75%
77/102
53%
29/54
2.037
 
 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  2
   public Bib(BibliomaniaDatabase db) {
 15  2
     this.db = db;
 16  2
   }
 17  
 
 18  
   public BibliomaniaDatabase getDatabase() {
 19  3
     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  23
     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  20
     return db.getStaticRootURL();
 40  
   }
 41  
 
 42  
   public String getContentStaticRootURL() {
 43  0
     return db.getContentStaticRootURL();
 44  
   }
 45  
 
 46  
   public String getCacheRootURL() {
 47  3
     return db.getCacheRootURL();
 48  
   }
 49  
 
 50  
   public String getHomepageURL() {
 51  3
     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  0
     return getStaticRootURL() + "/logo.html";
 65  
   }
 66  
 
 67  
   public String getFaqURL() {
 68  3
     return getCacheRootURL() + "/Faq.html";
 69  
   }
 70  
 
 71  
   public String getCommentURL() {
 72  0
     return "mailto:comments@boards.bibliomania.com";
 73  
   }
 74  
 
 75  
   public String getTrolleyURL() {
 76  0
     return "/trolley";
 77  
   }
 78  
 
 79  
   public String urlPrefixedReally(
 80  
     String prefix,
 81  
     SectionGroup area,
 82  
     Section section,
 83  
     Unit object,
 84  
     Integer page) {
 85  24
     if (object != null && object instanceof Chapter) {
 86  17
       return addPage(
 87  
         urlDirectoryPrefixedReally(prefix, area, section, object),
 88  
         page)
 89  
         .toString();
 90  
     } else {
 91  7
       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  24
     if (object == null) {
 106  
       //sectionid = null; 
 107  0
       authorid = bookid = chapterid = null;
 108  
     } else {
 109  24
       if (object instanceof Chapter) {
 110  17
         Chapter chapter = (Chapter)object;
 111  17
         Book book = chapter.getBook();
 112  17
         chapterid = chapter.troid();
 113  17
         bookid = book.troid();
 114  17
         authorid = book.getAuthorTroid();
 115  17
         section = book.getSection();
 116  17
       } else if (object instanceof Book) {
 117  6
         Book book = (Book)object;
 118  6
         chapterid = null;
 119  6
         bookid = book.troid();
 120  6
         authorid = book.getAuthorTroid();
 121  6
         section = book.getSection();
 122  6
       } else if (object instanceof Author) {
 123  1
         Author author = (Author)object;
 124  1
         chapterid = null;
 125  1
         bookid = null;
 126  1
         authorid = author.troid();
 127  1
         if (section == null)
 128  1
           if ((section = author.getSection()) == null)
 129  0
             section = db.getSectionTable().getSectionObject(0); // FIXME
 130  1
       } else if (object instanceof Section) {
 131  0
         chapterid = null;
 132  0
         bookid = null;
 133  0
         authorid = null;
 134  0
         section = (Section)object;
 135  
       } else {
 136  0
         chapterid = null;
 137  0
         bookid = null;
 138  0
         authorid = null;
 139  0
         section = null;
 140  0
         area = (SectionGroup)object;
 141  
       }
 142  
     }
 143  
 
 144  24
     if (area == null)
 145  24
       area = section != null ? section.getGroup() : db.getReadSectionGroup();
 146  
 
 147  24
     StringBuffer it = new StringBuffer();
 148  24
     it.append(prefix);
 149  24
     it.append('/');
 150  24
     if (area.getUrlprefix() != null) {
 151  0
       it.append(area.getUrlprefix());
 152  0
       it.append('/');
 153  
     }
 154  24
     it.append(area.troid());
 155  24
     if (section != null) {
 156  24
       it.append('/');
 157  24
       it.append(section.troid());
 158  24
       if (authorid != null) {
 159  24
         it.append('/');
 160  24
         it.append(authorid);
 161  24
         if (bookid != null) {
 162  23
           it.append('/');
 163  23
           it.append(bookid);
 164  23
           if (chapterid != null) {
 165  17
             it.append('/');
 166  17
             it.append(chapterid);
 167  
           }
 168  
         }
 169  
       }
 170  
     }
 171  24
     return it;
 172  
   }
 173  
 
 174  
   private StringBuffer addPage(StringBuffer url, Integer page) {
 175  17
     url.append('/');
 176  17
     if (page == null) {
 177  9
       url.append(1);
 178  
     } else {
 179  8
       url.append(page.intValue());
 180  
     }
 181  17
     url.append(".html");
 182  17
     return url;
 183  
   }
 184  
 
 185  
   public String urlPrefixed(
 186  
     String prefix,
 187  
     SectionGroup area,
 188  
     Section section,
 189  
     Unit object,
 190  
     Integer page) {
 191  24
     if (object != null && object instanceof Book) {
 192  6
       Book book = (Book)object;
 193  6
       Boolean hnfp = book.getHasnofrontpage();
 194  6
       if (hnfp != null && hnfp.booleanValue())
 195  0
         object = book.getChapter0();
 196  
     }
 197  
 
 198  24
     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  24
     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  16
     return url(area, null, object, null);
 223  
   }
 224  
 
 225  
   public String url(Chapter object, Integer page) {
 226  5
     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  3
     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  16
     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  1
     String url = url(object);
 256  1
     if (url.endsWith(".html"))
 257  0
       url = url.substring(0, url.length() - 5);
 258  
 
 259  1
     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  2
     String url = url(chapter, page);
 271  2
     if (url.endsWith(".html"))
 272  2
       url = url.substring(0, url.length() - 5);
 273  
 
 274  2
     return url + "/frameset.html";
 275  
   }
 276  
 
 277  
   // FIXME I don't think this is used
 278  
   public String urlReally(Unit object) {
 279  0
     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  0
     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  0
     Integer page = chapter.pageOfAnchor(anchor);
 304  0
     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  0
     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  0
     return object instanceof Chapter
 326  
       ? ((Chapter)object).getBook().getAuthor()
 327  
       : object instanceof Book
 328  
       ? ((Book)object).getAuthor()
 329  
       : object;
 330  
   }
 331  
 }