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.bmp.beans;
023:
024: import java.rmi.RemoteException;
025: import java.sql.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029: import java.util.Collection;
030: import java.util.Vector;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.DuplicateKeyException;
034: import javax.ejb.EJBException;
035: import javax.ejb.EntityBean;
036: import javax.ejb.EntityContext;
037: import javax.ejb.FinderException;
038: import javax.naming.InitialContext;
039: import javax.naming.NamingException;
040: import javax.sql.DataSource;
041:
042: import org.jboss.logging.Logger;
043: import org.jboss.test.bmp.interfaces.SimpleBMPHome;
044:
045: public class SimpleBMPBean implements EntityBean {
046: /** The serialVersionUID */
047: private static final long serialVersionUID = 1L;
048:
049: Logger log = Logger.getLogger(getClass());
050: EntityContext ctx = null;
051: DataSource ds = null;
052:
053: // bmp fields
054: Integer id;
055: String name;
056: boolean ejbStoreInvoked = false;
057: boolean tempEjbStoreInvoked = false;
058:
059: public Integer ejbCreate(int _id, String _name)
060: throws CreateException, RemoteException {
061: log.debug("ejbCreate (int, String) called");
062:
063: id = new Integer(_id);
064:
065: boolean dublicate = false;
066:
067: Connection con = null;
068: try {
069: con = ds.getConnection();
070: Statement s = con.createStatement();
071: ResultSet rs = s
072: .executeQuery("SELECT id FROM simplebean WHERE id="
073: + id.toString());
074: dublicate = rs.next();
075: rs.close();
076: s.close();
077:
078: if (!dublicate) {
079: PreparedStatement ps = con
080: .prepareStatement("INSERT INTO simplebean VALUES (?,?)");
081: ps.setInt(1, _id);
082: ps.setString(2, _name);
083: ps.execute();
084: ps.close();
085:
086: name = _name;
087: }
088: } catch (Exception _e) {
089: throw new EJBException("couldnt create: " + _e.getMessage());
090: } finally {
091: try {
092: if (con != null)
093: con.close();
094: } catch (Exception _sqle) {
095: }
096: }
097:
098: if (dublicate)
099: throw new DuplicateKeyException("Bean with id=" + _id
100: + " already exists.");
101:
102: return id;
103: }
104:
105: public Integer ejbCreateMETHOD(int _id, String _name)
106: throws CreateException, RemoteException {
107: log.debug("ejbCreateMETHOD (int, String) called");
108:
109: id = new Integer(_id);
110:
111: boolean dublicate = false;
112:
113: Connection con = null;
114: try {
115: con = ds.getConnection();
116: Statement s = con.createStatement();
117: ResultSet rs = s
118: .executeQuery("SELECT id FROM simplebean WHERE id="
119: + id.toString());
120: dublicate = rs.next();
121: rs.close();
122: s.close();
123:
124: if (!dublicate) {
125: PreparedStatement ps = con
126: .prepareStatement("INSERT INTO simplebean VALUES (?,?)");
127: ps.setInt(1, _id);
128: ps.setString(2, _name);
129: ps.execute();
130: ps.close();
131:
132: name = _name;
133: }
134: } catch (Exception _e) {
135: throw new EJBException("couldnt create: " + _e.getMessage());
136: } finally {
137: try {
138: if (con != null)
139: con.close();
140: } catch (Exception _sqle) {
141: }
142: }
143:
144: if (dublicate)
145: throw new DuplicateKeyException("Bean with id=" + _id
146: + " already exists.");
147:
148: return id;
149: }
150:
151: public void ejbPostCreate(int _id, String _name)
152: throws CreateException, RemoteException {
153: log.debug("ejbPostCreate (int, String) called");
154:
155: tempEjbStoreInvoked = false;
156: // Do a find all to see whether ejbStore gets invoked
157: SimpleBMPHome home = (SimpleBMPHome) ctx.getEJBHome();
158: try {
159: home.findAll();
160: } catch (FinderException e) {
161: throw new RemoteException(
162: "Unexpected error invoking findAll", e);
163: }
164: ejbStoreInvoked = tempEjbStoreInvoked;
165: }
166:
167: public void ejbPostCreateMETHOD(int _id, String _name)
168: throws CreateException, RemoteException {
169: log.debug("ejbPostCreateMETHOD (int, String) called");
170: }
171:
172: public void ejbLoad() {
173: log.debug("ejbLoad () called " + this );
174:
175: Connection con = null;
176: try {
177: con = ds.getConnection();
178: PreparedStatement ps = con
179: .prepareStatement("SELECT id,name FROM simplebean WHERE id=?");
180: ps.setInt(1, ((Integer) ctx.getPrimaryKey()).intValue());
181: ResultSet rs = ps.executeQuery();
182: if (rs.next()) {
183: id = new Integer(rs.getInt("id"));
184: name = rs.getString("name");
185: }
186: rs.close();
187: ps.close();
188: } catch (Exception _e) {
189: throw new EJBException("couldnt load: " + _e.getMessage());
190: } finally {
191: try {
192: if (con != null)
193: con.close();
194: } catch (Exception _sqle) {
195: }
196: }
197: }
198:
199: public void ejbStore() {
200: log.debug("ejbStore () called " + this );
201: tempEjbStoreInvoked = true;
202:
203: Connection con = null;
204: try {
205: con = ds.getConnection();
206: PreparedStatement ps = con
207: .prepareStatement("UPDATE simplebean SET name=? WHERE id=?");
208: ps.setString(1, name);
209: ps.setInt(2, id.intValue());
210: ps.execute();
211: ps.close();
212: } catch (Exception _e) {
213: throw new EJBException("couldnt store: " + _e.getMessage());
214: } finally {
215: try {
216: if (con != null)
217: con.close();
218: } catch (Exception _sqle) {
219: }
220: }
221: }
222:
223: public void ejbRemove() {
224: log.debug("ejbRemove () called " + this );
225:
226: Connection con = null;
227: try {
228: con = ds.getConnection();
229: PreparedStatement ps = con
230: .prepareStatement("DELETE FROM simplebean WHERE id=?");
231: ps.setInt(1, id.intValue());
232: ps.execute();
233: ps.close();
234: } catch (Exception _e) {
235: throw new EJBException("couldnt remove: " + _e.getMessage());
236: } finally {
237: try {
238: if (con != null)
239: con.close();
240: } catch (Exception _sqle) {
241: }
242: }
243: }
244:
245: public Integer ejbFindByPrimaryKey(Integer _key)
246: throws FinderException {
247: log.debug("ejbFindByPrimaryKey (Integer) called " + this );
248:
249: Connection con = null;
250: boolean found = false;
251: try {
252: con = ds.getConnection();
253: PreparedStatement ps = con
254: .prepareStatement("SELECT id FROM simplebean WHERE id=?");
255: ps.setInt(1, _key.intValue());
256: ResultSet rs = ps.executeQuery();
257: found = rs.next();
258: rs.close();
259: ps.close();
260: } catch (Exception _e) {
261: throw new EJBException("couldnt seek: " + _e.getMessage());
262: } finally {
263:
264: try {
265: if (con != null)
266: con.close();
267: } catch (Exception _sqle) {
268: }
269: }
270: if (!found)
271: throw new FinderException("No bean with id=" + _key
272: + " found.");
273:
274: return _key;
275: }
276:
277: public Collection ejbFindAll() throws FinderException {
278: log.debug("ejbFindAll () called");
279:
280: Connection con = null;
281: Vector result = new Vector();
282: try {
283: con = ds.getConnection();
284: Statement s = con.createStatement();
285: ResultSet rs = s.executeQuery("SELECT id FROM simplebean");
286: while (rs.next()) {
287: result.add(new Integer(rs.getInt("id")));
288: }
289: rs.close();
290: s.close();
291: } catch (Exception _e) {
292: throw new EJBException("couldnt seek: " + _e.getMessage());
293: } finally {
294:
295: try {
296: if (con != null)
297: con.close();
298: } catch (Exception _sqle) {
299: }
300: }
301:
302: return result;
303: }
304:
305: public void ejbActivate() {
306: log.debug("ejbActivate () called " + this );
307: }
308:
309: public void ejbPassivate() {
310: log
311: .debug("ejbPassivate () called " + this , new Exception(
312: "ST"));
313: }
314:
315: public void setEntityContext(EntityContext _ctx) {
316: log.debug("setEntityContext() called " + this );
317:
318: ctx = _ctx;
319: // lookup the datasource
320: try {
321: ds = (DataSource) new InitialContext()
322: .lookup("java:comp/env/datasource");
323: } catch (NamingException _ne) {
324: throw new EJBException("Datasource not found: "
325: + _ne.getMessage());
326: }
327: }
328:
329: public void unsetEntityContext() {
330: log.debug("unsetEntityContext () called");
331:
332: ctx = null;
333: }
334:
335: // business methods ---------------------------------------------------------------
336:
337: public Integer getIdViaEJBObject() {
338: try {
339: Integer result = (Integer) ctx.getEJBObject()
340: .getPrimaryKey();
341: log.debug(result + " " + ctx.getPrimaryKey());
342: return result;
343: } catch (RemoteException e) {
344: throw new EJBException(e);
345: }
346: }
347:
348: public void setName(String _name) {
349: name = _name;
350: }
351:
352: public String getName() {
353: return name;
354: }
355:
356: public boolean isEjbStoreInvoked() {
357: return ejbStoreInvoked;
358: }
359: }
|