KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28 import javax.ejb.RemoveException JavaDoc;
29
30 import org.jboss.ejb.EntityEnterpriseContext;
31 import org.jboss.ejb.EntityContainer;
32 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
34 import org.jboss.logging.Logger;
35 import org.jboss.metadata.ConfigurationMetaData;
36 import org.jboss.deployment.DeploymentException;
37
38
39 /**
40  * JDBCRemoveEntityCommand executes a DELETE FROM table WHERE command.
41  *
42  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
43  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
44  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
45  * @author <a HREF="mailto:shevlandj@kpi.com.au">Joe Shevland</a>
46  * @author <a HREF="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
47  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
48  * @version $Revision: 37459 $
49  */

50 public final class JDBCRemoveEntityCommand
51 {
52    private final JDBCStoreManager manager;
53    private final JDBCEntityBridge entity;
54    private final Logger log;
55    private final String JavaDoc removeEntitySQL;
56    private final boolean syncOnCommitOnly;
57    private boolean batchCascadeDelete;
58
59    public JDBCRemoveEntityCommand(JDBCStoreManager manager)
60       throws DeploymentException
61    {
62       this.manager = manager;
63       entity = (JDBCEntityBridge) manager.getEntityBridge();
64
65       // Create the Log
66
log = Logger.getLogger(
67          this.getClass().getName() +
68          "." +
69          manager.getMetaData().getName());
70
71       StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
72       sql.append(SQLUtil.DELETE_FROM)
73          .append(entity.getQualifiedTableName())
74          .append(SQLUtil.WHERE);
75       SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), sql);
76
77       removeEntitySQL = sql.toString();
78       if(log.isDebugEnabled())
79          log.debug("Remove SQL: " + removeEntitySQL);
80
81       ConfigurationMetaData containerConfig = manager.getContainer().
82          getBeanMetaData().getContainerConfiguration();
83       syncOnCommitOnly = containerConfig.getSyncOnCommitOnly();
84
85       JDBCCMRFieldBridge[] cmrFields = (JDBCCMRFieldBridge[]) entity.getCMRFields();
86       for(int i = 0; i < cmrFields.length; ++i)
87       {
88          if(cmrFields[i].isBatchCascadeDelete())
89          {
90             batchCascadeDelete = true;
91             break;
92          }
93       }
94    }
95
96    public void execute(EntityEnterpriseContext ctx)
97       throws RemoveException JavaDoc, RemoteException JavaDoc
98    {
99       if(entity.isRemoved(ctx))
100       {
101          throw new IllegalStateException JavaDoc("Instance was already removed: id=" + ctx.getId());
102       }
103
104       entity.setIsBeingRemoved(ctx);
105
106       // remove entity from all relations
107
Object JavaDoc[] oldRelationsRef = new Object JavaDoc[1];
108       boolean needsSync = entity.removeFromRelations(ctx, oldRelationsRef);
109
110       // update the related entities (stores the removal from relationships)
111
// if one of the store fails an EJBException will be thrown
112
if(!syncOnCommitOnly && needsSync)
113       {
114          EntityContainer.synchronizeEntitiesWithinTransaction(ctx.getTransaction());
115       }
116
117       if(!batchCascadeDelete)
118       {
119          if(!entity.isScheduledForBatchCascadeDelete(ctx))
120          {
121             executeDeleteSQL(ctx);
122          }
123          else
124          {
125             if(log.isTraceEnabled())
126                log.trace("Instance is scheduled for cascade delete. id=" + ctx.getId());
127          }
128       }
129
130       // cascate-delete to old relations, if relation uses cascade.
131
if(oldRelationsRef[0] != null)
132       {
133          Map JavaDoc oldRelations = (Map JavaDoc)oldRelationsRef[0];
134          entity.cascadeDelete(ctx, oldRelations);
135       }
136
137       if(batchCascadeDelete)
138       {
139          if(!entity.isScheduledForBatchCascadeDelete(ctx))
140          {
141             executeDeleteSQL(ctx);
142          }
143          else
144          {
145             if(log.isTraceEnabled())
146                log.debug("Instance is scheduled for cascade delete. id=" + ctx.getId());
147          }
148       }
149
150       entity.setRemoved(ctx);
151       manager.getReadAheadCache().removeCachedData(ctx.getId());
152    }
153
154    private void executeDeleteSQL(EntityEnterpriseContext ctx) throws RemoveException JavaDoc
155    {
156       Object JavaDoc key = ctx.getId();
157       Connection JavaDoc con = null;
158       PreparedStatement JavaDoc ps = null;
159       int rowsAffected = 0;
160       try
161       {
162          if(log.isDebugEnabled())
163             log.debug("Executing SQL: " + removeEntitySQL);
164
165          // get the connection
166
con = entity.getDataSource().getConnection();
167          ps = con.prepareStatement(removeEntitySQL);
168
169          // set the parameters
170
entity.setPrimaryKeyParameters(ps, 1, key);
171
172          // execute statement
173
rowsAffected = ps.executeUpdate();
174       }
175       catch(Exception JavaDoc e)
176       {
177          log.error("Could not remove " + key, e);
178          throw new RemoveException JavaDoc("Could not remove " + key + ": " + e.getMessage());
179       }
180       finally
181       {
182          JDBCUtil.safeClose(ps);
183          JDBCUtil.safeClose(con);
184       }
185
186       // check results
187
if(rowsAffected == 0)
188       {
189          log.error("Could not remove entity " + key);
190          throw new RemoveException JavaDoc("Could not remove entity");
191       }
192
193       if(log.isTraceEnabled())
194          log.trace("Remove: Rows affected = " + rowsAffected);
195    }
196 }
Popular Tags