summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java
blob: dc670b3d6e9c14f70329d8d33c9df2eb77ab678f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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();
		}
	}
}