KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > keygen > JDBCPkSqlCreateCommand


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.keygen;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30
31 import org.jboss.deployment.DeploymentException;
32 import org.jboss.ejb.EntityEnterpriseContext;
33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
34 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
35 import org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCreateCommand;
36 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
37 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
38
39 /**
40  * Create command that uses an SQL statement to generate the primary key.
41  * Typically used with databases that support sequences.
42  *
43  * @author <a HREF="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
44  *
45  * @version $Revision: 37459 $
46  */

47 public class JDBCPkSqlCreateCommand extends JDBCInsertPKCreateCommand
48 {
49    protected String JavaDoc pkSQL;
50    protected JDBCCMPFieldBridge pkField;
51
52    public void init(JDBCStoreManager manager) throws DeploymentException
53    {
54       super.init(manager);
55       pkField = getGeneratedPKField();
56    }
57
58    protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException
59    {
60       super.initEntityCommand(entityCommand);
61
62       pkSQL = entityCommand.getAttribute("pk-sql");
63       if(pkSQL == null)
64       {
65          throw new DeploymentException("pk-sql attribute must be set for entity " + entity.getEntityName());
66       }
67       if(debug)
68       {
69          log.debug("Generate PK sql is: " + pkSQL);
70       }
71    }
72
73    protected void generateFields(EntityEnterpriseContext ctx) throws CreateException JavaDoc
74    {
75       super.generateFields(ctx);
76
77       Connection JavaDoc con = null;
78       Statement JavaDoc s = null;
79       ResultSet JavaDoc rs = null;
80       try
81       {
82          if(debug)
83          {
84             log.debug("Executing SQL: " + pkSQL);
85          }
86
87          DataSource JavaDoc dataSource = entity.getDataSource();
88          con = dataSource.getConnection();
89          s = con.createStatement();
90
91          rs = s.executeQuery(pkSQL);
92          if(!rs.next())
93          {
94             throw new CreateException JavaDoc("Error fetching next primary key value: result set contains no rows");
95          }
96          pkField.loadInstanceResults(rs, 1, ctx);
97       }
98       catch(SQLException JavaDoc e)
99       {
100          log.error("Error fetching the next primary key value", e);
101          throw new CreateException JavaDoc("Error fetching the next primary key value:" + e);
102       }
103       finally
104       {
105          JDBCUtil.safeClose(rs);
106          JDBCUtil.safeClose(s);
107          JDBCUtil.safeClose(con);
108       }
109    }
110 }
111
Popular Tags