KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
68  * DBSequence.java
69  *
70  * Written by Adam Rossi
71  * PlatinumSolutions, Inc.
72  * adam.rossi@platinumsolutions.com
73  *
74  * Copyright 2000, 2001 Jcorporate Ltd.
75  */

76
77 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData;
78 import com.jcorporate.expresso.core.db.DBConnection;
79 import com.jcorporate.expresso.core.db.DBException;
80
81 import java.util.Iterator JavaDoc;
82 import java.util.Vector JavaDoc;
83
84
85 /**
86  * DBSequence is a special DBObject that wraps a database sequence.
87  * Currently, this class only works with PostgreSQL sequences, but it
88  * should be easy to add support for Oracle or others (see createTable() and
89  * retrieve() methods specifically). If you attempt to use this with a database
90  * other than PostgreSQL, it will throw an exception.
91  * <p/>
92  * To make this compatible with the the schema autocreation of DBObjects magic,
93  * security scheme, etc., this class subclasses DBObject. But there are many
94  * methods that have been overriden, because they do not make sense for a sequence
95  * (ex: search, clear, etc). If these methods are called, an exception is thrown.
96  * <p/>
97  * To get the next value in the sequence, call getNext().
98  * <p/>
99  * make sure that you override the setupFields() method and supply the proper
100  * setup parameters.
101  * <p/>
102  * ex:
103  * <p/>
104  * super.setupFields() //Always!
105  * setTargetTable("SomeSequence");
106  * setDescription("Some example Database Sequence");
107  * this.setIncrement(new Integer(1)); // set the increment for the sequence
108  * this.setMinValue(new Integer(1)); // set the min value
109  * this.setMaxValue(new Integer(1000)); //set the max value (optional)
110  * this.setStart(new Integer(1)); //set the start value (optional)
111  *
112  * @author Adam Rossi
113  */

