001
014
015 package com.liferay.portlet.amazonrankings.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Time;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.webcache.WebCacheItem;
026 import com.liferay.portal.kernel.xml.Document;
027 import com.liferay.portal.kernel.xml.Element;
028 import com.liferay.portal.kernel.xml.SAXReaderUtil;
029 import com.liferay.portlet.amazonrankings.model.AmazonRankings;
030
031 import java.text.DateFormat;
032
033 import java.util.ArrayList;
034 import java.util.Date;
035 import java.util.HashMap;
036 import java.util.List;
037 import java.util.Locale;
038 import java.util.Map;
039
040
045 public class AmazonRankingsWebCacheItem implements WebCacheItem {
046
047 public AmazonRankingsWebCacheItem(String isbn) {
048 _isbn = isbn;
049 }
050
051 public Object convert(String key) {
052 AmazonRankings amazonRankings = null;
053
054 try {
055 amazonRankings = doConvert(key);
056 }
057 catch (Exception e) {
058 _log.error(e, e);
059 }
060
061 return amazonRankings;
062 }
063
064 public long getRefreshTime() {
065 return _REFRESH_TIME;
066 }
067
068 protected AmazonRankings doConvert(String key) throws Exception {
069 Map<String, String> parameters = new HashMap<String, String>();
070
071 parameters.put(
072 "AWSAccessKeyId", AmazonRankingsUtil.getAmazonAccessKeyId());
073 parameters.put("IdType", "ASIN");
074 parameters.put("ItemId", _isbn);
075 parameters.put("Operation", "ItemLookup");
076 parameters.put(
077 "ResponseGroup", "Images,ItemAttributes,Offers,SalesRank");
078 parameters.put("Service", "AWSECommerceService");
079 parameters.put("Timestamp", AmazonRankingsUtil.getTimestamp());
080
081 String urlWithSignature =
082 AmazonSignedRequestsUtil.generateUrlWithSignature(parameters);
083
084 String xml = HttpUtil.URLtoString(urlWithSignature);
085
086 Document document = SAXReaderUtil.read(xml);
087
088 Element rootElement = document.getRootElement();
089
090 if (rootElement == null) {
091 return null;
092 }
093
094 if (hasErrorMessage(rootElement)) {
095 return null;
096 }
097
098 Element itemsElement = rootElement.element("Items");
099
100 if (itemsElement == null) {
101 return null;
102 }
103
104 Element requestElement = itemsElement.element("Request");
105
106 if (requestElement != null) {
107 Element errorsElement = requestElement.element("Errors");
108
109 if (hasErrorMessage(errorsElement)) {
110 return null;
111 }
112 }
113
114 Element itemElement = itemsElement.element("Item");
115
116 if (itemElement == null) {
117 return null;
118 }
119
120 Element itemAttributesElement = itemElement.element("ItemAttributes");
121
122 if (itemAttributesElement == null) {
123 return null;
124 }
125
126 String productName = itemAttributesElement.elementText("Title");
127 String catalog = StringPool.BLANK;
128 String[] authors = getAuthors(itemAttributesElement);
129 String releaseDateAsString = itemAttributesElement.elementText(
130 "PublicationDate");
131 Date releaseDate = getReleaseDate(releaseDateAsString);
132 String manufacturer = itemAttributesElement.elementText("Manufacturer");
133 String smallImageURL = getImageURL(itemElement, "SmallImage");
134 String mediumImageURL = getImageURL(itemElement, "MediumImage");
135 String largeImageURL = getImageURL(itemElement, "LargeImage");
136 double listPrice = getPrice(itemAttributesElement.element("ListPrice"));
137
138 double ourPrice = 0;
139
140 Element offerListingElement = getOfferListing(itemElement);
141
142 if (offerListingElement != null) {
143 ourPrice = getPrice(offerListingElement.element("Price"));
144 }
145
146 double usedPrice = 0;
147 double collectiblePrice = 0;
148 double thirdPartyNewPrice = 0;
149
150 Element offerSummaryElement = itemElement.element("OfferSummary");
151
152 if (offerSummaryElement != null) {
153 usedPrice = getPrice(
154 offerSummaryElement.element("LowestUsedPrice"));
155
156 collectiblePrice = getPrice(
157 offerSummaryElement.element("LowestCollectiblePrice"));
158
159 thirdPartyNewPrice = getPrice(
160 offerSummaryElement.element("LowestNewPrice"));
161 }
162
163 int salesRank = GetterUtil.getInteger(
164 itemElement.elementText("SalesRank"));
165 String media = StringPool.BLANK;
166 String availability = getAvailability(offerListingElement);
167
168 return new AmazonRankings(
169 _isbn, productName, catalog, authors, releaseDate,
170 releaseDateAsString, manufacturer, smallImageURL, mediumImageURL,
171 largeImageURL, listPrice, ourPrice, usedPrice, collectiblePrice,
172 thirdPartyNewPrice, salesRank, media, availability);
173 }
174
175 protected String[] getAuthors(Element itemAttributesElement) {
176 List<String> authors = new ArrayList<String>();
177
178 for (Element authorElement : itemAttributesElement.elements("Author")) {
179 authors.add(authorElement.getText());
180 }
181
182 return authors.toArray(new String[authors.size()]);
183 }
184
185 protected String getAvailability(Element offerListingElement) {
186 if (offerListingElement == null) {
187 return null;
188 }
189
190 Element availabilityElement = offerListingElement.element(
191 "Availability");
192
193 return availabilityElement.elementText("Availability");
194 }
195
196 protected String getImageURL(Element itemElement, String name) {
197 String imageURL = null;
198
199 Element imageElement = itemElement.element(name);
200
201 if (imageElement != null) {
202 imageURL = imageElement.elementText("URL");
203 }
204
205 return imageURL;
206 }
207
208 protected Element getOfferListing(Element itemElement) {
209 Element offersElement = itemElement.element("Offers");
210
211 if (offersElement == null) {
212 return null;
213 }
214
215 Element offerElement = offersElement.element("Offer");
216
217 if (offerElement == null) {
218 return null;
219 }
220
221 return offerElement.element("OfferListing");
222 }
223
224 protected double getPrice(Element priceElement) {
225 if (priceElement == null) {
226 return 0;
227 }
228
229 return GetterUtil.getInteger(priceElement.elementText("Amount")) * 0.01;
230 }
231
232 protected Date getReleaseDate(String releaseDateAsString) {
233 if (Validator.isNull(releaseDateAsString)) {
234 return null;
235 }
236
237 DateFormat dateFormat = null;
238
239 if (releaseDateAsString.length() > 7) {
240 dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
241 "yyyy-MM-dd", Locale.US);
242 }
243 else {
244 dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
245 "yyyy-MM", Locale.US);
246 }
247
248 return GetterUtil.getDate(releaseDateAsString, dateFormat);
249 }
250
251 protected boolean hasErrorMessage(Element element) {
252 if (element == null) {
253 return false;
254 }
255
256 Element errorElement = element.element("Error");
257
258 if (errorElement == null) {
259 return false;
260 }
261
262 Element messageElement = errorElement.element("Message");
263
264 if (messageElement == null) {
265 return false;
266 }
267
268 _log.error(messageElement.getText());
269
270 return true;
271 }
272
273 private static final long _REFRESH_TIME = Time.MINUTE * 20;
274
275 private static Log _log = LogFactoryUtil.getLog(
276 AmazonRankingsWebCacheItem.class);
277
278 private String _isbn;
279
280 }