View Javadoc

1   /*
2    *  
3    *  author nambi sankaran
4    *  copyright (C) 2009 nambi sankaran.
5    *
6    *  This program is free software: you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation, either version 3 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   *
19   */
20  package net.sf.emarket.quote.service;
21  
22  import java.sql.Timestamp;
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.GregorianCalendar;
26  import java.util.List;
27  import java.util.TimeZone;
28  
29  import net.sf.emarket.quote.domain.Instrument;
30  import net.sf.emarket.quote.domain.Quote;
31  import net.sf.emarket.quote.repository.IQuoteDao;
32  
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.stereotype.Service;
35  
36  @Service
37  public class QuoteManagerServiceImpl implements IQuoteManagerService {
38  	
39  	private IQuoteDao quoteDao = null;
40      private IInstrumentManager instrumentMgr = null;
41  	
42  	@Autowired
43  	public void setQuoteDao(IQuoteDao dao){
44  		quoteDao = dao;
45  	}
46  
47      @Autowired
48      public void setInstrumentManager( IInstrumentManager mgr){
49          instrumentMgr = mgr;
50      }
51  
52  	public void addQuote(Quote quote) {
53  		
54  		long quoteId = quoteDao.generateQuoteId();
55  		quote.setId(quoteId);
56  		
57  		quoteDao.addQuote(quote);
58  	}
59  
60  	public Quote deleteQuote(Quote quote) {
61  		return quoteDao.deleteQuote(quote);
62  	}
63  
64  	public Quote getQuote(String symbol) {
65  
66          // get the instrument
67          Instrument  instr = instrumentMgr.getInstrument(symbol);
68  
69          // get the quote
70  		Quote quote = quoteDao.getQuote(symbol);
71          quote.setInstrument( instr );
72                  
73          return quote;
74  	}
75  
76  	public Quote getQuote(Quote quote) {
77  		return quoteDao.getQuote(quote);
78  	}
79  
80  	public Quote updateQuote(Quote newQuote) {
81  
82          // get the current quote
83          Quote oldQuote =getQuote( newQuote.getSymbol());
84          Timestamp last = oldQuote.getTime();
85      
86          // FIXME: Get the timezone from configuration
87          Calendar prevCal = new GregorianCalendar(TimeZone.getTimeZone("IST"));
88          prevCal.setTimeInMillis(last.getTime());
89          
90          // FIXME: Get the timezone from configuration
91          Calendar curCal = new GregorianCalendar( TimeZone.getTimeZone("IST"));
92          curCal.setTimeInMillis( (new Date()).getTime() );
93          
94          // now check whether the date has changed
95          if( curCal .getTimeInMillis() > prevCal.getTimeInMillis() ){
96  	        if( (prevCal.get(Calendar.DAY_OF_MONTH) != curCal.get(Calendar.DAY_OF_MONTH)) &&
97  	            (prevCal.get(Calendar.MONTH) != curCal.get(Calendar.MONTH)) && 
98  	            (prevCal.get(Calendar.YEAR) != curCal.get(Calendar.YEAR) )){
99  	        		newQuote.setPreviousClose(oldQuote.getLastPrice());
100 	        }else{
101 	        	newQuote.setPreviousClose(oldQuote.getPreviousClose());
102 	        }
103         }else{
104         	//FIXME: something weird is going on
105         }
106         
107         float change = newQuote.getLastPrice() - newQuote.getPreviousClose();
108         newQuote.setChange( change);
109 		
110 		long quoteId = quoteDao.generateQuoteId();
111 		newQuote.setId(quoteId);
112 
113 		quoteDao.addQuoteHistory(newQuote);
114 		return quoteDao.updateQuote(newQuote);
115 	}
116 	
117 	public List<Quote> getAllQuotes(){
118 		List<Quote> quotes = quoteDao.getAllQuotes();
119 
120         for( Quote quote : quotes ){
121             Instrument instr = instrumentMgr.getInstrument( quote.getSymbol() );
122             quote.setInstrument( instr );
123         }
124 
125         return quotes;
126 	}
127 
128     public List<Quote> getQuotes(List<String> symbols) throws SymbolsCannotBeEmptyException {
129     	
130     	if( symbols == null || symbols.size() ==0 ){
131     			throw new SymbolsCannotBeEmptyException("");
132     	}
133 
134 		List<Quote> quotes = quoteDao.getQuotes( symbols);
135 
136         for( Quote quote : quotes ){
137             Instrument instr = instrumentMgr.getInstrument( quote.getSymbol() );
138             quote.setInstrument( instr );
139         }
140 
141         return quotes;
142     }
143     
144    /* public static void main(String[] args){
145     	
146     	Date today = new Date();
147     	long dayInMillis = 24 * 60 *60 * 1000;
148         
149         // Get all time zone ids
150         String[] zoneIds = TimeZone.getAvailableIDs();
151         
152         Calendar c1 = new GregorianCalendar( TimeZone.getTimeZone("IST"));
153         c1.setTimeInMillis( today.getTime() );
154         
155         Calendar c2 = new GregorianCalendar( TimeZone.getTimeZone("IST"));
156         c2.setTimeInMillis(today.getTime() - dayInMillis);
157         
158         System.out.println( c1.get(Calendar.DAY_OF_MONTH) +":" + c1.get(Calendar.HOUR_OF_DAY) + ":" + c1.get(Calendar.MINUTE));
159         System.out.println( c2.get(Calendar.DAY_OF_MONTH) +":" + c2.get(Calendar.HOUR_OF_DAY) + ":" + c2.get(Calendar.MINUTE));
160         
161         // View every time zone
162         for (int i=0; i<zoneIds.length; i++) {
163             // Get time zone by time zone id
164             TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);
165         
166             // Get the display name
167             String shortName = tz.getDisplayName(tz.inDaylightTime(today), TimeZone.SHORT);
168             String longName = tz.getDisplayName(tz.inDaylightTime(today), TimeZone.LONG);
169         
170             // Get the number of hours from GMT
171             int rawOffset = tz.getRawOffset();
172             int hour = rawOffset / (60*60*1000);
173             int min = Math.abs(rawOffset / (60*1000)) % 60;
174         
175             // Does the time zone have a daylight savings time period?
176             boolean hasDST = tz.useDaylightTime();
177         
178             // Is the time zone currently in a daylight savings time?
179             boolean inDST = tz.inDaylightTime(today);
180             
181             System.out.println( zoneIds[i] + ", "+ shortName+ "," + longName + ": "+ rawOffset + ", "+ hour + ", "+ min + ", " + hasDST + ", "+ inDST);
182         }
183     }
184 */
185 }