1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import javax.ejb.EJBException ; 25 26 import org.jboss.ejb.Container; 27 import org.jboss.ejb.EntityContainer; 28 import org.jboss.ejb.EntityEnterpriseContext; 29 import org.jboss.invocation.Invocation; 30 import org.jboss.ejb.plugins.AbstractInterceptor; 31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage; 32 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge; 33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRInvocation; 34 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 35 import org.jboss.logging.Logger; 36 37 46 public final class JDBCRelationInterceptor extends AbstractInterceptor 47 { 48 50 53 private EntityContainer container; 54 55 58 private Logger log; 59 60 62 64 public void setContainer(Container container) 66 { 67 this.container = (EntityContainer)container; 68 if(container != null) 69 { 70 log = Logger.getLogger( 71 this.getClass().getName() + 72 "." + 73 container.getBeanMetaData().getEjbName()); 74 } 75 } 76 77 public Container getContainer() 78 { 79 return container; 80 } 81 82 84 public Object invoke(Invocation mi) throws Exception 85 { 86 if(!(mi instanceof CMRInvocation)) 87 return getNext().invoke(mi); 88 89 CMRMessage relationshipMessage = ((CMRInvocation)mi).getCmrMessage(); 90 if(relationshipMessage == null) 91 { 92 return getNext().invoke(mi); 94 } 95 96 EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext(); 98 JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge)mi.getArguments()[0]; 99 100 if(CMRMessage.GET_RELATED_ID == relationshipMessage) 101 { 102 if(log.isTraceEnabled()) 104 { 105 log.trace("Getting related id: field=" + cmrField.getFieldName() + " id=" + ctx.getId()); 106 } 107 return cmrField.getRelatedId(ctx); 108 109 } 110 else if(CMRMessage.ADD_RELATION == relationshipMessage) 111 { 112 Object relatedId = mi.getArguments()[1]; 114 if(log.isTraceEnabled()) 115 { 116 log.trace("Add relation: field=" + cmrField.getFieldName() + 117 " id=" + ctx.getId() + 118 " relatedId=" + relatedId); 119 } 120 121 cmrField.addRelation(ctx, relatedId); 122 123 return null; 124 125 } 126 else if(CMRMessage.REMOVE_RELATION == relationshipMessage) 127 { 128 Object relatedId = mi.getArguments()[1]; 130 if(log.isTraceEnabled()) 131 { 132 log.trace("Remove relation: field=" + cmrField.getFieldName() + 133 " id=" + ctx.getId() + 134 " relatedId=" + relatedId); 135 } 136 137 cmrField.removeRelation(ctx, relatedId); 138 139 return null; 140 } 141 else if(CMRMessage.SCHEDULE_FOR_CASCADE_DELETE == relationshipMessage) 142 { 143 JDBCEntityBridge entity = (JDBCEntityBridge)cmrField.getEntity(); 144 entity.scheduleForCascadeDelete(ctx); 145 return null; 146 } 147 else if(CMRMessage.SCHEDULE_FOR_BATCH_CASCADE_DELETE == relationshipMessage) 148 { 149 JDBCEntityBridge entity = (JDBCEntityBridge)cmrField.getEntity(); 150 entity.scheduleForBatchCascadeDelete(ctx); 151 return null; 152 } 153 else 154 { 155 throw new EJBException ("Unknown cmp2.0-relationship-message=" + 157 relationshipMessage); 158 } 159 } 160 } 161 162 | Popular Tags |