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