1 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 ; 34 import javax.ejb.DuplicateKeyException ; 35 import java.lang.reflect.Method ; 36 import java.sql.SQLException ; 37 import java.sql.Connection ; 38 import java.sql.ResultSet ; 39 import java.sql.PreparedStatement ; 40 41 45 public class PkSqlCreateCommand 46 implements CreateCommand 47 { 48 private Logger log; 49 private JDBCEntityBridge2 entityBridge; 50 private String 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 execute(Method m, Object [] args, EntityEnterpriseContext ctx) throws CreateException 78 { 79 Object pk; 80 PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext(); 81 if(ctx.getId() == null) 82 { 83 Connection con = null; 84 PreparedStatement ps = null; 85 ResultSet 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 ("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 e) 107 { 108 log.error("Failed to execute pk sql. error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState(), e); 109 throw new CreateException ("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 ("Primary key for created instance is null."); 123 } 124 125 pctx.setPk(pk); 126 } 127 else 128 { 129 try 131 { 132 pctx.flush(); 133 } 134 catch(SQLException e) 135 { 136 if("23000".equals(e.getSQLState())) 137 { 138 throw new DuplicateKeyException ("Unique key violation or invalid foreign key value: pk=" + ctx.getId()); 139 } 140 else 141 { 142 throw new CreateException ("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 |