1   /**
2    * Copyright (c) 2000-2008 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.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Time;
27  import com.liferay.portal.kernel.webcache.WebCacheException;
28  import com.liferay.portal.kernel.webcache.WebCacheItem;
29  import com.liferay.portlet.amazonrankings.model.AmazonRankings;
30  
31  import java.net.URL;
32  
33  import java.text.SimpleDateFormat;
34  
35  import java.util.Date;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import org.dom4j.Document;
41  import org.dom4j.Element;
42  import org.dom4j.io.SAXReader;
43  
44  /**
45   * <a href="AmazonRankingsWebCacheItem.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class AmazonRankingsWebCacheItem implements WebCacheItem {
51  
52      public AmazonRankingsWebCacheItem(String isbn) {
53          _isbn = isbn;
54      }
55  
56      public Object convert(String key) throws WebCacheException {
57          String isbn = _isbn;
58  
59          AmazonRankings amazonRankings = null;
60  
61          try {
62              URL url = new URL(
63                  "http://xml.amazon.com/onca/xml2?t=webservices-20&dev-t=" +
64                      AmazonRankingsUtil.getAmazonKey() + "&AsinSearch=" + isbn +
65                          "&type=heavy&f=xml");
66  
67              SAXReader reader = new SAXReader();
68  
69              Document doc = reader.read(url);
70  
71              Iterator<Element> itr = doc.getRootElement().elements(
72                  "ErrorMsg").iterator();
73  
74              if (itr.hasNext()) {
75                  Element el = itr.next();
76  
77                  String errorMsg = el.getText();
78  
79                  throw new WebCacheException(isbn + " " + errorMsg);
80              }
81  
82              Element details = (Element)doc.getRootElement().elements(
83                  "Details").iterator().next();
84  
85              String productName = details.elementText("ProductName");
86              String catalog = details.elementText("Catalog");
87  
88              List<Element> authorsList = details.element(
89                  "Authors").elements("Author");
90  
91              String[] authors = new String[authorsList.size()];
92  
93              for (int i = 0; i < authorsList.size(); i++) {
94                  Element author = authorsList.get(i);
95  
96                  authors[i] = author.getTextTrim();
97              }
98  
99              String releaseDateAsString = details.elementText("ReleaseDate");
100             Date releaseDate = GetterUtil.getDate(
101                 releaseDateAsString,
102                 new SimpleDateFormat("dd MMMMM,yyyy", Locale.US));
103             String manufacturer = details.elementText("Manufacturer");
104             String smallImageURL = details.elementText("ImageUrlSmall");
105             String mediumImageURL = details.elementText("ImageUrlMedium");
106             String largeImageURL = details.elementText("ImageUrlLarge");
107             double listPrice = GetterUtil.getDouble(
108                 details.elementText("ListPrice"));
109             double ourPrice = GetterUtil.getDouble(
110                 details.elementText("OurPrice"), listPrice);
111             double usedPrice = GetterUtil.getDouble(
112                 details.elementText("UsedPrice"));
113             double collectiblePrice = GetterUtil.getDouble(
114                 details.elementText("CollectiblePrice"));
115             double thirdPartyNewPrice = GetterUtil.getDouble(
116                 details.elementText("ThirdPartyNewPrice"));
117             int salesRank = GetterUtil.getInteger(
118                 details.elementText("SalesRank"));
119             String media = details.elementText("Media");
120             String availability = details.elementText("Availability");
121 
122             amazonRankings = new AmazonRankings(
123                 isbn, productName, catalog, authors, releaseDate,
124                 releaseDateAsString, manufacturer, smallImageURL,
125                 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
126                 collectiblePrice, thirdPartyNewPrice, salesRank, media,
127                 availability);
128         }
129         catch (WebCacheException ce) {
130             throw ce;
131         }
132         catch (Exception e) {
133             throw new WebCacheException(isbn + " " + e.toString());
134         }
135 
136         return amazonRankings;
137     }
138 
139     public long getRefreshTime() {
140         return _REFRESH_TIME;
141     }
142 
143     private static final long _REFRESH_TIME = Time.MINUTE * 20;
144 
145     private String _isbn;
146 
147 }