1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.amazonrankings.util;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.HttpUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Time;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.kernel.webcache.WebCacheItem;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portlet.amazonrankings.model.AmazonRankings;
32  
33  import java.text.SimpleDateFormat;
34  
35  import java.util.ArrayList;
36  import java.util.Date;
37  import java.util.List;
38  import java.util.Locale;
39  
40  /**
41   * <a href="AmazonRankingsWebCacheItem.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Samuel Kong
45   *
46   */
47  public class AmazonRankingsWebCacheItem implements WebCacheItem {
48  
49      public AmazonRankingsWebCacheItem(String isbn) {
50          _isbn = isbn;
51      }
52  
53      public Object convert(String key) {
54          String isbn = _isbn;
55  
56          AmazonRankings amazonRankings = null;
57  
58          try {
59              StringBuilder sb = new StringBuilder();
60  
61              sb.append("http://ecs.amazonaws.com/onca/xml?Service=");
62              sb.append("AWSECommerceService&AWSAccessKeyId=");
63              sb.append(AmazonRankingsUtil.getAmazonAccessKeyId());
64              sb.append("&Operation=ItemLookup&IdType=ASIN&ItemId=");
65              sb.append(isbn);
66              sb.append("&ResponseGroup=Images,ItemAttributes,Offers,SalesRank&");
67              sb.append("Version=2009-02-01");
68  
69              String xml = HttpUtil.URLtoString(sb.toString());
70  
71              Document doc = SAXReaderUtil.read(xml);
72  
73              Element root = doc.getRootElement();
74  
75              if (root == null) {
76                  return null;
77              }
78  
79              Element items = root.element("Items");
80  
81              if (items == null) {
82                  return null;
83              }
84  
85              Element item = items.element("Item");
86  
87              if (item == null) {
88                  return null;
89              }
90  
91              Element itemAttributes = item.element("ItemAttributes");
92  
93              if (itemAttributes == null) {
94                  return null;
95              }
96  
97              String productName = itemAttributes.elementText("Title");
98              String catalog = StringPool.BLANK;
99              String[] authors = getAuthors(itemAttributes);
100             String releaseDateAsString = itemAttributes.elementText(
101                 "PublicationDate");
102             Date releaseDate = getReleaseDate(releaseDateAsString);
103             String manufacturer = itemAttributes.elementText("Manufacturer");
104             String smallImageURL = getImageURL(item, "SmallImage");
105             String mediumImageURL = getImageURL(item, "MediumImage");
106             String largeImageURL = getImageURL(item, "LargeImage");
107             double listPrice = getPrice(itemAttributes.element("ListPrice"));
108 
109             double ourPrice = 0;
110 
111             Element offerListing = getOfferListing(item);
112 
113             if (offerListing != null) {
114                 ourPrice = getPrice(offerListing.element("Price"));
115             }
116 
117             double usedPrice = 0;
118             double collectiblePrice = 0;
119             double thirdPartyNewPrice = 0;
120 
121             Element offerSummary = item.element("OfferSummary");
122 
123             if (offerSummary != null) {
124                 usedPrice = getPrice(offerSummary.element("LowestUsedPrice"));
125 
126                 collectiblePrice = getPrice(
127                     offerSummary.element("LowestCollectiblePrice"));
128 
129                 thirdPartyNewPrice = getPrice(
130                     offerSummary.element("LowestNewPrice"));
131             }
132 
133             int salesRank = GetterUtil.getInteger(
134                 item.elementText("SalesRank"));
135             String media = StringPool.BLANK;
136             String availability = getAvailability(offerListing);
137 
138             amazonRankings = new AmazonRankings(
139                 isbn, productName, catalog, authors, releaseDate,
140                 releaseDateAsString, manufacturer, smallImageURL,
141                 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
142                 collectiblePrice, thirdPartyNewPrice, salesRank, media,
143                 availability);
144         }
145         catch (Exception e) {
146         }
147 
148         return amazonRankings;
149     }
150 
151     public long getRefreshTime() {
152         return _REFRESH_TIME;
153     }
154 
155     protected String[] getAuthors(Element itemAttributes) {
156         List<String> authors = new ArrayList<String>();
157 
158         for (Element author : itemAttributes.elements("Author")) {
159             authors.add(author.getText());
160         }
161 
162         return authors.toArray(new String[authors.size()]);
163     }
164 
165     protected String getAvailability(Element offerListing) {
166         if (offerListing == null) {
167             return null;
168         }
169 
170         Element availabilityAttributes = offerListing.element(
171             "AvailabilityAttributes");
172 
173         return availabilityAttributes.elementText("AvailabilityType");
174     }
175 
176     protected String getImageURL(Element item, String name) {
177         String imageURL = null;
178 
179         Element image = item.element(name);
180 
181         if (image != null) {
182             imageURL = image.elementText("URL");
183         }
184 
185         return imageURL;
186     }
187 
188     protected Element getOfferListing(Element item) {
189         Element offers = item.element("Offers");
190 
191         if (offers == null) {
192             return null;
193         }
194 
195         Element offer = offers.element("Offer");
196 
197         if (offer == null) {
198             return null;
199         }
200 
201         return offer.element("OfferListing");
202     }
203 
204     protected double getPrice(Element price) {
205         if (price == null) {
206             return 0;
207         }
208 
209         return GetterUtil.getInteger(price.elementText("Amount")) * 0.01;
210     }
211 
212     protected Date getReleaseDate(String releaseDateAsString) {
213         if (Validator.isNull(releaseDateAsString)) {
214             return null;
215         }
216 
217         SimpleDateFormat dateFormat = null;
218 
219         if (releaseDateAsString.length() > 7) {
220             dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
221         }
222         else {
223             dateFormat = new SimpleDateFormat("yyyy-MM", Locale.US);
224         }
225 
226         return GetterUtil.getDate(releaseDateAsString, dateFormat);
227     }
228 
229     private static final long _REFRESH_TIME = Time.MINUTE * 20;
230 
231     private String _isbn;
232 
233 }