1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.amazonrankings.util;
16  
17  import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Time;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.kernel.webcache.WebCacheItem;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portlet.amazonrankings.model.AmazonRankings;
29  
30  import java.text.DateFormat;
31  
32  import java.util.ArrayList;
33  import java.util.Date;
34  import java.util.List;
35  import java.util.Locale;
36  
37  /**
38   * <a href="AmazonRankingsWebCacheItem.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Samuel Kong
42   */
43  public class AmazonRankingsWebCacheItem implements WebCacheItem {
44  
45      public AmazonRankingsWebCacheItem(String isbn) {
46          _isbn = isbn;
47      }
48  
49      public Object convert(String key) {
50          String isbn = _isbn;
51  
52          AmazonRankings amazonRankings = null;
53  
54          try {
55              StringBundler sb = new StringBundler(7);
56  
57              sb.append("http://ecs.amazonaws.com/onca/xml?Service=");
58              sb.append("AWSECommerceService&AWSAccessKeyId=");
59              sb.append(AmazonRankingsUtil.getAmazonAccessKeyId());
60              sb.append("&Operation=ItemLookup&IdType=ASIN&ItemId=");
61              sb.append(isbn);
62              sb.append("&ResponseGroup=Images,ItemAttributes,Offers,SalesRank&");
63              sb.append("Version=2009-02-01");
64  
65              String xml = HttpUtil.URLtoString(sb.toString());
66  
67              Document doc = SAXReaderUtil.read(xml);
68  
69              Element root = doc.getRootElement();
70  
71              if (root == null) {
72                  return null;
73              }
74  
75              Element items = root.element("Items");
76  
77              if (items == null) {
78                  return null;
79              }
80  
81              Element item = items.element("Item");
82  
83              if (item == null) {
84                  return null;
85              }
86  
87              Element itemAttributes = item.element("ItemAttributes");
88  
89              if (itemAttributes == null) {
90                  return null;
91              }
92  
93              String productName = itemAttributes.elementText("Title");
94              String catalog = StringPool.BLANK;
95              String[] authors = getAuthors(itemAttributes);
96              String releaseDateAsString = itemAttributes.elementText(
97                  "PublicationDate");
98              Date releaseDate = getReleaseDate(releaseDateAsString);
99              String manufacturer = itemAttributes.elementText("Manufacturer");
100             String smallImageURL = getImageURL(item, "SmallImage");
101             String mediumImageURL = getImageURL(item, "MediumImage");
102             String largeImageURL = getImageURL(item, "LargeImage");
103             double listPrice = getPrice(itemAttributes.element("ListPrice"));
104 
105             double ourPrice = 0;
106 
107             Element offerListing = getOfferListing(item);
108 
109             if (offerListing != null) {
110                 ourPrice = getPrice(offerListing.element("Price"));
111             }
112 
113             double usedPrice = 0;
114             double collectiblePrice = 0;
115             double thirdPartyNewPrice = 0;
116 
117             Element offerSummary = item.element("OfferSummary");
118 
119             if (offerSummary != null) {
120                 usedPrice = getPrice(offerSummary.element("LowestUsedPrice"));
121 
122                 collectiblePrice = getPrice(
123                     offerSummary.element("LowestCollectiblePrice"));
124 
125                 thirdPartyNewPrice = getPrice(
126                     offerSummary.element("LowestNewPrice"));
127             }
128 
129             int salesRank = GetterUtil.getInteger(
130                 item.elementText("SalesRank"));
131             String media = StringPool.BLANK;
132             String availability = getAvailability(offerListing);
133 
134             amazonRankings = new AmazonRankings(
135                 isbn, productName, catalog, authors, releaseDate,
136                 releaseDateAsString, manufacturer, smallImageURL,
137                 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
138                 collectiblePrice, thirdPartyNewPrice, salesRank, media,
139                 availability);
140         }
141         catch (Exception e) {
142         }
143 
144         return amazonRankings;
145     }
146 
147     public long getRefreshTime() {
148         return _REFRESH_TIME;
149     }
150 
151     protected String[] getAuthors(Element itemAttributes) {
152         List<String> authors = new ArrayList<String>();
153 
154         for (Element author : itemAttributes.elements("Author")) {
155             authors.add(author.getText());
156         }
157 
158         return authors.toArray(new String[authors.size()]);
159     }
160 
161     protected String getAvailability(Element offerListing) {
162         if (offerListing == null) {
163             return null;
164         }
165 
166         Element availabilityAttributes = offerListing.element(
167             "AvailabilityAttributes");
168 
169         return availabilityAttributes.elementText("AvailabilityType");
170     }
171 
172     protected String getImageURL(Element item, String name) {
173         String imageURL = null;
174 
175         Element image = item.element(name);
176 
177         if (image != null) {
178             imageURL = image.elementText("URL");
179         }
180 
181         return imageURL;
182     }
183 
184     protected Element getOfferListing(Element item) {
185         Element offers = item.element("Offers");
186 
187         if (offers == null) {
188             return null;
189         }
190 
191         Element offer = offers.element("Offer");
192 
193         if (offer == null) {
194             return null;
195         }
196 
197         return offer.element("OfferListing");
198     }
199 
200     protected double getPrice(Element price) {
201         if (price == null) {
202             return 0;
203         }
204 
205         return GetterUtil.getInteger(price.elementText("Amount")) * 0.01;
206     }
207 
208     protected Date getReleaseDate(String releaseDateAsString) {
209         if (Validator.isNull(releaseDateAsString)) {
210             return null;
211         }
212 
213         DateFormat dateFormat = null;
214 
215         if (releaseDateAsString.length() > 7) {
216             dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
217                 "yyyy-MM-dd", Locale.US);
218         }
219         else {
220             dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
221                 "yyyy-MM", Locale.US);
222         }
223 
224         return GetterUtil.getDate(releaseDateAsString, dateFormat);
225     }
226 
227     private static final long _REFRESH_TIME = Time.MINUTE * 20;
228 
229     private String _isbn;
230 
231 }