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;
018    
019    
020    import java.io.Serializable;
021    import java.math.BigDecimal;
022    import java.util.Date;
023    
024    import javax.persistence.Entity;
025    import javax.persistence.GeneratedValue;
026    import javax.persistence.Id;
027    import javax.persistence.ManyToOne;
028    import javax.persistence.NamedQueries;
029    import javax.persistence.NamedQuery;
030    import javax.persistence.OneToOne;
031    import javax.persistence.Version;
032    import javax.persistence.Table;
033    import javax.persistence.Column;
034    import javax.persistence.Transient;
035    
036    import org.apache.geronimo.samples.daytrader.util.Log;
037    
038    
039    @Entity(name="orderejb")
040    @Table(name = "orderejb")
041    @NamedQueries({
042             @NamedQuery(name="closedOrders",
043             query="SELECT o FROM orderejb o WHERE " +
044            "                    o.orderStatus = 'closed' AND " +
045            "                    o.account.profile.userID  = :userID"),
046             @NamedQuery(name="completeClosedOrders",
047             query="UPDATE orderejb o SET o.orderStatus = 'completed' WHERE " +
048            "                    o.orderStatus = 'closed' AND " +
049            "                    o.account.profile.userID  = :userID")
050            })
051    public class OrderDataBean implements Serializable
052    {
053    
054        @Id
055        @GeneratedValue
056        private Integer             orderID;                        /* orderID */
057        @Column(length=250)
058        private String              orderType;                      /* orderType (buy, sell, etc.) */
059        @Column(length=250)
060        private String              orderStatus;            /* orderStatus (open, processing, completed, closed, cancelled) */
061        private Date                openDate;                       /* openDate (when the order was entered) */
062        private Date                completionDate;         /* completionDate */
063        private double      quantity;                       /* quantity */
064        private BigDecimal  price;                          /* price */
065        private BigDecimal  orderFee;                       /* price */
066        @ManyToOne
067        private AccountDataBean account;
068        @OneToOne
069        private QuoteDataBean quote;
070        @OneToOne
071        private HoldingDataBean holding;
072    //    @Version
073    //    private Integer optLock;
074    
075    
076        /* Fields for relationship fields are not kept in the Data Bean */
077        @Transient
078        private String              symbol;
079    
080        public OrderDataBean() {}
081        /**
082         * OrderDataBean
083         */
084        public OrderDataBean(Integer orderID,
085                                String orderType,
086                                String orderStatus,
087                                Date openDate,
088                                Date completionDate,
089                                double quantity,
090                                BigDecimal price,
091                                BigDecimal orderFee,
092                                String symbol
093                                )
094        {
095            setOrderID(orderID);
096            setOrderType(orderType);
097            setOrderStatus(orderStatus);
098            setOpenDate(openDate);
099            setCompletionDate(completionDate);
100            setQuantity(quantity);
101            setPrice(price);
102            setOrderFee(orderFee);
103            setSymbol(symbol);
104        }
105        public OrderDataBean(String orderType,
106                String orderStatus,
107                Date openDate,
108                Date completionDate,
109                double quantity,
110                BigDecimal price,
111                BigDecimal orderFee,
112                AccountDataBean account,
113                QuoteDataBean quote, HoldingDataBean holding)
114        {
115            setOrderType(orderType);
116            setOrderStatus(orderStatus);
117            setOpenDate(openDate);
118            setCompletionDate(completionDate);
119            setQuantity(quantity);
120            setPrice(price);
121            setOrderFee(orderFee);
122            setAccount(account);
123            setQuote(quote);
124            setHolding(holding);
125        }
126    
127        public static OrderDataBean getRandomInstance() {
128            return new OrderDataBean(
129                new Integer(TradeConfig.rndInt(100000)),
130                TradeConfig.rndBoolean() ? "buy" : "sell",
131                "open",
132                new java.util.Date(TradeConfig.rndInt(Integer.MAX_VALUE)),
133                new java.util.Date(TradeConfig.rndInt(Integer.MAX_VALUE)),
134                TradeConfig.rndQuantity(),
135                TradeConfig.rndBigDecimal(1000.0f),
136                TradeConfig.rndBigDecimal(1000.0f),
137                TradeConfig.rndSymbol()
138            );
139        }
140    
141        public String toString()
142        {
143            return "Order " + getOrderID()
144                    + "\n\t      orderType: " + getOrderType()
145                    + "\n\t    orderStatus: " +     getOrderStatus()
146                    + "\n\t       openDate: " +     getOpenDate()
147                    + "\n\t completionDate: " +     getCompletionDate()
148                    + "\n\t       quantity: " +     getQuantity()
149                    + "\n\t          price: " +     getPrice()
150                    + "\n\t       orderFee: " +     getOrderFee()
151                    + "\n\t         symbol: " +     getSymbol()
152                    ;
153        }
154        public String toHTML()
155        {
156            return "<BR>Order <B>" + getOrderID() + "</B>"
157                    + "<LI>      orderType: " + getOrderType() + "</LI>"
158                    + "<LI>    orderStatus: " +       getOrderStatus() + "</LI>"
159                    + "<LI>       openDate: " +       getOpenDate() + "</LI>"
160                    + "<LI> completionDate: " +       getCompletionDate() + "</LI>"
161                    + "<LI>       quantity: " +       getQuantity() + "</LI>"
162                    + "<LI>          price: " +       getPrice() + "</LI>"
163                    + "<LI>       orderFee: " +       getOrderFee() + "</LI>"
164                    + "<LI>         symbol: " +       getSymbol() + "</LI>"
165                    ;
166        }
167    
168        public void print()
169        {
170            Log.log( this.toString() );
171        }
172    
173        /**
174         * Gets the orderID
175         * @return Returns a Integer
176         */
177        public Integer getOrderID() {
178            return orderID;
179        }
180        /**
181         * Sets the orderID
182         * @param orderID The orderID to set
183         */
184        public void setOrderID(Integer orderID) {
185            this.orderID = orderID;
186        }
187    
188    
189        /**
190         * Gets the orderType
191         * @return Returns a String
192         */
193        public String getOrderType() {
194            return orderType;
195        }
196        /**
197         * Sets the orderType
198         * @param orderType The orderType to set
199         */
200        public void setOrderType(String orderType) {
201            this.orderType = orderType;
202        }
203    
204    
205        /**
206         * Gets the orderStatus
207         * @return Returns a String
208         */
209        public String getOrderStatus() {
210            return orderStatus;
211        }
212        /**
213         * Sets the orderStatus
214         * @param orderStatus The orderStatus to set
215         */
216        public void setOrderStatus(String orderStatus) {
217            this.orderStatus = orderStatus;
218        }
219    
220    
221        /**
222         * Gets the openDate
223         * @return Returns a Date
224         */
225        public Date getOpenDate() {
226            return openDate;
227        }
228        /**
229         * Sets the openDate
230         * @param openDate The openDate to set
231         */
232        public void setOpenDate(Date openDate) {
233            this.openDate = openDate;
234        }
235    
236    
237        /**
238         * Gets the completionDate
239         * @return Returns a Date
240         */
241        public Date getCompletionDate() {
242            return completionDate;
243        }
244        /**
245         * Sets the completionDate
246         * @param completionDate The completionDate to set
247         */
248        public void setCompletionDate(Date completionDate) {
249            this.completionDate = completionDate;
250        }
251    
252    
253        /**
254         * Gets the quantity
255         * @return Returns a BigDecimal
256         */
257        public double getQuantity() {
258            return quantity;
259        }
260        /**
261         * Sets the quantity
262         * @param quantity The quantity to set
263         */
264        public void setQuantity(double quantity) {
265            this.quantity = quantity;
266        }
267    
268    
269        /**
270         * Gets the price
271         * @return Returns a BigDecimal
272         */
273        public BigDecimal getPrice() {
274            return price;
275        }
276        /**
277         * Sets the price
278         * @param price The price to set
279         */
280        public void setPrice(BigDecimal price) {
281            this.price = price;
282        }
283    
284    
285        /**
286         * Gets the orderFee
287         * @return Returns a BigDecimal
288         */
289        public BigDecimal getOrderFee() {
290            return orderFee;
291        }
292        /**
293         * Sets the orderFee
294         * @param orderFee The orderFee to set
295         */
296        public void setOrderFee(BigDecimal orderFee) {
297            this.orderFee = orderFee;
298        }
299    
300        /**
301         * Gets the symbol
302         * @return Returns a String
303         */
304        public String getSymbol() {
305            if (quote != null) {
306                return quote.getSymbol();
307            }
308            return symbol;
309        }
310        /**
311         * Sets the symbol
312         * @param symbol The symbol to set
313         */
314        public void setSymbol(String symbol) {
315            this.symbol = symbol;
316        }
317    
318        public AccountDataBean getAccount() {
319            return account;
320        }
321    
322        public void setAccount(AccountDataBean account) {
323            this.account = account;
324        }
325    
326        public QuoteDataBean getQuote() {
327            return quote;
328        }
329    
330        public void setQuote(QuoteDataBean quote) {
331            this.quote = quote;
332        }
333    
334        public HoldingDataBean getHolding() {
335            return holding;
336        }
337    
338        public void setHolding(HoldingDataBean holding) {
339            this.holding = holding;
340        }
341    
342        public boolean isBuy()
343        {
344            String orderType = getOrderType();
345            if ( orderType.compareToIgnoreCase("buy") == 0 )
346                    return true;
347            return false;
348        }
349    
350        public boolean isSell()
351        {
352            String orderType = getOrderType();
353            if ( orderType.compareToIgnoreCase("sell") == 0 )
354                    return true;
355            return false;
356        }
357    
358        public boolean isOpen()
359        {
360            String orderStatus = getOrderStatus();
361            if ( (orderStatus.compareToIgnoreCase("open") == 0) ||
362                     (orderStatus.compareToIgnoreCase("processing") == 0) )
363                            return true;
364            return false;
365        }
366    
367        public boolean isCompleted()
368        {
369            String orderStatus = getOrderStatus();
370            if ( (orderStatus.compareToIgnoreCase("completed") == 0) ||
371                     (orderStatus.compareToIgnoreCase("alertcompleted") == 0)    ||
372                     (orderStatus.compareToIgnoreCase("cancelled") == 0) )
373                            return true;
374            return false;
375        }
376    
377        public boolean isCancelled()
378        {
379            String orderStatus = getOrderStatus();
380            if (orderStatus.compareToIgnoreCase("cancelled") == 0)
381                            return true;
382            return false;
383        }
384    
385    
386            public void cancel()
387            {
388                    setOrderStatus("cancelled");
389            }
390    
391    }
392