KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCInsertPKCreateCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.DuplicateKeyException JavaDoc;
30
31 import org.jboss.ejb.EntityEnterpriseContext;
32 import org.jboss.deployment.DeploymentException;
33
34 /**
35  * Base class for create commands that actually insert the primary key value.
36  * If an exception processor is not supplied, this command will perform an
37  * additional query to determine if a DuplicateKeyException should be thrown.
38  *
39  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
40  */

41 public abstract class JDBCInsertPKCreateCommand extends JDBCAbstractCreateCommand
42 {
43    protected String JavaDoc existsSQL;
44
45    public void init(JDBCStoreManager manager) throws DeploymentException
46    {
47       super.init(manager);
48
49       // if no exception processor is defined, we will perform a existance
50
// check before trying the insert to report duplicate key
51
if(exceptionProcessor == null)
52       {
53          initExistsSQL();
54       }
55    }
56
57    protected void initExistsSQL()
58    {
59       StringBuffer JavaDoc sql = new StringBuffer JavaDoc(300);
60       sql.append(SQLUtil.SELECT).append("COUNT(*)").append(SQLUtil.FROM)
61          .append(entity.getQualifiedTableName())
62          .append(SQLUtil.WHERE);
63       SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), sql);
64       existsSQL = sql.toString();
65       if(debug)
66       {
67          log.debug("Entity Exists SQL: " + existsSQL);
68       }
69    }
70
71    protected void beforeInsert(EntityEnterpriseContext ctx) throws CreateException JavaDoc
72    {
73       // are we checking existance by query?
74
if(existsSQL != null)
75       {
76          Connection JavaDoc c = null;
77          PreparedStatement JavaDoc ps = null;
78          ResultSet JavaDoc rs = null;
79          try
80          {
81             if(debug)
82                log.debug("Executing SQL: " + existsSQL);
83
84             c = entity.getDataSource().getConnection();
85             ps = c.prepareStatement(existsSQL);
86
87             // bind PK
88
// @todo add a method to EntityBridge that binds pk fields directly
89
Object JavaDoc pk = entity.extractPrimaryKeyFromInstance(ctx);
90             entity.setPrimaryKeyParameters(ps, 1, pk);
91
92             rs = ps.executeQuery();
93             if(!rs.next())
94             {
95                throw new CreateException JavaDoc("Error checking if entity with primary pk " + pk + "exists: SQL returned no rows");
96             }
97             if(rs.getInt(1) > 0)
98             {
99                throw new DuplicateKeyException JavaDoc("Entity with primary key " + pk + " already exists");
100             }
101          }
102          catch(SQLException JavaDoc e)
103          {
104             log.error("Error checking if entity exists", e);
105             throw new CreateException JavaDoc("Error checking if entity exists:" + e);
106          }
107          finally
108          {
109             JDBCUtil.safeClose(rs);
110             JDBCUtil.safeClose(ps);
111             JDBCUtil.safeClose(c);
112          }
113       }
114    }
115 }
116
Popular Tags