001: package org.julp;
002:
003: import javax.sql.*;
004: import java.util.*;
005: import java.sql.SQLException;
006: import java.io.Serializable;
007: import java.lang.reflect.*;
008: import java.beans.*;
009:
010: public class MetaData implements Serializable, Cloneable {
011:
012: /**
013: * Populates and caches columns, tables, fields, etc info
014: *
015: */
016: public MetaData() {
017: }
018:
019: public String toString() {
020: StringBuffer sb = new StringBuffer(
021: "julp ============= org.julp.MetaData: ");
022: sb.append("\ncolumnCount: " + columnCount);
023: sb.append("\ncatalogName: " + Arrays.asList(catalogName));
024: sb.append("\nschemaName: " + Arrays.asList(schemaName));
025: sb.append("\ntableName: " + Arrays.asList(tableName));
026: //sb.append("\ncolumnClassName: " + Arrays.asList(columnClassName));
027: sb.append("\ncolumnLabel: " + Arrays.asList(columnLabel));
028: sb.append("\ncolumnName: " + Arrays.asList(columnName));
029: sb.append("\nfullColumnName: " + Arrays.asList(fullColumnName));
030: sb.append("\nfieldName: " + Arrays.asList(fieldName));
031: sb.append("\nreadMethod: " + Arrays.asList(readMethod));
032: sb.append("\nwriteMethod: " + Arrays.asList(writeMethod));
033: //sb.append("\nwritable: " + Arrays.asList(writable));
034: sb.append("\nfieldClassName: " + Arrays.asList(fieldClassName));
035: sb.append("\nrequestor: " + requestor);
036: return sb.toString();
037: }
038:
039: protected boolean[] autoIncrement = null;
040: protected boolean[] caseSensitive = null;
041: protected boolean[] currency = null;
042: protected boolean[] definitelyWritable = null;
043: protected boolean[] readOnly = null;
044: protected boolean[] searchable = null;
045: protected boolean[] signed = null;
046: protected boolean[] writable = null;
047: protected int[] columnDisplaySize = null;
048: protected int[] columnType = null;
049: protected int[] nullable = null;
050: protected int[] precision = null;
051: protected int[] scale = null;
052: protected String[] catalogName = null;
053: protected String[] schemaName = null;
054: protected String[] tableName = null;
055: protected String[] columnClassName = null;
056: protected String[] columnLabel = null;
057: protected String[] columnName = null;
058: protected String[] columnTypeName = null;
059: protected String[] fullColumnName = null;
060: protected String[] fieldName = null;
061: // java.lang.reflect.Method is not serializeable, so after sending this object
062: // to another JavaVM writeMethod and readMethod will become null
063: protected transient Method[] readMethod = null;
064: protected transient Method[] writeMethod = null;
065: protected String[] fieldClassName = null;
066: protected Class[] fieldClass = null;
067: protected int columnCount = 0;
068: protected Class requestor = null; // DomainObject
069: protected Map tables = new HashMap();
070: // Throw Exception or ignore if DomainObject has less fields than mappings
071: protected boolean throwMissingFieldException = false;
072:
073: /** Gets the designated column's table's catalog name.
074: *
075: * @param columnIndex the first column is 1, the second is 2, ...
076: * @return the name of the catalog for the table in which the given column
077: * appears or "" if not applicable
078: * @exception SQLException if a database access error occurs
079: *
080: */
081: public String getCatalogName(int columnIndex) throws SQLException {
082: return this .catalogName[columnIndex - 1];
083: }
084:
085: /** <p>Returns the fully-qualified name of the Java class whose instances
086: * are manufactured if the method <code>ResultSet.getObject</code>
087: * is called to retrieve a value
088: * from the column. <code>ResultSet.getObject</code> may return a subclass of the
089: * class returned by this method.
090: *
091: * @param columnIndex the first column is 1, the second is 2, ...
092: * @return the fully-qualified name of the class in the Java programming
093: * language that would be used by the method
094: * <code>ResultSet.getObject</code> to retrieve the value in the specified
095: * column. This is the class name used for custom mapping.
096: * @exception SQLException if a database access error occurs
097: * @since 1.2
098: *
099: */
100: public String getColumnClassName(int columnIndex)
101: throws SQLException {
102: return this .columnClassName[columnIndex - 1];
103: }
104:
105: /** Returns the number of columns in this <code>ResultSet</code> object.
106: *
107: * @return the number of columns
108: * @exception SQLException if a database access error occurs
109: *
110: */
111: public int getColumnCount() throws SQLException {
112: return this .columnCount;
113: }
114:
115: /** Indicates the designated column's normal maximum width in characters.
116: *
117: * @param columnIndex the first column is 1, the second is 2, ...
118: * @return the normal maximum number of characters allowed as the width
119: * of the designated column
120: * @exception SQLException if a database access error occurs
121: *
122: */
123: public int getColumnDisplaySize(int columnIndex)
124: throws SQLException {
125: return this .columnDisplaySize[columnIndex - 1];
126: }
127:
128: /** Gets the designated column's suggested title for use in printouts and
129: * displays.
130: *
131: * @param columnIndex the first column is 1, the second is 2, ...
132: * @return the suggested column title
133: * @exception SQLException if a database access error occurs
134: *
135: */
136: public String getColumnLabel(int columnIndex) throws SQLException {
137: return this .columnLabel[columnIndex - 1];
138: }
139:
140: /** Get the designated column's name.
141: *
142: * @param columnIndex the first column is 1, the second is 2, ...
143: * @return column name
144: * @exception SQLException if a database access error occurs
145: *
146: */
147: public String getColumnName(int columnIndex) throws SQLException {
148: return this .columnName[columnIndex - 1];
149: }
150:
151: /** Retrieves the designated column's SQL type.
152: *
153: * @param columnIndex the first column is 1, the second is 2, ...
154: * @return SQL type from java.sql.Types
155: * @exception SQLException if a database access error occurs
156: * @see java.sql.Types
157: *
158: */
159: public int getColumnType(int columnIndex) throws SQLException {
160: return this .columnType[columnIndex - 1];
161: }
162:
163: /** Retrieves the designated column's database-specific type name.
164: *
165: * @param columnIndex the first column is 1, the second is 2, ...
166: * @return type name used by the database. If the column type is
167: * a user-defined type, then a fully-qualified type name is returned.
168: * @exception SQLException if a database access error occurs
169: *
170: */
171: public String getColumnTypeName(int columnIndex)
172: throws SQLException {
173: return this .columnTypeName[columnIndex - 1];
174: }
175:
176: /** Get the designated column's number of decimal digits.
177: *
178: * @param columnIndex the first column is 1, the second is 2, ...
179: * @return precision
180: * @exception SQLException if a database access error occurs
181: *
182: */
183: public int getPrecision(int columnIndex) throws SQLException {
184: return this .precision[columnIndex - 1];
185: }
186:
187: /** Gets the designated column's number of digits to right of the decimal point.
188: *
189: * @param columnIndex the first column is 1, the second is 2, ...
190: * @return scale
191: * @exception SQLException if a database access error occurs
192: *
193: */
194: public int getScale(int columnIndex) throws SQLException {
195: return this .scale[columnIndex - 1];
196: }
197:
198: /** Get the designated column's table's schema.
199: *
200: * @param columnIndex the first column is 1, the second is 2, ...
201: * @return schema name or "" if not applicable
202: * @exception SQLException if a database access error occurs
203: *
204: */
205: public String getSchemaName(int columnIndex) throws SQLException {
206: return this .schemaName[columnIndex - 1];
207: }
208:
209: /** Gets the designated column's table name.
210: *
211: * @param columnIndex the first column is 1, the second is 2, ...
212: * @return table name or "" if not applicable
213: * @exception SQLException if a database access error occurs
214: *
215: */
216: public String getTableName(int columnIndex) throws SQLException {
217: return this .tableName[columnIndex - 1];
218: }
219:
220: /** Indicates whether the designated column is automatically numbered, thus read-only.
221: *
222: * @param columnIndex the first column is 1, the second is 2, ...
223: * @return <code>true</code> if so; <code>false</code> otherwise
224: * @exception SQLException if a database access error occurs
225: *
226: */
227: public boolean isAutoIncrement(int columnIndex) throws SQLException {
228: return this .autoIncrement[columnIndex - 1];
229: }
230:
231: /** Indicates whether a column's case matters.
232: *
233: * @param columnIndex the first column is 1, the second is 2, ...
234: * @return <code>true</code> if so; <code>false</code> otherwise
235: * @exception SQLException if a database access error occurs
236: *
237: */
238: public boolean isCaseSensitive(int columnIndex) throws SQLException {
239: return this .caseSensitive[columnIndex - 1];
240: }
241:
242: /** Indicates whether the designated column is a cash value.
243: *
244: * @param columnIndex the first column is 1, the second is 2, ...
245: * @return <code>true</code> if so; <code>false</code> otherwise
246: * @exception SQLException if a database access error occurs
247: *
248: */
249: public boolean isCurrency(int columnIndex) throws SQLException {
250: return this .currency[columnIndex - 1];
251: }
252:
253: /** Indicates whether a write on the designated column will definitely succeed.
254: *
255: * @param columnIndex the first column is 1, the second is 2, ...
256: * @return <code>true</code> if so; <code>false</code> otherwise
257: * @exception SQLException if a database access error occurs
258: *
259: */
260: public boolean isDefinitelyWritable(int columnIndex)
261: throws SQLException {
262: return this .definitelyWritable[columnIndex - 1];
263: }
264:
265: /** Indicates the nullability of values in the designated column.
266: *
267: * @param columnIndex the first column is 1, the second is 2, ...
268: * @return the nullability status of the given column; one of <code>columnNoNulls</code>,
269: * <code>columnNullable</code> or <code>columnNullableUnknown</code>
270: * @exception SQLException if a database access error occurs
271: *
272: */
273: public int isNullable(int columnIndex) throws SQLException {
274: return this .nullable[columnIndex - 1];
275: }
276:
277: /** Indicates whether the designated column is definitely not writable.
278: *
279: * @param columnIndex the first column is 1, the second is 2, ...
280: * @return <code>true</code> if so; <code>false</code> otherwise
281: * @exception SQLException if a database access error occurs
282: *
283: */
284: public boolean isReadOnly(int columnIndex) throws SQLException {
285: return this .readOnly[columnIndex - 1];
286: }
287:
288: /** Indicates whether the designated column can be used in a where clause.
289: *
290: * @param columnIndex the first column is 1, the second is 2, ...
291: * @return <code>true</code> if so; <code>false</code> otherwise
292: * @exception SQLException if a database access error occurs
293: *
294: */
295: public boolean isSearchable(int columnIndex) throws SQLException {
296: return this .searchable[columnIndex - 1];
297: }
298:
299: /** Indicates whether values in the designated column are signed numbers.
300: *
301: * @param columnIndex the first column is 1, the second is 2, ...
302: * @return <code>true</code> if so; <code>false</code> otherwise
303: * @exception SQLException if a database access error occurs
304: *
305: */
306: public boolean isSigned(int columnIndex) throws SQLException {
307: return this .signed[columnIndex - 1];
308: }
309:
310: /** Indicates whether it is possible for a write on the designated column to succeed.
311: *
312: * @param columnIndex the first column is 1, the second is 2, ...
313: * @return <code>true</code> if so; <code>false</code> otherwise
314: * @exception SQLException if a database access error occurs
315: *
316: */
317: public boolean isWritable(int columnIndex) throws SQLException {
318: return this .writable[columnIndex - 1];
319: }
320:
321: /** Sets whether the designated column is automatically numbered,
322: * and thus read-only. The default is for a <code>RowSet</code> object's
323: * columns not to be automatically numbered.
324: *
325: * @param columnIndex the first column is 1, the second is 2, ...
326: * @param property <code>true</code> if the column is automatically
327: * numbered; <code>false</code> if it is not
328: *
329: * @exception SQLException if a database access error occurs
330: *
331: */
332: public void setAutoIncrement(int columnIndex, boolean property)
333: throws SQLException {
334: this .autoIncrement[columnIndex - 1] = property;
335: }
336:
337: /** Sets whether the designated column is case sensitive.
338: * The default is <code>false</code>.
339: *
340: * @param columnIndex the first column is 1, the second is 2, ...
341: * @param property <code>true</code> if the column is case sensitive;
342: * <code>false</code> if it is not
343: *
344: * @exception SQLException if a database access error occurs
345: *
346: */
347: public void setCaseSensitive(int columnIndex, boolean property)
348: throws SQLException {
349: this .caseSensitive[columnIndex - 1] = property;
350: }
351:
352: /** Sets the designated column's table's catalog name, if any, to the given
353: * <code>String</code>.
354: *
355: * @param columnIndex the first column is 1, the second is 2, ...
356: * @param catalogName the column's catalog name
357: * @exception SQLException if a database access error occurs
358: *
359: */
360: public void setCatalogName(int columnIndex, String catalogName)
361: throws SQLException {
362: this .catalogName[columnIndex - 1] = catalogName;
363: }
364:
365: /** Sets the number of columns in the <code>RowSet</code> object to
366: * the given number.
367: *
368: * @param columnCount the number of columns in the <code>RowSet</code> object
369: * @exception SQLException if a database access error occurs
370: *
371: */
372: public void setColumnCount(int columnCount) throws SQLException {
373: this .columnCount = columnCount;
374: }
375:
376: /** Sets the designated column's normal maximum width in chars to the
377: * given <code>int</code>.
378: *
379: * @param columnIndex the first column is 1, the second is 2, ...
380: * @param size the normal maximum number of characters for
381: * the designated column
382: *
383: * @exception SQLException if a database access error occurs
384: *
385: */
386: public void setColumnDisplaySize(int columnIndex, int size)
387: throws SQLException {
388: this .columnDisplaySize[columnIndex - 1] = size;
389: }
390:
391: /** Sets the suggested column title for use in printouts and
392: * displays, if any, to the given <code>String</code>.
393: *
394: * @param columnIndex the first column is 1, the second is 2, ...
395: * @param label the column title
396: * @exception SQLException if a database access error occurs
397: *
398: */
399: public void setColumnLabel(int columnIndex, String label)
400: throws SQLException {
401: this .columnLabel[columnIndex - 1] = label;
402: }
403:
404: /** Sets the name of the designated column to the given <code>String</code>.
405: *
406: * @param columnIndex the first column is 1, the second is 2, ...
407: * @param columnName the designated column's name
408: * @exception SQLException if a database access error occurs
409: *
410: */
411: public void setColumnName(int columnIndex, String columnName)
412: throws SQLException {
413: this .columnName[columnIndex - 1] = columnName;
414: }
415:
416: /** Sets the designated column's SQL type to the one given.
417: *
418: * @param columnIndex the first column is 1, the second is 2, ...
419: * @param SQLType the column's SQL type
420: * @exception SQLException if a database access error occurs
421: * @see java.sql.Types
422: *
423: */
424: public void setColumnType(int columnIndex, int SQLType)
425: throws SQLException {
426: this .columnType[columnIndex - 1] = SQLType;
427: }
428:
429: /** Sets the designated column's type name that is specific to the
430: * data source, if any, to the given <code>String</code>.
431: *
432: * @param columnIndex the first column is 1, the second is 2, ...
433: * @param typeName data source specific type name.
434: * @exception SQLException if a database access error occurs
435: *
436: */
437: public void setColumnTypeName(int columnIndex, String typeName)
438: throws SQLException {
439: this .columnTypeName[columnIndex - 1] = typeName;
440: }
441:
442: /** Sets whether the designated column is a cash value.
443: * The default is <code>false</code>.
444: *
445: * @param columnIndex the first column is 1, the second is 2, ...
446: * @param property <code>true</code> if the column is a cash value;
447: * <code>false</code> if it is not
448: *
449: * @exception SQLException if a database access error occurs
450: *
451: */
452: public void setCurrency(int columnIndex, boolean property)
453: throws SQLException {
454: this .currency[columnIndex - 1] = property;
455: }
456:
457: /** Sets whether the designated column's value can be set to
458: * <code>NULL</code>.
459: * The default is <code>ResultSetMetaData.columnNullableUnknown</code>
460: *
461: * @param columnIndex the first column is 1, the second is 2, ...
462: * @param property one of the following constants:
463: * <code>ResultSetMetaData.columnNoNulls</code>,
464: * <code>ResultSetMetaData.columnNullable</code>, or
465: * <code>ResultSetMetaData.columnNullableUnknown</code>
466: *
467: * @exception SQLException if a database access error occurs
468: *
469: */
470: public void setNullable(int columnIndex, int property)
471: throws SQLException {
472: this .nullable[columnIndex - 1] = property;
473: }
474:
475: /** Sets the designated column's number of decimal digits to the
476: * given <code>int</code>.
477: *
478: * @param columnIndex the first column is 1, the second is 2, ...
479: * @param precision the total number of decimal digits
480: * @exception SQLException if a database access error occurs
481: *
482: */
483: public void setPrecision(int columnIndex, int precision)
484: throws SQLException {
485: this .precision[columnIndex - 1] = precision;
486: }
487:
488: /** Sets the designated column's number of digits to the
489: * right of the decimal point to the given <code>int</code>.
490: *
491: * @param columnIndex the first column is 1, the second is 2, ...
492: * @param scale the number of digits to right of decimal point
493: * @exception SQLException if a database access error occurs
494: *
495: */
496: public void setScale(int columnIndex, int scale)
497: throws SQLException {
498: this .scale[columnIndex - 1] = scale;
499: }
500:
501: /** Sets the name of the designated column's table's schema, if any, to
502: * the given <code>String</code>.
503: *
504: * @param columnIndex the first column is 1, the second is 2, ...
505: * @param schemaName the schema name
506: * @exception SQLException if a database access error occurs
507: *
508: */
509: public void setSchemaName(int columnIndex, String schemaName)
510: throws SQLException {
511: this .schemaName[columnIndex - 1] = schemaName;
512: }
513:
514: /** Sets whether the designated column can be used in a where clause.
515: * The default is <code>false</code>.
516: *
517: * @param columnIndex the first column is 1, the second is 2, ...
518: * @param property <code>true</code> if the column can be used in a
519: * <code>WHERE</code> clause; <code>false</code> if it cannot
520: *
521: * @exception SQLException if a database access error occurs
522: *
523: */
524: public void setSearchable(int columnIndex, boolean property)
525: throws SQLException {
526: this .searchable[columnIndex - 1] = property;
527: }
528:
529: /** Sets whether the designated column is a signed number.
530: * The default is <code>false</code>.
531: *
532: * @param columnIndex the first column is 1, the second is 2, ...
533: * @param property <code>true</code> if the column is a signed number;
534: * <code>false</code> if it is not
535: *
536: * @exception SQLException if a database access error occurs
537: *
538: */
539: public void setSigned(int columnIndex, boolean property)
540: throws SQLException {
541: this .signed[columnIndex - 1] = property;
542: }
543:
544: /** Sets the designated column's table name, if any, to the given
545: * <code>String</code>.
546: *
547: * @param columnIndex the first column is 1, the second is 2, ...
548: * @param tableName the column's table name
549: * @exception SQLException if a database access error occurs
550: *
551: */
552: public void setTableName(int columnIndex, String tableName)
553: throws SQLException {
554: this .tableName[columnIndex - 1] = tableName;
555: }
556:
557: public void setWritable(int columnIndex, boolean writable) {
558: this .writable[columnIndex - 1] = writable;
559: this .readOnly[columnIndex - 1] = !writable;
560: }
561:
562: public void setDefinitelyWritable(int columnIndex,
563: boolean definitelyWritable) {
564: this .definitelyWritable[columnIndex - 1] = definitelyWritable;
565: }
566:
567: public void setColumnClassName(int columnIndex,
568: java.lang.String columnClassName) {
569: this .columnClassName[columnIndex - 1] = columnClassName;
570: }
571:
572: public void setReadOnly(int columnIndex, boolean readOnly)
573: throws SQLException {
574: this .readOnly[columnIndex - 1] = readOnly;
575: this .writable[columnIndex - 1] = !readOnly;
576: }
577:
578: public java.util.Map getTables() {
579: return tables;
580: }
581:
582: public void setTables(java.util.Map tables) {
583: this .tables = tables;
584: }
585:
586: public String getFullColumnName(int columnIndex)
587: throws SQLException {
588: return this .fullColumnName[columnIndex - 1];
589: }
590:
591: public void setFullColumnName(int columnIndex, String columnName)
592: throws SQLException {
593: this .fullColumnName[columnIndex - 1] = columnName;
594: }
595:
596: public String getFieldName(int columnIndex) throws SQLException {
597: return this .fieldName[columnIndex - 1];
598: }
599:
600: public void setFieldName(int columnIndex, String fieldName)
601: throws SQLException {
602: this .fieldName[columnIndex - 1] = fieldName;
603: }
604:
605: public Method getWriteMethod(int columnIndex) throws SQLException {
606: if (this .writeMethod == null
607: || this .writeMethod[columnIndex - 1] == null) {
608: populateWriteMethod(columnIndex, fieldName[columnIndex - 1]);
609: }
610: return this .writeMethod[columnIndex - 1];
611: }
612:
613: public void setWriteMethod(int columnIndex, Method writeMethod) {
614: if (this .writeMethod == null) {
615: this .writeMethod = new Method[columnCount];
616: }
617: this .writeMethod[columnIndex - 1] = writeMethod;
618: }
619:
620: public Method getReadMethod(int columnIndex) throws SQLException {
621: if (this .readMethod == null
622: || this .readMethod[columnIndex - 1] == null) {
623: populateReadMethod(columnIndex, fieldName[columnIndex - 1]);
624: }
625: return this .readMethod[columnIndex - 1];
626: }
627:
628: public void setReadMethod(int columnIndex, Method readMethod) {
629: if (this .readMethod == null) {
630: this .readMethod = new Method[columnCount];
631: }
632: this .readMethod[columnIndex - 1] = readMethod;
633: }
634:
635: public String getFieldClassName(int columnIndex)
636: throws SQLException {
637: return this .fieldClassName[columnIndex - 1];
638: }
639:
640: public void setFieldClassName(int columnIndex, String fieldClassName)
641: throws SQLException {
642: this .fieldClassName[columnIndex - 1] = fieldClassName;
643: }
644:
645: public java.lang.Class getFieldClass(int columnIndex) {
646: return this .fieldClass[columnIndex - 1];
647: }
648:
649: public void setFieldClass(int columnIndex, Class fieldClass) {
650: this .fieldClass[columnIndex - 1] = fieldClass;
651: }
652:
653: protected void populateReadMethod(int columnIndex, String fieldName)
654: throws java.sql.SQLException {
655: try {
656: Method mRead = (new PropertyDescriptor(fieldName,
657: this .requestor)).getReadMethod();
658: this .setReadMethod(columnIndex, mRead);
659: } catch (IntrospectionException e) {
660: if (throwMissingFieldException) {
661: e.printStackTrace();
662: throw new RuntimeException(e);
663: }
664: }
665: }
666:
667: protected void populateWriteMethod(int columnIndex, String fieldName)
668: throws java.sql.SQLException {
669: try {
670: Method mWrite = (new PropertyDescriptor(fieldName,
671: this .requestor)).getWriteMethod();
672: this .setWriteMethod(columnIndex, mWrite);
673: Class[] paramTypes = mWrite.getParameterTypes();
674: Class paramClass = paramTypes[0];
675: this .setFieldClass(columnIndex, paramClass);
676: this .setFieldClassName(columnIndex, paramClass.getName());
677: } catch (IntrospectionException e) {
678: if (throwMissingFieldException) {
679: e.printStackTrace();
680: throw new RuntimeException(e);
681: }
682: }
683: }
684:
685: public void populate(Map mapping, Class requestor)
686: throws java.sql.SQLException {
687: if (mapping == null || mapping.isEmpty()) {
688: throw new SQLException("MetaData: missing/invalid mapping");
689: }
690: this .requestor = requestor;
691: int columnCount = mapping.size();
692: this .setColumnCount(columnCount);
693: this .catalogName = new String[columnCount];
694: this .schemaName = new String[columnCount];
695: this .tableName = new String[columnCount];
696: this .columnName = new String[columnCount];
697: this .columnLabel = new String[columnCount];
698: this .readOnly = new boolean[columnCount];
699: this .writable = new boolean[columnCount];
700: this .columnType = new int[columnCount];
701: this .fullColumnName = new String[columnCount];
702: this .fieldName = new String[columnCount];
703: this .readMethod = new Method[columnCount];
704: this .writeMethod = new Method[columnCount];
705: this .fieldClass = new Class[columnCount];
706: this .fieldClassName = new String[columnCount];
707:
708: Iterator iter = mapping.keySet().iterator();
709: int columnIndex = 1;
710: while (iter.hasNext()) {
711: String catalog = null;
712: String schema = null;
713: String table = null;
714: String tableId = null;
715: String column = null;
716: String fullColName = (String) iter.next();
717: StringTokenizer st = new StringTokenizer(fullColName, ".",
718: false);
719: int tokens = st.countTokens();
720: if (tokens == 2) { // only table and column names, no schema and no catalog
721: int idx1 = fullColName.indexOf(".");
722: tableId = fullColName.substring(0, idx1).trim();
723: table = tableId;
724: } else if (tokens == 3) { // no catalog is supplied
725: int idx1 = fullColName.indexOf(".");
726: int idx2 = fullColName.lastIndexOf(".");
727: tableId = fullColName.substring(0, idx2).trim();
728: table = fullColName.substring(idx1 + 1, idx2).trim();
729: schema = fullColName.substring(0, idx1).trim();
730: } else if (tokens == 4) { // catalog, schema, table and column
731: tableId = fullColName.substring(0,
732: fullColName.lastIndexOf(".")).trim();
733: int i = 0;
734: while (st.hasMoreTokens() && i < 3) {
735: String token = st.nextToken().trim();
736: if (i == 0) {
737: catalog = token;
738: } else if (i == 1) {
739: schema = token;
740: } else if (i == 2) {
741: table = token;
742: }
743: i++;
744: }
745: } else {
746: throw new IllegalArgumentException(
747: "Invalid DB Mappings");
748: }
749: this .setCatalogName(columnIndex, catalog);
750: this .setSchemaName(columnIndex, schema);
751: this .setTableName(columnIndex, table);
752: this .setColumnName(columnIndex, fullColName
753: .substring(fullColName.lastIndexOf(".") + 1));
754: this .setFullColumnName(columnIndex, fullColName);
755: String fieldName = (String) mapping.get(fullColName);
756: this .setFieldName(columnIndex, fieldName);
757: String[] distinctTables = new String[3];
758: distinctTables[0] = catalog;
759: distinctTables[1] = schema;
760: distinctTables[2] = table;
761: this .tables.put(tableId, distinctTables);
762: this .setReadOnly(columnIndex, false);
763: //this.setWritable(columnIndex, true); //it's done: see setReadOnly() method
764: this .populateReadMethod(columnIndex, fieldName);
765: this .populateWriteMethod(columnIndex, fieldName);
766: this .setColumnLabel(columnIndex,
767: toLabel(getColumnName(columnIndex)));
768: columnIndex++;
769: }
770: }
771:
772: public int getColumnIndexByFieldName(String searchedFieldName) {
773: if (searchedFieldName == null)
774: return -1;
775: for (int i = 0; i < this .fieldName.length; i++) {
776: if (fieldName[i] == null)
777: return -1;
778: if (searchedFieldName.equalsIgnoreCase(fieldName[i])) {
779: return i + 1;
780: }
781: }
782: return -1;
783: }
784:
785: public String toLabel(String columnName) {
786: String label = ((String) columnName).toLowerCase();
787: StringBuffer sb = new StringBuffer();
788: boolean toUpper = false;
789: for (int i = 0; i < label.length(); i++) {
790: char c = label.charAt(i);
791: if (c != '_') {
792: if (i == 0) {
793: toUpper = true;
794: }
795: if (toUpper) {
796: c = Character.toUpperCase(c);
797: } else {
798: }
799: sb.append(c);
800: toUpper = false;
801: } else {
802: if (i > 0) {
803: sb.append(' ');
804: }
805: toUpper = true;
806: }
807: }
808: return sb.toString();
809: }
810:
811: public boolean isThrowMissingFieldException() {
812: return throwMissingFieldException;
813: }
814:
815: public void setThrowMissingFieldException(
816: boolean throwMissingFieldException) {
817: this.throwMissingFieldException = throwMissingFieldException;
818: }
819:
820: }
|