1
22
23 package com.liferay.portlet.amazonrankings.util;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.Time;
27 import com.liferay.portal.kernel.webcache.WebCacheException;
28 import com.liferay.portal.kernel.webcache.WebCacheItem;
29 import com.liferay.portal.kernel.xml.Document;
30 import com.liferay.portal.kernel.xml.Element;
31 import com.liferay.portal.kernel.xml.SAXReaderUtil;
32 import com.liferay.portlet.amazonrankings.model.AmazonRankings;
33
34 import java.net.URL;
35
36 import java.text.SimpleDateFormat;
37
38 import java.util.Date;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Locale;
42
43
49 public class AmazonRankingsWebCacheItem implements WebCacheItem {
50
51 public AmazonRankingsWebCacheItem(String isbn) {
52 _isbn = isbn;
53 }
54
55 public Object convert(String key) throws WebCacheException {
56 String isbn = _isbn;
57
58 AmazonRankings amazonRankings = null;
59
60 try {
61 URL url = new URL(
62 "http://xml.amazon.com/onca/xml2?t=webservices-20&dev-t=" +
63 AmazonRankingsUtil.getAmazonKey() + "&AsinSearch=" + isbn +
64 "&type=heavy&f=xml");
65
66 Document doc = SAXReaderUtil.read(url);
67
68 Iterator<Element> itr = doc.getRootElement().elements(
69 "ErrorMsg").iterator();
70
71 if (itr.hasNext()) {
72 Element el = itr.next();
73
74 String errorMsg = el.getText();
75
76 throw new WebCacheException(isbn + " " + errorMsg);
77 }
78
79 Element details = doc.getRootElement().elements(
80 "Details").iterator().next();
81
82 String productName = details.elementText("ProductName");
83 String catalog = details.elementText("Catalog");
84
85 List<Element> authorsList = details.element(
86 "Authors").elements("Author");
87
88 String[] authors = new String[authorsList.size()];
89
90 for (int i = 0; i < authorsList.size(); i++) {
91 Element author = authorsList.get(i);
92
93 authors[i] = author.getTextTrim();
94 }
95
96 String releaseDateAsString = details.elementText("ReleaseDate");
97 Date releaseDate = GetterUtil.getDate(
98 releaseDateAsString,
99 new SimpleDateFormat("dd MMMMM,yyyy", Locale.US));
100 String manufacturer = details.elementText("Manufacturer");
101 String smallImageURL = details.elementText("ImageUrlSmall");
102 String mediumImageURL = details.elementText("ImageUrlMedium");
103 String largeImageURL = details.elementText("ImageUrlLarge");
104 double listPrice = GetterUtil.getDouble(
105 details.elementText("ListPrice"));
106 double ourPrice = GetterUtil.getDouble(
107 details.elementText("OurPrice"), listPrice);
108 double usedPrice = GetterUtil.getDouble(
109 details.elementText("UsedPrice"));
110 double collectiblePrice = GetterUtil.getDouble(
111 details.elementText("CollectiblePrice"));
112 double thirdPartyNewPrice = GetterUtil.getDouble(
113 details.elementText("ThirdPartyNewPrice"));
114 int salesRank = GetterUtil.getInteger(
115 details.elementText("SalesRank"));
116 String media = details.elementText("Media");
117 String availability = details.elementText("Availability");
118
119 amazonRankings = new AmazonRankings(
120 isbn, productName, catalog, authors, releaseDate,
121 releaseDateAsString, manufacturer, smallImageURL,
122 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
123 collectiblePrice, thirdPartyNewPrice, salesRank, media,
124 availability);
125 }
126 catch (WebCacheException ce) {
127 throw ce;
128 }
129 catch (Exception e) {
130 throw new WebCacheException(isbn + " " + e.toString());
131 }
132
133 return amazonRankings;
134 }
135
136 public long getRefreshTime() {
137 return _REFRESH_TIME;
138 }
139
140 private static final long _REFRESH_TIME = Time.MINUTE * 20;
141
142 private String _isbn;
143
144 }