001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc;
023:
024: import javax.ejb.EJBException;
025:
026: import org.jboss.ejb.Container;
027: import org.jboss.ejb.EntityContainer;
028: import org.jboss.ejb.EntityEnterpriseContext;
029: import org.jboss.invocation.Invocation;
030: import org.jboss.ejb.plugins.AbstractInterceptor;
031: import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage;
032: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
033: import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRInvocation;
034: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
035: import org.jboss.logging.Logger;
036:
037: /**
038: *
039: * The role of this interceptor relationship messages from a related CMR field
040: * and invoke the specified message on this container's cmr field of the
041: * relationship. This interceptor also manages the relation table data.
042: *
043: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
044: * @version $Revision: 57209 $
045: */
046: public final class JDBCRelationInterceptor extends AbstractInterceptor {
047: // Attributes ----------------------------------------------------
048:
049: /**
050: * The container of this interceptor.
051: */
052: private EntityContainer container;
053:
054: /**
055: * The log.
056: */
057: private Logger log;
058:
059: // Static --------------------------------------------------------
060:
061: // Constructors --------------------------------------------------
062:
063: // Public --------------------------------------------------------
064: public void setContainer(Container container) {
065: this .container = (EntityContainer) container;
066: if (container != null) {
067: log = Logger.getLogger(this .getClass().getName() + "."
068: + container.getBeanMetaData().getEjbName());
069: }
070: }
071:
072: public Container getContainer() {
073: return container;
074: }
075:
076: // Interceptor implementation --------------------------------------
077:
078: public Object invoke(Invocation mi) throws Exception {
079: if (!(mi instanceof CMRInvocation))
080: return getNext().invoke(mi);
081:
082: CMRMessage relationshipMessage = ((CMRInvocation) mi)
083: .getCmrMessage();
084: if (relationshipMessage == null) {
085: // Not a relationship message. Invoke down the chain
086: return getNext().invoke(mi);
087: }
088:
089: // We are going to work with the context a lot
090: EntityEnterpriseContext ctx = (EntityEnterpriseContext) mi
091: .getEnterpriseContext();
092: JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge) mi
093: .getArguments()[0];
094:
095: if (CMRMessage.GET_RELATED_ID == relationshipMessage) {
096: // call getRelateId
097: if (log.isTraceEnabled()) {
098: log.trace("Getting related id: field="
099: + cmrField.getFieldName() + " id="
100: + ctx.getId());
101: }
102: return cmrField.getRelatedId(ctx);
103:
104: } else if (CMRMessage.ADD_RELATION == relationshipMessage) {
105: // call addRelation
106: Object relatedId = mi.getArguments()[1];
107: if (log.isTraceEnabled()) {
108: log.trace("Add relation: field="
109: + cmrField.getFieldName() + " id="
110: + ctx.getId() + " relatedId=" + relatedId);
111: }
112:
113: cmrField.addRelation(ctx, relatedId);
114:
115: return null;
116:
117: } else if (CMRMessage.REMOVE_RELATION == relationshipMessage) {
118: // call removeRelation
119: Object relatedId = mi.getArguments()[1];
120: if (log.isTraceEnabled()) {
121: log.trace("Remove relation: field="
122: + cmrField.getFieldName() + " id="
123: + ctx.getId() + " relatedId=" + relatedId);
124: }
125:
126: cmrField.removeRelation(ctx, relatedId);
127:
128: return null;
129: } else if (CMRMessage.SCHEDULE_FOR_CASCADE_DELETE == relationshipMessage) {
130: JDBCEntityBridge entity = (JDBCEntityBridge) cmrField
131: .getEntity();
132: entity.scheduleForCascadeDelete(ctx);
133: return null;
134: } else if (CMRMessage.SCHEDULE_FOR_BATCH_CASCADE_DELETE == relationshipMessage) {
135: JDBCEntityBridge entity = (JDBCEntityBridge) cmrField
136: .getEntity();
137: entity.scheduleForBatchCascadeDelete(ctx);
138: return null;
139: } else {
140: // this should not be possible we are using a type safe enum
141: throw new EJBException(
142: "Unknown cmp2.0-relationship-message="
143: + relationshipMessage);
144: }
145: }
146: }
|