KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > PkSqlCreateCommand


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.jdbc2;
23
24 import org.jboss.ejb.EntityEnterpriseContext;
25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
26 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2;
27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
30 import org.jboss.logging.Logger;
31 import org.jboss.deployment.DeploymentException;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.DuplicateKeyException JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Connection JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40
41 /**
42  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
43  * @version <tt>$Revision: 37459 $</tt>
44  */

45 public class PkSqlCreateCommand
46    implements CreateCommand
47 {
48    private Logger log;
49    private JDBCEntityBridge2 entityBridge;
50    private String JavaDoc pkSql;
51    private JDBCCMPFieldBridge2 pkField;
52
53    public void init(JDBCStoreManager2 manager) throws DeploymentException
54    {
55       this.entityBridge = (JDBCEntityBridge2) manager.getEntityBridge();
56       log = Logger.getLogger(getClass().getName() + "." + entityBridge.getEntityName());
57
58       final JDBCFieldBridge[] pkFields = entityBridge.getPrimaryKeyFields();
59       if(pkFields.length > 1)
60       {
61          throw new DeploymentException("This entity-command cannot be used with composite primary keys!");
62       }
63       this.pkField = (JDBCCMPFieldBridge2) pkFields[0];
64
65       JDBCEntityCommandMetaData metadata = entityBridge.getMetaData().getEntityCommand();
66       pkSql = metadata.getAttribute("pk-sql");
67       if(pkSql == null)
68       {
69          throw new DeploymentException("pk-sql attribute must be set for entity " + entityBridge.getEntityName());
70       }
71       if(log.isDebugEnabled())
72       {
73          log.debug("entity-command generate pk sql: " + pkSql);
74       }
75    }
76
77    public Object JavaDoc execute(Method JavaDoc m, Object JavaDoc[] args, EntityEnterpriseContext ctx) throws CreateException JavaDoc
78    {
79       Object JavaDoc pk;
80       PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
81       if(ctx.getId() == null)
82       {
83          Connection JavaDoc con = null;
84          PreparedStatement JavaDoc ps = null;
85          ResultSet JavaDoc rs = null;
86          try
87          {
88             if(log.isDebugEnabled())
89             {
90                log.debug("executing sql: " + pkSql);
91             }
92
93             con = entityBridge.getDataSource().getConnection();
94             ps = con.prepareStatement(pkSql);
95             rs = ps.executeQuery();
96
97             if(!rs.next())
98             {
99                throw new CreateException JavaDoc("pk-sql " + pkSql + " returned no results!");
100             }
101
102             pk = pkField.loadArgumentResults(rs, 1);
103             pctx.setFieldValue(pkField.getRowIndex(), pk);
104             pk = entityBridge.extractPrimaryKeyFromInstance(ctx);
105          }
106          catch(SQLException JavaDoc e)
107          {
108             log.error("Failed to execute pk sql. error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState(), e);
109             throw new CreateException JavaDoc("Failed to execute pk sql: " + e.getMessage() +
110                ", error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState());
111          }
112          finally
113          {
114             JDBCUtil.safeClose(rs);
115             JDBCUtil.safeClose(ps);
116             JDBCUtil.safeClose(con);
117          }
118
119          if(pk == null)
120          {
121             log.error("Primary key for created instance is null.");
122             throw new CreateException JavaDoc("Primary key for created instance is null.");
123          }
124
125          pctx.setPk(pk);
126       }
127       else
128       {
129          // insert-after-ejb-post-create
130
try
131          {
132             pctx.flush();
133          }
134          catch(SQLException JavaDoc e)
135          {
136             if("23000".equals(e.getSQLState()))
137             {
138                throw new DuplicateKeyException JavaDoc("Unique key violation or invalid foreign key value: pk=" + ctx.getId());
139             }
140             else
141             {
142                throw new CreateException JavaDoc("Failed to create instance: pk=" +
143                   ctx.getId() +
144                   ", state=" +
145                   e.getSQLState() +
146                   ", msg=" + e.getMessage());
147             }
148          }
149          pk = ctx.getId();
150       }
151       return pk;
152    }
153 }
154
Popular Tags