package org.jboss.ejb.plugins.cmp.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import org.jboss.ejb.EntityEnterpriseContext;
import org.jboss.deployment.DeploymentException;
public abstract class JDBCInsertPKCreateCommand extends JDBCAbstractCreateCommand
{
protected String existsSQL;
public void init(JDBCStoreManager manager) throws DeploymentException
{
super.init(manager);
if(exceptionProcessor == null)
{
initExistsSQL();
}
}
protected void initExistsSQL()
{
StringBuffer sql = new StringBuffer(300);
sql.append(SQLUtil.SELECT).append("COUNT(*)").append(SQLUtil.FROM)
.append(entity.getQualifiedTableName())
.append(SQLUtil.WHERE);
SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), sql);
existsSQL = sql.toString();
if(debug)
{
log.debug("Entity Exists SQL: " + existsSQL);
}
}
protected void beforeInsert(EntityEnterpriseContext ctx) throws CreateException
{
if(existsSQL != null)
{
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
if(debug)
log.debug("Executing SQL: " + existsSQL);
c = entity.getDataSource().getConnection();
ps = c.prepareStatement(existsSQL);
Object pk = entity.extractPrimaryKeyFromInstance(ctx);
entity.setPrimaryKeyParameters(ps, 1, pk);
rs = ps.executeQuery();
if(!rs.next())
{
throw new CreateException("Error checking if entity with primary pk " + pk + "exists: SQL returned no rows");
}
if(rs.getInt(1) > 0)
{
throw new DuplicateKeyException("Entity with primary key " + pk + " already exists");
}
}
catch(SQLException e)
{
log.error("Error checking if entity exists", e);
throw new CreateException("Error checking if entity exists:" + e);
}
finally
{
JDBCUtil.safeClose(rs);
JDBCUtil.safeClose(ps);
JDBCUtil.safeClose(c);
}
}
}
}