summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java')
-rw-r--r--src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java b/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java
new file mode 100644
index 0000000..dc670b3
--- /dev/null
+++ b/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java
@@ -0,0 +1,158 @@
+/*
+ * Book Catalog - Catalog your book collection.
+ *
+ * Copyright (C) 2009 Joshua Walters
+ * URL: http://joshwalters.com
+ *
+ * This file is part of Book Catalog.
+ *
+ * Book Catalog is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Book Catalog is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Book Catalog. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.joshwalters.bookcatalog.isbnlookup;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+
+import com.google.gdata.client.books.BooksService;
+import com.google.gdata.client.books.VolumeQuery;
+import com.google.gdata.data.books.VolumeEntry;
+import com.google.gdata.data.books.VolumeFeed;
+import com.google.gdata.data.dublincore.Creator;
+import com.google.gdata.data.dublincore.Subject;
+import com.google.gdata.data.dublincore.Title;
+import com.google.gdata.util.ServiceException;
+
+/**
+ * Performs book lookup with GData.
+ *
+ * @author Josh Walters
+ */
+public class GDataISBNLookup extends ISBNLookup {
+
+ /**
+ * Used for looking up the book.
+ */
+ private BooksService service;
+ /**
+ * The URL for the Google volumes feed.
+ */
+ public final String FEED_URL = "http://books.google.com/books/feeds/volumes";
+
+ /**
+ * Sets up the book service.
+ */
+ public GDataISBNLookup() {
+ service = new BooksService("BookCatalog-1.0");
+ }
+
+ /**
+ * Searches for a book.
+ */
+ public void searchBooks(String barcode) throws NoBookFound,
+ InvalidSearchTerms {
+ // Clear all the book data fields
+ emptyDataFields();
+ // Purify the barcode, get the price if possible, and return just the
+ // ISBN
+ ISBN = purifyBarcode(barcode);
+ // Perform the query
+ VolumeQuery query;
+ try {
+ query = new VolumeQuery(new URL(FEED_URL));
+ query.setFullTextQuery(ISBN);
+ // Only return one result
+ query.setMaxResults(1);
+ VolumeFeed volumeFeed = service.query(query, VolumeFeed.class);
+ // Process the result
+ processVolumeFeed(volumeFeed);
+ } catch (MalformedURLException e) {
+ // Print the error to the error stream
+ System.err.println(e.getMessage());
+ } catch (IOException e) {
+ // Print the error to the error stream
+ System.err.println(e.getMessage());
+ } catch (ServiceException e) {
+ // Print the error to the error stream
+ System.err.println(e.getMessage());
+ }
+ }
+
+ /**
+ * Processes the result feed to get the book data.
+ *
+ * @param volumeFeed
+ * @throws IOException
+ * @throws ServiceException
+ * @throws NoBookFound
+ */
+ private void processVolumeFeed(VolumeFeed volumeFeed) throws IOException,
+ ServiceException, NoBookFound {
+ List<VolumeEntry> volumeEntries = volumeFeed.getEntries();
+ if (volumeEntries.size() == 0) {
+ throw new NoBookFound();
+ }
+ // Send the data to be stored in the class
+ storeVolumeEntry(volumeEntries.get(0));
+ }
+
+ /**
+ * Store the data in the class variables.
+ *
+ * @param entry
+ * @throws IOException
+ * @throws ServiceException
+ */
+ private void storeVolumeEntry(VolumeEntry entry) throws IOException,
+ ServiceException {
+ // Get the book title(s).
+ for (Title t : entry.getTitles()) {
+ if (title == null) {
+ title = t.getValue();
+ } else {
+ title += ", " + t.getValue();
+ }
+ }
+ // Get the book creator(s).
+ for (Creator c : entry.getCreators()) {
+ if (author == null) {
+ author = c.getValue();
+ } else {
+ author += ", " + c.getValue();
+ }
+ }
+ // Get the book date.
+ if (entry.getDates().size() > 0) {
+ date = entry.getDates().get(0).getValue();
+ }
+ // Get the book description.
+ if (entry.getDescriptions().size() > 0) {
+ description = entry.getDescriptions().get(0).getValue();
+ }
+ // Get the book subject(s).
+ for (Subject s : entry.getSubjects()) {
+ if (subject == null) {
+ subject = s.getValue();
+ } else {
+ subject += ", " + s.getValue();
+ }
+ }
+ // Get the book publisher.
+ if (entry.getPublishers().size() > 0) {
+ publisher = entry.getPublishers().get(0).getValue();
+ }
+ }
+} \ No newline at end of file