KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCRelationInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import javax.ejb.EJBException JavaDoc;
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 /**
38  *
39  * The role of this interceptor relationship messages from a related CMR field
40  * and invoke the specified message on this container's cmr field of the
41  * relationship. This interceptor also manages the relation table data.
42  *
43  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
44  * @version $Revision: 37459 $
45  */

46 public final class JDBCRelationInterceptor extends AbstractInterceptor
47 {
48    // Attributes ----------------------------------------------------
49

50    /**
51     * The container of this interceptor.
52     */

53    private EntityContainer container;
54
55    /**
56     * The log.
57     */

58    private Logger log;
59
60    // Static --------------------------------------------------------
61

62    // Constructors --------------------------------------------------
63

64    // Public --------------------------------------------------------
65
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    // Interceptor implementation --------------------------------------
83

84    public Object JavaDoc invoke(Invocation mi) throws Exception JavaDoc
85    {
86       if(!(mi instanceof CMRInvocation))
87          return getNext().invoke(mi);
88
89       CMRMessage relationshipMessage = ((CMRInvocation)mi).getCmrMessage();
90       if(relationshipMessage == null)
91       {
92          // Not a relationship message. Invoke down the chain
93
return getNext().invoke(mi);
94       }
95
96       // We are going to work with the context a lot
97
EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext();
98       JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge)mi.getArguments()[0];
99
100       if(CMRMessage.GET_RELATED_ID == relationshipMessage)
101       {
102          // call getRelateId
103
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          // call addRelation
113
Object JavaDoc 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          // call removeRelation
129
Object JavaDoc 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          // this should not be possible we are using a type safe enum
156
throw new EJBException JavaDoc("Unknown cmp2.0-relationship-message=" +
157             relationshipMessage);
158       }
159    }
160 }
161
162
Popular Tags