KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > genclass > RdbGenClassNameGetter


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.rdb.genclass;
25
26 import org.objectweb.jorm.api.PExceptionIO;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
29 import org.objectweb.jorm.naming.api.PNameGetter;
30 import org.objectweb.jorm.genclass.api.FieldDesc;
31 import org.objectweb.jorm.type.api.PExceptionTyping;
32 import org.objectweb.jorm.type.api.PType;
33
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.math.BigInteger JavaDoc;
39 import java.math.BigDecimal JavaDoc;
40
41 /**
42  * This is a class that implements the PNameGetter interface and that provides
43  * a generic access based on the description of fields of the element of the
44  * relevant generic class instance.
45  * @author P. D?chamboux
46  */

47
48 class RdbGenClassNameGetter implements PNameGetter {
49     /**
50      * The ResultSet pointing to the row from which to fetch the value of the
51      * fields of a composite name.
52      */

53     protected ResultSet JavaDoc resultSet;
54     /**
55      * The description of the fields of a composite name.
56      */

57     protected FieldDesc[] elemFields;
58
59     protected PreparedStatement JavaDoc ps;
60     
61     protected RdbAdapter adapter;
62     
63     protected int startIndex = 0;
64
65     /**
66      * Constructs a getter that enables to retrieve the values of the fields
67      * of a composite name.
68      */

69     RdbGenClassNameGetter(PreparedStatement JavaDoc ps,
70             ResultSet JavaDoc rs,
71             FieldDesc[] rfds,
72             int startIndex,
73             RdbAdapter adapter) {
74         resultSet = rs;
75         elemFields = rfds;
76         this.ps = ps;
77         this.startIndex = startIndex;
78         this.adapter = adapter;
79     }
80
81     protected void finalize() throws Throwable JavaDoc {
82         if (resultSet != null)
83             resultSet.close();
84         if (ps != null)
85             ps.close();
86         super.finalize();
87     }
88
89     /**
90      * It retrieves the value of a byte field.
91      * @param fn A String containing the name of the field.
92      * @param ctx A context used by PNameGetter to extract the related data.
93      * @return The byte value of the field as stored within the composite
94      * name.
95      * @exception PExceptionTyping There is no field with this name and
96      * this type within this composite name.
97      */

98     public byte pngetByteField(String JavaDoc fn, Object JavaDoc ctx)
99             throws PExceptionTyping, PExceptionIO {
100         try {
101             for (short i = 1; i < elemFields.length; i++)
102                 if (fn.equals(elemFields[i].compositeName))
103                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_BYTE)
104                         return adapter.getByte(resultSet, startIndex + i, (byte) -1);
105                     else
106                         break;
107         } catch (SQLException JavaDoc e) {
108             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
109         }
110         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
111     }
112
113     /**
114      * It retrieves the value of a Byte field.
115      * @param fn A String containing the name of the field.
116      * @return The Byte value of the field as stored within the composite
117      * name.
118      * @exception PExceptionTyping There is no field with this name and
119      * this type within this composite name.
120      * @exception PExceptionIO It is raised when a problem occurs while
121      * potentially accessing the DSI.
122      */

123     public Byte JavaDoc pngetObyteField(String JavaDoc fn, Object JavaDoc context) throws PExceptionTyping, PExceptionIO {
124         try {
125             for (short i = 1; i < elemFields.length; i++)
126                 if (fn.equals(elemFields[i].compositeName))
127                     if (elemFields[i].type.getTypeCode()
128                             == PType.TYPECODE_OBJBYTE) {
129                         return adapter.getObyte(resultSet, startIndex + i, null);
130                     } else
131                         break;
132         } catch (SQLException JavaDoc e) {
133             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
134         }
135         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
136     }
137
138     /**
139      * It retrieves the value of a char field.
140      * @param fn A String containing the name of the field.
141      * @param ctx A context used by PNameGetter to extract the related data.
142      * @return The char value of the field as stored within the composite
143      * name.
144      * @exception PExceptionTyping There is no field with this name and
145      * this type within this composite name.
146      */

