1
22
23 package com.liferay.portlet.amazonrankings.util;
24
25 import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.HttpUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.Time;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.kernel.webcache.WebCacheItem;
32 import com.liferay.portal.kernel.xml.Document;
33 import com.liferay.portal.kernel.xml.Element;
34 import com.liferay.portal.kernel.xml.SAXReaderUtil;
35 import com.liferay.portlet.amazonrankings.model.AmazonRankings;
36
37 import java.text.DateFormat;
38
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
42 import java.util.Locale;
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) {
57 String isbn = _isbn;
58
59 AmazonRankings amazonRankings = null;
60
61 try {
62 StringBuilder sb = new StringBuilder();
63
64 sb.append("http://ecs.amazonaws.com/onca/xml?Service=");
65 sb.append("AWSECommerceService&AWSAccessKeyId=");
66 sb.append(AmazonRankingsUtil.getAmazonAccessKeyId());
67 sb.append("&Operation=ItemLookup&IdType=ASIN&ItemId=");
68 sb.append(isbn);
69 sb.append("&ResponseGroup=Images,ItemAttributes,Offers,SalesRank&");
70 sb.append("Version=2009-02-01");
71
72 String xml = HttpUtil.URLtoString(sb.toString());
73
74 Document doc = SAXReaderUtil.read(xml);
75
76 Element root = doc.getRootElement();
77
78 if (root == null) {
79 return null;
80 }
81
82 Element items = root.element("Items");
83
84 if (items == null) {
85 return null;
86 }
87
88 Element item = items.element("Item");
89
90 if (item == null) {
91 return null;
92 }
93
94 Element itemAttributes = item.element("ItemAttributes");
95
96 if (itemAttributes == null) {
97 return null;
98 }
99
100 String productName = itemAttributes.elementText("Title");
101 String catalog = StringPool.BLANK;
102 String[] authors = getAuthors(itemAttributes);
103 String releaseDateAsString = itemAttributes.elementText(
104 "PublicationDate");
105 Date releaseDate = getReleaseDate(releaseDateAsString);
106 String manufacturer = itemAttributes.elementText("Manufacturer");
107 String smallImageURL = getImageURL(item, "SmallImage");
108 String mediumImageURL = getImageURL(item, "MediumImage");
109 String largeImageURL = getImageURL(item, "LargeImage");
110 double listPrice = getPrice(itemAttributes.element("ListPrice"));
111
112 double ourPrice = 0;
113
114 Element offerListing = getOfferListing(item);
115
116 if (offerListing != null) {
117 ourPrice = getPrice(offerListing.element("Price"));
118 }
119
120 double usedPrice = 0;
121 double collectiblePrice = 0;
122 double thirdPartyNewPrice = 0;
123
124 Element offerSummary = item.element("OfferSummary");
125
126 if (offerSummary != null) {
127 usedPrice = getPrice(offerSummary.element("LowestUsedPrice"));
128
129 collectiblePrice = getPrice(
130 offerSummary.element("LowestCollectiblePrice"));
131
132 thirdPartyNewPrice = getPrice(
133 offerSummary.element("LowestNewPrice"));
134 }
135
136 int salesRank = GetterUtil.getInteger(
137 item.elementText("SalesRank"));
138 String media = StringPool.BLANK;
139 String availability = getAvailability(offerListing);
140
141 amazonRankings = new AmazonRankings(
142 isbn, productName, catalog, authors, releaseDate,
143 releaseDateAsString, manufacturer, smallImageURL,
144 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
145 collectiblePrice, thirdPartyNewPrice, salesRank, media,
146 availability);
147 }
148 catch (Exception e) {
149 }
150
151 return amazonRankings;
152 }
153
154 public long getRefreshTime() {
155 return _REFRESH_TIME;
156 }
157
158 protected String[] getAuthors(Element itemAttributes) {
159 List<String> authors = new ArrayList<String>();
160
161 for (Element author : itemAttributes.elements("Author")) {
162 authors.add(author.getText());
163 }
164
165 return authors.toArray(new String[authors.size()]);
166 }
167
168 protected String getAvailability(Element offerListing) {
169 if (offerListing == null) {
170 return null;
171 }
172
173 Element availabilityAttributes = offerListing.element(
174 "AvailabilityAttributes");
175
176 return availabilityAttributes.elementText("AvailabilityType");
177 }
178
179 protected String getImageURL(Element item, String name) {
180 String imageURL = null;
181
182 Element image = item.element(name);
183
184 if (image != null) {
185 imageURL = image.elementText("URL");
186 }
187
188 return imageURL;
189 }
190
191 protected Element getOfferListing(Element item) {
192 Element offers = item.element("Offers");
193
194 if (offers == null) {
195 return null;
196 }
197
198 Element offer = offers.element("Offer");
199
200 if (offer == null) {
201 return null;
202 }
203
204 return offer.element("OfferListing");
205 }
206
207 protected double getPrice(Element price) {
208 if (price == null) {
209 return 0;
210 }
211
212 return GetterUtil.getInteger(price.elementText("Amount")) * 0.01;
213 }
214
215 protected Date getReleaseDate(String releaseDateAsString) {
216 if (Validator.isNull(releaseDateAsString)) {
217 return null;
218 }
219
220 DateFormat dateFormat = null;
221
222 if (releaseDateAsString.length() > 7) {
223 dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
224 "yyyy-MM-dd", Locale.US);
225 }
226 else {
227 dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
228 "yyyy-MM", Locale.US);
229 }
230
231 return GetterUtil.getDate(releaseDateAsString, dateFormat);
232 }
233
234 private static final long _REFRESH_TIME = Time.MINUTE * 20;
235
236 private String _isbn;
237
238 }