114 public abstract class DBSequence
115         extends SecuredDBObject {
116
117     private static final String JavaDoc THIS_CLASS = DBSequence.class.getName();
118
119     private Integer JavaDoc increment = null;
120     private Integer JavaDoc minvalue = null;
121     private Integer JavaDoc maxvalue = null;
122     private Integer JavaDoc start = null;
123
124     /**
125      * DBSequence constructor comment.
126      *
127      * @throws com.jcorporate.expresso.core.db.DBException
128      * The exception description.
129      */

130     public DBSequence()
131             throws com.jcorporate.expresso.core.db.DBException {
132         super();
133     } /* DBSequence() */
134
135     /**
136      * Creation date: (4/7/00 2:45:10 PM)
137      *
138      * @param theConnection com.jcorporate.expresso.core.db.DBConnection
139      */

140     public DBSequence(DBConnection theConnection)
141             throws DBException {
142         super(theConnection);
143     } /* DBSequence(DBConnection) */
144
145
146     public DBSequence(int uid)
147             throws DBException {
148         super(uid);
149     }
150
151     /**
152      * @throws DBException
153      */

154     public void add()
155             throws DBException {
156         throw new DBException("You cannot add to a sequence");
157     } /* add() */
158
159
160     /**
161      * @throws com.jcorporate.expresso.core.db.DBException
162      * The exception description.
163      */

164     public synchronized void clear()
165             throws DBException {
166         throw new DBException("Cannot call clear() on a DBSequence");
167     } /* clear() */
168
169
170     /**
171      * Create the sequence in the database. Assumes it is not there already.
172      */

173     public synchronized void createTable()
174             throws DBException {
175         String JavaDoc myName = (THIS_CLASS + "createTable()");
176         String JavaDoc createStatement = ("CREATE SEQUENCE ");
177         DBConnection myConnection = null;
178
179         try {
180             myConnection = this.getConnectionPool().getConnection(myName);
181             createStatement = createStatement + getJDBCMetaData().getTargetTable();
182
183             /* If we're connection to PostgreSQL, this is how we make the */
184             /* sequence SQL: */
185             if (myConnection.getDBDriver().equals("postgresql.Driver") ||
186                     myConnection.getDBDriver().equals("org.postgresql.Driver")) {
187                 if (increment != null) {
188                     createStatement = createStatement + " increment = " +
189                             increment.intValue();
190                 }
191                 if (minvalue != null) {
192                     createStatement = createStatement + " minvalue = " +
193                             minvalue.intValue();
194                 }
195                 if (maxvalue != null) {
196                     createStatement = createStatement + " maxvalue = " +
197                             maxvalue.intValue();
198                 }
199                 if (start != null) {
200                     createStatement = createStatement + " start = " +
201                             start.intValue();
202                 }
203             } else {
204                 throw new DBException("Only PostgreSQL currently supported");
205             }
206
207             myConnection.executeUpdate(createStatement);
208         } catch (DBException de) {
209             throw de;
210         } finally {
211             myConnection.release();
212         }
213     } /* createTable() */
214
215
216     /**
217      * @throws com.jcorporate.expresso.core.db.DBException
218      * The exception description.
219      */

220     public void delete()
221             throws com.jcorporate.expresso.core.db.DBException {
222         throw new DBException("Can't Delete from a sequence");
223     } /* delete() */
224
225
226     /**
227      * @return boolean
228      * @throws com.jcorporate.expresso.core.db.DBException
229      * The exception description.
230      */

231     public boolean find()
232             throws com.jcorporate.expresso.core.db.DBException {
233         throw new DBException("Can't execute find on a DBSequence");
234     } /* find() */
235
236
237     /**
238      * @return long
239      */

240     public long getNext()
241             throws DBException {
242         this.retrieve();
243
244         long returnValue;
245
246         try {
247             returnValue = new Long JavaDoc(getField("nextval")).longValue();
248         } catch (NumberFormatException JavaDoc ne) {
249             throw new DBException("DBSequence.getNext: Could not convert \'" +
250                     getField("nextval") + "\' to a number:" +
251                     ne.getMessage());
252         } catch (Exception JavaDoc e) {
253             throw new DBException("DBSequence.getNext: " + e.toString());
254         }
255
256         return returnValue;
257     } /* getNext() */
258
259
260     /**
261      * NOTE: Subclass must override. This method should return a new object
262      * of the type of the subclass
263      *
264      * @return DBObject A newly allocated object of the subclass's class
265      */

266     public abstract DBObject getThisDBObj()
267             throws DBException;
268
269     /* getThisDBObj() */
270     /**
271      * @throws com.jcorporate.expresso.core.db.DBException
272      * The exception description.
273      */

274     public void retrieve()
275             throws com.jcorporate.expresso.core.db.DBException {
276         String JavaDoc myName = (THIS_CLASS + "retrieve()");
277         DBConnection myConnection = null;
278         JDBCObjectMetaData metadata = this.getJDBCMetaData();
279         try {
280             myConnection = this.getConnectionPool().getConnection(myName);
281
282             String JavaDoc myStatement = "SELECT ";
283
284             /* If we're connection to PostgreSQL, this is how we get a */
285             /* sequence number: */
286             if (myConnection.getDBDriver().equals("postgresql.Driver") ||
287                     myConnection.getDBDriver().equals("org.postgresql.Driver")) {
288                 myStatement = myStatement + "nextval('" + metadata.getTargetTable() +
289                         "') as nextval";
290             } else {
291                 throw new DBException("DBSequence only supported in PostgreSQL");
292             }
293             if (myConnection == null) {
294                 throw new DBException(myName + ":No connection - object not " +
295                         " correctly initialized (" + metadata.getName() +
296                         ") No such record");
297             }
298             try {
299                 myConnection.execute(myStatement);
300
301                 String JavaDoc oneFieldName = ("");
302                 String JavaDoc oneFieldValue = ("");
303
304                 if (myConnection.next()) {
305                     int i = 1;
306
307                     for (Iterator JavaDoc e = metadata.getFieldListArray().iterator();
308                          e.hasNext();) {
309                         oneFieldName = (String JavaDoc) e.next();
310
311                         try {
312
313                             //we assume here that the result will be an int
314
int myint = myConnection.getInt(i);
315                             oneFieldValue = "" + myint;
316                         } catch (DBException de1) {
317                             throw new DBException(myName + ": (" + metadata.getName() +
318                                     ") Error retrieving field '" +
319                                     oneFieldName + "' index " + i +
320                                     ":" + de1.getMessage(),
321                                     de1.getDBMessage());
322                         }
323
324                         i++;
325                         setField(oneFieldName, oneFieldValue);
326                     }
327                 } else { /* for each field */
328                     throw new DBException(myName + ": (" + metadata.getName() +
329                             ") No such record");
330                 }
331             } catch (DBException de) {
332                 throw de;
333             } finally {
334                 myConnection.release();
335             }
336         } catch (Exception JavaDoc e) {
337             throw new DBException("DBSequence retrieve error: " +
338                     e.toString());
339         }
340     } /* retrieve() */
341
342
343     /**
344      * @throws com.jcorporate.expresso.core.db.DBException
345      * The exception description.
346      */

347     public synchronized void search()
348             throws com.jcorporate.expresso.core.db.DBException {
349         throw new DBException("Can't search on a DBSequence");
350     } /* search() */
351
352
353     /**
354      * @return java.util.Vector
355      * @throws com.jcorporate.expresso.core.db.DBException
356      * The exception description.
357      */

358     public synchronized Vector JavaDoc searchAndRetrieve()
359             throws com.jcorporate.expresso.core.db.DBException {
360         throw new DBException("Can't search on a DBSequence");
361     } /* searchAndRetrieve() */
362
363
364     /**
365      * @param sortKeyString java.lang.String
366      * @return java.util.Vector
367      * @throws com.jcorporate.expresso.core.db.DBException
368      * The exception description.
369      */

370     public synchronized Vector JavaDoc searchAndRetrieve(String JavaDoc sortKeyString)
371             throws com.jcorporate.expresso.core.db.DBException {
372         throw new DBException("can't search on a DBSequence");
373     } /* searchAndRetrieve(String) */
374
375
376     /**
377      * Size of the increment step (defaults to 1 usually in most DB's).
378      *
379      * @param I java.lang.Integer
380      */

381     public void setIncrement(Integer JavaDoc I) {
382         increment = I;
383     } /* setIncrement(Integer) */
384
385     /**
386      * Maximum value for the sequence.
387      *
388      * @param I java.lang.Integer
389      */

390     public void setMaxValue(Integer JavaDoc I) {
391         maxvalue = I;
392     } /* setMaxValue(Integer) */
393
394     /**
395      * Minumum value for the sequence when created
396      *
397      * @param I java.lang.Integer
398      */

399     public void setMinValue(Integer JavaDoc I) {
400         minvalue = I;
401     } /* setMinValue(Integer) */
402
403     /**
404      * Where the sequence starts
405      *
406      * @param I java.lang.Integer
407      */

408     public void setStart(Integer JavaDoc I) {
409         start = I;
410     } /* setStart(Integer) */
411
412     /**
413      * make sure that you override the setupFields() method and supply the
414      * proper setup parameters.
415      * <p/>
416      * ex:
417      * <p/>
418      * super.setupFields() //Always!
419      * setTargetTable("SomeSequence");
420      * setDescription("Some example Database Sequence");
421      * this.setIncrement(new Integer(1)); // set the increment for the sequence
422      * this.setMinValue(new Integer(1)); // set the min value
423      * this.setMaxValue(new Integer(1000)); //set the max value (optional)
424      * this.setStart(new Integer(1)); //set the start value (optional)
425      */

426     protected void setupFields()
427             throws com.jcorporate.expresso.core.db.DBException {
428         addField("nextval", "int", 0, false, "Next Number Value");
429     } /* setupFields() */
430
431
432     /**
433      * Not allowed...
434      *
435      * @throws com.jcorporate.expresso.core.db.DBException
436      * The exception description.
437      */

438     public void update()
439             throws com.jcorporate.expresso.core.db.DBException {
440         throw new DBException("Can't call update on a DBSequence");
441     } /* update() */
442
443
444     /**
445      * Does nothing...I am not sure what do to verify a sequence.
446      */

447     public void verify()
448             throws com.jcorporate.expresso.core.db.DBException {
449     } /* verify() */
450
451
452 } /* DBSequence */
453
454 /* DBSequence */
Popular Tags