1
22
23 package com.liferay.portlet.amazonrankings.util;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.util.WebCacheable;
27 import com.liferay.portlet.amazonrankings.model.AmazonRankings;
28 import com.liferay.util.ConverterException;
29 import com.liferay.util.Time;
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
50 public class AmazonRankingsConverter implements WebCacheable {
51
52 public AmazonRankingsConverter(String isbn) {
53 _isbn = isbn;
54 }
55
56 public Object convert(String id) throws ConverterException {
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 itr = doc.getRootElement().elements("ErrorMsg").iterator();
72
73 if (itr.hasNext()) {
74 String errorMsg = ((Element)itr.next()).getText();
75
76 throw new ConverterException(isbn + " " + errorMsg);
77 }
78
79 Element details = (Element)doc.getRootElement().elements(
80 "Details").iterator().next();
81
82 String productName = details.elementText("ProductName");
83 String catalog = details.elementText("Catalog");
84
85 List authorsList = details.element("Authors").elements("Author");
86
87 String[] authors = new String[authorsList.size()];
88
89 for (int i = 0; i < authorsList.size(); i++) {
90 Element author = (Element)authorsList.get(i);
91
92 authors[i] = author.getTextTrim();
93 }
94
95 String releaseDateAsString = details.elementText("ReleaseDate");
96 Date releaseDate = GetterUtil.getDate(
97 releaseDateAsString,
98 new SimpleDateFormat("dd MMMMM,yyyy", Locale.US));
99 String manufacturer = details.elementText("Manufacturer");
100 String smallImageURL = details.elementText("ImageUrlSmall");
101 String mediumImageURL = details.elementText("ImageUrlMedium");
102 String largeImageURL = details.elementText("ImageUrlLarge");
103 double listPrice = GetterUtil.getDouble(
104 details.elementText("ListPrice"));
105 double ourPrice = GetterUtil.getDouble(
106 details.elementText("OurPrice"), listPrice);
107 double usedPrice = GetterUtil.getDouble(
108 details.elementText("UsedPrice"));
109 double collectiblePrice = GetterUtil.getDouble(
110 details.elementText("CollectiblePrice"));
111 double thirdPartyNewPrice = GetterUtil.getDouble(
112 details.elementText("ThirdPartyNewPrice"));
113 int salesRank = GetterUtil.getInteger(
114 details.elementText("SalesRank"));
115 String media = details.elementText("Media");
116 String availability = details.elementText("Availability");
117
118 amazonRankings = new AmazonRankings(
119 isbn, productName, catalog, authors, releaseDate,
120 releaseDateAsString, manufacturer, smallImageURL,
121 mediumImageURL, largeImageURL, listPrice, ourPrice, usedPrice,
122 collectiblePrice, thirdPartyNewPrice, salesRank, media,
123 availability);
124 }
125 catch (ConverterException ce) {
126 throw ce;
127 }
128 catch (Exception e) {
129 throw new ConverterException(isbn + " " + e.toString());
130 }
131
132 return amazonRankings;
133 }
134
135 public long getRefreshTime() {
136 return _REFRESH_TIME;
137 }
138
139 private static final long _REFRESH_TIME = Time.MINUTE * 20;
140
141 private String _isbn;
142
143 }