View Javadoc

1   package org.paneris.bibliomania;
2   
3   import java.util.Enumeration;
4   import java.util.Hashtable;
5   
6   import org.melati.Melati;
7   import org.melati.poem.Persistent;
8   import org.melati.poem.PoemThread;
9   import org.webmacro.servlet.WebContext;
10  
11  public class Buy extends BibliomaniaServlet {
12  
13    /**
14     * 
15     */
16    private static final long serialVersionUID = 1L;
17  
18    protected String bibliomaniaHandle(Melati melati, WebContext context)
19      throws Exception {
20  
21      BibliomaniaDatabase database = (BibliomaniaDatabase)melati.getDatabase();
22  
23      melati.setBufferingOff();
24      melati.setFlushingOn();
25      melati.setPassbackExceptionHandling();
26  
27      Persistent obj = melati.getObject();
28  
29      String title_search = context.getForm("title_search");
30      String author_search = context.getForm("author_search");
31  
32      StockingsSearch search;
33  
34      boolean noTerms =
35        (title_search == null || title_search.trim().equals(""))
36          && (author_search == null || author_search.trim().equals(""));
37  
38      if (noTerms) {
39        if (obj instanceof Book) {
40          search = database.getStockingsSearchTable().searchFor((Book)obj);
41          title_search = search.getTitleterm();
42          author_search = search.getAuthorterm();
43        } else {
44          search = null;
45          if (obj instanceof Author)
46            author_search = ((Author)obj).getLongname();
47        }
48      } else
49        search =
50          database.getStockingsSearchTable().searchFor(
51            author_search,
52            title_search);
53  
54      if (search != null) {
55        String cachedResults = search.cachedResultsPath();
56        if (cachedResults != null)
57          context.put("cachedResults", cachedResults);
58        else {
59          if (!database.okToRunABookshopSearch())
60            return bibliomaniaTemplate("metasearch/TooBusy.wm");
61  
62          context.put(
63            "results",
64            new CollatedHitsEnumeration(
65              search.newSearchResults(),
66              database.bookshops().length,
67              database.getBookStockingsOutputStartOffset()));
68        }
69      }
70  
71      PoemThread.commit();
72  
73      context.put("title_search", title_search);
74      context.put("author_search", author_search);
75      return bibliomaniaTemplate("metasearch/Results.wm");
76    }
77  
78    public static class Hit {
79      public Integer lineNo;
80      public Integer headerLineNo;
81      public boolean isFirst;
82      public boolean isFirstThumbnail;
83      public boolean hadThumbnail;
84      public BookStocking stocking;
85    }
86  
87    public static class CollatedHitsEnumeration implements Enumeration {
88      private Enumeration hits;
89      private int numBookshops;
90      private int startRow;
91      private Hashtable lastHitOfISBN = new Hashtable();
92      private int discreteBooks;
93  
94      public CollatedHitsEnumeration(
95        Enumeration hits,
96        int numBookshops,
97        int startRow) {
98        this.hits = hits;
99        this.numBookshops = numBookshops;
100       this.startRow = startRow;
101     }
102 
103     public boolean hasMoreElements() {
104       return hits.hasMoreElements();
105     }
106 
107     public Object nextElement() {
108       BookStocking stocking = (BookStocking)hits.nextElement();
109       Hit hit = new Hit();
110       hit.stocking = stocking;
111       String isbn = stocking.getIsbn();
112 
113       hit.isFirstThumbnail = stocking.getThumbnailurl() != null;
114       hit.hadThumbnail = hit.isFirstThumbnail;
115 
116       Hit last =
117         isbn == null || isbn.equals("") ? null : (Hit)lastHitOfISBN.get(isbn);
118 
119       if (last == null) {
120         hit.isFirst = true;
121         hit.headerLineNo =
122           new Integer(startRow + discreteBooks++ * (numBookshops + 3));
123         hit.lineNo = new Integer(hit.headerLineNo.intValue() + 2);
124       } else {
125         hit.headerLineNo = last.headerLineNo;
126         hit.lineNo = new Integer(last.lineNo.intValue() + 1);
127         if (last.hadThumbnail) {
128           hit.hadThumbnail = true;
129           hit.isFirstThumbnail = false;
130         }
131       }
132 
133       if (isbn != null)
134         lastHitOfISBN.put(isbn, hit);
135 
136       return hit;
137     }
138   }
139 }