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    
018    package org.apache.geronimo.samples.daytrader.ejb;
019    
020    import javax.ejb.*;
021    import javax.naming.*;
022    
023    import org.apache.geronimo.samples.daytrader.util.*;
024    
025    import java.util.Collection;
026    import java.util.Iterator;
027    import java.util.ArrayList;
028    import java.math.BigDecimal;
029    import java.sql.Timestamp;
030    import org.apache.geronimo.samples.daytrader.*;
031    
032    public abstract class AccountBean
033                    implements EntityBean {
034    
035        private EntityContext context;
036        private LocalAccountProfileHome accountProfileHome;
037    
038        /* Accessor methods for persistent fields */
039    
040        public abstract Integer             getAccountID();                         /* accountID */
041        public abstract void                        setAccountID(Integer accountID);
042        public abstract int                 getLoginCount();                        /* loginCount */
043        public abstract void                        setLoginCount(int loginCount);
044        public abstract int                 getLogoutCount();                       /* logoutCount */
045        public abstract void                        setLogoutCount(int logoutCount);    
046        public abstract Timestamp           getLastLogin();                         /* lastLogin Date */
047        public abstract void                        setLastLogin(Timestamp lastLogin);        
048        public abstract Timestamp           getCreationDate();                      /* creationDate */
049        public abstract void                        setCreationDate(Timestamp creationDate);
050        public abstract BigDecimal  getBalance();                           /* balance */
051        public abstract void                        setBalance(BigDecimal balance);            
052        public abstract BigDecimal  getOpenBalance();                       /* open balance */
053        public abstract void                        setOpenBalance(BigDecimal openBalance);                
054        
055        /* Accessor methods for relationship fields */
056        public abstract LocalAccountProfile getProfile();                           /* This account's profile */
057        public abstract void                                setProfile(LocalAccountProfile profile);    
058        public abstract Collection          getHoldings();                          /* This account's holdings */
059        public abstract void                                setHoldings(Collection holdings);    
060        public abstract Collection          getOrders();                            /* This account's orders */
061        public abstract void                                setOrders(Collection orders);    
062       
063        /* Select methods */
064    
065        /* Business methods */
066        
067        public void login(String password)
068        {
069            LocalAccountProfile profile = getProfile();
070            if ( (profile==null) || (profile.getPasswd().equals(password) == false) )
071            {
072                    String error = "AccountBean:Login failure for account: " + getAccountID() + 
073                                            ( (profile==null)? "null AccountProfile" :
074                                                    "\n\tIncorrect password-->" + profile.getUserID() + ":" + profile.getPasswd() );
075                    throw new EJBException(error);
076            }
077            
078            setLastLogin( new Timestamp(System.currentTimeMillis()) );
079            setLoginCount( getLoginCount() + 1 );
080            }
081            
082        public void logout()
083        {
084            setLogoutCount( getLogoutCount() + 1 );
085            }
086    
087            public AccountProfileDataBean updateAccountProfile(AccountProfileDataBean profileData)
088            throws FinderException
089            {
090                    return getProfileForUpdate().updateAccountProfile(profileData);
091            }
092            
093            public AccountDataBean getDataBean()
094            {
095                    return new AccountDataBean(getAccountID(),
096                                                                            getLoginCount(),
097                                                                            getLogoutCount(),
098                                                                            getLastLogin(),
099                                                                            getCreationDate(),
100                                                                            getBalance(),
101                                                                            getOpenBalance(),
102                                                                            (String)getProfile().getPrimaryKey());
103            }    
104    
105            public AccountProfileDataBean getProfileDataBean()
106            {
107                    return getProfile().getDataBean();
108            }
109            public Collection getHoldingDataBeans()
110            {
111                    Collection holdings = getHoldings();
112                    ArrayList holdingDataBeans = new ArrayList(holdings.size());
113                    Iterator it = holdings.iterator();
114                    while (it.hasNext())
115                    {
116                            LocalHolding holding = (LocalHolding) it.next();
117                            HoldingDataBean holdingData = holding.getDataBean();
118                            holdingDataBeans.add(holdingData);
119                    }
120                    return holdingDataBeans;        
121            }
122    
123            
124        /* Select methods */
125        public abstract Collection ejbSelectClosedOrders(Integer accountID)
126                throws FinderException;     
127                          
128            
129            public Collection getClosedOrders()
130                            throws FinderException
131            {
132                    return ejbSelectClosedOrders(getAccountID());
133            }
134            public LocalAccountProfile getProfileForUpdate()
135                            throws FinderException
136            {
137                    return getProfile();
138            }       
139            
140            public Collection getOrderDataBeans()
141            {
142                    Collection orders = getOrders();
143                    ArrayList orderDataBeans = new ArrayList(orders.size());
144                    Iterator it = orders.iterator();
145                    while (it.hasNext())
146                    {
147                            LocalOrder order = (LocalOrder) it.next();
148                            OrderDataBean orderData = order.getDataBean();
149                            orderDataBeans.add(orderData);
150                    }
151                    return orderDataBeans;
152            }
153            
154    
155            public String toString()
156            {
157                    return getDataBean().toString();
158            }
159            
160            
161    
162        /* Required javax.ejb.EntityBean interface methods */
163        public Integer ejbCreate (int accountID, String userID, String password, BigDecimal openBalance,
164                                                                    String fullname, String address, String email, String creditcard)
165        throws CreateException {
166            return ejbCreate(new Integer(accountID), userID, password, openBalance,
167                                                                            fullname, address, email, creditcard);
168        }
169        
170        public Integer ejbCreate (Integer accountID, String userID, String password, BigDecimal openBalance, 
171                                                                    String fullname, String address, String email, String creditCard)
172            throws CreateException {
173                    
174            setAccountID(accountID);
175                    setLoginCount(0);
176                    setLogoutCount(0);
177                    Timestamp current = new Timestamp(System.currentTimeMillis());
178            setLastLogin(current);
179            setCreationDate(current);
180                    openBalance = openBalance.setScale(FinancialUtils.SCALE, FinancialUtils.ROUND);
181            setBalance (openBalance);
182            setOpenBalance (openBalance);
183            
184            return null;
185        }
186    
187        public void ejbPostCreate (Integer accountID, String userID, String password, BigDecimal openBalance,
188                                                                    String fullname, String address, String email, String creditCard)          
189            throws CreateException {
190                     //Account creates a new AccountProfile entity here.        
191                    LocalAccountProfile profile = accountProfileHome.create(userID, password, fullname, address, email, creditCard);
192                    setProfile(profile);
193            }
194                
195        public void ejbPostCreate (int accountID, String userID, String password, BigDecimal openBalance,
196                                                                    String fullname, String address, String email, String creditcard)
197            throws CreateException { 
198                    ejbPostCreate(new Integer(accountID), userID, password, openBalance,fullname, address, email, creditcard);      
199            }
200    
201        public void setEntityContext(EntityContext ctx) {
202            context = ctx;
203            try {
204                            InitialContext ic = new InitialContext();
205                            accountProfileHome = (LocalAccountProfileHome) ic.lookup("java:comp/env/ejb/AccountProfile");
206                    }
207                catch (NamingException ne)
208                {
209                            Log.error(ne, "Account EJB: Lookup of Local Entity Homes Failed\n" + ne);
210                    }
211            }
212        
213        public void unsetEntityContext() {
214            context = null;
215        }
216        
217        public void ejbRemove() {
218        }
219        
220        public void ejbLoad() {
221        }
222        
223        public void ejbStore() {
224        }
225        
226        public void ejbPassivate() { 
227        }
228        
229        public void ejbActivate() { 
230        }
231    }