147     public char pngetCharField(String JavaDoc fn, Object JavaDoc ctx)
148             throws PExceptionTyping, PExceptionIO {
149         try {
150             for (short i = 1; i < elemFields.length; i++)
151                 if (fn.equals(elemFields[i].compositeName))
152                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_CHAR)
153                         return adapter.getChar(resultSet, startIndex + i, (char) -1);
154                     else
155                         break;
156         } catch (SQLException JavaDoc e) {
157             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
158         }
159         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
160     }
161
162     /**
163      * It retrieves the value of a Character field.
164      * @param fn A String containing the name of the field.
165      * @return The Character value of the field as stored within the composite
166      * name.
167      * @exception PExceptionTyping There is no field with this name and
168      * this type within this composite name.
169      * @exception PExceptionIO It is raised when a problem occurs while
170      * potentially accessing the DSI.
171      */

172     public Character JavaDoc pngetOcharField(String JavaDoc fn, Object JavaDoc context) throws PExceptionTyping, PExceptionIO {
173         try {
174             for (short i = 1; i < elemFields.length; i++)
175                 if (fn.equals(elemFields[i].compositeName))
176                     if (elemFields[i].type.getTypeCode()
177                             == PType.TYPECODE_OBJCHAR) {
178                         return adapter.getOchar(resultSet, startIndex + i, null);
179                     } else
180                         break;
181         } catch (SQLException JavaDoc e) {
182             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
183         }
184         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
185     }
186
187     /**
188      * It retrieves the value of a short field.
189      * @param fn A String containing the name of the field.
190      * @param ctx A context used by PNameGetter to extract the related data.
191      * @return The short value of the field as stored within the composite
192      * name.
193      * @exception PExceptionTyping There is no field with this name and
194      * this type within this composite name.
195      */

196     public short pngetShortField(String JavaDoc fn, Object JavaDoc ctx)
197             throws PExceptionTyping, PExceptionIO {
198         try {
199             for (short i = 1; i < elemFields.length; i++)
200                 if (fn.equals(elemFields[i].compositeName))
201                     if (elemFields[i].type.getTypeCode()
202                             == PType.TYPECODE_SHORT)
203                         return adapter.getShort(resultSet, startIndex + i, (short) -1);
204                     else
205                         break;
206         } catch (SQLException JavaDoc e) {
207             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
208         }
209         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
210     }
211
212     /**
213      * It retrieves the value of a Short field.
214      * @param fn A String containing the name of the field.
215      * @return The Short value of the field as stored within the composite
216      * name.
217      * @exception PExceptionTyping There is no field with this name and
218      * this type within this composite name.
219      * @exception PExceptionIO It is raised when a problem occurs while
220      * potentially accessing the DSI.
221      */

222     public Short JavaDoc pngetOshortField(String JavaDoc fn, Object JavaDoc context) throws PExceptionTyping, PExceptionIO {
223         try {
224             for (short i = 1; i < elemFields.length; i++)
225                 if (fn.equals(elemFields[i].compositeName))
226                     if (elemFields[i].type.getTypeCode()
227                             == PType.TYPECODE_OBJSHORT) {
228                         return adapter.getOshort(resultSet, startIndex + i, null);
229                     } else
230                         break;
231         } catch (SQLException JavaDoc e) {
232             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
233         }
234         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
235     }
236
237     /**
238      * It retrieves the value of an int field.
239      * @param fn A String containing the name of the field.
240      * @param ctx A context used by PNameGetter to extract the related data.
241      * @return The int value of the field as stored within the composite
242      * name.
243      * @exception PExceptionTyping There is no field with this name and
244      * this type within this composite name.
245      */

246     public int pngetIntField(String JavaDoc fn, Object JavaDoc ctx)
247             throws PExceptionTyping, PExceptionIO {
248         try {
249             for (short i = 1; i < elemFields.length; i++)
250                 if (fn.equals(elemFields[i].compositeName))
251                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_INT)
252                         return adapter.getInt(resultSet, startIndex + i, -1);
253                     else
254                         break;
255         } catch (SQLException JavaDoc e) {
256             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
257         }
258         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
259     }
260
261     /**
262      * It retrieves the value of an Integer field.
263      * @param fn A String containing the name of the field.
264      * @return The Integer value of the field as stored within the composite
265      * name.
266      * @exception PExceptionTyping There is no field with this name and
267      * this type within this composite name.
268      * @exception PExceptionIO It is raised when a problem occurs while
269      * potentially accessing the DSI.
270      */

