01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * 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: */package com.liferay.portlet.currencyconverter.util;
22:
23: import com.liferay.portal.kernel.util.GetterUtil;
24: import com.liferay.portal.kernel.util.StringPool;
25: import com.liferay.portal.kernel.webcache.WebCacheException;
26: import com.liferay.portal.kernel.webcache.WebCacheItem;
27: import com.liferay.portlet.currencyconverter.model.Currency;
28: import com.liferay.util.Http;
29: import com.liferay.util.Time;
30:
31: import java.util.StringTokenizer;
32:
33: /**
34: * <a href="CurrencyWebCacheItem.java.html"><b><i>View Source</i></b></a>
35: *
36: * @author Brian Wing Shun Chan
37: *
38: */
39: public class CurrencyWebCacheItem implements WebCacheItem {
40:
41: public CurrencyWebCacheItem(String symbol) {
42: _symbol = symbol;
43: }
44:
45: public Object convert(String key) throws WebCacheException {
46: String symbol = _symbol;
47: double rate = 0.0;
48:
49: try {
50: if (symbol.length() == 6) {
51: String fromSymbol = symbol.substring(0, 3);
52: String toSymbol = symbol.substring(3, 6);
53:
54: if (!CurrencyUtil.isCurrency(fromSymbol)
55: || !CurrencyUtil.isCurrency(toSymbol)) {
56:
57: throw new WebCacheException(symbol);
58: }
59: } else if (symbol.length() == 3) {
60: if (!CurrencyUtil.isCurrency(symbol)) {
61: throw new WebCacheException(symbol);
62: }
63: } else {
64: throw new WebCacheException(symbol);
65: }
66:
67: String text = Http
68: .URLtoString("http://finance.yahoo.com/d/quotes.csv?s="
69: + symbol + "=X&f=sl1d1t1c1ohgv&e=.csv");
70:
71: StringTokenizer st = new StringTokenizer(text,
72: StringPool.COMMA);
73:
74: // Skip symbol
75:
76: st.nextToken();
77:
78: rate = GetterUtil.getDouble(st.nextToken()
79: .replace('"', ' ').trim());
80: } catch (Exception e) {
81: throw new WebCacheException(e);
82: }
83:
84: return new Currency(symbol, rate);
85: }
86:
87: public long getRefreshTime() {
88: return _REFRESH_TIME;
89: }
90:
91: private static final long _REFRESH_TIME = Time.MINUTE * 20;
92:
93: private String _symbol;
94:
95: }
|