1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * 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   */
22  
23  package com.liferay.portlet.stocks.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.util.WebCacheable;
28  import com.liferay.portlet.stocks.model.Stocks;
29  import com.liferay.util.ConverterException;
30  import com.liferay.util.Http;
31  import com.liferay.util.Time;
32  
33  import java.util.StringTokenizer;
34  
35  /**
36   * <a href="StocksConverter.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class StocksConverter implements WebCacheable {
42  
43      public StocksConverter(String symbol) {
44          _symbol = symbol;
45      }
46  
47      public Object convert(String id) throws ConverterException {
48          String symbol = _symbol;
49          double lastTrade = 0.0;
50          double change = 0.0;
51          double open = 0.0;
52          double dayHigh = 0.0;
53          double dayLow = 0.0;
54          long volume = 0;
55  
56          Stocks stocks = new Stocks(
57              symbol, lastTrade, change, open, dayHigh, dayLow, volume);
58  
59          try {
60              String text = Http.URLtoString(
61                  "http://finance.yahoo.com/d/quotes.csv?s=" +
62                      symbol + "&f=sl1d1t1c1ohgv&e=.csv");
63  
64              StringTokenizer st = new StringTokenizer(text, StringPool.COMMA);
65  
66              // Skip symbol
67  
68              st.nextToken();
69  
70              try {
71                  lastTrade = GetterUtil.getDouble(
72                      st.nextToken().replace('"', ' ').trim());
73  
74                  stocks.setLastTrade(lastTrade);
75              }
76              catch (NumberFormatException nfe) {
77                  stocks.setLastTradeAvailable(false);
78              }
79  
80              // Skip date and time
81  
82              st.nextToken();
83              st.nextToken();
84  
85              try {
86                  change = GetterUtil.getDouble(
87                      st.nextToken().replace('"', ' ').trim());
88  
89                  stocks.setChange(change);
90              }
91              catch (NumberFormatException nfe) {
92                  stocks.setChangeAvailable(false);
93              }
94  
95              try {
96                  open = GetterUtil.getDouble(
97                      st.nextToken().replace('"', ' ').trim());
98  
99                  stocks.setOpen(open);
100             }
101             catch (NumberFormatException nfe) {
102                 stocks.setOpenAvailable(false);
103             }
104 
105             try {
106                 dayHigh = GetterUtil.getDouble(
107                     st.nextToken().replace('"', ' ').trim());
108 
109                 stocks.setDayHigh(dayHigh);
110             }
111             catch (NumberFormatException nfe) {
112                 stocks.setDayHighAvailable(false);
113             }
114 
115             try {
116                 dayLow = GetterUtil.getDouble(
117                     st.nextToken().replace('"', ' ').trim());
118 
119                 stocks.setDayLow(dayLow);
120             }
121             catch (NumberFormatException nfe) {
122                 stocks.setDayLowAvailable(false);
123             }
124 
125             try {
126                 volume = GetterUtil.getLong(
127                     st.nextToken().replace('"', ' ').trim());
128 
129                 stocks.setVolume(volume);
130             }
131             catch (NumberFormatException nfe) {
132                 stocks.setVolumeAvailable(false);
133             }
134 
135             if (!stocks.isValid()) {
136                 throw new ConverterException(symbol);
137             }
138         }
139         catch (Exception e) {
140             throw new ConverterException(symbol + " " + e.toString());
141         }
142 
143         return stocks;
144     }
145 
146     public long getRefreshTime() {
147         return _REFRESH_TIME;
148     }
149 
150     private static final long _REFRESH_TIME = Time.MINUTE * 20;
151 
152     private String _symbol;
153 
154 }