1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
67 Instrument instr = instrumentMgr.getInstrument(symbol);
68
69
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
83 Quote oldQuote =getQuote( newQuote.getSymbol());
84 Timestamp last = oldQuote.getTime();
85
86
87 Calendar prevCal = new GregorianCalendar(TimeZone.getTimeZone("IST"));
88 prevCal.setTimeInMillis(last.getTime());
89
90
91 Calendar curCal = new GregorianCalendar( TimeZone.getTimeZone("IST"));
92 curCal.setTimeInMillis( (new Date()).getTime() );
93
94
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 }