KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > banknew > ejb > AccountSessionBean


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.banknew.ejb;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import javax.ejb.CreateException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.FinderException JavaDoc;
32 import javax.ejb.RemoveException JavaDoc;
33 import javax.ejb.SessionContext JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 import org.jboss.test.banknew.interfaces.Account;
38 import org.jboss.test.banknew.interfaces.AccountData;
39 import org.jboss.test.banknew.interfaces.AccountHome;
40 import org.jboss.test.banknew.interfaces.AccountPK;
41 import org.jboss.test.banknew.interfaces.Constants;
42 import org.jboss.test.banknew.interfaces.Transaction;
43 import org.jboss.test.banknew.interfaces.TransactionHome;
44 import org.jboss.test.util.ejb.SessionSupport;
45
46 /**
47  * The Session bean represents the account's business interface
48  *
49  * @author Andreas Schaefer
50  * @version $Revision: 41581 $
51  *
52  * @ejb:bean name="bank/AccountSession"
53  * display-name="Account Session"
54  * type="Stateless"
55  * view-type="remote"
56  * jndi-name="ejb/bank/AccountSession"
57  *
58  * @ejb:interface extends="javax.ejb.EJBObject"
59  *
60  * @ejb:home extends="javax.ejb.EJBHome"
61  *
62  * @ejb:pk extends="java.lang.Object"
63  *
64  * @ejb:transaction type="Required"
65  *
66  * @ejb:ejb-ref ejb-name="bank/Account"
67  *
68  * @ejb:ejb-ref ejb-name="bank/Transaction"
69  */

