001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.stocks.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.webcache.WebCacheException;
026: import com.liferay.portal.kernel.webcache.WebCacheItem;
027: import com.liferay.portlet.stocks.model.Stocks;
028: import com.liferay.util.Http;
029: import com.liferay.util.Time;
030:
031: import java.util.StringTokenizer;
032:
033: /**
034: * <a href="StocksWebCacheItem.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class StocksWebCacheItem implements WebCacheItem {
040:
041: public StocksWebCacheItem(String symbol) {
042: _symbol = symbol;
043: }
044:
045: public Object convert(String key) throws WebCacheException {
046: String symbol = _symbol;
047: double lastTrade = 0.0;
048: double change = 0.0;
049: double open = 0.0;
050: double dayHigh = 0.0;
051: double dayLow = 0.0;
052: long volume = 0;
053:
054: Stocks stocks = new Stocks(symbol, lastTrade, change, open,
055: dayHigh, dayLow, volume);
056:
057: try {
058: String text = Http
059: .URLtoString("http://finance.yahoo.com/d/quotes.csv?s="
060: + symbol + "&f=sl1d1t1c1ohgv&e=.csv");
061:
062: StringTokenizer st = new StringTokenizer(text,
063: StringPool.COMMA);
064:
065: // Skip symbol
066:
067: st.nextToken();
068:
069: try {
070: lastTrade = GetterUtil.getDouble(st.nextToken()
071: .replace('"', ' ').trim());
072:
073: stocks.setLastTrade(lastTrade);
074: } catch (NumberFormatException nfe) {
075: stocks.setLastTradeAvailable(false);
076: }
077:
078: // Skip date and time
079:
080: st.nextToken();
081: st.nextToken();
082:
083: try {
084: change = GetterUtil.getDouble(st.nextToken().replace(
085: '"', ' ').trim());
086:
087: stocks.setChange(change);
088: } catch (NumberFormatException nfe) {
089: stocks.setChangeAvailable(false);
090: }
091:
092: try {
093: open = GetterUtil.getDouble(st.nextToken().replace('"',
094: ' ').trim());
095:
096: stocks.setOpen(open);
097: } catch (NumberFormatException nfe) {
098: stocks.setOpenAvailable(false);
099: }
100:
101: try {
102: dayHigh = GetterUtil.getDouble(st.nextToken().replace(
103: '"', ' ').trim());
104:
105: stocks.setDayHigh(dayHigh);
106: } catch (NumberFormatException nfe) {
107: stocks.setDayHighAvailable(false);
108: }
109:
110: try {
111: dayLow = GetterUtil.getDouble(st.nextToken().replace(
112: '"', ' ').trim());
113:
114: stocks.setDayLow(dayLow);
115: } catch (NumberFormatException nfe) {
116: stocks.setDayLowAvailable(false);
117: }
118:
119: try {
120: volume = GetterUtil.getLong(st.nextToken().replace('"',
121: ' ').trim());
122:
123: stocks.setVolume(volume);
124: } catch (NumberFormatException nfe) {
125: stocks.setVolumeAvailable(false);
126: }
127:
128: if (!stocks.isValid()) {
129: throw new WebCacheException(symbol);
130: }
131: } catch (Exception e) {
132: throw new WebCacheException(symbol + " " + e.toString());
133: }
134:
135: return stocks;
136: }
137:
138: public long getRefreshTime() {
139: return _REFRESH_TIME;
140: }
141:
142: private static final long _REFRESH_TIME = Time.MINUTE * 20;
143:
144: private String _symbol;
145:
146: }
|