1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.lang.reflect.Method ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 28 import org.jboss.ejb.EntityEnterpriseContext; 29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge; 30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge; 32 33 41 public final class JDBCPostCreateEntityCommand 42 { 43 private final JDBCEntityBridge entity; 45 private final JDBCCMRFieldBridge[] cmrWithFKMappedToCMP; 46 47 public JDBCPostCreateEntityCommand(JDBCStoreManager manager) 49 { 50 entity = (JDBCEntityBridge) manager.getEntityBridge(); 51 JDBCFieldBridge[] cmrFields = entity.getCMRFields(); 52 List fkToCMPList = new ArrayList (4); 53 for(int i = 0; i < cmrFields.length; ++i) 54 { 55 JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge)cmrFields[i]; 56 JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge)cmrField.getRelatedCMRField(); 57 if(cmrField.hasFKFieldsMappedToCMPFields() 58 || relatedCMRField.hasFKFieldsMappedToCMPFields()) 59 { 60 fkToCMPList.add(cmrField); 61 } 62 } 63 if(fkToCMPList.isEmpty()) 64 cmrWithFKMappedToCMP = null; 65 else 66 cmrWithFKMappedToCMP = (JDBCCMRFieldBridge[])fkToCMPList 67 .toArray(new JDBCCMRFieldBridge[fkToCMPList.size()]); 68 } 69 70 public Object execute(Method m, Object [] args, EntityEnterpriseContext ctx) 72 { 73 if(cmrWithFKMappedToCMP == null) 74 return null; 75 76 for(int i = 0; i < cmrWithFKMappedToCMP.length; ++i) 77 { 78 JDBCCMRFieldBridge cmrField = cmrWithFKMappedToCMP[i]; 79 JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge)cmrField.getRelatedCMRField(); 80 if(cmrField.hasFKFieldsMappedToCMPFields()) 81 { 82 Object relatedId = cmrField.getRelatedIdFromContext(ctx); 83 if(relatedId != null) 84 { 85 try 86 { 87 if(cmrField.isForeignKeyValid(relatedId)) 88 { 89 cmrField.createRelationLinks(ctx, relatedId); 90 } 91 else 92 { 93 relatedCMRField.addRelatedPKWaitingForMyPK(relatedId, ctx.getId()); 94 } 95 } 96 catch(Exception e) 97 { 98 } 100 } 101 } 102 else if(relatedCMRField.hasFKFieldsMappedToCMPFields()) 103 { 104 cmrField.addRelatedPKsWaitedForMe(ctx); 105 } 106 } 107 return null; 108 } 109 } 110 | Popular Tags |