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 | 0 | 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 | 0 | BibliomaniaDatabase database = (BibliomaniaDatabase)melati.getDatabase(); |
22 | |
|
23 | 0 | melati.setBufferingOff(); |
24 | 0 | melati.setFlushingOn(); |
25 | 0 | melati.setPassbackExceptionHandling(); |
26 | |
|
27 | 0 | Persistent obj = melati.getObject(); |
28 | |
|
29 | 0 | String title_search = context.getForm("title_search"); |
30 | 0 | String author_search = context.getForm("author_search"); |
31 | |
|
32 | |
StockingsSearch search; |
33 | |
|
34 | 0 | boolean noTerms = |
35 | |
(title_search == null || title_search.trim().equals("")) |
36 | |
&& (author_search == null || author_search.trim().equals("")); |
37 | |
|
38 | 0 | if (noTerms) { |
39 | 0 | if (obj instanceof Book) { |
40 | 0 | search = database.getStockingsSearchTable().searchFor((Book)obj); |
41 | 0 | title_search = search.getTitleterm(); |
42 | 0 | author_search = search.getAuthorterm(); |
43 | |
} else { |
44 | 0 | search = null; |
45 | 0 | if (obj instanceof Author) |
46 | 0 | author_search = ((Author)obj).getLongname(); |
47 | |
} |
48 | |
} else |
49 | 0 | search = |
50 | |
database.getStockingsSearchTable().searchFor( |
51 | |
author_search, |
52 | |
title_search); |
53 | |
|
54 | 0 | if (search != null) { |
55 | 0 | String cachedResults = search.cachedResultsPath(); |
56 | 0 | if (cachedResults != null) |
57 | 0 | context.put("cachedResults", cachedResults); |
58 | |
else { |
59 | 0 | if (!database.okToRunABookshopSearch()) |
60 | 0 | return bibliomaniaTemplate("metasearch/TooBusy.wm"); |
61 | |
|
62 | 0 | context.put( |
63 | |
"results", |
64 | |
new CollatedHitsEnumeration( |
65 | |
search.newSearchResults(), |
66 | |
database.bookshops().length, |
67 | |
database.getBookStockingsOutputStartOffset())); |
68 | |
} |
69 | |
} |
70 | |
|
71 | 0 | PoemThread.commit(); |
72 | |
|
73 | 0 | context.put("title_search", title_search); |
74 | 0 | context.put("author_search", author_search); |
75 | 0 | return bibliomaniaTemplate("metasearch/Results.wm"); |
76 | |
} |
77 | |
|
78 | 0 | 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 | 0 | public static class CollatedHitsEnumeration implements Enumeration { |
88 | |
private Enumeration hits; |
89 | |
private int numBookshops; |
90 | |
private int startRow; |
91 | 0 | private Hashtable lastHitOfISBN = new Hashtable(); |
92 | |
private int discreteBooks; |
93 | |
|
94 | |
public CollatedHitsEnumeration( |
95 | |
Enumeration hits, |
96 | |
int numBookshops, |
97 | 0 | int startRow) { |
98 | 0 | this.hits = hits; |
99 | 0 | this.numBookshops = numBookshops; |
100 | 0 | this.startRow = startRow; |
101 | 0 | } |
102 | |
|
103 | |
public boolean hasMoreElements() { |
104 | 0 | return hits.hasMoreElements(); |
105 | |
} |
106 | |
|
107 | |
public Object nextElement() { |
108 | 0 | BookStocking stocking = (BookStocking)hits.nextElement(); |
109 | 0 | Hit hit = new Hit(); |
110 | 0 | hit.stocking = stocking; |
111 | 0 | String isbn = stocking.getIsbn(); |
112 | |
|
113 | 0 | hit.isFirstThumbnail = stocking.getThumbnailurl() != null; |
114 | 0 | hit.hadThumbnail = hit.isFirstThumbnail; |
115 | |
|
116 | 0 | Hit last = |
117 | |
isbn == null || isbn.equals("") ? null : (Hit)lastHitOfISBN.get(isbn); |
118 | |
|
119 | 0 | if (last == null) { |
120 | 0 | hit.isFirst = true; |
121 | 0 | hit.headerLineNo = |
122 | |
new Integer(startRow + discreteBooks++ * (numBookshops + 3)); |
123 | 0 | hit.lineNo = new Integer(hit.headerLineNo.intValue() + 2); |
124 | |
} else { |
125 | 0 | hit.headerLineNo = last.headerLineNo; |
126 | 0 | hit.lineNo = new Integer(last.lineNo.intValue() + 1); |
127 | 0 | if (last.hadThumbnail) { |
128 | 0 | hit.hadThumbnail = true; |
129 | 0 | hit.isFirstThumbnail = false; |
130 | |
} |
131 | |
} |
132 | |
|
133 | 0 | if (isbn != null) |
134 | 0 | lastHitOfISBN.put(isbn, hit); |
135 | |
|
136 | 0 | return hit; |
137 | |
} |
138 | |
} |
139 | |
} |