KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.ejb.EJBException JavaDoc;
27
28 import org.jboss.ejb.EntityEnterpriseContext;
29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
32 import org.jboss.logging.Logger;
33
34 /**
35  * JDBCStoreEntityCommand updates the row with the new state.
36  * In the event that no field is dirty the command just returns.
37  * Note: read-only fields are never considered dirty.
38  *
39  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
40  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
41  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
42  * @author <a HREF="mailto:shevlandj@kpi.com.au">Joe Shevland</a>
43  * @author <a HREF="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
44  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
45  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
46  * @version $Revision: 37459 $
47  */

48 public final class JDBCStoreEntityCommand
49 {
50    private final JDBCEntityBridge entity;
51    private final JDBCFieldBridge[] primaryKeyFields;
52    private final Logger log;
53
54    public JDBCStoreEntityCommand(JDBCStoreManager manager)
55    {
56       entity = (JDBCEntityBridge) manager.getEntityBridge();
57       primaryKeyFields = entity.getPrimaryKeyFields();
58
59       // Create the Log
60
log = Logger.getLogger(
61          this.getClass().getName() +
62          "." +
63          manager.getMetaData().getName());
64    }
65
66    public void execute(EntityEnterpriseContext ctx)
67    {
68       // scheduled for batch cascade-delete instance should not be updated
69
// because foreign key fields could be updated to null and cascade-delete will fail.
70
JDBCEntityBridge.FieldIterator dirtyIterator = entity.getDirtyIterator(ctx);
71       if(!dirtyIterator.hasNext() || entity.isBeingRemoved(ctx) || entity.isScheduledForBatchCascadeDelete(ctx))
72       {
73          if(log.isTraceEnabled())
74          {
75             log.trace("Store command NOT executed. Entity is not dirty "
76                + ", is being removed or scheduled for *batch* cascade delete: pk=" + ctx.getId());
77          }
78          return;
79       }
80
81       // generate sql
82
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
83       sql.append(SQLUtil.UPDATE)
84          .append(entity.getQualifiedTableName())
85          .append(SQLUtil.SET);
86       SQLUtil.getSetClause(dirtyIterator, sql)
87          .append(SQLUtil.WHERE);
88       SQLUtil.getWhereClause(primaryKeyFields, sql);
89
90       boolean hasLockedFields = entity.hasLockedFields(ctx);
91       JDBCEntityBridge.FieldIterator lockedIterator = null;
92       if(hasLockedFields)
93       {
94          lockedIterator = entity.getLockedIterator(ctx);
95          while(lockedIterator.hasNext())
96          {
97             sql.append(SQLUtil.AND);
98             JDBCCMPFieldBridge field = lockedIterator.next();
99             if(field.getLockedValue(ctx) == null)
100             {
101                SQLUtil.getIsNullClause(false, field, "", sql);
102                lockedIterator.remove();
103             }
104             else
105             {
106                SQLUtil.getWhereClause(field, sql);
107             }
108          }
109       }
110
111       Connection JavaDoc con = null;
112       PreparedStatement JavaDoc ps = null;
113       int rowsAffected = 0;
114       try
115       {
116          // create the statement
117
if(log.isDebugEnabled())
118          {
119             log.debug("Executing SQL: " + sql);
120          }
121
122          // get the connection
123
con = entity.getDataSource().getConnection();
124          ps = con.prepareStatement(sql.toString());
125
126          // SET: set the dirty fields parameters
127
int index = 1;
128          dirtyIterator.reset();
129          while(dirtyIterator.hasNext())
130          {
131             index = dirtyIterator.next().setInstanceParameters(ps, index, ctx);
132          }
133
134          // WHERE: set primary key fields
135
index = entity.setPrimaryKeyParameters(ps, index, ctx.getId());
136
137          // WHERE: set optimistically locked field values
138
if(hasLockedFields)
139          {
140             lockedIterator.reset();
141             while(lockedIterator.hasNext())
142             {
143                JDBCCMPFieldBridge field = lockedIterator.next();
144                Object JavaDoc value = field.getLockedValue(ctx);
145                index = field.setArgumentParameters(ps, index, value);
146             }
147          }
148
149          // execute statement
150
rowsAffected = ps.executeUpdate();
151       }
152       catch(EJBException JavaDoc e)
153       {
154          throw e;
155       }
156       catch(Exception JavaDoc e)
157       {
158          throw new EJBException JavaDoc("Store failed", e);
159       }
160       finally
161       {
162          JDBCUtil.safeClose(ps);
163          JDBCUtil.safeClose(con);
164       }
165
166       // check results
167
if(rowsAffected != 1)
168       {
169          throw new EJBException JavaDoc("Update failed. Expected one affected row: rowsAffected=" +
170             rowsAffected + ", id=" + ctx.getId());
171       }
172
173       // Mark the updated fields as clean.
174
dirtyIterator.reset();
175       while(dirtyIterator.hasNext())
176       {
177          dirtyIterator.next().setClean(ctx);
178       }
179    }
180 }
181
Popular Tags