1 | |
package org.paneris.bibliomania; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.IOException; |
5 | |
import java.sql.Timestamp; |
6 | |
import java.util.Enumeration; |
7 | |
import java.util.Hashtable; |
8 | |
import java.util.Vector; |
9 | |
|
10 | |
import org.melati.poem.AccessToken; |
11 | |
import org.melati.poem.PoemTask; |
12 | |
import org.melati.poem.PoemThread; |
13 | |
import org.melati.poem.UnexpectedExceptionPoemException; |
14 | |
import org.melati.poem.util.ArrayEnumeration; |
15 | |
import org.melati.poem.util.Order; |
16 | |
import org.melati.poem.util.SortUtils; |
17 | |
import org.melati.poem.transaction.ToTidyList; |
18 | |
import org.melati.util.UnexpectedExceptionException; |
19 | |
import org.paneris.bibliomania.generated.StockingsSearchBase; |
20 | |
import org.paneris.bibliomania.metasearch.Metasearch; |
21 | |
import org.webmacro.WebMacroException; |
22 | |
|
23 | 0 | public class StockingsSearch extends StockingsSearchBase { |
24 | |
|
25 | |
public BibliomaniaDatabase getBibliomaniaDatabase () { |
26 | 0 | return (BibliomaniaDatabase)getDatabase(); |
27 | |
} |
28 | |
|
29 | 0 | public StockingsSearch() {} |
30 | |
|
31 | |
private long timeout; |
32 | |
private SearchThread searchThread; |
33 | |
|
34 | 0 | private static class DisplayOrder implements Order { |
35 | |
|
36 | |
private static int iom(Integer i) { |
37 | 0 | return i == null ? Integer.MAX_VALUE : i.intValue(); |
38 | |
} |
39 | |
|
40 | |
public boolean lessOrEqual(Object a, Object b) { |
41 | 0 | BookStocking sa = (BookStocking)a; |
42 | 0 | BookStocking sb = (BookStocking)b; |
43 | |
|
44 | 0 | int fca = iom(sa.getFormatcode_unsafe()); |
45 | 0 | int fcb = iom(sb.getFormatcode_unsafe()); |
46 | |
|
47 | 0 | if (fca < fcb) return true; |
48 | 0 | else if (fca > fcb) return false; |
49 | |
|
50 | 0 | int pna = iom(sa.getPricenumber_unsafe()); |
51 | 0 | int pnb = iom(sb.getPricenumber_unsafe()); |
52 | |
|
53 | 0 | return pna <= pnb; |
54 | |
} |
55 | |
|
56 | 0 | public static final Order it = new DisplayOrder(); |
57 | |
} |
58 | |
|
59 | |
public class CollatingResults { |
60 | |
|
61 | |
private Object[] sorted; |
62 | |
|
63 | 0 | public CollatingResults(Vector baseResults) { |
64 | 0 | sorted = SortUtils.sorted(DisplayOrder.it, baseResults); |
65 | 0 | } |
66 | |
|
67 | |
public Enumeration startingAtRow(int row) { |
68 | 0 | return new Buy.CollatedHitsEnumeration( |
69 | |
new ArrayEnumeration(sorted), |
70 | |
getBibliomaniaDatabase().bookshops().length, row); |
71 | |
} |
72 | |
} |
73 | |
|
74 | |
private class SearchThread extends Thread { |
75 | |
|
76 | |
Metasearch search; |
77 | 0 | ToTidyList toTidy = new ToTidyList(); |
78 | |
|
79 | 0 | public SearchThread() { |
80 | 0 | final BibliomaniaDatabase database = getBibliomaniaDatabase(); |
81 | 0 | database.notifyBookshopSearchStart(); |
82 | 0 | search = new Metasearch(database.bookshops(), |
83 | |
getTitleterm_unsafe(), |
84 | |
getAuthorterm_unsafe(), |
85 | |
toTidy); |
86 | 0 | start(); |
87 | 0 | } |
88 | |
|
89 | |
public void run() { |
90 | 0 | final BibliomaniaDatabase database = getBibliomaniaDatabase(); |
91 | |
|
92 | |
try { |
93 | 0 | synchronized (search) { |
94 | |
try { |
95 | 0 | search.wait(timeout); |
96 | |
} |
97 | 0 | catch (InterruptedException e) { |
98 | 0 | search.close(); |
99 | 0 | return; |
100 | 0 | } |
101 | 0 | } |
102 | |
|
103 | 0 | if (search.running()) |
104 | 0 | search.close(); |
105 | |
|
106 | 0 | toTidy.close(); |
107 | |
|
108 | 0 | database.inSession( |
109 | |
AccessToken.root, |
110 | 0 | new PoemTask() { |
111 | |
public void run() { |
112 | |
try { |
113 | 0 | File cache = cachedResultsFile(); |
114 | |
|
115 | 0 | File dir = cache.getParentFile(); |
116 | 0 | if (!dir.exists()) |
117 | 0 | dir.mkdirs(); |
118 | |
|
119 | 0 | Hashtable extras = new Hashtable(); |
120 | 0 | extras.put("results", |
121 | |
new CollatingResults(search.currentResults())); |
122 | |
|
123 | 0 | getBibliomaniaDatabase().interpolateTemplateToFile( |
124 | |
"metasearch/CachedResults.wm", cache, null, extras); |
125 | |
} |
126 | 0 | catch (WebMacroException e) { |
127 | 0 | throw new UnexpectedExceptionException(e); |
128 | |
} |
129 | 0 | catch (IOException e) { |
130 | 0 | throw new UnexpectedExceptionException(e); |
131 | |
} |
132 | |
finally { |
133 | 0 | setLastperformed(new Timestamp(System.currentTimeMillis())); |
134 | 0 | PoemThread.commit(); |
135 | 0 | } |
136 | 0 | } |
137 | |
}); |
138 | |
} |
139 | 0 | catch (Exception e) { |
140 | 0 | System.err.println("Exception in StockingsSearch for `" + |
141 | |
getAuthorterm_unsafe() + "'/`" + |
142 | |
getTitleterm_unsafe() + "'"); |
143 | 0 | e.printStackTrace(); |
144 | |
} |
145 | |
finally { |
146 | 0 | database.notifyBookshopSearchStop(); |
147 | 0 | synchronized (StockingsSearch.this) { |
148 | 0 | searchThread = null; |
149 | 0 | } |
150 | 0 | } |
151 | 0 | } |
152 | |
} |
153 | |
|
154 | |
private Metasearch search() { |
155 | 0 | SearchThread running = searchThread; |
156 | |
|
157 | 0 | if (running == null) { |
158 | 0 | synchronized (this) { |
159 | 0 | if (searchThread == null) { |
160 | 0 | timeout = |
161 | |
getBibliomaniaDatabase().getBibBookBookshopSearchTimeoutMillis(); |
162 | 0 | searchThread = new SearchThread(); |
163 | |
} |
164 | 0 | running = searchThread; |
165 | 0 | } |
166 | |
} |
167 | |
|
168 | 0 | return running.search; |
169 | |
} |
170 | |
|
171 | |
public void runNewSearch() { |
172 | 0 | search(); |
173 | 0 | } |
174 | |
|
175 | |
public Enumeration newSearchResults() { |
176 | 0 | return search().currentAndFutureResults(); |
177 | |
} |
178 | |
|
179 | |
private String cachedResultsFileLocation() { |
180 | 0 | StringBuffer it = new StringBuffer(); |
181 | 0 | it.append(getBibliomaniaDatabase().getBookStockingsCacheDir()); |
182 | |
|
183 | 0 | String t = troid().toString(); |
184 | |
|
185 | 0 | if (t.length() % 2 == 1) |
186 | 0 | t = '0' + t; |
187 | |
|
188 | 0 | for (int i = 0; i < t.length(); i += 2) { |
189 | 0 | it.append('/'); |
190 | 0 | it.append(t.substring(i, i + 2)); |
191 | |
} |
192 | |
|
193 | 0 | it.append(".html"); |
194 | |
|
195 | 0 | return it.toString(); |
196 | |
} |
197 | |
|
198 | |
private File cachedResultsFile() { |
199 | 0 | return new File(cachedResultsFileLocation()); |
200 | |
} |
201 | |
|
202 | |
public boolean upToDate() { |
203 | 0 | Timestamp last = getLastperformed(); |
204 | 0 | return last != null && |
205 | |
System.currentTimeMillis() - last.getTime() < |
206 | |
getBibliomaniaDatabase().getBookStockingsCheckIntervalMillis(); |
207 | |
} |
208 | |
|
209 | |
public String cachedResultsPath() { |
210 | 0 | if (!upToDate()) |
211 | 0 | return null; |
212 | |
else { |
213 | 0 | File it = cachedResultsFile(); |
214 | 0 | return it.exists() ? it.toString() : null; |
215 | |
} |
216 | |
} |
217 | |
|
218 | |
public void delete_unsafe() { |
219 | 0 | super.delete_unsafe(); |
220 | 0 | cachedResultsFile().delete(); |
221 | 0 | } |
222 | |
|
223 | |
public static void main(final String[] args) { |
224 | 0 | final BibliomaniaDatabase bib = new BibliomaniaDatabase(false, true); |
225 | 0 | bib.connect("bibliomania","org.melati.poem.dbms.Postgresql", |
226 | |
"jdbc:postgresql:bibliomania", "postgres", "*",8); |
227 | |
|
228 | 0 | bib.setLogSQL(true); |
229 | |
|
230 | 0 | bib.inSession( |
231 | |
AccessToken.root, |
232 | 0 | new PoemTask() { |
233 | |
public void run() { |
234 | |
try { |
235 | 0 | StockingsSearch s = |
236 | |
bib.getStockingsSearchTable().searchFor(args[0], args[1]); |
237 | 0 | String res = s.cachedResultsPath(); |
238 | 0 | if (res != null) |
239 | 0 | System.out.println("-> " + res); |
240 | |
else { |
241 | 0 | System.err.println("searching ..........."); |
242 | 0 | for (Enumeration r = s.newSearchResults(); |
243 | 0 | r.hasMoreElements();) { |
244 | 0 | BookStocking st = (BookStocking)r.nextElement(); |
245 | 0 | System.out.println(st.getPublisher_unsafe() + " " + |
246 | |
st.getShop_unsafe() + " " + |
247 | |
st.getPrice_unsafe()); |
248 | 0 | } |
249 | 0 | System.err.println("......... done searching"); |
250 | |
} |
251 | |
} |
252 | 0 | catch (Exception e) { |
253 | 0 | throw new UnexpectedExceptionPoemException(e); |
254 | 0 | } |
255 | 0 | } |
256 | |
}); |
257 | 0 | } |
258 | |
} |