1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
55   * <a href="AmazonRankingsWebCacheItem.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
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         // ISBN
215 
216         List<String> itemId = new ArrayList<String>();
217 
218         itemId.add(isbn);
219 
220         itemLookupRequest.setItemId(itemId);
221 
222         // Response group
223 
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 }