KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > listOfValues > util > LovTypeManager


1 /**
2  * Copyright (c) 2002 Bright Side Factory. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * 3. The end-user documentation included with the redistribution,
17  * if any, must include the following acknowledgment:
18  * "This product includes software developed by the
19  * Bright Side Factory (http://www.bs-factory.org/)."
20  * Alternately, this acknowledgment may appear in the software itself,
21  * if and wherever such third-party acknowledgments normally appear.
22  *
23  * 4. The names "Bright Side", "BS Factory" and "Bright Side Factory" must
24  * not be used to endorse or promote products derived from this
25  * software without prior written permission. For written
26  * permission, please contact info@bs-factory.org.
27  *
28  * 5. Products derived from this software may not be called "Bright Side",
29  * nor may "Bright Side" appear in their name, without prior written
30  * permission of the Apache Software Foundation.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of the Bright Side Factory. For more
48  * information on the Bright Side Factory, please see
49  * <http://www.bs-factory.org/>.
50  */

51 package org.bsf.listOfValues.util;
52
53 import java.sql.ResultSet JavaDoc;
54 import java.sql.SQLException JavaDoc;
55
56 /**
57  * This class contains the definition of the type used by the LovBean and the
58  * methods to handle the resultset regarding those types. Boolean are handled
59  * from 0(false) and 1 (true). The type comparaisons are case insensitive and
60  * check with Strings like "java.lang.Double" or "java.lang.String"...
61  *
62  */