271     public Integer JavaDoc pngetOintField(String JavaDoc fn, Object JavaDoc ctxt) throws PExceptionTyping, PExceptionIO {
272         try {
273             for (short i = 1; i < elemFields.length; i++)
274                 if (fn.equals(elemFields[i].compositeName))
275                     if (elemFields[i].type.getTypeCode()
276                             == PType.TYPECODE_OBJINT) {
277                         return adapter.getOint(resultSet, startIndex + i, null);
278                     } else
279                         break;
280         } catch (SQLException JavaDoc e) {
281             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
282         }
283         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
284     }
285
286     /**
287      * It retrieves the value of a long field.
288      * @param fn A String containing the name of the field.
289      * @param ctx A context used by PNameGetter to extract the related data.
290      * @return The long value of the field as stored within the composite
291      * name.
292      * @exception PExceptionTyping There is no field with this name and
293      * this type within this composite name.
294      */

295     public long pngetLongField(String JavaDoc fn, Object JavaDoc ctx)
296             throws PExceptionTyping, PExceptionIO {
297         try {
298             for (short i = 1; i < elemFields.length; i++)
299                 if (fn.equals(elemFields[i].compositeName))
300                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_LONG)
301                         return adapter.getLong(resultSet, startIndex + i, -1);
302                     else
303                         break;
304         } catch (SQLException JavaDoc e) {
305             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
306         }
307         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
308     }
309
310     /**
311      * It retrieves the value of a Long field.
312      * @param fn A String containing the name of the field.
313      * @return The Long value of the field as stored within the composite
314      * name.
315      * @exception PExceptionTyping There is no field with this name and
316      * this type within this composite name.
317      * @exception PExceptionIO It is raised when a problem occurs while
318      * potentially accessing the DSI.
319      */

320     public Long JavaDoc pngetOlongField(String JavaDoc fn, Object JavaDoc ctxt) throws PExceptionTyping, PExceptionIO {
321         try {
322             for (short i = 1; i < elemFields.length; i++)
323                 if (fn.equals(elemFields[i].compositeName))
324                     if (elemFields[i].type.getTypeCode()
325                             == PType.TYPECODE_OBJLONG) {
326                         return adapter.getOlong(resultSet, startIndex + i, null);
327                     } else
328                         break;
329         } catch (SQLException JavaDoc e) {
330             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
331         }
332         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
333     }
334
335     /**
336      * It retrieves the value of a string field.
337      * @param fn A String containing the name of the field.
338      * @param ctx A context used by PNameGetter to extract the related data.
339      * @return The java.lang.String value of the field as stored within the
340      * composite name.
341      * @exception PExceptionTyping There is no field with this name and
342      * this type within this composite name.
343      */

344     public String JavaDoc pngetStringField(String JavaDoc fn, Object JavaDoc ctx)
345             throws PExceptionTyping, PExceptionIO {
346         try {
347             for (short i = 1; i < elemFields.length; i++)
348                 if (fn.equals(elemFields[i].compositeName))
349                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_STRING) {
350                         return adapter.getString(resultSet, startIndex + i, null);
351                     } else
352                         break;
353         } catch (SQLException JavaDoc e) {
354             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
355         }
356         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
357     }
358
359     /**
360      * It retrieves the value of a string field.
361      * @param fn A String containing the name of the field.
362      * @param ctx A context used by PNameGetter to extract the related data.
363      * @return The java.lang.String value of the field as stored within the
364      * composite name.
365      * @exception PExceptionTyping There is no field with this name and
366      * this type within this composite name.
367      */

