001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.banknew.ejb;
023:
024: import java.rmi.RemoteException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.ejb.CreateException;
030: import javax.ejb.EJBException;
031: import javax.ejb.FinderException;
032: import javax.ejb.RemoveException;
033: import javax.ejb.SessionContext;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036:
037: import org.jboss.test.banknew.interfaces.Account;
038: import org.jboss.test.banknew.interfaces.AccountData;
039: import org.jboss.test.banknew.interfaces.AccountHome;
040: import org.jboss.test.banknew.interfaces.AccountPK;
041: import org.jboss.test.banknew.interfaces.Constants;
042: import org.jboss.test.banknew.interfaces.Transaction;
043: import org.jboss.test.banknew.interfaces.TransactionHome;
044: import org.jboss.test.util.ejb.SessionSupport;
045:
046: /**
047: * The Session bean represents the account's business interface
048: *
049: * @author Andreas Schaefer
050: * @version $Revision: 57211 $
051: *
052: * @ejb:bean name="bank/AccountSession"
053: * display-name="Account Session"
054: * type="Stateless"
055: * view-type="remote"
056: * jndi-name="ejb/bank/AccountSession"
057: *
058: * @ejb:interface extends="javax.ejb.EJBObject"
059: *
060: * @ejb:home extends="javax.ejb.EJBHome"
061: *
062: * @ejb:pk extends="java.lang.Object"
063: *
064: * @ejb:transaction type="Required"
065: *
066: * @ejb:ejb-ref ejb-name="bank/Account"
067: *
068: * @ejb:ejb-ref ejb-name="bank/Transaction"
069: */
070: public class AccountSessionBean extends SessionSupport {
071:
072: // Constants -----------------------------------------------------
073:
074: // Attributes ----------------------------------------------------
075:
076: // Static --------------------------------------------------------
077:
078: // Constructors --------------------------------------------------
079:
080: // Public --------------------------------------------------------
081:
082: /** The serialVersionUID */
083: private static final long serialVersionUID = 1963027300468464478L;
084:
085: /**
086: * @ejb:interface-method view-type="remote"
087: **/
088: public AccountData getAccount(String pAccountId)
089: throws FinderException, RemoteException {
090: return getAccountHome().findByPrimaryKey(
091: new AccountPK(pAccountId)).getData();
092: }
093:
094: /**
095: * @ejb:interface-method view-type="remote"
096: **/
097: public AccountData getAccount(String pCustomerId, int pType)
098: throws FinderException, RemoteException {
099: return getAccountHome().findByCustomerAndType(pCustomerId,
100: pType).getData();
101: }
102:
103: /**
104: * @ejb:interface-method view-type="remote"
105: **/
106: public Collection getAccounts(String pCustomerId)
107: throws FinderException, RemoteException {
108: Collection lAccounts = getAccountHome().findByCustomer(
109: pCustomerId);
110: Collection lList = new ArrayList(lAccounts.size());
111: Iterator i = lAccounts.iterator();
112: while (i.hasNext()) {
113: lList.add(((Account) i.next()).getData());
114: }
115: return lList;
116: }
117:
118: /**
119: * @ejb:interface-method view-type="remote"
120: **/
121: public Collection getTransactions(String pAccountId)
122: throws FinderException, RemoteException {
123: Collection lTransactions = getTransactionHome().findByAccount(
124: pAccountId);
125: Iterator i = lTransactions.iterator();
126: Collection lList = new ArrayList(lTransactions.size());
127: while (i.hasNext()) {
128: lList.add(((Transaction) i.next()).getData());
129: }
130: return lList;
131: }
132:
133: /**
134: * @ejb:interface-method view-type="remote"
135: **/
136: public AccountData createAccount(String pCustomerId, int pType,
137: float pInitialDeposit) throws CreateException,
138: RemoteException {
139: AccountData lData = new AccountData();
140: lData.setCustomerId(pCustomerId);
141: lData.setType(pType);
142: lData.setBalance(pInitialDeposit);
143: Account lAccount = getAccountHome().create(lData);
144: AccountData lNew = lAccount.getData();
145: getTransactionHome().create(lNew.getId(),
146: Constants.INITIAL_DEPOSIT, pInitialDeposit,
147: "Account Creation");
148: return lNew;
149: }
150:
151: /**
152: * @ejb:interface-method view-type="remote"
153: **/
154: public void removeAccount(String pAccountId)
155: throws RemoveException, RemoteException {
156: try {
157: Account lAccount = getAccountHome().findByPrimaryKey(
158: new AccountPK(pAccountId));
159: removeAccount(lAccount);
160: } catch (FinderException fe) {
161: // When not found then ignore it because account is already removed
162: } catch (CreateException ce) {
163: throw new EJBException(ce);
164: }
165: }
166:
167: /**
168: * @ejb:interface-method view-type="remote"
169: **/
170: public void removeAccount(String pCustomerId, int pType)
171: throws RemoveException, RemoteException {
172: try {
173: Account lAccount = getAccountHome().findByCustomerAndType(
174: pCustomerId, pType);
175: removeAccount(lAccount);
176: } catch (FinderException fe) {
177: // When not found then ignore it because account is already removed
178: } catch (CreateException ce) {
179: throw new EJBException(ce);
180: }
181: }
182:
183: /**
184: * @ejb:interface-method view-type="remote"
185: **/
186: public void removeTransactions(String pAccountId)
187: throws RemoveException, RemoteException {
188: try {
189: Collection lTransactions = getTransactionHome()
190: .findByAccount(pAccountId);
191: Iterator i = lTransactions.iterator();
192: while (i.hasNext()) {
193: Transaction lTransaction = (Transaction) i.next();
194: lTransaction.remove();
195: }
196: } catch (FinderException fe) {
197: }
198: }
199:
200: private void removeAccount(Account pAccount)
201: throws RemoveException, CreateException, RemoteException {
202: AccountData lData = pAccount.getData();
203: pAccount.remove();
204: getTransactionHome().create(lData.getId(),
205: Constants.FINAL_WITHDRAW, lData.getBalance(),
206: "Account Closure");
207: }
208:
209: /**
210: * @ejb:interface-method view-type="remote"
211: **/
212: public void deposit(String pAccountId, float pAmount)
213: throws FinderException, RemoteException {
214: Account lAccount = getAccountHome().findByPrimaryKey(
215: new AccountPK(pAccountId));
216: AccountData lData = lAccount.getData();
217: lData.setBalance(lData.getBalance() + pAmount);
218: lAccount.setData(lData);
219: try {
220: getTransactionHome().create(lData.getId(),
221: Constants.DEPOSIT, pAmount, "Account Deposit");
222: } catch (CreateException ce) {
223: throw new EJBException(ce);
224: }
225: }
226:
227: /**
228: * @ejb:interface-method view-type="remote"
229: **/
230: public void withdraw(String pAccountId, float pAmount)
231: throws FinderException, RemoteException {
232: Account lAccount = getAccountHome().findByPrimaryKey(
233: new AccountPK(pAccountId));
234: AccountData lData = lAccount.getData();
235: lData.setBalance(lData.getBalance() - pAmount);
236: lAccount.setData(lData);
237: try {
238: getTransactionHome().create(lData.getId(),
239: Constants.WITHDRAW, pAmount, "Account Withdraw");
240: } catch (CreateException ce) {
241: throw new EJBException(ce);
242: }
243: }
244:
245: /**
246: * @ejb:interface-method view-type="remote"
247: **/
248: public void transfer(String pFromAccountId, String pToAccountId,
249: float pAmount) throws FinderException, RemoteException {
250: try {
251: withdraw(pFromAccountId, pAmount);
252: deposit(pToAccountId, pAmount);
253: } catch (RemoteException re) {
254: re.printStackTrace();
255: throw re;
256: }
257: }
258:
259: private AccountHome getAccountHome() {
260: try {
261: return (AccountHome) new InitialContext()
262: .lookup(AccountHome.COMP_NAME);
263: } catch (NamingException ne) {
264: throw new EJBException(ne);
265: }
266: }
267:
268: private TransactionHome getTransactionHome() {
269: try {
270: return (TransactionHome) new InitialContext()
271: .lookup(TransactionHome.COMP_NAME);
272: } catch (NamingException ne) {
273: throw new EJBException(ne);
274: }
275: }
276:
277: // SessionBean implementation ------------------------------------
278: public void setSessionContext(SessionContext context) {
279: super .setSessionContext(context);
280: }
281: }
282:
283: /*
284: * $Id: AccountSessionBean.java 57211 2006-09-26 12:39:46Z dimitris@jboss.org $
285: * Currently locked by:$Locker$
286: * Revision:
287: * $Log$
288: * Revision 1.1.16.2 2006/03/01 15:58:56 adrian
289: * Remove xdoclet from the jca tests + other tidyup
290: *
291: * Revision 1.1.16.1 2005/10/29 05:04:35 starksm
292: * Update the LGPL header
293: *
294: * Revision 1.1 2002/05/04 01:08:25 schaefera
295: * Added new Stats classes (JMS related) to JSR-77 implemenation and added the
296: * bank-new test application but this does not work right now properly but
297: * it is not added to the default tests so I shouldn't bother someone.
298: *
299: * Revision 1.1.2.2 2002/04/29 21:05:17 schaefera
300: * Added new marathon test suite using the new bank application
301: *
302: * Revision 1.1.2.1 2002/04/17 05:07:24 schaefera
303: * Redesigned the banknew example therefore to a create separation between
304: * the Entity Bean (CMP) and the Session Beans (Business Logic).
305: * The test cases are redesigned but not finished yet.
306: *
307: */
|