63 public class LovTypeManager {
64     // The available types
65
public static final String JavaDoc BOOLEAN_TYPE = "java.lang.Boolean";
66     public static final String JavaDoc DOUBLE_TYPE = "java.lang.Double";
67     public static final String JavaDoc FLOAT_TYPE = "java.lang.Float";
68     public static final String JavaDoc INTEGER_TYPE = "java.lang.Integer";
69     public static final String JavaDoc LONG_TYPE = "java.lang.Long";
70     public static final String JavaDoc STRING_TYPE = "java.lang.String";
71     public static final String JavaDoc DATE_TYPE = "java.util.Date";
72     public static final String JavaDoc SQL_DATE_TYPE = "java.sql.Date";
73
74     /**
75      * @param p_rs The resultset to use as source of the needed Object.
76      * @param p_columnIndex The index of the column that we want to retrieve.
77      *
78      * @return The desired Object formatted as an Integer or null if the Object
79      * in the resultset is null.
80      *
81      * @throws SQLException If a database access error occurs.
82      */

83     public static Integer JavaDoc getInteger( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
84         if ( p_rs.getBigDecimal( p_columnIndex ) != null ) {
85             return new Integer JavaDoc( p_rs.getInt( p_columnIndex ) );
86         } else {
87             return null;
88         }
89     }
90
91     /**
92      * @param p_rs The resultset to use as source of the needed Object.
93      * @param p_columnIndex The index of the column that we want to retrieve.
94      *
95      * @return The desired Object formatted as a Long or null if the Object
96      * in the resultset is null.
97      *
98      * @throws SQLException If a database access error occurs.
99      */

100     public static Long JavaDoc getLong( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
101         if ( p_rs.getBigDecimal( p_columnIndex ) != null ) {
102             return new Long JavaDoc( p_rs.getLong( p_columnIndex ) );
103         } else {
104             return null;
105         }
106     }
107
108     /**
109      * @param p_rs The resultset to use as source of the needed Object.
110      * @param p_columnIndex The index of the column that we want to retrieve.
111      *
112      * @return The desired Object formatted as a String.
113      *
114      * @throws SQLException If a database access error occurs.
115      */

116     public static String JavaDoc getString( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
117         return p_rs.getString( p_columnIndex );
118     }
119
120     /**
121      * @param p_rs The resultset to use as source of the needed Object.
122      * @param p_columnIndex The index of the column that we want to retrieve.
123      *
124      * @return The desired Object formatted as a Boolean (0 is handled as a
125      * Boolean.FALSE while 1 is handled as a Boolean.TRUE) or null if the Object
126      * in the resultset is null.
127      *
128      * @throws SQLException If a database access error occurs.
129      */

130     public static Boolean JavaDoc getBoolean( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
131         if ( p_rs.getString( p_columnIndex ) != null ) {
132             return p_rs.getString( p_columnIndex ).equals( "0" ) ? Boolean.FALSE : Boolean.TRUE;
133         } else {
134             return null;
135         }
136     }
137
138     /**
139      * @param p_rs The resultset to use as source of the needed Object.
140      * @param p_columnIndex The index of the column that we want to retrieve.
141      *
142      * @return The desired Object formatted as a Double or null if the Object
143      * in the resultset is null.
144      *
145      * @throws SQLException If a database access error occurs.
146      */

147     public static Double JavaDoc getDouble( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
148         if ( p_rs.getBigDecimal( p_columnIndex ) != null ) {
149             return new Double JavaDoc( p_rs.getDouble( p_columnIndex ) );
150         } else {
151             return null;
152         }
153     }
154
155     /**
156      * @param p_rs The resultset to use as source of the needed Object.
157      * @param p_columnIndex The index of the column that we want to retrieve.
158      *
159      * @return The desired Object formatted as a Float or null if the Object
160      * in the resultset is null.
161      *
162      * @throws SQLException If a database access error occurs.
163      */

164     public static Float JavaDoc getFloat( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
165         if ( p_rs.getBigDecimal( p_columnIndex ) != null ) {
166             return new Float JavaDoc( p_rs.getFloat( p_columnIndex ) );
167         } else {
168             return null;
169         }
170     }
171
172     /**
173      * @param p_rs The resultset to use as source of the needed Object.
174      * @param p_columnIndex The index of the column that we want to retrieve.
175      *
176      * @return The desired Object formatted as a Date or null if the Object
177      * in the resultset is null.
178      *
179      * @throws SQLException If a database access error occurs.
180      */

181     public static java.util.Date JavaDoc getDate( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
182         if ( p_rs.getDate( p_columnIndex ) != null ) {
183             return new java.util.Date JavaDoc( p_rs.getDate( p_columnIndex ).getTime() );
184         } else {
185             return null;
186         }
187     }
188
189     /**
190      * @param p_rs The resultset to use as source of the needed Object.
191      * @param p_columnIndex The index of the column that we want to retrieve.
192      *
193      * @return The desired Object formatted as a SQL Date or null if the Object
194      * in the resultset is null.
195      *
196      * @throws SQLException If a database access error occurs.
197      */

198     public static java.sql.Date JavaDoc getSqlDate( ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
199         if ( p_rs.getDate( p_columnIndex ) != null ) {
200             return new java.sql.Date JavaDoc( p_rs.getDate( p_columnIndex ).getTime() );
201         } else {
202             return null;
203         }
204     }
205
206     /**
207      * @param p_type The type to use for the returned Object (should be a String
208      * like "java.lang.Long" or "java.lang.String".
209      * @param p_rs The resultset to use as source of the needed Object.
210      * @param p_columnIndex The index of the column that we want to retrieve.
211      *
212      * @return The desired Object retrieve from the resultset as the given type or
213      * null if the Object in the resultset is null.
214      *
215      * @throws SQLException If a database access error occurs.
216      * @throws IllegalArgumentException if the type or the resultset is null or
217      * if the type is unknown.
218      */

219     public static Object JavaDoc getObjectOfType( String JavaDoc p_type, ResultSet JavaDoc p_rs, int p_columnIndex ) throws SQLException JavaDoc {
220         if ( p_type == null || p_rs == null ) {
221             String JavaDoc msg = "Need a non null typeOID and resultset to return an Object...";
222             throw new IllegalArgumentException JavaDoc( msg );
223         }
224
225         if ( INTEGER_TYPE.equalsIgnoreCase( p_type ) ) {
226             return getInteger( p_rs, p_columnIndex );
227         } else if ( LONG_TYPE.equalsIgnoreCase( p_type ) ) {
228             return getLong( p_rs, p_columnIndex );
229         } else if ( STRING_TYPE.equalsIgnoreCase( p_type ) ) {
230             return getString( p_rs, p_columnIndex );
231         } else if ( BOOLEAN_TYPE.equalsIgnoreCase( p_type ) ) {
232             return getBoolean( p_rs, p_columnIndex );
233         } else if ( DOUBLE_TYPE.equalsIgnoreCase( p_type ) ) {
234             return getDouble( p_rs, p_columnIndex );
235         } else if ( FLOAT_TYPE.equalsIgnoreCase( p_type ) ) {
236             return getFloat( p_rs, p_columnIndex );
237         } else if ( DATE_TYPE.equalsIgnoreCase( p_type ) ) {
238             return getDate( p_rs, p_columnIndex );
239         } else if ( SQL_DATE_TYPE.equalsIgnoreCase( p_type ) ) {
240             return getSqlDate( p_rs, p_columnIndex );
241         }
242
243         throw new IllegalArgumentException JavaDoc( "Unknown type " + p_type );
244     }
245 }
Popular Tags