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    
020    import javax.ejb.*;
021    
022    import org.apache.geronimo.samples.daytrader.util.*;
023    
024    import java.math.BigDecimal;
025    import java.sql.Timestamp;
026    import org.apache.geronimo.samples.daytrader.*;
027    
028    
029    public abstract class OrderBean
030                    implements EntityBean {
031    
032    
033        private EntityContext context;
034    
035        /* Accessor methods for persistent fields */
036    
037    
038        public abstract Integer             getOrderID();                           /* orderID */
039        public abstract void                setOrderID(Integer orderID);
040        public abstract String              getOrderType();                         /* orderType (buy, sell, etc.) */
041        public abstract void                setOrderType(String orderType);    
042        public abstract String              getOrderStatus();                       /* orderStatus (open, completed, etc.) */
043        public abstract void                setOrderStatus(String orderType);        
044        public abstract Timestamp   getOpenDate();                          /* openDate (when the order was entered) */
045        public abstract void                setOpenDate(Timestamp openDate);      
046        public abstract Timestamp   getCompletionDate();            /* completionDate */
047        public abstract void                setCompletionDate(Timestamp completionDate);            
048        public abstract double              getQuantity();                          /* quantity */
049        public abstract void                setQuantity(double quantity);            
050        public abstract BigDecimal  getPrice();                                     /* price */
051        public abstract void                setPrice(BigDecimal price);                
052        public abstract BigDecimal  getOrderFee();                          /* orderFee */
053        public abstract void                setOrderFee(BigDecimal price);                    
054        
055        /* Accessor methods for relationship fields */
056        public abstract LocalAccount        getAccount();                   /* The account which placed the order */
057        public abstract void                        setAccount(LocalAccount account);    
058        public abstract LocalQuote          getQuote();                             /* The stock purchased/sold in this order */
059        public abstract void                        setQuote(LocalQuote quote);         /* null for cash transactions */
060        public abstract LocalHolding        getHolding();                   /* The created/removed holding during this order */
061        public abstract void                        setHolding(LocalHolding holding);   /* null for cash transactions */
062        
063        /* Select methods */
064    
065    
066    
067        /* Business methods */
068        public LocalHolding getHoldingForUpdate()   /* The holding for this order access with intent to update */    
069        {
070            return getHolding();
071        }
072        public boolean isBuy()
073        {
074            String orderType = getOrderType();
075            if ( orderType.compareToIgnoreCase("buy") == 0 ) 
076                    return true;
077            return false;
078        }
079        
080        public boolean isSell()
081        {
082            String orderType = getOrderType();
083            if ( orderType.compareToIgnoreCase("sell") == 0 ) 
084                    return true;
085            return false;
086        }
087        
088        public boolean isOpen()
089        {
090            String orderStatus = getOrderStatus();
091            if ( (orderStatus.compareToIgnoreCase("open") == 0) ||
092                     (orderStatus.compareToIgnoreCase("processing") == 0) ) 
093                            return true;
094            return false;
095        }
096        
097        public boolean isCompleted()
098        {
099            String orderStatus = getOrderStatus();
100            if ( (orderStatus.compareToIgnoreCase("completed") == 0) ||
101                     (orderStatus.compareToIgnoreCase("alertcompleted") == 0)    ||
102                     (orderStatus.compareToIgnoreCase("cancelled") == 0) )           
103                            return true;
104            return false;   
105        }
106        
107        public boolean isCancelled()
108        {
109            String orderStatus = getOrderStatus();
110            if (orderStatus.compareToIgnoreCase("cancelled") == 0)
111                            return true;
112            return false;   
113        }
114        
115    
116            public void cancel()
117            {
118                    setOrderStatus("cancelled");
119            }
120    
121    
122            public OrderDataBean getDataBean()
123            {
124                    return new OrderDataBean(getOrderID(),
125                                                                            getOrderType(),
126                                                                            getOrderStatus(),
127                                                                            getOpenDate(),
128                                                                            getCompletionDate(),
129                                                                            getQuantity(),
130                                                                            getPrice(),
131                                                                            getOrderFee(),
132                                                                            (String)getQuote().getPrimaryKey()
133                                                                            );
134    
135            }
136            
137    
138            public String toString()
139            {
140                    return getDataBean().toString();
141            }
142            
143        /* Required javax.ejb.EntityBean interface methods */
144        
145        public Integer ejbCreate (int orderID, LocalAccount account, LocalQuote quote, LocalHolding holding, String orderType, double quantity)
146        throws CreateException {
147            return ejbCreate(new Integer(orderID), account, quote, holding, orderType, quantity);
148        }
149        
150        public Integer ejbCreate (Integer orderID, LocalAccount account, LocalQuote quote, LocalHolding holding, String orderType, double quantity)
151            throws CreateException {
152    
153    
154                    Timestamp currentDate = new Timestamp(System.currentTimeMillis());
155                                    
156            setOrderID(orderID);
157            setOrderType(orderType);
158            setOrderStatus("open");
159            setOpenDate(currentDate);
160            setQuantity (quantity);
161            setPrice (quote.getPrice().setScale(FinancialUtils.SCALE, FinancialUtils.ROUND));        
162    
163    
164                    setOrderFee(TradeConfig.getOrderFee(orderType));
165                    
166            return null;
167        }
168    
169    
170        public void ejbPostCreate (Integer orderID, LocalAccount account, LocalQuote quote, LocalHolding holding, String orderType, double quantity)
171            throws CreateException {
172                    setAccount(account);
173                    setQuote(quote);
174                    setHolding(holding);
175            }
176                
177        public void ejbPostCreate (int orderID, LocalAccount account, LocalQuote quote, LocalHolding holding, String orderType, double quantity)
178            throws CreateException { 
179                    ejbPostCreate(new Integer(orderID), account, quote, holding, orderType, quantity);
180            }
181    
182    
183        public void setEntityContext(EntityContext ctx) {
184            context = ctx;
185            }
186        
187        public void unsetEntityContext() {
188            context = null;
189        }
190        
191        public void ejbRemove() {
192        }
193        
194    
195        public void ejbLoad() {
196        }
197        
198        public void ejbStore() {
199        }
200        
201        public void ejbPassivate() { 
202        }
203        
204        public void ejbActivate() { 
205        }
206    }