Listing 2 [Directory.java] /*===================================================================* * Copyright © 1999-2001 by Bluestone Software, Inc. All rights Reserved. * *====================================================================* * $Archive $ * $Revision: $ * $Date: $ * $Author: $ *====================================================================*/ package com.hp.mw.mobile.samples.voice.airlineDirectory; import java.rmi.RemoteException; /** * Directory is the remote interface for the EJB directory app * * @author Lionel Lavallee */ public interface Directory extends javax.ejb.EJBObject { /** * Gets the number for this object * * @return String representation of the telephone number * * @throws RemoteException */ public String getNumber() throws RemoteException; } [DirectoryBean.java] /*==========================================================================* * Copyright © 1999-2001 by Bluestone Software, Inc. All rights Reserved. * *==========================================================================* * $Archive $ * $Revision: $ * $Date: $ * $Author: $ *==========================================================================*/ package com.hp.mw.mobile.samples.voice.airlineDirectory; import javax.ejb.EntityContext; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.FinderException; import javax.ejb.RemoveException; import java.rmi.RemoteException; import com.bluestone.common.sql.JDBCBean; /** * Directory Entity Bean. Since this class is performing Bean Managed Persistence, * there is more work required. Connections, record sets, etc. all need to be * managed from within this class. We will utilize the Application server for * connection pool management. This class is responsible for creating new, updating * and deleting records. In container managed persistence models (CMP), the Application server * manages these tasks. * * @author Lionel Lavallee */ public class DirectoryBean implements javax.ejb.EntityBean { /** * Datasource name */ private static String JDBC_RESOURCE_NAME = "jdbc/mobile"; /** * table name */ private static String DIRECTORY_TABLE = "DIRECTORY"; /** * column names */ private static String INSERT_COLS = "( ID, NUMBER )"; /** * number of columns in DIRECTORY */ private static int NUM_COLS = 2; /** * EJB Context */ private EntityContext m_ctx; /** * the Airline ID and primary key for the entity */ public int m_id; /** * Airline listing phone number string * "The number for American Airlines is 1.800.etc" */ public String m_number; /** * JDBCbean instance to access the database record for this bean. */ private JDBCBean m_jdbcBean; /** * Creates the bean * * @param id primary key * @param number String of the listing for Text To Speech engines * @param numberURL String of the .wav file location (relative) * * @return DirectoryPK the primary key object * * @throws CreateException */ public DirectoryPK ejbCreate( int id, String number ) throws CreateException { m_id = id; m_number = number; StringBuffer insertCommand = new StringBuffer(); System.out.println( "ejbCreate()" ); insertCommand.append( "INSERT INTO " ).append( DIRECTORY_TABLE ).append( INSERT_COLS ).append( " VALUES (" ); insertCommand.append( m_id + ", " ); insertCommand.append( convertStringToSqlString( m_number + ", " ) ); insertCommand.append( " )" ); m_jdbcBean = getJdbcBean(); m_jdbcBean.setQuery( insertCommand.toString() ); if( m_jdbcBean.executeQuery() ) { return new DirectoryPK( id ); } else { throw new CreateException( "Failed to create new entity" ); } } /** * Does any post creation work - none necessary here. * * @param id * @param number */ public void ejbPostCreate( int id, String number ) { ; } /** * Gets the telephone number for this object * * @return String representation of the listing telephone number for TTS engines. */ public String getNumber() { return m_number; } /** * Sets the context * * @param ctx */ public void setEntityContext( EntityContext ctx ) { m_ctx = ctx; } /** * Removes context */ public void unsetEntityContext() { m_ctx = null; } /** * Call back which tells the bean it's about to be activated. Nothing special * necessary here. */ public void ejbActivate() { ; } /** * Call back which tells the bean it's about to be passivated (removed from service) */ public void ejbPassivate() { ; } /** * Loads the entity from the data source * * @throws RemoteException */ public void ejbLoad() throws RemoteException { try { DirectoryPK key = (DirectoryPK)m_ctx.getPrimaryKey(); loadUsingId( key.m_id ); } catch( FinderException fe ) { throw new RemoteException( fe.toString() ); } } /** * Find the record that matches the specified primary key. * * @param primaryKey The primary key to get. * * @return The primary key of the bean. * * @exception RemoteException System related execptions. * @exception FinderException Failed to find the specified record. */ public DirectoryPK ejbFindByPrimaryKey( DirectoryPK primaryKey ) throws RemoteException, FinderException { loadUsingId( primaryKey.m_id ); return primaryKey; } /** * Updates the entity for any changes that have been made in this context * * @throws EJBException */ public void ejbStore() throws EJBException { StringBuffer updateCommand = new StringBuffer(); updateCommand.append( "UPDATE " ).append( DIRECTORY_TABLE ).append( " SET " ).append( "DIRECTORY.ID" ).append( " = " ).append( m_id ).append( ", " ).append( "DIRECTORY.TELE_NUMBER" ).append( " = " ).append( convertStringToSqlString( m_number ) ).append( ", " ).append( "DIRECTORY.NUMBER_URL" ).append( " = " ).append( convertStringToSqlString( m_numberURL ) ).append( " WHERE " ).append( "DIRECTORY.ID" ).append( " = " ).append( m_id ); m_jdbcBean = getJdbcBean(); m_jdbcBean.setQuery( updateCommand.toString() ); m_jdbcBean.executeQuery(); } /** * Deletes this entity from the datasource * * @throws RemoveException */ public void ejbRemove() throws RemoveException { System.out.println( "ejbRemove()" ); String deleteCommand = "DELETE FROM " + DIRECTORY_TABLE + " WHERE " + "DIRECTORY.ID = " + Integer.toString( m_id ); m_jdbcBean = getJdbcBean(); m_jdbcBean.setQuery( deleteCommand ); m_jdbcBean.executeQuery(); } /** * Get a JDBCBean instance to access the database. * * @return A JDBCBean instance to access the database. */ private JDBCBean getJdbcBean() { if( m_jdbcBean == null ) { m_jdbcBean = new JDBCBean( JDBC_RESOURCE_NAME ); } return m_jdbcBean; } /** * Load data using the database record specified by the airline id. * * * @param id * @exception FinderException Signals error searching for database record. */ private void loadUsingId( int id ) throws FinderException { String idStr = Integer.toString( id ); String sqlCommand = "SELECT * FROM " + DIRECTORY_TABLE + " WHERE DIRECTORY.ID = " + idStr; m_jdbcBean = getJdbcBean(); m_jdbcBean.setQuery( sqlCommand ); if( m_jdbcBean.executeQuery() ) { loadFromJdbcBean( 0 );// Assume only one row is fetched. } else { throw new FinderException( "Cannot find record with Airline id = " + idStr ); } } /** * Load data into the instance variables using the given JDBCBean * instance and the index into the row of items in the JDBCBean * instance. * * @param rowIndex Index into the row of items in the JDBCBean instance. * @exception FinderException Signals a mismatch in the number of columns * in the JDBCBean instance and the expected number of columns. */ private void loadFromJdbcBean( int rowIndex ) throws FinderException { if( ( m_jdbcBean.getNumCols() ) != NUM_COLS ) { throw new FinderException( "Unexpected number of columns in table." ); } int columnIndex = 0; m_id = Integer.parseInt( m_jdbcBean.getItem( rowIndex, columnIndex++ ) ); m_number = m_jdbcBean.getItem( rowIndex, columnIndex++ ); } /** * Convert the given String object to a String object that * can be used in SQL queries i.e surrounded by single quotes * * * @param str * @return a string quoted in single quotes, or, if the input * String object parameter is null, return the string with * the characters "NULL" in it. */ private static String convertStringToSqlString( String str ) { String result; if( str != null ) { result = "'" + str + "'"; } else { result = "NULL"; } return result; } } [DirectoryHome.java] /*==========================================================================* * Copyright © 1999-2001 by Bluestone Software, Inc. All rights Reserved. * *==========================================================================* * $Archive $ * $Revision: $ * $Date: $ * $Author: $ *==========================================================================*/ package com.hp.mw.mobile.samples.voice.airlineDirectory; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.FinderException; /** * Home interface * * @author Lionel Lavallee */ public interface DirectoryHome extends javax.ejb.EJBHome { /** * Creates the entity * * @param id * * @return Directory entity * * @throws CreateException * @throws RemoteException */ public Directory create( int id, String number ) throws CreateException, RemoteException; /** * Finds the Directory entity by primary key * * @param pk the id for this object * * @return Directory entity * * @throws FinderException * @throws RemoteException */ public Directory findByPrimaryKey( DirectoryPK pk ) throws FinderException, RemoteException; } [DirectoryPK.java] /*==========================================================================* * Copyright © 1999-2001 by Bluestone Software, Inc. All rights Reserved. * *==========================================================================* * $Archive $ * $Revision: $ * $Date: $ * $Author: $ *==========================================================================*/ package com.hp.mw.mobile.samples.voice.airlineDirectory; /** * Primary key class for Directory objects * * @author Lionel Lavallee */ public class DirectoryPK implements java.io.Serializable { // ID for the object - its primary key public int m_id; /** * constructor */ public DirectoryPK( int id ) { m_id = id; } /** * Unique hash * * @return */ public int hashCode() { return m_id; } /** * Overide for equals based on hash for this object. * * @param obj * * @return */ public boolean equals( Object obj ) { if( obj instanceof DirectoryPK ) { return ( m_id == ( (DirectoryPK)obj ).m_id ); } return false; } }