001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.amazonrankings.util;
016    
017    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Time;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.webcache.WebCacheItem;
025    import com.liferay.portal.kernel.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    import com.liferay.portlet.amazonrankings.model.AmazonRankings;
029    
030    import java.text.DateFormat;
031    
032    import java.util.ArrayList;
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Locale;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Samuel Kong
040     */
041    public class AmazonRankingsWebCacheItem implements WebCacheItem {
042    
043            public AmazonRankingsWebCacheItem(String isbn) {
044                    _isbn = isbn;
045            }
046    
047            public Object convert(String key) {
048                    String isbn = _isbn;
049    
050                    AmazonRankings amazonRankings = null;
051    
052                    try {
053                            StringBundler sb = new StringBundler(7);
054    
055                            sb.append("http://ecs.amazonaws.com/onca/xml?Service=");
056                            sb.append("AWSECommerceService&AWSAccessKeyId=");
057                            sb.append(AmazonRankingsUtil.getAmazonAccessKeyId());
058                            sb.append("&Operation=ItemLookup&IdType=ASIN&ItemId=");
059                            sb.append(isbn);
060                            sb.append("&ResponseGroup=Images,ItemAttributes,Offers,SalesRank&");
061                            sb.append("Version=2009-02-01");
062    
063                            String xml = HttpUtil.URLtoString(sb.toString());
064    
065                            Document doc = SAXReaderUtil.read(xml);
066    
067                            Element root = doc.getRootElement();
068    
069                            if (root == null) {
070                                    return null;
071                            }
072    
073                            Element items = root.element("Items");
074    
075                            if (items == null) {
076                                    return null;
077                            }
078    
079                            Element item = items.element("Item");
080    
081                            if (item == null) {
082                                    return null;
083                            }
084    
085                            Element itemAttributes = item.element("ItemAttributes");
086    
087                            if (itemAttributes == null) {
088                                    return null;
089                            }
090    
091                            String productName = itemAttributes.elementText("Title");
092                            String catalog = StringPool.BLANK;
093                            String[] authors = getAuthors(itemAttributes);
094                            String releaseDateAsString = itemAttributes.elementText(
095                                    "PublicationDate");
096                            Date releaseDate = getReleaseDate(releaseDateAsString);
097                            String manufacturer = itemAttributes.elementText("Manufacturer");
098                            String smallImageURL = getImageURL(item, "SmallImage");
099                            String mediumImageURL = getImageURL(item, "MediumImage");
100                            String largeImageURL = getImageURL(item, "LargeImage");
101                            double listPrice = getPrice(itemAttributes.element("ListPrice"));
102    
103                            double ourPrice = 0;
104    
105                            Element offerListing = getOfferListing(item);
106    
107                            if (offerListing != null) {
108                                    ourPrice = getPrice(offerListing.element("Price"));
109                            }
110    
111                            double usedPrice = 0;
112                            double collectiblePrice = 0;
113                            double thirdPartyNewPrice = 0;
114    
115                            Element offerSummary = item.element("OfferSummary");
116    
117                            if (offerSummary != null) {
118                                    usedPrice = getPrice(offerSummary.element("LowestUsedPrice"));
119    
120                                    collectiblePrice = getPrice(
121                                            offerSummary.element("LowestCollectiblePrice"));
122    
123                                    thirdPartyNewPrice = getPrice(
124                                            offerSummary.element("LowestNewPrice"));
125                            }
126    
127                            int salesRank = GetterUtil.getInteger(
128                                    item.elementText("SalesRank"));
129                            String media = StringPool.BLANK;
130                            String availability = getAvailability(offerListing);
131    
132                            amazonRankings = new AmazonRankings(
133                                    isbn, productName, catalog, authors, releaseDate,
134                                    releaseDateAsString, manufacturer, smallImageURL,
135                                    mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
136                                    collectiblePrice, thirdPartyNewPrice, salesRank, media,
137                                    availability);
138                    }
139                    catch (Exception e) {
140                    }
141    
142                    return amazonRankings;
143            }
144    
145            public long getRefreshTime() {
146                    return _REFRESH_TIME;
147            }
148    
149            protected String[] getAuthors(Element itemAttributes) {
150                    List<String> authors = new ArrayList<String>();
151    
152                    for (Element author : itemAttributes.elements("Author")) {
153                            authors.add(author.getText());
154                    }
155    
156                    return authors.toArray(new String[authors.size()]);
157            }
158    
159            protected String getAvailability(Element offerListing) {
160                    if (offerListing == null) {
161                            return null;
162                    }
163    
164                    Element availabilityAttributes = offerListing.element(
165                            "AvailabilityAttributes");
166    
167                    return availabilityAttributes.elementText("AvailabilityType");
168            }
169    
170            protected String getImageURL(Element item, String name) {
171                    String imageURL = null;
172    
173                    Element image = item.element(name);
174    
175                    if (image != null) {
176                            imageURL = image.elementText("URL");
177                    }
178    
179                    return imageURL;
180            }
181    
182            protected Element getOfferListing(Element item) {
183                    Element offers = item.element("Offers");
184    
185                    if (offers == null) {
186                            return null;
187                    }
188    
189                    Element offer = offers.element("Offer");
190    
191                    if (offer == null) {
192                            return null;
193                    }
194    
195                    return offer.element("OfferListing");
196            }
197    
198            protected double getPrice(Element price) {
199                    if (price == null) {
200                            return 0;
201                    }
202    
203                    return GetterUtil.getInteger(price.elementText("Amount")) * 0.01;
204            }
205    
206            protected Date getReleaseDate(String releaseDateAsString) {
207                    if (Validator.isNull(releaseDateAsString)) {
208                            return null;
209                    }
210    
211                    DateFormat dateFormat = null;
212    
213                    if (releaseDateAsString.length() > 7) {
214                            dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
215                                    "yyyy-MM-dd", Locale.US);
216                    }
217                    else {
218                            dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
219                                    "yyyy-MM", Locale.US);
220                    }
221    
222                    return GetterUtil.getDate(releaseDateAsString, dateFormat);
223            }
224    
225            private static final long _REFRESH_TIME = Time.MINUTE * 20;
226    
227            private String _isbn;
228    
229    }