001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.beans;
017:
018: import javax.ejb.EntityContext;
019: import javax.ejb.FinderException;
020: import javax.naming.InitialContext;
021: import java.sql.Connection;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.Statement;
025:
026: public class EmployeeBean implements javax.ejb.EntityBean {
027: int id;
028: String lastName;
029: String firstName;
030:
031: EntityContext ejbContext;
032:
033: public int ejbHomeSum(int one, int two) {
034: return one + two;
035: }
036:
037: public Integer ejbFindByPrimaryKey(Integer primaryKey)
038: throws javax.ejb.FinderException {
039: boolean found = false;
040: try {
041: InitialContext jndiContext = new InitialContext();
042:
043: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
044: .lookup("java:comp/env/jdbc/orders");
045:
046: Connection con = ds.getConnection();
047:
048: try {
049: PreparedStatement stmt = con
050: .prepareStatement("select * from Employees where EmployeeID = ?");
051: try {
052: stmt.setInt(1, primaryKey.intValue());
053: ResultSet rs = stmt.executeQuery();
054: found = rs.next();
055: } finally {
056: stmt.close();
057: }
058: } finally {
059: con.close();
060: }
061: } catch (Exception e) {
062: e.printStackTrace();
063: throw new FinderException("FindByPrimaryKey failed");
064: }
065:
066: if (found)
067: return primaryKey;
068: else
069: throw new javax.ejb.ObjectNotFoundException();
070:
071: }
072:
073: public java.util.Collection ejbFindAll() throws FinderException {
074: try {
075: InitialContext jndiContext = new InitialContext();
076:
077: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
078: .lookup("java:comp/env/jdbc/orders");
079:
080: Connection con = ds.getConnection();
081:
082: java.util.Vector keys;
083: try {
084: Statement stmt = con.createStatement();
085: try {
086: ResultSet rs = stmt
087: .executeQuery("select EmployeeID from Employees");
088: keys = new java.util.Vector();
089: while (rs.next()) {
090: keys.addElement(new Integer(rs
091: .getInt("EmployeeID")));
092: }
093: } finally {
094: stmt.close();
095: }
096: } finally {
097: con.close();
098: }
099: return keys;
100: } catch (Exception e) {
101: e.printStackTrace();
102: throw new FinderException("FindAll failed");
103: }
104: }
105:
106: public Integer ejbCreate(String fname, String lname)
107: throws javax.ejb.CreateException {
108: try {
109: lastName = lname;
110: firstName = fname;
111:
112: InitialContext jndiContext = new InitialContext();
113:
114: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
115: .lookup("java:comp/env/jdbc/orders");
116:
117: Connection con = ds.getConnection();
118:
119: try {
120: PreparedStatement stmt = con
121: .prepareStatement("insert into Employees (FirstName, LastName) values (?,?)");
122: try {
123: stmt.setString(1, firstName);
124: stmt.setString(2, lastName);
125: stmt.executeUpdate();
126:
127: stmt = con
128: .prepareStatement("select EmployeeID from Employees where FirstName = ? AND LastName = ?");
129: stmt.setString(1, firstName);
130: stmt.setString(2, lastName);
131: ResultSet set = stmt.executeQuery();
132: while (set.next())
133: id = set.getInt("EmployeeID");
134: } finally {
135: stmt.close();
136: }
137: } finally {
138: con.close();
139: }
140:
141: return new Integer(id);
142:
143: } catch (Exception e) {
144: e.printStackTrace();
145: throw new javax.ejb.CreateException("can't create");
146: }
147: }
148:
149: public String getLastName() {
150: return lastName;
151: }
152:
153: public String getFirstName() {
154: return firstName;
155: }
156:
157: public void setLastName(String lname) {
158: lastName = lname;
159: }
160:
161: public void setFirstName(String fname) {
162: firstName = fname;
163: }
164:
165: public void ejbLoad() {
166: try {
167: InitialContext jndiContext = new InitialContext();
168:
169: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
170: .lookup("java:comp/env/jdbc/orders");
171:
172: Connection con = ds.getConnection();
173: try {
174: PreparedStatement stmt = con
175: .prepareStatement("select * from Employees where EmployeeID = ?");
176: try {
177: Integer primaryKey = (Integer) ejbContext
178: .getPrimaryKey();
179: stmt.setInt(1, primaryKey.intValue());
180: ResultSet rs = stmt.executeQuery();
181: while (rs.next()) {
182: lastName = rs.getString("LastName");
183: firstName = rs.getString("FirstName");
184: }
185: } finally {
186: stmt.close();
187: }
188: } finally {
189: con.close();
190: }
191:
192: } catch (Exception e) {
193: e.printStackTrace();
194: }
195:
196: }
197:
198: public void ejbStore() {
199: try {
200: InitialContext jndiContext = new InitialContext();
201:
202: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
203: .lookup("java:comp/env/jdbc/orders");
204: Connection con = ds.getConnection();
205:
206: try {
207: PreparedStatement stmt = con
208: .prepareStatement("update Employees set FirstName = ?, LastName = ? where EmployeeID = ?");
209: try {
210: stmt.setString(1, firstName);
211: stmt.setString(2, lastName);
212: stmt.setInt(3, id);
213: stmt.execute();
214: } finally {
215: stmt.close();
216: }
217: } finally {
218: con.close();
219: }
220: } catch (Exception e) {
221: e.printStackTrace();
222: }
223:
224: }
225:
226: public void ejbActivate() {
227: }
228:
229: public void ejbPassivate() {
230: }
231:
232: public void ejbRemove() {
233:
234: try {
235: InitialContext jndiContext = new InitialContext();
236:
237: javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext
238: .lookup("java:comp/env/jdbc/orders");
239:
240: Connection con = ds.getConnection();
241:
242: try {
243: PreparedStatement stmt = con
244: .prepareStatement("delete from Employees where EmployeeID = ?");
245: try {
246: Integer primaryKey = (Integer) ejbContext
247: .getPrimaryKey();
248: stmt.setInt(1, primaryKey.intValue());
249: stmt.executeUpdate();
250: } finally {
251: stmt.close();
252: }
253: } finally {
254: con.close();
255: }
256:
257: } catch (Exception e) {
258: e.printStackTrace();
259: }
260: }
261:
262: public void setEntityContext(javax.ejb.EntityContext cntx) {
263: ejbContext = cntx;
264: }
265:
266: public void unsetEntityContext() {
267: }
268: }
|