1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import javax.ejb.EJBException ; 27 28 import org.jboss.ejb.EntityEnterpriseContext; 29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge; 31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge; 32 import org.jboss.logging.Logger; 33 34 48 public final class JDBCStoreEntityCommand 49 { 50 private final JDBCEntityBridge entity; 51 private final JDBCFieldBridge[] primaryKeyFields; 52 private final Logger log; 53 54 public JDBCStoreEntityCommand(JDBCStoreManager manager) 55 { 56 entity = (JDBCEntityBridge) manager.getEntityBridge(); 57 primaryKeyFields = entity.getPrimaryKeyFields(); 58 59 log = Logger.getLogger( 61 this.getClass().getName() + 62 "." + 63 manager.getMetaData().getName()); 64 } 65 66 public void execute(EntityEnterpriseContext ctx) 67 { 68 JDBCEntityBridge.FieldIterator dirtyIterator = entity.getDirtyIterator(ctx); 71 if(!dirtyIterator.hasNext() || entity.isBeingRemoved(ctx) || entity.isScheduledForBatchCascadeDelete(ctx)) 72 { 73 if(log.isTraceEnabled()) 74 { 75 log.trace("Store command NOT executed. Entity is not dirty " 76 + ", is being removed or scheduled for *batch* cascade delete: pk=" + ctx.getId()); 77 } 78 return; 79 } 80 81 StringBuffer sql = new StringBuffer (200); 83 sql.append(SQLUtil.UPDATE) 84 .append(entity.getQualifiedTableName()) 85 .append(SQLUtil.SET); 86 SQLUtil.getSetClause(dirtyIterator, sql) 87 .append(SQLUtil.WHERE); 88 SQLUtil.getWhereClause(primaryKeyFields, sql); 89 90 boolean hasLockedFields = entity.hasLockedFields(ctx); 91 JDBCEntityBridge.FieldIterator lockedIterator = null; 92 if(hasLockedFields) 93 { 94 lockedIterator = entity.getLockedIterator(ctx); 95 while(lockedIterator.hasNext()) 96 { 97 sql.append(SQLUtil.AND); 98 JDBCCMPFieldBridge field = lockedIterator.next(); 99 if(field.getLockedValue(ctx) == null) 100 { 101 SQLUtil.getIsNullClause(false, field, "", sql); 102 lockedIterator.remove(); 103 } 104 else 105 { 106 SQLUtil.getWhereClause(field, sql); 107 } 108 } 109 } 110 111 Connection con = null; 112 PreparedStatement ps = null; 113 int rowsAffected = 0; 114 try 115 { 116 if(log.isDebugEnabled()) 118 { 119 log.debug("Executing SQL: " + sql); 120 } 121 122 con = entity.getDataSource().getConnection(); 124 ps = con.prepareStatement(sql.toString()); 125 126 int index = 1; 128 dirtyIterator.reset(); 129 while(dirtyIterator.hasNext()) 130 { 131 index = dirtyIterator.next().setInstanceParameters(ps, index, ctx); 132 } 133 134 index = entity.setPrimaryKeyParameters(ps, index, ctx.getId()); 136 137 if(hasLockedFields) 139 { 140 lockedIterator.reset(); 141 while(lockedIterator.hasNext()) 142 { 143 JDBCCMPFieldBridge field = lockedIterator.next(); 144 Object value = field.getLockedValue(ctx); 145 index = field.setArgumentParameters(ps, index, value); 146 } 147 } 148 149 rowsAffected = ps.executeUpdate(); 151 } 152 catch(EJBException e) 153 { 154 throw e; 155 } 156 catch(Exception e) 157 { 158 throw new EJBException ("Store failed", e); 159 } 160 finally 161 { 162 JDBCUtil.safeClose(ps); 163 JDBCUtil.safeClose(con); 164 } 165 166 if(rowsAffected != 1) 168 { 169 throw new EJBException ("Update failed. Expected one affected row: rowsAffected=" + 170 rowsAffected + ", id=" + ctx.getId()); 171 } 172 173 dirtyIterator.reset(); 175 while(dirtyIterator.hasNext()) 176 { 177 dirtyIterator.next().setClean(ctx); 178 } 179 } 180 } 181 | Popular Tags |