View Javadoc

1   package com.workingdogs.village;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.math.BigDecimal;
23  
24  import java.sql.Blob;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  import java.sql.Time;
29  import java.sql.Timestamp;
30  import java.sql.Types;
31  
32  import java.util.Calendar;
33  
34  /***
35   * A Value represents a single cell in a database table. In other words, 
36   * it is the cross between a row and column and contains the
37   * information held there.
38   *
39   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
40   * @version $Revision: 568 $
41   */
42  public class Value
43  {
44      /*** the object that is stored in this object */
45      private Object valueObject;
46  
47      /*** the column number that this object came from */
48      private int columnNumber;
49  
50      /*** what sql type of object is this? */
51      private int type;
52  
53      /***
54       * Creates a new Value object based on the ResultSet, columnNumber and 
55       * type
56       *
57       * @param rs
58       * @param columnNumber
59       * @param type
60       *
61       * @exception SQLException
62       */
63      public Value(ResultSet rs, int columnNumber, int type)
64              throws SQLException
65      {
66          this.columnNumber = columnNumber;
67          this.type = type;
68          this.valueObject = null;
69  
70          if (rs == null)
71          {
72              return;
73          }
74  
75          switch (type())
76          {
77          case Types.BIT:
78  
79              String tmp = rs.getString(columnNumber);
80  
81              if (tmp == null)
82              {
83                  valueObject = Boolean.FALSE;
84              }
85              else if (isTrue(tmp))
86              {
87                  valueObject = Boolean.TRUE;
88              }
89              else
90              {
91                  valueObject = Boolean.FALSE;
92              }
93  
94              break;
95  
96          case Types.TINYINT:
97              valueObject = new Byte(rs.getByte(columnNumber));
98  
99              break;
100 
101         case Types.BIGINT:
102             valueObject = new Long(rs.getLong(columnNumber));
103 
104             break;
105 
106         case Types.SMALLINT:
107             valueObject = new Short(rs.getShort(columnNumber));
108 
109             break;
110 
111         case Types.INTEGER:
112             valueObject = new Integer(rs.getInt(columnNumber));
113 
114             break;
115 
116         case Types.REAL:
117             valueObject = new Float(rs.getFloat(columnNumber));
118 
119             break;
120 
121         case Types.FLOAT:
122         case Types.DOUBLE:
123             valueObject = new Double(rs.getDouble(columnNumber));
124 
125             break;
126 
127         case Types.NUMERIC:
128         case Types.DECIMAL:
129 
130             String number = rs.getString(columnNumber);
131 
132             if (number == null)
133             {
134                 valueObject = null;
135             }
136             else
137             {
138                 valueObject = new BigDecimal(number);
139             }
140 
141             break;
142 
143         case Types.LONGVARBINARY:
144         case Types.VARBINARY:
145         case Types.BINARY:
146             valueObject = rs.getBytes(columnNumber);
147 
148             break;
149 
150         case Types.BLOB:
151 
152             Blob blob = rs.getBlob(columnNumber);
153             valueObject = blob.getBytes(1, (int) blob.length());
154 
155             break;
156 
157         case Types.LONGVARCHAR:
158         case Types.CHAR:
159         case Types.VARCHAR:
160         case Types.OTHER:
161             valueObject = rs.getString(columnNumber);
162 
163             break;
164 
165         case Types.DATE:
166             valueObject = rs.getDate(columnNumber);
167 
168             break;
169 
170         case Types.TIME:
171             valueObject = rs.getTime(columnNumber);
172 
173             break;
174 
175         case Types.TIMESTAMP:
176             valueObject = rs.getTimestamp(columnNumber);
177 
178             break;
179 
180         case Types.NULL:
181             valueObject = null;
182 
183             break;
184 
185         default:
186             valueObject = rs.getString(columnNumber);
187 
188             break;
189         }
190 
191         if (rs.wasNull())
192         {
193             valueObject = null;
194         }
195 
196         return;
197     }
198 
199     /***
200      * Sets the value of this object
201      *
202      * @param value
203      */
204     void setValue(Object value)
205     {
206         this.valueObject = value;
207     }
208 
209     /***
210      * Gets the object from this Value
211      *
212      * @return the object from this Value
213      */
214     Object getValue()
215     {
216         return this.valueObject;
217     }
218 
219     /***
220      * This is used in Record in order to do a saveWithInsert/Update/Delete
221      *
222      * @param stmt
223      * @param stmtNumber
224      *
225      * @exception DataSetException
226      * @exception SQLException
227      */
228     void setPreparedStatementValue(PreparedStatement stmt, int stmtNumber)
229             throws DataSetException, SQLException
230     {
231         if (isNull())
232         {
233             stmt.setNull(stmtNumber, type());
234 
235             return;
236         }
237 
238         switch (type())
239         {
240         case Types.BIT:
241             stmt.setBoolean(stmtNumber, this.asBoolean());
242 
243             break;
244 
245         case Types.TINYINT:
246             stmt.setByte(stmtNumber, this.asByte());
247 
248             break;
249 
250         case Types.BIGINT:
251             stmt.setLong(stmtNumber, this.asLong());
252 
253             break;
254 
255         case Types.SMALLINT:
256             stmt.setShort(stmtNumber, this.asShort());
257 
258             break;
259 
260         case Types.INTEGER:
261             stmt.setInt(stmtNumber, this.asInt());
262 
263             break;
264 
265         case Types.REAL:
266             stmt.setFloat(stmtNumber, this.asFloat());
267 
268             break;
269 
270         case Types.FLOAT:
271         case Types.DOUBLE:
272             stmt.setDouble(stmtNumber, this.asDouble());
273 
274             break;
275 
276         case Types.NUMERIC:
277         case Types.DECIMAL:
278             stmt.setBigDecimal(stmtNumber, this.asBigDecimal());
279 
280             break;
281 
282         case Types.LONGVARBINARY:
283         case Types.VARBINARY:
284         case Types.BINARY:
285         case Types.BLOB:
286 
287             // The following form is reported to work and be necessary for
288             // Oracle when the blob exceeds 4k.
289             byte [] value = this.asBytes();
290             stmt.setBinaryStream(stmtNumber, 
291                     new java.io.ByteArrayInputStream(value), value.length);
292 
293             break;
294 
295         case Types.LONGVARCHAR:
296         case Types.CHAR:
297         case Types.VARCHAR:
298         case Types.OTHER:
299             stmt.setString(stmtNumber, this.asString());
300 
301             break;
302 
303         case Types.DATE:
304             stmt.setDate(stmtNumber, this.asDate());
305 
306             break;
307 
308         case Types.TIME:
309             stmt.setTime(stmtNumber, this.asTime());
310 
311             break;
312 
313         case Types.TIMESTAMP:
314             stmt.setTimestamp(stmtNumber, this.asTimestamp());
315 
316             break;
317 
318         case Types.NULL:
319             stmt.setNull(stmtNumber, 0);
320 
321             break;
322 
323         default:
324             stmt.setString(stmtNumber, this.asString());
325 
326             break;
327         }
328     }
329 
330     /***
331      * Returns the string representation of this object
332      *
333      * @return a string
334      */
335     public String toString()
336     {
337         return this.asString();
338     }
339 
340     /***
341      * Returns the string representation of this object
342      *
343      * @return a string
344      */
345     public String asString()
346     {
347         if (isNull())
348         {
349             return null;
350         }
351         else if (isString())
352         {
353             return (String) valueObject;
354         }
355         else if (isBytes())
356         {
357             return new String((byte []) valueObject);
358         }
359         else
360         {
361             return valueObject.toString();
362         }
363     }
364 
365     /***
366      * Get the value as a BigDecimal
367      *
368      * @return a BigDecimal
369      *
370      * @exception DataSetException
371      */
372     public BigDecimal asBigDecimal()
373             throws DataSetException
374     {
375         try
376         {
377             if (isNull())
378             {
379                 return null;
380             }
381             else if (isBigDecimal())
382             {
383                 return (BigDecimal) valueObject;
384             }
385             else if ( isDouble() ) {
386                 return new BigDecimal(((Double) valueObject).doubleValue() );
387             }
388             else if ( isFloat() ) 
389             {
390                 return new BigDecimal(((Float) valueObject).doubleValue());
391             }
392             else if(isString() || isInt() || isLong() || isShort() || isByte())
393             {
394                 return new BigDecimal(asString());
395             }
396             else
397             {
398                 return null;
399             }
400         }
401         catch (Exception e)
402         {
403             throw new DataSetException("Illegal conversion: " + e.toString());
404         }
405     }
406 
407     /***
408      * Get the value as a BigDecimal
409      *
410      * @param scale TODO: DOCUMENT ME!
411      *
412      * @return a BigDecimal
413      *
414      * @exception DataSetException
415      */
416     public BigDecimal asBigDecimal(int scale)
417             throws DataSetException
418     {
419         try
420         {
421             if (isNull())
422             {
423                 return null;
424             }
425             else if (isBigDecimal())
426             {
427                 return ((BigDecimal) valueObject).setScale(scale);
428             }
429             else if ( isDouble() )
430             {
431                 return new BigDecimal( ((Double) valueObject).doubleValue())
432                                                              .setScale(scale);
433             } 
434             else if ( isFloat() ) 
435             {
436                 return new BigDecimal(((Float) valueObject).doubleValue())
437                                 .setScale(scale);
438             }
439             else if(isString() || isInt() || isLong() || isShort() || isByte())
440             {
441                 return new BigDecimal(asString()).setScale(scale);
442             }
443             else
444             {
445                 return null;
446             }
447         }
448         catch (Exception e)
449         {
450             throw new DataSetException("Bad conversion: " + e.toString());
451         }
452     }
453 
454     /***
455      * Get the value as a asBoolean
456      *
457      * @return a boolean
458      *
459      * @exception DataSetException
460      */
461     public boolean asBoolean()
462             throws DataSetException
463     {
464         try
465         {
466             if (isNull())
467             {
468                 return false;
469             }
470             else if (isBoolean())
471             {
472                 return ((Boolean) valueObject).booleanValue();
473             }
474 
475             String check = asString();
476 
477             return (check == null) ? false : isTrue(check);
478         }
479         catch (Exception e)
480         {
481             throw new DataSetException("Bad conversion: " + e.toString());
482         }
483     }
484 
485     /***
486      * Get the value as a Boolean object
487      *
488      * @return a Boolean
489      *
490      * @exception DataSetException
491      */
492     public Boolean asBooleanObj()
493             throws DataSetException
494     {
495         try
496         {
497             if (isNull())
498             {
499                 return null;
500             }
501             else if (isBoolean())
502             {
503                 return (Boolean) valueObject;
504             }
505 
506             String check = asString();
507 
508             if (check == null)
509             {
510                 return null;
511             }
512             else if (isTrue(check))
513             {
514                 return Boolean.TRUE;
515             }
516             else
517             {
518                 return Boolean.FALSE;
519             }
520         }
521         catch (Exception e)
522         {
523             throw new DataSetException("Bad conversion: " + e.toString());
524         }
525     }
526 
527     /***
528      * Get the value as a asInt
529      *
530      * @return an int
531      *
532      * @exception DataSetException
533      */
534     public int asInt()
535             throws DataSetException
536     {
537         try
538         {
539             if (isNull())
540             {
541                 return 0;
542             }
543             else if (isInt())
544             {
545                 return ((Integer) valueObject).intValue();
546             }
547             else if (isString())
548             {
549                 return Integer.valueOf((String) valueObject).intValue();
550             }
551             else if (isLong())
552             {
553                 return ((Long) valueObject).intValue();
554             }
555             else if (isDouble())
556             {
557                 return ((Double) valueObject).intValue();
558             }
559             else if (isFloat())
560             {
561                 return ((Float) valueObject).intValue();
562             }
563             else if (isBigDecimal())
564             {
565                 return ((BigDecimal) valueObject).intValue();
566             }
567             else
568             {
569                 return Integer.valueOf(asString()).intValue();
570             }
571         }
572         catch (Exception e)
573         {
574             throw new DataSetException("Bad conversion: " + e.toString());
575         }
576     }
577 
578     /***
579      * Get the value as a Integer Ojbect
580      *
581      * @return an Integer
582      *
583      * @exception DataSetException
584      */
585     public Integer asIntegerObj()
586             throws DataSetException
587     {
588         try
589         {
590             if (isNull())
591             {
592                 return null;
593             }
594             else if (isInt())
595             {
596                 return ((Integer) valueObject);
597             }
598             else if (isString() || isDouble() || isFloat() || isBigDecimal() 
599                      || isLong() || isShort() || isByte())
600             {
601                 return new Integer(asString());
602             }
603             else
604             {
605                 throw new DataSetException("Invalid type for Integer");
606             }
607         }
608         catch (Exception e)
609         {
610             throw new DataSetException("Illegal conversion: " + e.toString());
611         }
612     }
613 
614     /***
615      * Get the value as a asByte
616      *
617      * @return a byte
618      *
619      * @exception DataSetException
620      */
621     public byte asByte()
622             throws DataSetException
623     {
624         try
625         {
626             if (isNull())
627             {
628                 return 0;
629             }
630             else if (isByte())
631             {
632                 return ((Byte) valueObject).byteValue();
633             }
634             else if (isString())
635             {
636                 return Integer.valueOf((String) valueObject).byteValue();
637             }
638             else if (isShort())
639             {
640                 return ((Short) valueObject).byteValue();
641             }
642             else if (isInt())
643             {
644                 return ((Integer) valueObject).byteValue();
645             }
646             else if (isLong())
647             {
648                 return ((Long) valueObject).byteValue();
649             }
650             else if (isDouble())
651             {
652                 return ((Double) valueObject).byteValue();
653             }
654             else if (isFloat())
655             {
656                 return ((Float) valueObject).byteValue();
657             }
658             else if (isBigDecimal())
659             {
660                 return ((BigDecimal) valueObject).byteValue();
661             }
662             else
663             {
664                 return Integer.valueOf(asString()).byteValue();
665             }
666         }
667         catch (Exception e)
668         {
669             throw new DataSetException("Bad conversion: " + e.toString());
670         }
671     }
672 
673     /***
674      * Get the value as a Byte Object
675      *
676      * @return a Byte
677      *
678      * @exception DataSetException
679      */
680     public Byte asByteObj()
681             throws DataSetException
682     {
683         try
684         {
685             if (isNull())
686             {
687                 return null;
688             }
689             else if (isByte())
690             {
691                 return ((Byte) valueObject);
692             }
693             else if (isString() || isDouble() || isFloat() || isInt() || 
694                      isLong() || isShort() || isBigDecimal())
695             {
696                 return new Byte(asString());
697             }
698             else
699             {
700                 throw new DataSetException("Invalid type for Byte");
701             }
702         }
703         catch (Exception e)
704         {
705             throw new DataSetException("Illegal conversion: " + e.toString());
706         }
707     }
708 
709     /***
710      * Get the value as a asBytes
711      *
712      * @return a byte array
713      *
714      * @exception DataSetException
715      */
716     public byte [] asBytes()
717             throws DataSetException
718     {
719         try
720         {
721             if (isNull())
722             {
723                 return new byte[0];
724             }
725             else if (isBytes())
726             {
727                 return (byte []) valueObject;
728             }
729             else if (isString())
730             {
731                 return ((String) valueObject).getBytes();
732             }
733         }
734         catch (Exception e)
735         {
736             throw new DataSetException("Bad conversion: " + e.toString());
737         }
738 
739         return new byte[0];
740     }
741 
742     /***
743      * Get the value as a asShort
744      *
745      * @return a short
746      *
747      * @exception DataSetException
748      */
749     public short asShort()
750             throws DataSetException
751     {
752         try
753         {
754             if (isNull())
755             {
756                 return 0;
757             }
758             else if (isShort())
759             {
760                 return ((Short) valueObject).shortValue();
761             }
762             else if (isString())
763             {
764                 return Integer.valueOf((String) valueObject).shortValue();
765             }
766             else if (isInt())
767             {
768                 return ((Integer) valueObject).shortValue();
769             }
770             else if (isLong())
771             {
772                 return ((Long) valueObject).shortValue();
773             }
774             else if (isDouble())
775             {
776                 return ((Double) valueObject).shortValue();
777             }
778             else if (isFloat())
779             {
780                 return ((Float) valueObject).shortValue();
781             }
782             else if (isBigDecimal())
783             {
784                 return ((BigDecimal) valueObject).shortValue();
785             }
786             else
787             {
788                 return Integer.valueOf(asString()).shortValue();
789             }
790         }
791         catch (Exception e)
792         {
793             throw new DataSetException("Bad conversion: " + e.toString());
794         }
795     }
796 
797     /***
798      * Get the value as a Short Object
799      *
800      * @return a Short
801      *
802      * @exception DataSetException
803      */
804     public Short asShortObj()
805             throws DataSetException
806     {
807         try
808         {
809             if (isNull())
810             {
811                 return null;
812             }
813             else if (isShort())
814             {
815                 return ((Short) valueObject);
816             }
817             else if (isString() || isDouble() || isFloat() || isInt() || 
818                      isLong() || isBigDecimal() || isByte())
819             {
820                 return new Short(asString());
821             }
822             else
823             {
824                 throw new DataSetException("Invalid type for Short");
825             }
826         }
827         catch (Exception e)
828         {
829             throw new DataSetException("Illegal conversion: " + e.toString());
830         }
831     }
832 
833     /***
834      * Get the value as a asLong
835      *
836      * @return a long
837      *
838      * @exception DataSetException
839      */
840     public long asLong()
841             throws DataSetException
842     {
843         try
844         {
845             if (isNull())
846             {
847                 return 0;
848             }
849             else if (isLong())
850             {
851                 return ((Long) valueObject).longValue();
852             }
853             else if (isString())
854             {
855                 return Integer.valueOf((String) valueObject).longValue();
856             }
857             else if (isShort())
858             {
859                 return ((Short) valueObject).longValue();
860             }
861             else if (isInt())
862             {
863                 return ((Integer) valueObject).longValue();
864             }
865             else if (isDouble())
866             {
867                 return ((Double) valueObject).longValue();
868             }
869             else if (isFloat())
870             {
871                 return ((Float) valueObject).longValue();
872             }
873             else if (isBigDecimal())
874             {
875                 return ((BigDecimal) valueObject).longValue();
876             }
877             else
878             {
879                 return Integer.valueOf(asString()).longValue();
880             }
881         }
882         catch (Exception e)
883         {
884             throw new DataSetException("Bad conversion: " + e.toString());
885         }
886     }
887 
888     /***
889      * Get the value as a Long Object
890      *
891      * @return a Long
892      *
893      * @exception DataSetException
894      */
895     public Long asLongObj()
896             throws DataSetException
897     {
898         try
899         {
900             if (isNull())
901             {
902                 return null;
903             }
904             else if (isLong())
905             {
906                 return ((Long) valueObject);
907             }
908             else if (isString() || isDouble() || isFloat() || isInt() || 
909                      isBigDecimal() || isShort() || isByte())
910             {
911                 return new Long(asString());
912             }
913             else
914             {
915                 throw new DataSetException("Invalid type for Long");
916             }
917         }
918         catch (Exception e)
919         {
920             throw new DataSetException("Illegal conversion: " + e.toString());
921         }
922     }
923 
924     /***
925      * Get the value as a asDouble
926      *
927      * @return a double
928      *
929      * @exception DataSetException
930      */
931     public double asDouble()
932             throws DataSetException
933     {
934         try
935         {
936             if (isNull())
937             {
938                 return 0.0D;
939             }
940             else if (isDouble())
941             {
942                 return ((Double) valueObject).doubleValue();
943             }
944             else if (isString())
945             {
946                 return Integer.valueOf((String) valueObject).doubleValue();
947             }
948             else if (isShort())
949             {
950                 return ((Short) valueObject).doubleValue();
951             }
952             else if (isInt())
953             {
954                 return ((Integer) valueObject).doubleValue();
955             }
956             else if (isLong())
957             {
958                 return ((Long) valueObject).doubleValue();
959             }
960             else if (isFloat())
961             {
962                 return ((Float) valueObject).doubleValue();
963             }
964             else if (isBigDecimal())
965             {
966                 return ((BigDecimal) valueObject).doubleValue();
967             }
968             else
969             {
970                 return Integer.valueOf(asString()).doubleValue();
971             }
972         }
973         catch (Exception e)
974         {
975             throw new DataSetException("Bad conversion: " + e.toString());
976         }
977     }
978 
979     /***
980      * Get the value as a Double Object
981      *
982      * @return a Double
983      *
984      * @exception DataSetException
985      */
986     public Double asDoubleObj()
987             throws DataSetException
988     {
989         try
990         {
991             if (isNull())
992             {
993                 return null;
994             }
995             else if (isDouble())
996             {
997                 return ((Double) valueObject);
998             }
999             else if (isString() || isBigDecimal() || isFloat() || isInt() || 
1000                      isLong() || isShort() || isByte())
1001             {
1002                 return new Double(asString());
1003             }
1004             else
1005             {
1006                 throw new DataSetException("Invalid type for Double");
1007             }
1008         }
1009         catch (Exception e)
1010         {
1011             throw new DataSetException("Illegal conversion: " + e.toString());
1012         }
1013     }
1014 
1015     /***
1016      * Get the value as a asFloat
1017      *
1018      * @return a float
1019      *
1020      * @exception DataSetException
1021      */
1022     public float asFloat()
1023             throws DataSetException
1024     {
1025         try
1026         {
1027             if (isNull())
1028             {
1029                 return 0.0F;
1030             }
1031             else if (isFloat())
1032             {
1033                 return ((Float) valueObject).floatValue();
1034             }
1035             else if (isString())
1036             {
1037                 return Integer.valueOf((String) valueObject).floatValue();
1038             }
1039             else if (isShort())
1040             {
1041                 return ((Short) valueObject).floatValue();
1042             }
1043             else if (isInt())
1044             {
1045                 return ((Integer) valueObject).floatValue();
1046             }
1047             else if (isLong())
1048             {
1049                 return ((Long) valueObject).floatValue();
1050             }
1051             else if (isDouble())
1052             {
1053                 return ((Double) valueObject).floatValue();
1054             }
1055             else if (isBigDecimal())
1056             {
1057                 return ((BigDecimal) valueObject).floatValue();
1058             }
1059             else
1060             {
1061                 return Integer.valueOf(asString()).floatValue();
1062             }
1063         }
1064         catch (Exception e)
1065         {
1066             throw new DataSetException("Bad conversion: " + e.toString());
1067         }
1068     }
1069 
1070     /***
1071      * Get the value as a Float Obj
1072      *
1073      * @return a Float
1074      *
1075      * @exception DataSetException
1076      */
1077     public Float asFloatObj()
1078             throws DataSetException
1079     {
1080         try
1081         {
1082             if (isNull())
1083             {
1084                 return null;
1085             }
1086             else if (isFloat())
1087             {
1088                 return ((Float) valueObject);
1089             }
1090             else if (isString() || isDouble() || isBigDecimal() || isInt() || 
1091                      isLong() || isShort() || isByte())
1092             {
1093                 return new Float(asString());
1094             }
1095             else
1096             {
1097                 throw new DataSetException("Invalid type for Float");
1098             }
1099         }
1100         catch (Exception e)
1101         {
1102             throw new DataSetException("Illegal conversion: " + e.toString());
1103         }
1104     }
1105 
1106     /***
1107      * Get the value as a asTime
1108      *
1109      * @return a Time
1110      *
1111      * @exception DataSetException
1112      */
1113     public Time asTime()
1114             throws DataSetException
1115     {
1116         try
1117         {
1118             if (isNull())
1119             {
1120                 return null;
1121             }
1122             else if (isTime())
1123             {
1124                 return (Time) valueObject;
1125             }
1126 
1127             Calendar cal = Calendar.getInstance();
1128 
1129             if (isTimestamp())
1130             {
1131                 cal.setTime((Timestamp) valueObject);
1132 
1133                 return new Time(cal.getTime().getTime());
1134             }
1135             else if (isUtilDate())
1136             {
1137                 cal.setTime((java.util.Date) valueObject);
1138 
1139                 return new Time(cal.getTime().getTime());
1140             }
1141             else if (isString())
1142             {
1143                 return Time.valueOf((String) valueObject);
1144             }
1145             else
1146             {
1147                 return Time.valueOf(asString());
1148             }
1149         }
1150         catch (IllegalArgumentException a)
1151         {
1152             throw new DataSetException("Bad date value - " + 
1153                     "Java Time Objects cannot be earlier than 1/1/70");
1154         }
1155         catch (Exception b)
1156         {
1157             throw new DataSetException("Bad conversion: " + b.toString());
1158         }
1159     }
1160 
1161     /***
1162      * Get the value as a asTimestamp
1163      *
1164      * @return a Timestamp
1165      *
1166      * @exception DataSetException
1167      */
1168     public Timestamp asTimestamp()
1169             throws DataSetException
1170     {
1171         try
1172         {
1173             if (isNull())
1174             {
1175                 return null;
1176             }
1177             else if (isTimestamp())
1178             {
1179                 return (Timestamp) valueObject;
1180             }
1181 
1182             if (isTime())
1183             {
1184                 Calendar cal = Calendar.getInstance();
1185                 cal.setTime((Time) valueObject);
1186 
1187                 return new Timestamp(cal.getTime().getTime());
1188             }
1189             else if (isUtilDate())
1190             {
1191                 return new Timestamp(((java.util.Date) valueObject).getTime());
1192             }
1193             else if (isString())
1194             {
1195                 return Timestamp.valueOf((String) valueObject);
1196             }
1197             else
1198             {
1199                 return Timestamp.valueOf(asString());
1200             }
1201         }
1202         catch (IllegalArgumentException a)
1203         {
1204             throw new DataSetException("Bad date value - " + 
1205                        "Java Timestamp Objects cannot be earlier than 1/1/70");
1206         }
1207         catch (Exception b)
1208         {
1209             throw new DataSetException("Bad conversion: " + b.toString());
1210         }
1211     }
1212 
1213     /***
1214      * Get the value as a asDate
1215      *
1216      * @return a java.sql.Date
1217      *
1218      * @exception DataSetException
1219      */
1220     public java.sql.Date asDate()
1221             throws DataSetException
1222     {
1223         try
1224         {
1225             if (isNull())
1226             {
1227                 return null;
1228             }
1229             else if (isDate())
1230             {
1231                 return (java.sql.Date) valueObject;
1232             }
1233 
1234             Calendar cal = Calendar.getInstance();
1235 
1236             if (isTimestamp())
1237             {
1238                 Timestamp ts = (Timestamp) valueObject;
1239                 long date = ts.getTime();
1240                 int nanos = ts.getNanos();
1241 
1242                 return new java.sql.Date(date + (nanos / 1000000));
1243             }
1244             else if (isTime())
1245             {
1246                 cal.setTime((Time) valueObject);
1247 
1248                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + 
1249                                             (cal.get(Calendar.MONTH) + 1) + "-"
1250                                             + cal.get(Calendar.DAY_OF_MONTH));
1251             }
1252             else if (isUtilDate())
1253             {
1254                 cal.setTime((java.util.Date) valueObject);
1255 
1256                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + 
1257                                             (cal.get(Calendar.MONTH) + 1) + "-"
1258                                             + cal.get(Calendar.DAY_OF_MONTH));
1259             }
1260             else if (isString())
1261             {
1262                 return java.sql.Date.valueOf((String) valueObject);
1263             }
1264             else
1265             {
1266                 return java.sql.Date.valueOf(asString());
1267             }
1268         }
1269         catch (IllegalArgumentException a)
1270         {
1271             throw new DataSetException("Bad date value - " + 
1272                     "Java Timestamp Objects cannot be earlier than 1/1/70");
1273         }
1274         catch (Exception b)
1275         {
1276             throw new DataSetException("Bad conversion: " + b.toString());
1277         }
1278     }
1279 
1280     /***
1281      * Get the value as a asUtilDate
1282      *
1283      * @return a java.util.Date
1284      *
1285      * @exception DataSetException
1286      */
1287     public java.util.Date asUtilDate()
1288             throws DataSetException
1289     {
1290         try
1291         {
1292             if (isNull())
1293             {
1294                 return null;
1295             }
1296             else if (isUtilDate())
1297             {
1298                 return (java.util.Date) valueObject;
1299             }
1300 
1301             Calendar cal = Calendar.getInstance();
1302 
1303             if (isTimestamp())
1304             {
1305                 Timestamp ts = (Timestamp) valueObject;
1306                 long date = ts.getTime();
1307                 int nanos = ts.getNanos();
1308 
1309                 return new java.util.Date(date + (nanos / 1000000));
1310             }
1311             else if (isTime())
1312             {
1313                 cal.setTime((Time) valueObject);
1314 
1315                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + 
1316                                             (cal.get(Calendar.MONTH) + 1) + "-"
1317                                             + cal.get(Calendar.DAY_OF_MONTH));
1318             }
1319             else if (isUtilDate())
1320             {
1321                 cal.setTime((java.util.Date) valueObject);
1322 
1323                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-" + 
1324                                             (cal.get(Calendar.MONTH) + 1) + "-"
1325                                             + cal.get(Calendar.DAY_OF_MONTH));
1326             }
1327             else
1328             {
1329                 return null;
1330             }
1331         }
1332         catch (IllegalArgumentException a)
1333         {
1334             throw new DataSetException("Bad date value - " + 
1335                   "Java java.util.Date Objects cannot be earlier than 1/1/70");
1336         }
1337         catch (Exception b)
1338         {
1339             throw new DataSetException("Bad conversion: " + b.toString());
1340         }
1341     }
1342 
1343     /***
1344      * Is the value a isBigDecimal
1345      *
1346      * @return true if BigDecimal
1347      */
1348     public boolean isBigDecimal()
1349     {
1350         return valueObject instanceof BigDecimal;
1351     }
1352 
1353     /***
1354      * Is the value a isByte
1355      *
1356      * @return true if is Byte
1357      */
1358     public boolean isByte()
1359     {
1360         return valueObject instanceof Byte;
1361     }
1362 
1363     /***
1364      * Is the value a isBytes
1365      *
1366      * @return true if is byte[]
1367      */
1368     public boolean isBytes()
1369     {
1370         return valueObject instanceof byte [];
1371     }
1372 
1373     /***
1374      * Is the value a isDate
1375      *
1376      * @return true if is java.sql.Date
1377      */
1378     public boolean isDate()
1379     {
1380         return valueObject instanceof java.sql.Date;
1381     }
1382 
1383     /***
1384      * Is the value a isShort
1385      *
1386      * @return true if is Short
1387      */
1388     public boolean isShort()
1389     {
1390         return valueObject instanceof Short;
1391     }
1392 
1393     /***
1394      * Is the value a isInt
1395      *
1396      * @return true if is Integer
1397      */
1398     public boolean isInt()
1399     {
1400         return valueObject instanceof Integer;
1401     }
1402 
1403     /***
1404      * Is the value a isLong
1405      *
1406      * @return true if is Long
1407      */
1408     public boolean isLong()
1409     {
1410         return valueObject instanceof Long;
1411     }
1412 
1413     /***
1414      * Is the value a isDouble
1415      *
1416      * @return true if is Double
1417      */
1418     public boolean isDouble()
1419     {
1420         return valueObject instanceof Double;
1421     }
1422 
1423     /***
1424      * Is the value a isFloat
1425      *
1426      * @return true if is Float
1427      */
1428     public boolean isFloat()
1429     {
1430         return valueObject instanceof Float;
1431     }
1432 
1433     /***
1434      * Is the value a isBoolean
1435      *
1436      * @return true if is Boolean
1437      */
1438     public boolean isBoolean()
1439     {
1440         return valueObject instanceof Boolean;
1441     }
1442 
1443     /***
1444      * Is the value a isNull
1445      *
1446      * @return true if is null
1447      */
1448     public boolean isNull()
1449     {
1450         return valueObject == null;
1451     }
1452 
1453     /***
1454      * Is the value a isString
1455      *
1456      * @return true if is String
1457      */
1458     public boolean isString()
1459     {
1460         return valueObject instanceof String;
1461     }
1462 
1463     /***
1464      * Is the value a isTime
1465      *
1466      * @return true if is java.sql.Time
1467      */
1468     public boolean isTime()
1469     {
1470         return valueObject instanceof java.sql.Time;
1471     }
1472 
1473     /***
1474      * Is the value a isTimestamp
1475      *
1476      * @return true if is java.sql.Timestamp
1477      */
1478     public boolean isTimestamp()
1479     {
1480         return valueObject instanceof java.sql.Timestamp;
1481     }
1482 
1483     /***
1484      * Is the value a isUtilDate
1485      *
1486      * @return true if is java.util.Date
1487      */
1488     public boolean isUtilDate()
1489     {
1490         return valueObject instanceof java.util.Date;
1491     }
1492 
1493     /***
1494      * Return the type of this value
1495      *
1496      * @return the type of this value
1497      */
1498     public int type()
1499     {
1500         return this.type;
1501     }
1502 
1503     /***
1504      * Gets the columnNumber which this value represents.
1505      *
1506      * @return an int
1507      */
1508     int columnNumber()
1509     {
1510         return this.columnNumber;
1511     }
1512 
1513     /***
1514      * DOCUMENT ME!
1515      *
1516      * @param value TODO: DOCUMENT ME!
1517      *
1518      * @return true if (true || t | yes | y | 1)
1519      */
1520     private boolean isTrue(String value)
1521     {
1522         return (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") 
1523                 || value.equalsIgnoreCase("yes")
1524         || value.equalsIgnoreCase("y") || value.equals("1"));
1525     }
1526 }