001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader.ejb;
018    
019    import javax.ejb.*;
020    
021    import org.apache.geronimo.samples.daytrader.util.*;
022    
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.math.BigDecimal;
026    import org.apache.geronimo.samples.daytrader.*;
027    
028    public abstract class QuoteBean 
029                    implements EntityBean {
030    
031        private EntityContext context;
032        
033        /* Accessor methods for persistent fields */
034    
035        public abstract String              getSymbol();                            /* symbol */
036        public abstract void                setSymbol(String symbol);
037        public abstract String              getCompanyName();                       /* companyName */
038        public abstract void                setCompanyName(String companyName);
039        public abstract double              getVolume();                            /* volume */
040        public abstract void                setVolume(double volume);    
041        public abstract BigDecimal  getPrice();                                     /* price */
042        public abstract void                setPrice(BigDecimal price);
043        public abstract BigDecimal  getOpen();                                      /* open price */ 
044        public abstract void                setOpen(BigDecimal price);            
045        public abstract BigDecimal  getLow();                                       /* low price */
046        public abstract void                setLow(BigDecimal price);
047        public abstract BigDecimal  getHigh();                                      /* high price */
048        public abstract void                setHigh(BigDecimal price);
049        public abstract double              getChange();                            /* price change */
050        public abstract void                setChange(double change);
051        
052        /* Accessor methods for relationship fields */
053        public abstract Collection  getOrders();
054        public abstract void                setOrders(Collection orders);
055    
056        /* Select methods */
057    /*                      
058        public abstract Collection ejbSelectTopGainPrices()
059                throws FinderException;
060        public abstract Collection ejbSelectTopLossPrices()
061                throws FinderException;    
062            public abstract Collection ejbSelectChangeGreaterThan(double minGain)
063                throws FinderException;    
064            public abstract Collection ejbSelectChangeLessThan(double minLoss)
065                throws FinderException;       
066        public abstract Collection ejbSelectPricesForTSIA()
067            throws FinderException;
068        public abstract Collection ejbSelectOpenPricesForTSIA()
069            throws FinderException;
070    */        
071        public abstract Collection ejbSelectTotalVolume()
072            throws FinderException;       
073    
074        /* Business methods */
075    
076        public void updatePrice(BigDecimal current)
077        {
078            int compare = current.compareTo(getPrice());
079            // Do nothing if the price has not changed
080                    if ( compare == 0) return;      
081    
082                    setPrice(current.setScale(FinancialUtils.SCALE, FinancialUtils.ROUND));  //Update current price
083                    setChange( current.doubleValue() - getOpen().doubleValue() );
084                    
085                    //Reset high, low if applicable
086                    if ( current.compareTo(getHigh()) > 0 ) setHigh(current);
087                    else if ( current.compareTo(getLow()) < 0 ) setLow(current);
088        }
089        
090        public void updatePrice(double current)
091        {
092            updatePrice( new BigDecimal(current));
093        }
094        
095        public void addToVolume(double quantity)
096        {
097            setVolume( getVolume() + quantity);
098        }
099    
100        
101    /* To get TopGainers and Losers, 
102     * using an algorithm in TradeSession EJB instead of ejbSelect approach
103     *
104     *
105        public Collection getTopGainers(int count) 
106            throws FinderException
107        {
108    //      LocalQuote quote = (LocalQuote) context.getEJBLocalObject();            
109          Collection topGainPrices = ejbSelectTopGainPrices();
110          ArrayList topGains = new ArrayList(topGainPrices);
111     
112              count = count -1; //index starts with zero
113               double minGain = 0.0;
114              if ( topGains.size() >= count )
115                      minGain = ((Double)topGains.get(count)).doubleValue();
116    
117              return ejbSelectChangeGreaterThan(minGain);
118        }
119    
120        public Collection getTopLosers(int count) 
121            throws FinderException
122        {
123    //      LocalQuote quote = (LocalQuote) context.getEJBLocalObject();            
124          Collection topLossPrices = ejbSelectTopLossPrices();
125          ArrayList topLoss = new ArrayList(topLossPrices);
126    
127              count = count -1; //index starts with zero
128              double minLoss = 0.0;
129              if ( topLoss.size() >= count )
130                      minLoss = ((Double)topLoss.get(count)).doubleValue();
131    
132              return ejbSelectChangeLessThan(minLoss);
133        }
134    
135        public BigDecimal getTSIA() 
136            throws FinderException
137        {
138         // LocalQuote quote = (LocalQuote) context.getEJBLocalObject();
139              BigDecimal TSIA = FinancialUtils.ZERO;
140              Collection currentPrices = ejbSelectPricesForTSIA();
141              int size = currentPrices.size();
142     
143              if (size > 0)
144              {
145                      Iterator it = currentPrices.iterator();
146                      while (it.hasNext())
147                      {
148                            BigDecimal price = (BigDecimal)it.next();
149                            TSIA = TSIA.add(price);
150                      }
151              
152                  TSIA = TSIA.divide(new BigDecimal(size), FinancialUtils.ROUND);
153              }
154              return TSIA;
155        }
156        
157        public BigDecimal getOpenTSIA() 
158            throws FinderException
159        {
160         // LocalQuote quote = (LocalQuote) context.getEJBLocalObject();
161              BigDecimal openTSIA = FinancialUtils.ZERO;
162              Collection openPrices = ejbSelectOpenPricesForTSIA();
163              int size = openPrices.size();
164     
165              if (size > 0)
166              {
167                      Iterator it = openPrices.iterator();
168                      while (it.hasNext())
169                      {
170                            BigDecimal price = (BigDecimal)it.next();
171                            openTSIA = openTSIA.add(price);
172                      }
173              
174                  openTSIA = openTSIA.divide(new BigDecimal(size), FinancialUtils.ROUND);
175              }
176              return openTSIA;
177        }
178     *
179     */
180        
181            public double getTotalVolume()
182            throws FinderException  
183            {
184                    double totalVolume = 0.0;
185                    Collection volumes = ejbSelectTotalVolume();
186                    Iterator it = volumes.iterator();
187    
188                    while (it.hasNext())
189                    {
190                            Double volume;
191                            Object o = it.next();
192                            try { 
193                                    Float f = (Float) o; 
194                                    volume = new Double(f.doubleValue());
195                            } 
196                            catch (Exception e)
197                            { 
198                                    volume = (Double) o;
199                            }
200                            totalVolume = totalVolume + volume.doubleValue();
201    
202                    }
203                    return totalVolume;
204            }    
205        
206        public QuoteDataBean getDataBean()
207        {
208            return new QuoteDataBean(getSymbol(),getCompanyName(),getVolume(),getPrice(),getOpen(),
209                                                                    getLow(),getHigh(),getChange());
210        }
211    
212            public String toString()
213            {
214                    return getDataBean().toString();
215            }
216            
217        /* Required javax.ejb.EntityBean interface methods */
218    
219        public String ejbCreate (String symbol, String companyName, BigDecimal price) 
220            throws CreateException {
221    
222                    if (Log.doTrace()) Log.traceEnter("QuoteBean:ejbCreate");
223            setSymbol(symbol);
224            setCompanyName(companyName);
225            price = price.setScale(FinancialUtils.SCALE, FinancialUtils.ROUND);
226            setVolume(0.0);
227            setPrice(price);
228            setOpen (price);
229            setLow  (price);
230            setHigh (price);
231            setChange(0.0);
232    
233                    if (Log.doTrace()) Log.traceExit("QuoteBean:ejbCreate");
234            return null;
235        }
236             
237        public void ejbPostCreate (String symbol, String companyName, BigDecimal price) 
238            throws CreateException { }
239    
240        public void setEntityContext(EntityContext ctx) {
241            context = ctx;
242        }
243        
244        public void unsetEntityContext() {
245            context = null;
246        }
247        
248        public void ejbRemove() {
249        }
250        
251        public void ejbLoad() {
252        }
253        
254        public void ejbStore() {
255        }
256        
257        public void ejbPassivate() { 
258        }
259        
260        public void ejbActivate() { 
261        }
262    }