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