001 package ejbControl;
002
003 import examples.ejb20.basic.containerManaged.Account;
004 import examples.ejb20.basic.containerManaged.ProcessingErrorException;
005 import javax.ejb.FinderException;
006 import javax.ejb.CreateException;
007 import java.rmi.RemoteException;
008 import java.util.Iterator;
009
010 /**
011 * <p>A web service that demonstrates use of an EJB control AccountEJBControl.jcx,
012 * which represents the AccountEJB Entity Bean and exposes its business interface
013 * to web services.</p>
014 * @common:target-namespace namespace="http://workshop.bea.com/AccountEJBClient"
015 */
016 public class AccountEJBClient implements com.bea.jws.WebService
017 {
018 /**
019 * @common:control
020 */
021 private ejbControl.AccountEJBControl account;
022
023 /**
024 * <p>Invokes the target EJB's <b>create</b> method and returns the result.</p>
025 *
026 * <ul>
027 * <li><b>key</b> is the account identifier (must be unique for each account).</li>
028 * <li><b>openingBalance</b> is the starting balance of the account.</li>
029 * <li><b>type</b> is the account type, e.g. "checking" or "savings".</li>
030 * </ul>
031 *
032 * @return A String describing the result of the account creation.
033 *
034 * @common:operation
035 */
036 public String createNewAccount(String key, double openingBalance, String type)
037 {
038 try
039 {
040 account.create(key, openingBalance, type);
041 }
042 catch (CreateException ce)
043 {
044 return "Error " + ce.getLocalizedMessage();
045 }
046 catch (RemoteException re)
047 {
048 return "Error " + re.getLocalizedMessage();
049 }
050 return "Successful";
051 }
052
053 /**
054 * <p>Invokes the target EJB's <b>getAccounts</b> method and returns a list of all
055 * accounts currently persisted by the target EJB.</p>
056 *
057 * @return A String listing all of the accounts.
058 *
059 * @common:operation
060 */
061 public String listAccounts()
062 {
063 return getAccounts(0.0d);
064 }
065
066 /**
067 * <p>Invokes the target EJB's <b>getAccounts</b> method and returns a list of accounts
068 * with balances greater than the specified <b>threshold</b>.</p>
069 *
070 * @return A String listing all of the accounts.
071 *
072 * @common:operation
073 */
074 public String listBigAccounts(double threshold)
075 {
076 return getAccounts(threshold);
077 }
078
079 /**
080 * <p>Invokes the target EJB's <b>getAccount</b> method and returns the account type of
081 * the account specified by <b>accountKey</b>.</p>
082 *
083 * @return A String account type.
084 *
085 * @common:operation
086 */
087 public String accountType(String accountKey)
088 {
089 try
090 {
091 return getAccount(accountKey).accountType();
092 }
093 catch (FinderException fe)
094 {
095 return "Error " + fe.getLocalizedMessage();
096 }
097 catch (RemoteException re)
098 {
099 return "Error " + re.getLocalizedMessage();
100 }
101 }
102
103 /**
104 * <p>Invokes the target EJB's <b>getAccount</b> on the account specified by
105 * <b>accountKey</b>, then the returned Account's <b>balance</b>
106 * method. Returns the account balance.</p>
107 *
108 * @return A double account balance, or -1 on a Finder Exception or -2 on Remote Exception.
109 *
110 * @common:operation
111 */
112 public double balance(String accountKey)
113 {
114 try
115 {
116 return getAccount(accountKey).balance();
117 }
118 catch (FinderException fe)
119 {
120 return -1d;
121 }
122 catch (RemoteException re)
123 {
124 return -2d;
125 }
126 }
127
128 /**
129 * <p>Invokes the target EJB's <b>getAccount</b> on the account specified by
130 * <b>accountKey</b>, then the returned Account's <b>deposit</b>
131 * method. Returns the resulting account balance.</p>
132 *
133 * @return A double account balance, or -1 on a Finder Exception or -2 on Remote Exception.
134 *
135 * @common:operation
136 */
137 public double deposit(String accountKey, double depositAmount)
138 {
139 try
140 {
141 return getAccount(accountKey).deposit(depositAmount);
142 }
143 catch (FinderException fe)
144 {
145 return -1d;
146 }
147 catch (RemoteException re)
148 {
149 return -2d;
150 }
151 }
152
153 /**
154 * <p>Invokes the target EJB's <b>getAccount</b> on the account specified by
155 * <b>accountKey</b>, then the returned account's <b>withdraw</b>
156 * method. Returns the resulting account balance.</p>
157 *
158 * @return A double account balance,
159 * or -1 on a Finder Exception,
160 * or -2 on Remote Exception,
161 * or -3 on Processing Error Exception.
162 *
163 * @common:operation
164 */
165 public double withdraw(String accountKey, double withdrawAmount)
166 {
167 try
168 {
169 return getAccount(accountKey).withdraw(withdrawAmount);
170 }
171 catch (FinderException fe)
172 {
173 return -1d;
174 }
175 catch (RemoteException re)
176 {
177 return -2d;
178 }
179 catch (ProcessingErrorException pe)
180 {
181 return -3d;
182 }
183 }
184
185 /**
186 * <p>Helper function -- returns an EJB account for a given key<p>
187 *
188 * @return An Account object
189 */
190 private Account getAccount(String accountKey)
191 throws FinderException, RemoteException
192 {
193 return account.findByPrimaryKey(accountKey);
194 }
195
196 /**
197 * <p>Helper function -- formats account list returns<p>
198 *
199 * @return new line delimited string of accounts
200 */
201 private String getAccounts(double limit)
202 {
203 Iterator itr = null;
204 StringBuffer retval = new StringBuffer();
205 try
206 {
207 itr = account.findBigAccounts(limit).iterator();
208 while (itr.hasNext())
209 {
210 Account a = (Account)itr.next();
211 retval.append(a.getPrimaryKey()).append(' ').append(a.accountType()).append(' ').append(a.balance()).append(' ').append('\n');
212 }
213 }
214 catch (FinderException fe)
215 {
216 return "Error " + fe.getLocalizedMessage();
217 }
218 catch (RemoteException re)
219 {
220 return "Error " + re.getLocalizedMessage();
221 }
222
223 return retval.toString();
224 }
225
226 }
|