001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.SQLException;
027: import java.sql.ResultSet;
028: import javax.ejb.CreateException;
029: import javax.ejb.DuplicateKeyException;
030:
031: import org.jboss.ejb.EntityEnterpriseContext;
032: import org.jboss.deployment.DeploymentException;
033:
034: /**
035: * Base class for create commands that actually insert the primary key value.
036: * If an exception processor is not supplied, this command will perform an
037: * additional query to determine if a DuplicateKeyException should be thrown.
038: *
039: * @author <a href="mailto:jeremy@boynes.com">Jeremy Boynes</a>
040: */
041: public abstract class JDBCInsertPKCreateCommand extends
042: JDBCAbstractCreateCommand {
043: protected String existsSQL;
044:
045: public void init(JDBCStoreManager manager)
046: throws DeploymentException {
047: super .init(manager);
048:
049: // if no exception processor is defined, we will perform a existance
050: // check before trying the insert to report duplicate key
051: if (exceptionProcessor == null) {
052: initExistsSQL();
053: }
054: }
055:
056: protected void initExistsSQL() {
057: StringBuffer sql = new StringBuffer(300);
058: sql.append(SQLUtil.SELECT).append("COUNT(*)").append(
059: SQLUtil.FROM).append(entity.getQualifiedTableName())
060: .append(SQLUtil.WHERE);
061: SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), sql);
062: existsSQL = sql.toString();
063: if (debug) {
064: log.debug("Entity Exists SQL: " + existsSQL);
065: }
066: }
067:
068: protected void beforeInsert(EntityEnterpriseContext ctx)
069: throws CreateException {
070: // are we checking existance by query?
071: if (existsSQL != null) {
072: Connection c = null;
073: PreparedStatement ps = null;
074: ResultSet rs = null;
075: try {
076: if (debug)
077: log.debug("Executing SQL: " + existsSQL);
078:
079: c = entity.getDataSource().getConnection();
080: ps = c.prepareStatement(existsSQL);
081:
082: // bind PK
083: // @todo add a method to EntityBridge that binds pk fields directly
084: Object pk = entity.extractPrimaryKeyFromInstance(ctx);
085: entity.setPrimaryKeyParameters(ps, 1, pk);
086:
087: rs = ps.executeQuery();
088: if (!rs.next()) {
089: throw new CreateException(
090: "Error checking if entity with primary pk "
091: + pk
092: + "exists: SQL returned no rows");
093: }
094: if (rs.getInt(1) > 0) {
095: throw new DuplicateKeyException(
096: "Entity with primary key " + pk
097: + " already exists");
098: }
099: } catch (SQLException e) {
100: log.error("Error checking if entity exists", e);
101: throw new CreateException(
102: "Error checking if entity exists:" + e);
103: } finally {
104: JDBCUtil.safeClose(rs);
105: JDBCUtil.safeClose(ps);
106: JDBCUtil.safeClose(c);
107: }
108: }
109: }
110: }
|