001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.amazonrankings.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.webcache.WebCacheException;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portlet.amazonrankings.model.AmazonRankings;
027: import com.liferay.util.Time;
028:
029: import java.net.URL;
030:
031: import java.text.SimpleDateFormat;
032:
033: import java.util.Date;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Locale;
037:
038: import org.dom4j.Document;
039: import org.dom4j.Element;
040: import org.dom4j.io.SAXReader;
041:
042: /**
043: * <a href="AmazonRankingsWebCacheItem.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class AmazonRankingsWebCacheItem implements WebCacheItem {
049:
050: public AmazonRankingsWebCacheItem(String isbn) {
051: _isbn = isbn;
052: }
053:
054: public Object convert(String key) throws WebCacheException {
055: String isbn = _isbn;
056:
057: AmazonRankings amazonRankings = null;
058:
059: try {
060: URL url = new URL(
061: "http://xml.amazon.com/onca/xml2?t=webservices-20&dev-t="
062: + AmazonRankingsUtil.getAmazonKey()
063: + "&AsinSearch=" + isbn
064: + "&type=heavy&f=xml");
065:
066: SAXReader reader = new SAXReader();
067:
068: Document doc = reader.read(url);
069:
070: Iterator itr = doc.getRootElement().elements("ErrorMsg")
071: .iterator();
072:
073: if (itr.hasNext()) {
074: String errorMsg = ((Element) itr.next()).getText();
075:
076: throw new WebCacheException(isbn + " " + errorMsg);
077: }
078:
079: Element details = (Element) doc.getRootElement().elements(
080: "Details").iterator().next();
081:
082: String productName = details.elementText("ProductName");
083: String catalog = details.elementText("Catalog");
084:
085: List authorsList = details.element("Authors").elements(
086: "Author");
087:
088: String[] authors = new String[authorsList.size()];
089:
090: for (int i = 0; i < authorsList.size(); i++) {
091: Element author = (Element) authorsList.get(i);
092:
093: authors[i] = author.getTextTrim();
094: }
095:
096: String releaseDateAsString = details
097: .elementText("ReleaseDate");
098: Date releaseDate = GetterUtil.getDate(releaseDateAsString,
099: new SimpleDateFormat("dd MMMMM,yyyy", Locale.US));
100: String manufacturer = details.elementText("Manufacturer");
101: String smallImageURL = details.elementText("ImageUrlSmall");
102: String mediumImageURL = details
103: .elementText("ImageUrlMedium");
104: String largeImageURL = details.elementText("ImageUrlLarge");
105: double listPrice = GetterUtil.getDouble(details
106: .elementText("ListPrice"));
107: double ourPrice = GetterUtil.getDouble(details
108: .elementText("OurPrice"), listPrice);
109: double usedPrice = GetterUtil.getDouble(details
110: .elementText("UsedPrice"));
111: double collectiblePrice = GetterUtil.getDouble(details
112: .elementText("CollectiblePrice"));
113: double thirdPartyNewPrice = GetterUtil.getDouble(details
114: .elementText("ThirdPartyNewPrice"));
115: int salesRank = GetterUtil.getInteger(details
116: .elementText("SalesRank"));
117: String media = details.elementText("Media");
118: String availability = details.elementText("Availability");
119:
120: amazonRankings = new AmazonRankings(isbn, productName,
121: catalog, authors, releaseDate, releaseDateAsString,
122: manufacturer, smallImageURL, mediumImageURL,
123: largeImageURL, listPrice, ourPrice, usedPrice,
124: collectiblePrice, thirdPartyNewPrice, salesRank,
125: media, availability);
126: } catch (WebCacheException ce) {
127: throw ce;
128: } catch (Exception e) {
129: throw new WebCacheException(isbn + " " + e.toString());
130: }
131:
132: return amazonRankings;
133: }
134:
135: public long getRefreshTime() {
136: return _REFRESH_TIME;
137: }
138:
139: private static final long _REFRESH_TIME = Time.MINUTE * 20;
140:
141: private String _isbn;
142:
143: }
|