///////////////////////////////////////////////////////////////////////////////
// File     : EJB_PRODUCTBean.java
// Creation : 2002.04.29 (generated by ejen 1.0.0)

package org.ejb.test;

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.ObjectNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;
import java.util.Collection;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Entity bean class that maps the PRODUCT table.
 * <br><i>Ejen EJB 1.1 (BMP) demonstration</i>
 * @version 1.0
 * @author (unknown)
 * @see org.ejb.test.PRODUCTValues
 * @see org.ejb.test.EJB_PRODUCTHome
 * @see org.ejb.test.EJB_PRODUCT
 * @see org.ejb.test.EJB_PRODUCT_PK
 * @see org.ejb.test.EJB_TABLEAbstractBean
 */
public class EJB_PRODUCTBean extends EJB_TABLEAbstractBean {

    /** INSERT statement used in ejbCreate method (no sequence field, if any) */
    protected static final String SQL_INSERT = "INSERT INTO PRODUCT (NAME,COST) VALUES (?,?)";
    /** SELECT statement used in ejbCreate method (excuted once to find the sequence value) */
    protected static final String SQL_SELECT_SEQUENCE = "SELECT MAX(ID) FROM PRODUCT";
    /** SELECT statement used in ejbLoad method */
    protected static final String SQL_SELECT = "SELECT NAME,COST FROM PRODUCT WHERE ID=?";
    /** UPDATE statement used in ejbStore method */
    protected static final String SQL_UPDATE = "UPDATE PRODUCT SET NAME=?,COST=? WHERE ID=?";
    /** DELETE statement used in ejbRemove method */
    protected static final String SQL_DELETE = "DELETE FROM PRODUCT WHERE ID=?";
    /** SELECT statement used in ejbFindByPrimaryKey method */
    protected static final String SQL_SELECT_FIND_BY_PRIMARY_KEY = "SELECT ID FROM PRODUCT WHERE ID=?";
    /** SELECT statement used in ejbFindAll method */
    protected static final String SQL_SELECT_FIND_ALL = "SELECT ID FROM PRODUCT";

    /** Full JNDI name of the DataSource */
    protected static final String DATA_SOURCE_JNDI_NAME = "java:/DefaultDS";

    /** Values object variable */
    protected PRODUCTValues _values = null;
    /** Autoincremented sequence value (shared by all EJB_PRODUCTBean objects) */
    protected static int _sequence = -1;

    /**
     * Default constructor (empty).
     */
    public EJB_PRODUCTBean() {}

    /**
     * Returns a values object that contains all PRODUCT fields
     *         (except primary key fields).
     * @return object values that contains all PRODUCT fields
     *         (except primary key fields).
     */
    public PRODUCTValues getAll() {
        return _values;
    }

    /**
     * Sets a values object that contains all PRODUCT
     *        fields for update (except primary key fields).
     * @param PRODUCTValues values object that contains all
     *        PRODUCT fields for update (except primary key fields).
     */
    public void setAll(PRODUCTValues values) {
        _values = values;
        _dirty = true;
    }

    /**
     * Creates a new PRODUCT row.
     * @param values a values object that contains values for the
     *        PRODUCT row to be created.
     * @return remote interface of the created entity bean (that maps the new row).
     * @throws javax.ejb.CreateException row count for INSERT != 1
     *                                   or unable to get the sequence value.
     * @throws javax.ejb.EJBException SQL error.
     */
    public EJB_PRODUCT_PK ejbCreate(PRODUCTValues values) throws CreateException {
        _values = values;
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_INSERT);
            pstmt.setString(1, _values.getName());
            pstmt.setBigDecimal(2, _values.getCost());
            if (pstmt.executeUpdate() != 1)
                throw new CreateException("Failed to create new PRODUCT row");
            if (_sequence == -1) {
                _sequence = getSequenceValue(conn, SQL_SELECT_SEQUENCE);
                if (_sequence == -1)
                    throw new CreateException("Failed to get current sequence value from PRODUCT");
            } else
                ++_sequence;
            return new EJB_PRODUCT_PK(_sequence);
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Post-creation method (empty).
     * @param values a values object that contains values for the newly
     *        created PRODUCT row.
     */
    public void ejbPostCreate(PRODUCTValues values) {}

    /**
     * Loads a PRODUCT row.
     * @throws javax.ejb.EJBException SQL error or empty ResultSet.
     */
    public void ejbLoad() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        EJB_PRODUCT_PK pk = (EJB_PRODUCT_PK)(_entityContext.getPrimaryKey());
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT);
            pstmt.setInt(1, pk.getId());
            rs = pstmt.executeQuery();
            if (!rs.next())
                throw new EJBException("Failed to load PRODUCT row with pk:" + pk);
            if (_values == null)
                _values = new PRODUCTValues(rs.getString(1), rs.getBigDecimal(2));
            else {
                _values.setName(rs.getString(1));
                _values.setCost(rs.getBigDecimal(2));
            }
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Updates a PRODUCT row.
     * @throws javax.ejb.EJBException SQL error or row count for UPDATE != 1.
     */
    public void ejbStore() {
        if (!_dirty)
            return;
        Connection conn = null;
        PreparedStatement pstmt = null;
        EJB_PRODUCT_PK pk = (EJB_PRODUCT_PK)(_entityContext.getPrimaryKey());
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_UPDATE);
            pstmt.setString(1, _values.getName());
            pstmt.setBigDecimal(2, _values.getCost());
            pstmt.setInt(3, pk.getId());
            if (pstmt.executeUpdate() != 1)
                throw new EJBException("Failed to update PRODUCT row with pk:" + pk);
            _dirty = false;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Deletes a PRODUCT row.
     * @throws javax.ejb.EJBException SQL error or row count for DELETE != 1.
     */
    public void ejbRemove() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        EJB_PRODUCT_PK pk = (EJB_PRODUCT_PK)(_entityContext.getPrimaryKey());
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_DELETE);
            pstmt.setInt(1, pk.getId());
            if (pstmt.executeUpdate() != 1)
                throw new EJBException("Failed to delete PRODUCT row with pk:" + pk);
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds a single row, provided a primary key.
     * @param pk the primary key that indentifies the row to retrieve.
     * @return the primary key (same as parameter pk).
     * @throws javax.ejb.ObjectNotFoundException no row with this primary key.
     * @throws javax.ejb.EJBException SQL error.
     */
    public EJB_PRODUCT_PK ejbFindByPrimaryKey(EJB_PRODUCT_PK pk) throws ObjectNotFoundException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_BY_PRIMARY_KEY);
            pstmt.setInt(1, pk.getId());
            rs = pstmt.executeQuery();
            if (!rs.next())
                throw new ObjectNotFoundException("No PRODUCT row with pk:" + pk);
            return pk;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by id.
     * @param id value of the ID column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindById(int id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE ID=?");
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_PRODUCT_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by name.
     * @param name value of the NAME column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindByName(java.lang.String name) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE NAME=?");
            pstmt.setString(1, name);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_PRODUCT_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by cost.
     * @param cost value of the COST column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindByCost(java.math.BigDecimal cost) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE COST=?");
            pstmt.setBigDecimal(1, cost);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_PRODUCT_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds all rows in the PRODUCT table.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_PRODUCT_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Returns the full DataSource JNDI name.
     * @return the JNDI name.
     */
    protected String getDataSourceJNDIName() {
        return DATA_SOURCE_JNDI_NAME;
    }
}

EJB_PRODUCTBean.html (HTML view generated by ejen v.1.0.0).