KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > AutoDBObject


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dbobj;
66
67 import com.jcorporate.expresso.core.dataobjects.DataException;
68 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
69 import com.jcorporate.expresso.core.db.DBConnection;
70 import com.jcorporate.expresso.core.db.DBException;
71 import com.jcorporate.expresso.core.misc.ConfigManager;
72 import com.jcorporate.expresso.core.misc.ConfigurationException;
73 import com.jcorporate.expresso.core.misc.StringUtil;
74
75 import java.sql.DatabaseMetaData JavaDoc;
76 import java.sql.ResultSet JavaDoc;
77 import java.sql.SQLException JavaDoc;
78
79
80 /**
81  * This class provides convenience methods by querying the database table it is
82  * set to and automatically setting up it's own fields that way.
83  * Creation date: (10/3/00 11:07:02 AM)
84  *
85  * @author Michael Nash
86  */

87 public class AutoDBObject
88         extends SecuredDBObject {
89     private static final String JavaDoc THIS_CLASS = "com.jcorporate.expreso.core.dbobj.AutoDBObject.";
90
91     /**
92      * AutoDBObject constructor comment.
93      *
94      * @throws com.jcorporate.expresso.core.db.DBException
95      * The exception description.
96      */

97     public AutoDBObject()
98             throws com.jcorporate.expresso.core.db.DBException {
99         super();
100     } /* AutoDBObject() */
101
102     /**
103      * AutoDBObject constructor comment.
104      *
105      * @param newConnection com.jcorporate.expresso.core.db.DBConnection
106      * @throws com.jcorporate.expresso.core.db.DBException
107      * The exception description.
108      */

109     public AutoDBObject(com.jcorporate.expresso.core.db.DBConnection newConnection)
110             throws com.jcorporate.expresso.core.db.DBException {
111         super(newConnection);
112     } /* AutoDBObject(com.jcorporate.expresso.core.db.DBConnection) */
113
114
115     /**
116      * Initialize an AutoDBObject with a uid.
117      *
118      * @param uid the user ID or -1 if System privileges
119      * @throws DBException upon error
120      */

121     public AutoDBObject(int uid)
122             throws DBException {
123         super(uid);
124     }
125
126     /**
127      * getThisDBObj method comment.
128      *
129      * @return a fully instantiated DBObject
130      */

131     public DBObject getThisDBObj()
132             throws com.jcorporate.expresso.core.db.DBException {
133         AutoDBObject returnObj = new AutoDBObject();
134         returnObj.setTargetTable(this.getDefinitionName());
135
136         return returnObj;
137     } /* getThisDBObj() */
138
139
140     /**
141      * Set the target table for this AutoDBObject - this causes the object to
142      * set itself up with the fields from the database as it's fields. All existing
143      * fields are cleared.
144      *
145      * @param theTable Table for this object
146      */

147     public synchronized void setTargetTable(String JavaDoc theTable)
148             throws DBException {
149         clear();
150         DBObjectDef myDef = new DBObjectDef();
151         myDef.setName(getClass().getName());
152         sMetadataMap.put(getClass().getName(), myDef);
153         super.setTargetTable(theTable);
154         setupFields();
155     } /* setTargetTable(String) */
156
157     /**
158      * Use this method to set the key to the definition name for the Defineable
159      * database object. The actual meaning of the definitionName may be different.
160      * For example, AutoDBObject's definition name is the database table name.
161      * JoinedDataObject's definition name is the classpath URL location to the
162      * definition XML file.
163      *
164      * @param definitionName java.lang.String, the actual definition of the
165      * dataobject.
166      * @throws DataException if the DataObject is unable to initialize itself
167      * with the given definition name.
168      */

169     public void setDefinitionName(String JavaDoc definitionName) throws DataException {
170         try {
171             this.setTargetTable(definitionName);
172         } catch (DBException ex) {
173             throw new DataException("Unable to set definition name for AutoDBObject"
174                     , ex);
175         }
176     }
177
178     /**
179      * Retrieve the key to the definition name for the given DBObject.
180      *
181      * @return the definition name.
182      */

183     public String JavaDoc getDefinitionName() {
184         try {
185             return ((JDBCObjectMetaData) this.getMetaData()).getTargetTable();
186         } catch (DataException ex) {
187             return null;
188         }
189     }
190
191
192     /**
193      * setupFields method. avoids superclass usage with test
194      * at top of method. called for real as a side-effect of setTargetTable()
195      */

196     protected void setupFields()
197             throws DBException {
198
199         if (StringUtil.notNull(this.getDefinitionName()).equals("")) {
200             return;
201         }
202
203         DBConnection myConnection = null;
204
205         try {
206             if (localConnection != null) {
207                 myConnection = localConnection;
208             } else {
209                 String JavaDoc myName = (THIS_CLASS + "setupFields()");
210                 myConnection = this.getConnectionPool().getConnection(myName);
211             }
212
213             DatabaseMetaData JavaDoc dm = myConnection.getDBMetaData();
214             String JavaDoc tableName = getDefinitionName().toUpperCase();
215             setDescription("Automatic Object for " + tableName);
216             try {
217                 // hsqldb bug - can't figure out a way in hsqldb 1.6.1 to get keys - getPrimaryKeys returns SYSTEM_ID
218
// as the column name. So, just return exception.
219
if (ConfigManager.getJdbcRequired(this.getDataContext()).getDriver().equals("org.hsqldb.jdbcDriver")) {
220                     throw new DBException(this.getClass() + " does not support hsqldb.");
221                 }
222
223                 //Postgresql JDBC 7.2 driver bug. Tablename must be lower case
224
//which doesn't apply to other databases such as Oracle
225
if (ConfigManager.getJdbcRequired(this.getDataContext()).getDriver()
226                         .equals("org.postgresql.Driver")) {
227
228                     tableName = tableName.toLowerCase();
229                 }
230             } catch (ConfigurationException ce) {
231                 ce.printStackTrace();
232                 throw new DBException(ce);
233             }
234             setDescription("Automatic Object for " + tableName);
235
236             ResultSet JavaDoc rs = dm.getColumns(null, "%", tableName, "%");
237             String JavaDoc fieldName = null;
238             String JavaDoc fieldType;
239             int fieldSize;
240             int nullField = 0;
241             boolean nullable;
242             String JavaDoc fieldDescrip = null;
243
244             while (rs.next()) {
245                 fieldName = rs.getString(4);
246                 fieldSize = rs.getInt(7);
247                 fieldDescrip = rs.getString(12);
248
249                 if (fieldDescrip == null) {
250                     fieldDescrip = fieldName;
251                 }
252
253                 fieldType = rs.getString(5);
254                 nullField = rs.getInt(11);
255
256                 if (nullField == DatabaseMetaData.columnNoNulls) {
257                     nullable = false;
258                 } else {
259                     nullable = true;
260                 }
261                 if (nullable) {
262                     addField(fieldName, fieldType, fieldSize, true,
263                             fieldDescrip);
264                 } else {
265                     addField(fieldName, fieldType, fieldSize, false,
266                             fieldDescrip);
267                 }
268             }
269
270             /* for each field */
271             ResultSet JavaDoc rspk = dm.getPrimaryKeys(null, "%", tableName);
272
273             while (rspk.next()) {
274                 addKey(rspk.getString(4));
275             }
276         } catch (SQLException JavaDoc se) {
277             throw new DBException(se);
278         } finally {
279             if (localConnection == null) {
280                 myConnection.release();
281             }
282         }
283     } /* setupFields() */
284
285
286 }
287
288 /* AutoDBObject */
289
Popular Tags