| 1 | |
package org.paneris.bibliomania.metasearch; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileNotFoundException; |
| 5 | |
import java.io.IOException; |
| 6 | |
|
| 7 | |
import org.paneris.bibliomania.BookStocking; |
| 8 | |
import org.paneris.bibliomania.fti.StringDbHash; |
| 9 | |
|
| 10 | |
import com.sleepycat.db.DatabaseException; |
| 11 | |
|
| 12 | |
public abstract class BOLAndBOBBackendBase implements BookshopBackend { |
| 13 | |
|
| 14 | |
protected final StringDbHash isbnAndImageOfPrdid; |
| 15 | |
protected final String thumbnailURLPrefix; |
| 16 | |
|
| 17 | |
public BOLAndBOBBackendBase( |
| 18 | |
File dbHome, |
| 19 | |
String dbName, |
| 20 | |
int isbnOfPrdidCacheSize, |
| 21 | |
String thumbnailURLPrefix) |
| 22 | 0 | throws FileNotFoundException, DatabaseException { |
| 23 | 0 | this.thumbnailURLPrefix = thumbnailURLPrefix; |
| 24 | 0 | isbnAndImageOfPrdid = |
| 25 | |
new StringDbHash( |
| 26 | |
new File(dbHome, dbName).getPath(), |
| 27 | |
isbnOfPrdidCacheSize); |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
protected abstract byte[] productPage_nonaffil(BookStocking stocking) |
| 31 | |
throws IOException; |
| 32 | |
|
| 33 | |
protected abstract BookPageBase bookPage(byte[] text); |
| 34 | |
|
| 35 | |
public String retrievedISBNAndImage(BookStocking stocking) |
| 36 | |
throws IOException { |
| 37 | 0 | BookPageBase bookPage = bookPage(productPage_nonaffil(stocking)); |
| 38 | |
|
| 39 | 0 | if (bookPage.isbn == null) |
| 40 | 0 | return null; |
| 41 | |
|
| 42 | 0 | return bookPage.imageURL == null |
| 43 | |
? bookPage.isbn |
| 44 | |
: bookPage.isbn + "\u00b7" + bookPage.imageURL; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void resolve(BookStocking stocking) { |
| 48 | 0 | if (stocking.getIsbn_unsafe() == null) { |
| 49 | 0 | String vendorProductID = stocking.getVendorproductid_unsafe(); |
| 50 | 0 | String isbnAndImage = isbnAndImageOfPrdid.get(vendorProductID); |
| 51 | 0 | if (isbnAndImage == null) { |
| 52 | |
try { |
| 53 | 0 | isbnAndImage = retrievedISBNAndImage(stocking); |
| 54 | 0 | } catch (IOException e) { |
| 55 | 0 | return; |
| 56 | 0 | } |
| 57 | |
|
| 58 | 0 | if (isbnAndImage == null) |
| 59 | 0 | isbnAndImage = ""; |
| 60 | |
|
| 61 | 0 | isbnAndImageOfPrdid.put(vendorProductID, isbnAndImage); |
| 62 | 0 | isbnAndImageOfPrdid.flush(); |
| 63 | |
} |
| 64 | |
|
| 65 | 0 | int sep = isbnAndImage.indexOf("\u00b7"); |
| 66 | 0 | if (sep == -1) { |
| 67 | 0 | stocking.setIsbn_unsafe(isbnAndImage); |
| 68 | 0 | stocking.setThumbnailurl_unsafe(null); |
| 69 | |
} else { |
| 70 | 0 | stocking.setIsbn_unsafe(isbnAndImage.substring(0, sep)); |
| 71 | 0 | String tup = isbnAndImage.substring(sep + 1); |
| 72 | 0 | stocking.setThumbnailurl_unsafe( |
| 73 | |
tup.startsWith("http:") ? tup : thumbnailURLPrefix + tup); |
| 74 | |
} |
| 75 | |
} |
| 76 | 0 | } |
| 77 | |
} |