368     public byte[] pngetByteArrayField(String JavaDoc fn, Object JavaDoc ctx)
369             throws PExceptionTyping, PExceptionIO {
370         try {
371             for (short i = 1; i < elemFields.length; i++)
372                 if (fn.equals(elemFields[i].compositeName))
373                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_BYTEARRAY) {
374                         return adapter.getByteArray(resultSet, startIndex + i, null);
375                     } else
376                         break;
377         } catch (SQLException JavaDoc e) {
378             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
379         }
380         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
381     }
382
383     /**
384      * It retrieves the value of a string field.
385      * @param fn A String containing the name of the field.
386      * @param ctx A context used by PNameGetter to extract the related data.
387      * @return The java.lang.String value of the field as stored within the
388      * composite name.
389      * @exception PExceptionTyping There is no field with this name and
390      * this type within this composite name.
391      */

392     public char[] pngetCharArrayField(String JavaDoc fn, Object JavaDoc ctx)
393             throws PExceptionTyping, PExceptionIO {
394         try {
395             for (short i = 1; i < elemFields.length; i++)
396                 if (fn.equals(elemFields[i].compositeName))
397                     if (elemFields[i].type.getTypeCode() == PType.TYPECODE_CHARARRAY) {
398                         return adapter.getCharArray(resultSet, startIndex + i, null);
399                     } else
400                         break;
401         } catch (Exception JavaDoc e) {
402             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
403         }
404         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
405     }
406
407     /**
408      * It retrieves the value of a date field.
409      * @param fn A String containing the name of the field.
410      * @return The java.util.Date value of the field as stored within the
411      * composite name.
412      * @exception PExceptionTyping There is no field with this name and
413      * this type within this composite name.
414      * @exception PExceptionIO It is raised when a problem occurs while
415      * potentially accessing the DSI.
416      */

417     public Date JavaDoc pngetDateField(String JavaDoc fn, Object JavaDoc ctxt) throws PExceptionTyping, PExceptionIO {
418         try {
419             for (short i = 1; i < elemFields.length; i++)
420                 if (fn.equals(elemFields[i].compositeName))
421                     if (elemFields[i].type.getTypeCode()
422                             == PType.TYPECODE_DATE) {
423                         return adapter.getDate(resultSet, startIndex + i, null);
424                     } else
425                         break;
426         } catch (SQLException JavaDoc e) {
427             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
428         }
429         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
430     }
431
432     /**
433      * It retrieves the value of a BigInteger field.
434      * @param fn A String containing the name of the field.
435      * @param ctxt A context used by PNameGetter to extract the related data.
436      * @return The java.math.BigInteger value of the field as stored within the
437      * composite name.
438      * @exception PExceptionTyping There is no field with this name and
439      * this type within this composite name.
440      * @exception PExceptionIO It is raised when a problem occurs while
441      * potentially accessing the DSI.
442      */

443     public BigInteger JavaDoc pngetBigIntegerField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
444         try {
445             for (short i = 1; i < elemFields.length; i++)
446                 if (fn.equals(elemFields[i].compositeName))
447                     if (elemFields[i].type.getTypeCode()
448                             == PType.TYPECODE_BIGINTEGER) {
449                         return adapter.getBigInteger(resultSet, startIndex + i, null);
450                     } else
451                         break;
452         } catch (SQLException JavaDoc e) {
453             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
454         }
455         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
456     }
457
458     /**
459      * It retrieves the value of a BigDecimal field.
460      * @param fn A String containing the name of the field.
461      * @param ctxt A context used by PNameGetter to extract the related data.
462      * @return The java.math.BigDecimal value of the field as stored within the
463      * composite name.
464      * @exception PExceptionTyping There is no field with this name and
465      * this type within this composite name.
466      * @exception PExceptionIO It is raised when a problem occurs while
467      * potentially accessing the DSI.
468      */

469     public BigDecimal JavaDoc pngetBigDecimalField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
470         try {
471             for (short i = 1; i < elemFields.length; i++)
472                 if (fn.equals(elemFields[i].compositeName))
473                     if (elemFields[i].type.getTypeCode()
474                             == PType.TYPECODE_BIGDECIMAL) {
475                         return adapter.getBigDecimal(resultSet, startIndex + i, null);
476                     } else
477                         break;
478         } catch (SQLException JavaDoc e) {
479             throw new PExceptionIO(e, "Problem while retreiving PNameGetter field from ResultSet: field <" + fn + ">");
480         }
481         throw new PExceptionTyping("Wrong composite name field " + fn + " in a GenClass.");
482     }
483 }
484
Popular Tags