Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/visitor/remote/ApplicantBean.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: ApplicantBean.java,v $
5    * $Date: 2003/02/26 03:38:46 $
6    * $Revision: 1.6 $
7    */
8   
9   package com.clra.visitor.remote;
10  
11  import com.clra.visitor.Configuration;
12  import com.clra.util.DBConfiguration;
13  import com.clra.util.ISerializableComparator;
14  import java.rmi.RemoteException;
15  import java.sql.Connection;
16  import java.sql.PreparedStatement;
17  import java.sql.ResultSet;
18  import java.sql.SQLException;
19  import java.sql.Types;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Date;
23  import javax.ejb.CreateException;
24  import javax.ejb.EJBException;
25  import javax.ejb.EntityBean;
26  import javax.ejb.EntityContext;
27  import javax.ejb.FinderException;
28  import javax.ejb.NoSuchEntityException;
29  import javax.ejb.ObjectNotFoundException;
30  import javax.ejb.RemoveException;
31  import java.text.SimpleDateFormat;
32  import org.apache.log4j.Category;
33  
34  /**
35   * @author <a href="mailto:donaldzhu@sympatico.ca">Angela Yue</a>
36   * @version $Revision: 1.6 $ $Date: 2003/02/26 03:38:46 $
37   */
38  
39  
40  public class ApplicantBean implements EntityBean {
41  
42    private final static String base = ApplicantBean.class.getName();
43    private final static Category theLog = Category.getInstance( base );
44  
45    public final static SimpleDateFormat dateFormat =
46            new SimpleDateFormat( Configuration.SQL_DATE_FORMAT );
47  
48    private transient boolean isDirty = true;
49    private Date date = null;
50    private String email_addr = null;
51  
52    private EntityContext context;
53  
54    public String ejbCreate(String nlast,
55                    String nfirst,
56                    String nmiddle,
57                    String nsuffix,
58                    String mail,
59                    String tel_evening,
60                    String tel_day,
61                    String tel_other,
62                    String addr_str1,
63                    String addr_str2,
64                    String addr_city,
65                    String addr_state,
66                    String addr_zip,
67                    String experience_year,
68                    String recent_year,
69                    Date birth,
70                    String sex,
71                    Date apply_date,
72                    String status
73                  ) throws CreateException, java.sql.SQLException {
74  
75      email_addr = mail;
76      Connection conn = null;
77      PreparedStatement stmt = null;
78      ResultSet rs = null;
79      try {
80        conn = DBConfiguration.getConnection();
81  
82        stmt = conn.prepareStatement(Configuration.SQL_03);
83  
84        stmt.setString(1, nlast);
85        stmt.setString(2, nfirst);
86  
87        int paramInd = 3;
88        if (nmiddle != null) {
89            stmt.setString(paramInd, nmiddle);
90        } else
91            stmt.setNull(paramInd, Types.VARCHAR);
92        paramInd++;
93  
94        if (nsuffix != null) {
95            stmt.setString(paramInd, nsuffix);
96        } else
97            stmt.setNull(paramInd, Types.VARCHAR);
98        paramInd++;
99  
100       stmt.setString(paramInd, mail);
101       paramInd++;
102 
103       stmt.setString(paramInd, tel_evening);
104       paramInd++;
105 
106       if (tel_day != null) {
107           stmt.setString(paramInd, tel_day);
108       } else
109           stmt.setNull(paramInd, Types.VARCHAR);
110       paramInd++;
111 
112       if (tel_other != null) {
113           stmt.setString(paramInd, tel_other);
114       } else
115           stmt.setNull(paramInd, Types.VARCHAR);
116       paramInd++;
117 
118       stmt.setString(paramInd, addr_str1);
119       paramInd++;
120 
121       if (addr_str2 != null) {
122           stmt.setString(paramInd, addr_str2);
123       } else
124           stmt.setNull(paramInd, Types.VARCHAR);
125       paramInd++;
126 
127       stmt.setString(paramInd, addr_city);
128       paramInd++;
129 
130       stmt.setString(paramInd, addr_state);
131       paramInd++;
132 
133       stmt.setString(paramInd, addr_zip);
134       paramInd++;
135 
136       stmt.setString(paramInd, experience_year);
137       paramInd++;
138 
139       stmt.setString(paramInd, recent_year);
140       paramInd++;
141 
142       if (birth != null) {
143           String s = dateFormat.format( birth );
144           stmt.setString(paramInd, s);
145       } else
146           stmt.setNull(paramInd, Types.VARCHAR);
147       paramInd++;
148 
149       stmt.setString(paramInd, sex);
150       paramInd++;
151       
152       stmt.setString(paramInd, status);
153 
154       stmt.executeUpdate();
155 
156       if ( theLog.isDebugEnabled() ) {
157         theLog.info( "insertRow: row " + mail );
158       }
159 
160     } catch (Exception ex) {
161        theLog.error("Exception in ejbCreate: " + ex);
162        
163        if (ex instanceof java.sql.SQLException)
164          throw new java.sql.SQLException(ex.toString());
165     }
166     finally {
167       DBConfiguration.closeSQLResultSet( rs );
168       DBConfiguration.closeSQLStatement( stmt );
169       DBConfiguration.closeSQLConnection( conn );
170       rs = null;
171       stmt = null;
172       conn = null;
173     }
174 
175     return mail;
176   } // ejbCreate
177 
178   // email is the primaryKey the table
179   public String ejbFindByPrimaryKey(String primaryKey) 
180     throws FinderException {
181 
182     // Precondition
183     if ( primaryKey == null ) {
184       throw new FinderException( "null primaryKey" );
185     }
186 
187     Connection conn = null;
188     PreparedStatement stmt = null;
189     ResultSet rs = null;
190     boolean retVal = false;
191     try {
192       conn = DBConfiguration.getConnection();
193       stmt = conn.prepareStatement(Configuration.SQL_01);
194 
195       stmt.setString( 1, primaryKey );
196 
197       rs = stmt.executeQuery();
198       retVal = rs.next();
199       if ( theLog.isDebugEnabled() ) {
200         theLog.debug( "selectByPrimaryKey: retVal == " + retVal );
201       }
202 
203     } catch (Exception ex) {
204         theLog.debug("Exception in ejbFindByPrimaryKey: " + ex);
205     }
206     finally {
207       DBConfiguration.closeSQLResultSet( rs );
208       DBConfiguration.closeSQLStatement( stmt );
209       DBConfiguration.closeSQLConnection( conn );
210       rs = null;
211       stmt = null;
212       conn = null;
213     }
214 
215     if (!retVal) {
216       String msg = "Row for id " + primaryKey + " not found.";
217       throw new ObjectNotFoundException( msg );
218     }
219 
220     return primaryKey;
221   } // ejbFindByPrimaryKey(String)
222 
223   public Collection ejbFindAll()
224     throws FinderException {
225 
226     Connection conn = null;
227     PreparedStatement stmt = null;
228     ResultSet rs = null;
229     Collection retVal = new ArrayList();
230     try {
231       conn = DBConfiguration.getConnection();
232       stmt = conn.prepareStatement(Configuration.SQL_02);
233 
234       rs = stmt.executeQuery();
235 
236       int rowCount = 0;
237       while ( rs.next() ) {
238         String id = rs.getString(1);
239         retVal.add(id);
240         ++rowCount;
241       }
242 
243       if ( theLog.isDebugEnabled() ) {
244         String msg = "selectAll: rowCount == " + rowCount;
245         theLog.debug( msg );
246       }
247 
248     } catch (Exception ex) {
249         theLog.debug("Exception in ejbFindAll: " + ex);
250     }
251 
252     finally {
253       DBConfiguration.closeSQLResultSet( rs );
254       DBConfiguration.closeSQLStatement( stmt );
255       DBConfiguration.closeSQLConnection( conn );
256       rs = null;
257       stmt = null;
258       conn = null;
259     }
260 
261     return retVal;
262   } // ejbFindAll()
263 
264   /** @see delete() */
265   public void ejbRemove() {
266 
267     Connection conn = null;
268     PreparedStatement stmt = null;
269     ResultSet rs = null;
270     try {
271       conn = DBConfiguration.getConnection();
272       stmt = conn.prepareStatement(Configuration.SQL_04);
273 
274       stmt.setString( 1, email_addr);
275 
276       rs = stmt.executeQuery();
277 
278       if ( theLog.isDebugEnabled() ) {
279         theLog.debug( "deleteRow: row " + email_addr );
280       }
281 
282     } catch (Exception ex) {
283         theLog.debug("Exception in ejbRemove: " + ex);
284     }
285 
286     finally {
287       DBConfiguration.closeSQLResultSet( rs );
288       DBConfiguration.closeSQLStatement( stmt );
289       DBConfiguration.closeSQLConnection( conn );
290       rs = null;
291       stmt = null;
292       conn = null;
293     }
294 
295   } // ejbRemove()
296 
297   public void setEntityContext(EntityContext context) {
298 
299     this.context = context;
300     if ( theLog.isDebugEnabled() ) {
301       theLog.debug( "setEntityContext: context set" );
302     }
303 
304   } // setEntityContext(EntityContext)
305 
306   public void unsetEntityContext() {
307 
308     this.context = null;
309     if ( theLog.isDebugEnabled() ) {
310       theLog.debug( "unsetEntityContext: context nulled" );
311     }
312 
313   } // unsetEntityContext()
314 
315   public void ejbActivate() {
316     email_addr = (String)context.getPrimaryKey();
317     if ( theLog.isDebugEnabled() ) {
318       theLog.debug( "ejbActivate: id == " + email_addr );
319     }
320   }
321 
322   public void ejbPassivate() {
323     email_addr = null;
324     if ( theLog.isDebugEnabled() ) {
325       theLog.debug( "ejbPassivate: id nulled" );
326     }
327   }
328 
329   public void ejbLoad() {
330 
331   } // ejbLoad()
332   
333   public void ejbStore() {
334 
335   } // ejbStore()
336 
337   public void ejbPostCreate(String nlast,
338                   String nfirst,
339                   String nmiddle,
340                   String nsuffix,
341                   String mail,
342                   String tel_evening,
343                   String tel_day,
344                   String tel_other,
345                   String addr_str1,
346                   String addr_str2,
347                   String addr_city,
348                   String addr_state,
349                   String addr_zip,
350                   String experience_year,
351                   String recent_year,
352                   Date birth,
353                   String sex,
354                   Date apply_date,
355                   String status) {}
356 
357 } // ApplicantBean
358