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.jca.bank.ejb;
023:
024: import java.util.Collection;
025: import java.sql.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import javax.sql.DataSource;
029: import java.sql.SQLException;
030:
031: import javax.ejb.ObjectNotFoundException;
032: import javax.ejb.CreateException;
033: import javax.ejb.EJBException;
034: import javax.ejb.EntityBean;
035: import javax.ejb.EntityContext;
036: import javax.ejb.EntityContext;
037: import javax.ejb.FinderException;
038: import javax.ejb.NoSuchEntityException;
039: import javax.naming.InitialContext;
040: import java.util.ArrayList;
041:
042: import org.apache.log4j.Category;
043:
044: import org.jboss.test.jca.bank.interfaces.AccountLocal;
045: import org.jboss.test.jca.bank.interfaces.AccountLocalHome;
046:
047: /**
048: * Describe class <code>CustomerBean</code> here.
049: *
050: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
051: * @version 1.0
052: * @ejb:bean name="Customer"
053: * local-jndi-name="LocalCustomer"
054: * view-type="local"
055: * type="BMP"
056: * primkey-field="id"
057: * @ejb:pk class="java.lang.Integer"
058: */
059: public class CustomerBean implements EntityBean {
060:
061: private Connection c;
062:
063: public Integer id;
064: public String name;
065: public Collection accounts;
066:
067: private EntityContext ctx;
068:
069: /**
070: * field get-set pair for field id
071: * Get the value of id
072: * @return value of id
073: *
074: * @ejb:interface-method
075: */
076: public Integer getId() {
077: return id;
078: }
079:
080: /**
081: * Set the value of id
082: * @param id Value to assign to id
083: *
084: * @ejb:interface-method view-type="local"
085: */
086: public void setId(final Integer id) {
087: this .id = id;
088: }
089:
090: /**
091: * field get-set pair for field name
092: * Get the value of name
093: * @return value of name
094: *
095: * @ejb:interface-method
096: */
097: public String getName() {
098: return name;
099: }
100:
101: /**
102: * Set the value of name
103: * @param name Value to assign to name
104: *
105: * @ejb:interface-method view-type="local"
106: */
107: public void setName(final String name) {
108: this .name = name;
109: }
110:
111: /**
112: * field get-set pair for field accounts
113: * Get the value of accounts
114: * @return value of accounts
115: *
116: * @ejb:interface-method
117: */
118: public Collection getAccounts() {
119: return accounts;
120: }
121:
122: /**
123: * Describe <code>addAccountLocal</code> method here.
124: *
125: * @param account an <code>AccountLocal</code> value
126: * @ejb:interface-method
127: */
128: public void addAccount(AccountLocal account) {
129: try {
130: account.setCustomerId(id);
131: accounts.add(account);
132: } catch (Exception e) {
133: throw new EJBException("Problem in addAccount: " + e);
134: }
135: }
136:
137: /**
138: * Describe <code>removeAccount</code> method here.
139: *
140: * @param acct an <code>AccountLocal</code> value
141: * @ejb:interface-method
142: */
143: public void removeAccount(AccountLocal account) {
144: try {
145: accounts.remove(account);
146: account.remove();
147: } catch (Exception e) {
148: throw new EJBException("Problem in removeAccount: " + e);
149: }
150:
151: }
152:
153: // EntityHome implementation -------------------------------------
154: /**
155: * Describe <code>ejbCreate</code> method here.
156: *
157: * @param id an <code>Integer</code> value
158: * @param name a <code>String</code> value
159: * @return a <code>Integer</code> value
160: * @ejb:create-method
161: */
162: public Integer ejbCreate(Integer id, String name)
163: throws CreateException {
164: setId(id);
165: setName(name);
166: PreparedStatement ps = null;
167: try {
168:
169: ps = getConnection()
170: .prepareStatement(
171: "INSERT INTO CCBMPCUSTOMER (ID, NAME) VALUES (?, ?)");
172: ps.setInt(1, id.intValue());
173: ps.setString(2, name);
174: accounts = new ArrayList();
175: } catch (Exception e) {
176: throw new CreateException("Problem in ejbCreate: " + e);
177: } // end of try-catch
178: finally {
179: try {
180: ps.close();
181: } catch (SQLException e) {
182: Category.getInstance(getClass().getName()).info(
183: "SQLException closing ps: " + e);
184: } // end of try-catch
185: } // end of finally
186: return id;
187: }
188:
189: public void ejbPostCreate(Integer id, String name) {
190: }
191:
192: public Integer ejbFindByPrimaryKey(final Integer id)
193: throws FinderException {
194: PreparedStatement ps = null;
195: try {
196: ps = getConnection().prepareStatement(
197: "SELECT ID FROM CCBMPCUSTOMER WHERE ID = ?");
198: ps.setInt(1, id.intValue());
199: ResultSet rs = ps.executeQuery();
200: if (!rs.next()) {
201: throw new ObjectNotFoundException("No such customer: "
202: + id);
203: } // end of if ()
204: rs.close();
205:
206: } catch (Exception e) {
207: throw new EJBException("problem in findByPK: " + e);
208: } // end of try-catch
209: finally {
210: try {
211: ps.close();
212: } catch (SQLException e) {
213: Category.getInstance(getClass().getName()).info(
214: "SQLException closing ps: " + e);
215: } // end of try-catch
216: } // end of finally
217: return id;
218: }
219:
220: public void ejbPostCreate(String id, String name) {
221: }
222:
223: public void ejbActivate() {
224: }
225:
226: public void ejbPassivate() {
227: if (c != null) {
228: try {
229: c.close();
230: } catch (SQLException e) {
231: Category.getInstance(getClass().getName()).info(
232: "SQLException closing c: " + e);
233: } // end of try-catch
234: c = null;
235: } // end of if ()
236: }
237:
238: public void ejbLoad() {
239: id = (Integer) ctx.getPrimaryKey();
240: if (id == null)
241: throw new EJBException("Null id!");
242:
243: PreparedStatement ps = null;
244: try {
245:
246: ps = getConnection().prepareStatement(
247: "SELECT NAME FROM CCBMPCUSTOMER WHERE ID = ?");
248: ps.setInt(1, id.intValue());
249: ResultSet rs = ps.executeQuery();
250: if (rs.next() == false)
251: throw new NoSuchEntityException(
252: "Customer does not exist " + id.toString());
253: this .name = rs.getString(1);
254: AccountLocalHome ah = (AccountLocalHome) new InitialContext()
255: .lookup("AccountLocal");
256: accounts = ah.findByCustomerId(id);
257:
258: } catch (Exception e) {
259: throw new EJBException("Problem in ejbLoad: " + e);
260: } // end of try-catch
261: finally {
262: try {
263: ps.close();
264: } catch (SQLException e) {
265: Category.getInstance(getClass().getName()).info(
266: "SQLException closing ps: " + e);
267: } // end of try-catch
268: } // end of finally
269: }
270:
271: public void ejbStore() {
272: PreparedStatement ps = null;
273: try {
274: ps = getConnection().prepareStatement(
275: "UPDATE CCBMPCUSTOMER SET NAME = ? WHERE ID = ?");
276: ps.setString(1, name);
277: ps.setInt(2, id.intValue());
278: ps.execute();
279: } catch (Exception e) {
280: throw new EJBException("Problem in ejbStore: " + e);
281: } // end of try-catch
282: finally {
283: try {
284: ps.close();
285: } catch (SQLException e) {
286: Category.getInstance(getClass().getName()).info(
287: "SQLException closing ps: " + e);
288: } // end of try-catch
289: } // end of finally
290:
291: }
292:
293: public void ejbRemove() {
294: PreparedStatement ps = null;
295: try {
296: ps = getConnection().prepareStatement(
297: "DELETE FROM CCBMPCUSTOMER WHERE ID = ?");
298: ps.setInt(1, id.intValue());
299: ps.execute();
300: } catch (Exception e) {
301: throw new EJBException("Problem in ejbRemove: " + e);
302: } // end of try-catch
303: finally {
304: try {
305: ps.close();
306: } catch (SQLException e) {
307: Category.getInstance(getClass().getName()).info(
308: "SQLException closing ps: " + e);
309: } // end of try-catch
310: } // end of finally
311:
312: }
313:
314: public void setEntityContext(EntityContext ctx) {
315: this .ctx = ctx;
316: }
317:
318: public void unsetEntityContext() {
319: ctx = null;
320: }
321:
322: private Connection getConnection() throws Exception {
323: if (c == null) {
324: DataSource ds = (DataSource) new InitialContext()
325: .lookup("java:/DefaultDS");
326: c = ds.getConnection();
327:
328: } // end of if ()
329:
330: return c;
331: }
332: }
|