1
22
23 package com.liferay.portlet.amazonrankings.util;
24
25 import com.amazonaws.a2s.AmazonA2S;
26 import com.amazonaws.a2s.AmazonA2SClient;
27 import com.amazonaws.a2s.AmazonA2SException;
28 import com.amazonaws.a2s.AmazonA2SLocale;
29 import com.amazonaws.a2s.model.Item;
30 import com.amazonaws.a2s.model.ItemAttributes;
31 import com.amazonaws.a2s.model.ItemLookupRequest;
32 import com.amazonaws.a2s.model.ItemLookupResponse;
33 import com.amazonaws.a2s.model.Items;
34 import com.amazonaws.a2s.model.Offer;
35 import com.amazonaws.a2s.model.OfferListing;
36 import com.amazonaws.a2s.model.OfferSummary;
37 import com.amazonaws.a2s.model.Offers;
38 import com.amazonaws.a2s.model.Price;
39
40 import com.liferay.portal.kernel.util.GetterUtil;
41 import com.liferay.portal.kernel.util.StringPool;
42 import com.liferay.portal.kernel.util.Time;
43 import com.liferay.portal.kernel.webcache.WebCacheException;
44 import com.liferay.portal.kernel.webcache.WebCacheItem;
45 import com.liferay.portlet.amazonrankings.model.AmazonRankings;
46
47 import java.text.SimpleDateFormat;
48
49 import java.util.ArrayList;
50 import java.util.Date;
51 import java.util.List;
52 import java.util.Locale;
53
54
60 public class AmazonRankingsWebCacheItem implements WebCacheItem {
61
62 public AmazonRankingsWebCacheItem(String isbn) {
63 _isbn = isbn;
64 }
65
66 public Object convert(String key) throws WebCacheException {
67 String isbn = _isbn;
68
69 AmazonRankings amazonRankings = null;
70
71 try {
72 AmazonA2S a2service = new AmazonA2SClient(
73 AmazonRankingsUtil.getAmazonAccessKeyId(),
74 AmazonRankingsUtil.getAmazonAssociateTag(), AmazonA2SLocale.US);
75
76 ItemLookupRequest itemLookupRequest = getItemLookupRequest(isbn);
77
78 ItemLookupResponse itemLookupResponse = a2service.itemLookup(
79 itemLookupRequest);
80
81 Item item = getItem(itemLookupResponse);
82
83 if (!item.isSetItemAttributes()) {
84 throw new AmazonA2SException("Item attributes is not set");
85 }
86
87 ItemAttributes itemAttributes = item.getItemAttributes();
88
89 String productName = itemAttributes.getTitle();
90 String catalog = StringPool.BLANK;
91
92 String[] authors = null;
93
94 if (itemAttributes.isSetAuthor()) {
95 List<String> authorsList = itemAttributes.getAuthor();
96
97 authorsList.toArray(new String[authorsList.size()]);
98 }
99
100 Date releaseDate = null;
101 String releaseDateAsString = null;
102
103 if (itemAttributes.isSetPublicationDate()) {
104 releaseDateAsString = itemAttributes.getPublicationDate();
105
106 SimpleDateFormat dateFormat = null;
107
108 if (releaseDateAsString.length() > 7) {
109 dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
110 }
111 else {
112 dateFormat = new SimpleDateFormat("yyyy-MM", Locale.US);
113 }
114
115 releaseDate = GetterUtil.getDate(
116 releaseDateAsString, dateFormat);
117 }
118
119 String manufacturer = itemAttributes.getManufacturer();
120
121 String smallImageURL = null;
122
123 if (item.isSetSmallImage()) {
124 smallImageURL = item.getSmallImage().getURL();
125 }
126
127 String mediumImageURL = null;
128
129 if (item.isSetMediumImage()) {
130 mediumImageURL = item.getMediumImage().getURL();
131 }
132
133 String largeImageURL = null;
134
135 if (item.isSetLargeImage()) {
136 largeImageURL = item.getLargeImage().getURL();
137 }
138
139 double listPrice = 0;
140 double ourPrice = 0;
141 double usedPrice = 0;
142 double collectiblePrice = 0;
143 double thirdPartyNewPrice = 0;
144
145 listPrice = getPrice(itemAttributes.getListPrice());
146
147 OfferListing offerListing = getOfferListing(item);
148
149 if (offerListing != null) {
150 ourPrice = getPrice(offerListing.getPrice());
151 }
152
153 if (item.isSetOfferSummary()) {
154 OfferSummary offerSummary = item.getOfferSummary();
155
156 usedPrice = getPrice(offerSummary.getLowestUsedPrice());
157 collectiblePrice = getPrice(
158 offerSummary.getLowestCollectiblePrice());
159 thirdPartyNewPrice = getPrice(
160 offerSummary.getLowestNewPrice());
161
162 }
163
164 int salesRank = GetterUtil.getInteger(item.getSalesRank());
165 String media = StringPool.BLANK;
166
167 String availability = null;
168
169 if (offerListing != null) {
170 availability = offerListing.getAvailability();
171 }
172
173 amazonRankings = new AmazonRankings(
174 isbn, productName, catalog, authors, releaseDate,
175 releaseDateAsString, manufacturer, smallImageURL,
176 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
177 collectiblePrice, thirdPartyNewPrice, salesRank, media,
178 availability);
179 }
180 catch (Exception e) {
181 throw new WebCacheException(isbn + " " + e.toString());
182 }
183
184 return amazonRankings;
185 }
186
187 public long getRefreshTime() {
188 return _REFRESH_TIME;
189 }
190
191 protected Item getItem(ItemLookupResponse itemLookupResponse)
192 throws Exception {
193
194 List<Items> itemsList = itemLookupResponse.getItems();
195
196 if (itemsList.isEmpty()) {
197 throw new AmazonA2SException("Items list is empty");
198 }
199
200 Items items = itemsList.get(0);
201
202 List<Item> itemList = items.getItem();
203
204 if (itemList.isEmpty()) {
205 throw new AmazonA2SException("Item list is empty");
206 }
207
208 return itemList.get(0);
209 }
210
211 protected ItemLookupRequest getItemLookupRequest(String isbn) {
212 ItemLookupRequest itemLookupRequest = new ItemLookupRequest();
213
214
216 List<String> itemId = new ArrayList<String>();
217
218 itemId.add(isbn);
219
220 itemLookupRequest.setItemId(itemId);
221
222
224 List<String> responseGroup = new ArrayList<String>();
225
226 responseGroup.add("Images");
227 responseGroup.add("ItemAttributes");
228 responseGroup.add("Offers");
229 responseGroup.add("SalesRank");
230
231 itemLookupRequest.setResponseGroup(responseGroup);
232
233 return itemLookupRequest;
234 }
235
236 protected OfferListing getOfferListing(Item item) {
237 Offers offers = item.getOffers();
238
239 List<Offer> offersList = offers.getOffer();
240
241 if (offersList.isEmpty()) {
242 return null;
243 }
244
245 Offer offer = offersList.get(0);
246
247 List<OfferListing> offerListings = offer.getOfferListing();
248
249 if (offerListings.isEmpty()) {
250 return null;
251 }
252
253 return offerListings.get(0);
254 }
255
256 protected double getPrice(Price price) {
257 if (price == null) {
258 return 0;
259 }
260
261 return price.getAmount() * 0.01;
262 }
263
264 private static final long _REFRESH_TIME = Time.MINUTE * 20;
265
266 private String _isbn;
267
268 }