70 public class AccountSessionBean
71    extends SessionSupport
72 {
73    
74    // Constants -----------------------------------------------------
75

76    // Attributes ----------------------------------------------------
77

78    // Static --------------------------------------------------------
79

80    // Constructors --------------------------------------------------
81

82    // Public --------------------------------------------------------
83

84    /** The serialVersionUID */
85    private static final long serialVersionUID = 1963027300468464478L;
86
87    /**
88     * @ejb:interface-method view-type="remote"
89     **/

90    public AccountData getAccount( String JavaDoc pAccountId )
91       throws FinderException JavaDoc, RemoteException JavaDoc
92    {
93       return getAccountHome().findByPrimaryKey(
94          new AccountPK( pAccountId )
95       ).getData();
96    }
97    
98    /**
99     * @ejb:interface-method view-type="remote"
100     **/

101    public AccountData getAccount( String JavaDoc pCustomerId, int pType )
102       throws FinderException JavaDoc, RemoteException JavaDoc
103    {
104       return getAccountHome().findByCustomerAndType( pCustomerId, pType ).getData();
105    }
106    
107    /**
108     * @ejb:interface-method view-type="remote"
109     **/

110    public Collection JavaDoc getAccounts( String JavaDoc pCustomerId )
111       throws FinderException JavaDoc, RemoteException JavaDoc
112    {
113       Collection JavaDoc lAccounts = getAccountHome().findByCustomer( pCustomerId );
114       Collection JavaDoc lList = new ArrayList JavaDoc( lAccounts.size() );
115       Iterator JavaDoc i = lAccounts.iterator();
116       while( i.hasNext() ) {
117          lList.add( ( (Account) i.next() ).getData() );
118       }
119       return lList;
120    }
121    
122    /**
123     * @ejb:interface-method view-type="remote"
124     **/

125    public Collection JavaDoc getTransactions( String JavaDoc pAccountId )
126       throws FinderException JavaDoc, RemoteException JavaDoc
127    {
128       Collection JavaDoc lTransactions = getTransactionHome().findByAccount( pAccountId );
129       Iterator JavaDoc i = lTransactions.iterator();
130       Collection JavaDoc lList = new ArrayList JavaDoc( lTransactions.size() );
131       while( i.hasNext() ) {
132          lList.add( ( (Transaction) i.next() ).getData() );
133       }
134       return lList;
135    }
136    
137    /**
138     * @ejb:interface-method view-type="remote"
139     **/

140    public AccountData createAccount( String JavaDoc pCustomerId, int pType, float pInitialDeposit )
141       throws CreateException JavaDoc, RemoteException JavaDoc
142    {
143       AccountData lData = new AccountData();
144       lData.setCustomerId( pCustomerId );
145       lData.setType( pType );
146       lData.setBalance( pInitialDeposit );
147       Account lAccount = getAccountHome().create( lData );
148       AccountData lNew = lAccount.getData();
149       getTransactionHome().create(
150          lNew.getId(),
151          Constants.INITIAL_DEPOSIT,
152          pInitialDeposit,
153          "Account Creation"
154       );
155       return lNew;
156    }
157    
158    /**
159     * @ejb:interface-method view-type="remote"
160     **/

161    public void removeAccount( String JavaDoc pAccountId )
162       throws RemoveException JavaDoc, RemoteException JavaDoc
163    {
164       try {
165          Account lAccount = getAccountHome().findByPrimaryKey(
166             new AccountPK( pAccountId )
167          );
168          removeAccount( lAccount );
169       }
170       catch( FinderException JavaDoc fe ) {
171          // When not found then ignore it because account is already removed
172
}
173       catch( CreateException JavaDoc ce ) {
174          throw new EJBException JavaDoc( ce );
175       }
176    }
177    
178    /**
179     * @ejb:interface-method view-type="remote"
180     **/

181    public void removeAccount( String JavaDoc pCustomerId, int pType )
182       throws RemoveException JavaDoc, RemoteException JavaDoc
183    {
184       try {
185          Account lAccount = getAccountHome().findByCustomerAndType(
186             pCustomerId,
187             pType
188          );
189          removeAccount( lAccount );
190       }
191       catch( FinderException JavaDoc fe ) {
192          // When not found then ignore it because account is already removed
193
}
194       catch( CreateException JavaDoc ce ) {
195          throw new EJBException JavaDoc( ce );
196       }
197    }
198    
199    /**
200     * @ejb:interface-method view-type="remote"
201     **/

202    public void removeTransactions( String JavaDoc pAccountId )
203       throws RemoveException JavaDoc, RemoteException JavaDoc
204    {
205       try {
206          Collection JavaDoc lTransactions = getTransactionHome().findByAccount( pAccountId );
207          Iterator JavaDoc i = lTransactions.iterator();
208          while( i.hasNext() ) {
209             Transaction lTransaction = (Transaction) i.next();
210             lTransaction.remove();
211          }
212       }
213       catch( FinderException JavaDoc fe ) {
214       }
215    }
216    
217    private void removeAccount( Account pAccount )
218       throws RemoveException JavaDoc, CreateException JavaDoc, RemoteException JavaDoc
219    {
220       AccountData lData = pAccount.getData();
221       pAccount.remove();
222       getTransactionHome().create(
223          lData.getId(),
224          Constants.FINAL_WITHDRAW,
225          lData.getBalance(),
226          "Account Closure"
227       );
228    }
229    
230    /**
231     * @ejb:interface-method view-type="remote"
232     **/

233    public void deposit( String JavaDoc pAccountId, float pAmount )
234       throws FinderException JavaDoc, RemoteException JavaDoc
235    {
236       Account lAccount = getAccountHome().findByPrimaryKey(
237          new AccountPK( pAccountId )
238       );
239       AccountData lData = lAccount.getData();
240       lData.setBalance( lData.getBalance() + pAmount );
241       lAccount.setData( lData );
242       try {
243          getTransactionHome().create(
244             lData.getId(),
245             Constants.DEPOSIT,
246             pAmount,
247             "Account Deposit"
248          );
249       }
250       catch( CreateException JavaDoc ce ) {
251          throw new EJBException JavaDoc( ce );
252       }
253    }
254    
255    /**
256     * @ejb:interface-method view-type="remote"
257     **/

258    public void withdraw( String JavaDoc pAccountId, float pAmount )
259       throws FinderException JavaDoc, RemoteException JavaDoc
260    {
261       Account lAccount = getAccountHome().findByPrimaryKey(
262          new AccountPK( pAccountId )
263       );
264       AccountData lData = lAccount.getData();
265       lData.setBalance( lData.getBalance() - pAmount );
266       lAccount.setData( lData );
267       try {
268          getTransactionHome().create(
269             lData.getId(),
270             Constants.WITHDRAW,
271             pAmount,
272             "Account Withdraw"
273          );
274       }
275       catch( CreateException JavaDoc ce ) {
276          throw new EJBException JavaDoc( ce );
277       }
278    }
279    
280    /**
281     * @ejb:interface-method view-type="remote"
282     **/

283    public void transfer( String JavaDoc pFromAccountId, String JavaDoc pToAccountId, float pAmount )
284       throws FinderException JavaDoc, RemoteException JavaDoc
285    {
286       try {
287          withdraw( pFromAccountId, pAmount );
288          deposit( pToAccountId, pAmount );
289       }
290       catch( RemoteException JavaDoc re ) {
291          re.printStackTrace();
292          throw re;
293       }
294    }
295    
296    private AccountHome getAccountHome() {
297       try {
298          return (AccountHome) new InitialContext JavaDoc().lookup( AccountHome.COMP_NAME );
299       }
300       catch( NamingException JavaDoc ne ) {
301          throw new EJBException JavaDoc( ne );
302       }
303    }
304    
305    private TransactionHome getTransactionHome() {
306       try {
307          return (TransactionHome) new InitialContext JavaDoc().lookup( TransactionHome.COMP_NAME );
308       }
309       catch( NamingException JavaDoc ne ) {
310          throw new EJBException JavaDoc( ne );
311       }
312    }
313    
314    // SessionBean implementation ------------------------------------
315
public void setSessionContext(SessionContext JavaDoc context)
316    {
317       super.setSessionContext(context);
318    }
319 }
320
321 /*
322  * $Id: AccountSessionBean.java 41581 2006-03-01 16:10:00Z adrian $
323  * Currently locked by:$Locker$
324  * Revision:
325  * $Log$
326  * Revision 1.3 2006/03/01 16:09:58 adrian
327  * Remove xdoclet from jca tests
328  *
329  * Revision 1.1.16.1 2005/10/29 05:04:35 starksm
330  * Update the LGPL header
331  *
332  * Revision 1.1 2002/05/04 01:08:25 schaefera
333  * Added new Stats classes (JMS related) to JSR-77 implemenation and added the
334  * bank-new test application but this does not work right now properly but
335  * it is not added to the default tests so I shouldn't bother someone.
336  *
337  * Revision 1.1.2.2 2002/04/29 21:05:17 schaefera
338  * Added new marathon test suite using the new bank application
339  *
340  * Revision 1.1.2.1 2002/04/17 05:07:24 schaefera
341  * Redesigned the banknew example therefore to a create separation between
342  * the Entity Bean (CMP) and the Session Beans (Business Logic).
343  * The test cases are redesigned but not finished yet.
344  *
345  */

346
Popular Tags