KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > bridge > JDBCCMPFieldBridge2


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.jdbc2.bridge;
23
24 import org.jboss.ejb.plugins.cmp.bridge.CMPFieldBridge;
25 import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2;
26 import org.jboss.ejb.plugins.cmp.jdbc2.PersistentContext;
27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData;
28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCType;
29 import org.jboss.ejb.plugins.cmp.jdbc.JDBCEntityPersistenceStore;
30 import org.jboss.ejb.plugins.cmp.jdbc.JDBCResultSetReader;
31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
32 import org.jboss.ejb.EntityEnterpriseContext;
33 import org.jboss.deployment.DeploymentException;
34 import org.jboss.logging.Logger;
35
36 import javax.ejb.EJBException JavaDoc;
37 import java.lang.reflect.Field JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41
42 /**
43  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
44  * @version <tt>$Revision: 37459 $</tt>
45  */

46 public class JDBCCMPFieldBridge2
47    implements CMPFieldBridge, JDBCFieldBridge
48 {
49    private final JDBCEntityBridge2 entity;
50    private final int rowIndex;
51    private final JDBCType jdbcType;
52    private final Class JavaDoc pkClass;
53    private final Field JavaDoc pkField;
54    private final boolean isPrimaryKeyMember;
55    private final String JavaDoc fieldName;
56    private final Class JavaDoc fieldType;
57    private final String JavaDoc columnName;
58
59    private final JDBCCMPFieldBridge2 cmpFieldIAmMappedTo;
60
61    private final Logger log;
62
63    private int versionIndex = -1;
64
65    public JDBCCMPFieldBridge2(JDBCStoreManager2 manager,
66                               JDBCEntityBridge2 entity,
67                               JDBCCMPFieldMetaData metadata,
68                               int rowIndex)
69       throws DeploymentException
70    {
71       this.rowIndex = rowIndex;
72       this.entity = entity;
73       jdbcType = manager.getJDBCTypeFactory().getJDBCType(metadata);
74       pkClass = metadata.getEntity().getPrimaryKeyClass();
75       pkField = metadata.getPrimaryKeyField();
76       isPrimaryKeyMember = metadata.isPrimaryKeyMember();
77       fieldName = metadata.getFieldName();
78       fieldType = metadata.getFieldType();
79       cmpFieldIAmMappedTo = null;
80       columnName = metadata.getColumnName();
81
82       log = Logger.getLogger(this.getClass().getName() + "." + entity.getEntityName() + "#" + getFieldName());
83    }
84
85    public JDBCCMPFieldBridge2(JDBCCMPFieldBridge2 cmpField, JDBCCMPFieldBridge2 relatedPKField)
86    {
87       entity = cmpField.entity;
88       rowIndex = cmpField.rowIndex;
89       jdbcType = cmpField.jdbcType;
90       columnName = cmpField.columnName;
91
92       fieldName = relatedPKField.fieldName;
93       fieldType = relatedPKField.fieldType;
94       pkClass = relatedPKField.pkClass;
95       pkField = relatedPKField.pkField;
96
97       isPrimaryKeyMember = false;
98
99       cmpFieldIAmMappedTo = cmpField;
100
101       log = Logger.getLogger(this.getClass().getName() + "." + entity.getEntityName() + "#" + getFieldName());
102    }
103
104    // Public
105

106    public void initVersion()
107    {
108       versionIndex = entity.getTable().addVersionField();
109    }
110
111    public int getVersionIndex()
112    {
113       return versionIndex;
114    }
115    
116    public String JavaDoc getColumnName()
117    {
118       return columnName;
119    }
120
121    public Object JavaDoc setPrimaryKeyValue(Object JavaDoc primaryKey, Object JavaDoc value)
122       throws IllegalArgumentException JavaDoc
123    {
124       try
125       {
126          if(pkField != null)
127          {
128             // if we are trying to set a null value into a null pk, we are already done.
129
if(value == null && primaryKey == null)
130             {
131                return null;
132             }
133
134             // if we don't have a pk object yet create one
135
if(primaryKey == null)
136             {
137                primaryKey = pkClass.newInstance();
138             }
139
140             // Set this field's value into the primary key object.
141
pkField.set(primaryKey, value);
142             return primaryKey;
143          }
144          else
145          {
146             // This field is the primary key, so no extraction is necessary.
147
return value;
148          }
149       }
150       catch(Exception JavaDoc e)
151       {
152          // Non recoverable internal exception
153
throw new EJBException JavaDoc("Internal error setting instance field " + getFieldName(), e);
154       }
155    }
156
157    public void setValueInternal(EntityEnterpriseContext ctx, Object JavaDoc value, boolean makeDirty)
158    {
159       PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
160
161       // todo this is weird
162
if(cmpFieldIAmMappedTo != null && cmpFieldIAmMappedTo.isPrimaryKeyMember)
163       {
164          Object JavaDoc curValue = pctx.getFieldValue(rowIndex);
165          if(value != null && !value.equals(curValue))
166          {
167             throw new IllegalStateException JavaDoc(
168                "Attempt to modify a primary key field through a foreign key field mapped to it: "
169                +
170                entity.getEntityName()
171                + "."
172                + cmpFieldIAmMappedTo.getFieldName()
173                +
174                " -> "
175                + entity.getQualifiedTableName()
176                + "."
177                + cmpFieldIAmMappedTo.getColumnName() +
178                ", current value=" + curValue + ", new value=" + value
179             );
180          }
181
182          makeDirty = false;
183       }
184       else
185       {
186          pctx.setFieldValue(rowIndex, value);
187       }
188
189       if(makeDirty)
190       {
191          pctx.setDirty();
192       }
193    }
194
195    public int setArgumentParameters(PreparedStatement JavaDoc ps, int parameterIndex, Object JavaDoc arg)
196    {
197       try
198       {
199          int[] jdbcTypes = jdbcType.getJDBCTypes();
200          for(int i = 0; i < jdbcTypes.length; i++)
201          {
202             Object JavaDoc columnValue = jdbcType.getColumnValue(i, arg);
203             jdbcType.getParameterSetter()[i].set(ps, parameterIndex++, jdbcTypes[i], columnValue, log);
204             //JDBCUtil.setParameter(log, ps, parameterIndex++, jdbcTypes[i], columnValue);
205
}
206          return parameterIndex;
207       }
208       catch(SQLException JavaDoc e)
209       {
210          // Non recoverable internal exception
211
throw new EJBException JavaDoc("Internal error setting parameters for field " + getFieldName(), e);
212       }
213    }
214
215    public Object JavaDoc loadArgumentResults(ResultSet JavaDoc rs, int parameterIndex)
216       throws IllegalArgumentException JavaDoc
217    {
218       try
219       {
220          // update the value from the result set
221
Class JavaDoc[] javaTypes = jdbcType.getJavaTypes();
222          if(javaTypes.length > 1)
223          {
224             throw new IllegalStateException JavaDoc("Complex types are not supported yet.");
225          }
226
227          JDBCResultSetReader[] rsReaders = jdbcType.getResultSetReaders();
228
229          Object JavaDoc columnValue = null;
230          for(int i = 0; i < javaTypes.length; i++)
231          {
232             columnValue = rsReaders[i].get(rs, parameterIndex++, javaTypes[i], log);
233             columnValue = jdbcType.setColumnValue(i, null, columnValue);
234          }
235
236          // retrun the updated parameterIndex
237
return columnValue;
238       }
239       catch(SQLException JavaDoc e)
240       {
241          // Non recoverable internal exception
242
throw new EJBException JavaDoc("Internal error getting results for field member " + getFieldName(), e);
243       }
244    }
245
246    public int getRowIndex()
247    {
248       return rowIndex;
249    }
250
251    public JDBCEntityPersistenceStore getManager()
252    {
253       return entity.getManager();
254    }
255
256    // JDBCFieldBridge implementation
257

258    public void initInstance(EntityEnterpriseContext ctx)
259    {
260       Object JavaDoc value;
261       Class JavaDoc fieldType = getFieldType();
262       if(fieldType == boolean.class)
263       {
264          value = Boolean.FALSE;
265       }
266       else if(fieldType == byte.class)
267       {
268          value = new Byte JavaDoc((byte) 0);
269       }
270       else if(fieldType == int.class)
271       {
272          value = new Integer JavaDoc(0);
273       }
274       else if(fieldType == long.class)
275       {
276          value = new Long JavaDoc(0L);
277       }
278       else if(fieldType == short.class)
279       {
280          value = new Short JavaDoc((short) 0);
281       }
282       else if(fieldType == char.class)
283       {
284          value = new Character JavaDoc('\u0000');
285       }
286       else if(fieldType == double.class)
287       {
288          value = new Double JavaDoc(0d);
289       }
290       else if(fieldType == float.class)
291       {
292          value = new Float JavaDoc(0f);
293       }
294       else
295       {
296          value = null;
297       }
298       setValueInternal(ctx, value, false);
299    }
300
301    public void resetPersistenceContext(EntityEnterpriseContext ctx)
302    {
303       throw new UnsupportedOperationException JavaDoc();
304    }
305
306    public int setInstanceParameters(PreparedStatement JavaDoc ps, int parameterIndex, EntityEnterpriseContext ctx)
307    {
308       throw new UnsupportedOperationException JavaDoc();
309    }
310
311    public Object JavaDoc getInstanceValue(EntityEnterpriseContext ctx)
312    {
313       throw new UnsupportedOperationException JavaDoc();
314    }
315
316    public void setInstanceValue(EntityEnterpriseContext ctx, Object JavaDoc value)
317    {
318       throw new UnsupportedOperationException JavaDoc();
319    }
320
321    public int loadInstanceResults(ResultSet JavaDoc rs, int parameterIndex, EntityEnterpriseContext ctx)
322    {
323       throw new UnsupportedOperationException JavaDoc();
324    }
325
326    public int loadArgumentResults(ResultSet JavaDoc rs, int parameterIndex, Object JavaDoc[] argumentRef)
327    {
328       throw new UnsupportedOperationException JavaDoc();
329    }
330
331    public boolean isDirty(EntityEnterpriseContext ctx)
332    {
333       throw new UnsupportedOperationException JavaDoc();
334    }
335
336    public void setClean(EntityEnterpriseContext ctx)
337    {
338       throw new UnsupportedOperationException JavaDoc();
339    }
340
341    public boolean isCMPField()
342    {
343       return true;
344    }
345
346    public boolean isPrimaryKeyMember()
347    {
348       return isPrimaryKeyMember;
349    }
350
351    public boolean isReadOnly()
352    {
353       throw new UnsupportedOperationException JavaDoc();
354    }
355
356    public boolean isReadTimedOut(EntityEnterpriseContext ctx)
357    {
358       throw new UnsupportedOperationException JavaDoc();
359    }
360
361    public boolean isLoaded(EntityEnterpriseContext ctx)
362    {
363       throw new UnsupportedOperationException JavaDoc();
364    }
365
366    public JDBCType getJDBCType()
367    {
368       return jdbcType;
369    }
370
371    public Object JavaDoc getPrimaryKeyValue(Object JavaDoc primaryKey)
372       throws IllegalArgumentException JavaDoc
373    {
374       try
375       {
376          if(pkField != null)
377          {
378             if(primaryKey == null)
379             {
380                return null;
381             }
382
383             return pkField.get(primaryKey);
384          }
385          else
386          {
387             return primaryKey;
388          }
389       }
390       catch(Exception JavaDoc e)
391       {
392          throw new EJBException JavaDoc("Internal error getting primary key field member " + getFieldName(), e);
393       }
394    }
395
396    // CMPFieldBridge implementation
397

398    public String JavaDoc getFieldName()
399    {
400       return fieldName;
401    }
402
403    public Object JavaDoc getValue(EntityEnterpriseContext ctx)
404    {
405       PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
406       return pctx.getFieldValue(rowIndex);
407    }
408
409    public void setValue(EntityEnterpriseContext ctx, Object JavaDoc value)
410    {
411       setValueInternal(ctx, value, true);
412    }
413
414    public Class JavaDoc getFieldType()
415    {
416       return fieldType;
417    }
418 }
419
Popular Tags