KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdbc.JDBCObjectMetaData;
68 import com.jcorporate.expresso.core.db.DBException;
69 import com.jcorporate.expresso.core.misc.ConfigManager;
70 import com.jcorporate.expresso.core.misc.StringUtil;
71 import com.jcorporate.expresso.kernel.util.ClassLocator;
72 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
73 import org.apache.log4j.Logger;
74
75
76 /**
77  * Base class for pluggable next number autoincrementing system.
78  *
79  * @author Original by Michael Nash, rewritten by Michael Rimov
80  * <p/>
81  * Modify by Yves Henri AMAIZO <amy_amaizo@compuserve.com>
82  * @see com.jcorporate.expresso.core.dbobj.NextNumberImpl for more information
83  * @since $DatabaseSchema $Date: 2004/11/17 20:48:11 $
84  */

85 abstract public class NextNumber {
86     public static final String JavaDoc DEFAULT_CLASS_HANDLER = NextNumberImpl.class.getName();
87
88     /**
89      * Set this to false and recompile to cause NextNumber to not check it's input
90      * parameters. May result in NullPointerExceptions if there's bugs in the
91      * code. But will result in a speed increase.
92      */

93     protected static final boolean CHECK_PARAMETERS = true;
94
95     /**
96      * The lo4j log category.
97      */

98     protected static Logger log = Logger.getLogger(NextNumber.class);
99
100     /**
101      * The actual static instance of the Next Number implementation
102      */

103     protected static NextNumber theInstance = null;
104
105     /**
106      * Constructor - Do not call directly. Use getInstance() instead.
107      */

108     protected NextNumber() {
109     } /* NextNumber() */
110
111     /**
112      * Factory Method returns a constructed instance of the NextNumber Manager.
113      *
114      * @return instantiated NextNumber object
115      */

116     public static synchronized NextNumber getInstance()
117             throws DBException {
118         if (theInstance == null) {
119             // retest after getting sync
120
if (theInstance == null) {
121                 String JavaDoc classHandler = null;
122                 classHandler = ConfigManager.getClassHandler("nextNumber");
123
124                 if (classHandler == null) {
125                     classHandler = DEFAULT_CLASS_HANDLER;
126                 }
127                 try {
128                     theInstance = (NextNumber) ClassLocator.loadClass(classHandler).newInstance();
129                 } catch (ClassNotFoundException JavaDoc cnfe) {
130                     log.error("Class Not Found: " + classHandler, cnfe);
131                     throw new DBException(cnfe);
132                 } catch (IllegalAccessException JavaDoc iae) {
133                     log.error("Class Not Found: " + classHandler, iae);
134                     throw new DBException(iae);
135                 } catch (InstantiationException JavaDoc ine) {
136                     log.error("Class Not Found: " + classHandler, ine);
137                     throw new DBException(ine);
138                 }
139             }
140         }
141         return theInstance;
142     }
143
144     /**
145      * Gets the maximum value for a particular object and field Used if there's
146      * no value loaded in memory or state.
147      *
148      * @param db The dataContext to get for
149      * @param callingObject the calling DBObject
150      * @param oneField the field name to query
151      * @return long value covering the max used for the field so far
152      */

153     protected long getMax(String JavaDoc db, DBObject callingObject, String JavaDoc oneField)
154             throws DBException {
155         callingObject.setDataContext(db);
156
157         String JavaDoc value = callingObject.getMax(oneField);
158         long lo;
159
160         if (StringUtil.notNull(value).length() == 0) {
161             return 0;
162         }
163         try {
164             lo = Long.parseLong(value);
165         } catch (NumberFormatException JavaDoc nfe) {
166             throw new DBException("Error converting max values for field: " +
167                     oneField, nfe);
168         }
169
170         return (lo); //Nextnumber will be max + 1.
171
}
172
173     /**
174      * Register a field for next number information. This may happen even if it isn't an
175      * auto-inc field.
176      *
177      * @param db The database context to work with.
178      * @param callingDBObject The calling database object to register the field for
179      * @param fieldName The field name to register into the NextNumber engine
180      */

181     abstract public void registerField(String JavaDoc db, DBObject callingDBObject,
182                                        String JavaDoc fieldName)
183             throws DBException;
184
185     /**
186      * Get the nextnumber for this dbobject. Increments the internal value.
187      *
188      * @param db The datacontext. MAKE SURE THIS IS CORRECT or else you will get duplicate running counts for each incorrect DB name, and thus Duplicate key errors when trying to write rows.
189      * @param callingDBObject The calling DBOBject
190      * @param fieldName the name of the field to get the next number value for
191      * @return long integer representing the next number to use for that field
192      */

193     abstract public long getNext(String JavaDoc db, DBObject callingDBObject,
194                                  String JavaDoc fieldName)
195             throws DBException;
196
197     /**
198      * Reset the counts for the paritcular db each subsequent operation will
199      * require a new getMax(). There is questionable threadsafety about the
200      * reset methods. Please only do it on a "non-live" server. Used after a
201      * DBCreate or DeleteSchema has been called.
202      *
203      * @param db the db context to clear all the next number values for.
204      */

205     abstract public void reset(String JavaDoc db);
206
207     /**
208      * Clears the table on a particular dbobject. Similar to reset db, but
209      *
210      * @param db The db context that the next number resides in.
211      * @param callingObject the object that links to the various nextnumber
212      * objects
213      */

214     abstract public void reset(String JavaDoc db, DBObject callingObject);
215
216     /**
217      * <p>Fill in for potential resource removal Should be called by ConfigInit.
218      * destroy() Or by unit tests to reset everything to a pristine state.</p>
219      * <i>Note:</i> Do not call getInstance() to call destroy. Simply use
220      * NextNumber.destroy();
221      */

222     public static synchronized void destroy() {
223         if (theInstance == null) {
224             return;
225         } else {
226
227             //Clear static variables.
228
theInstance = null;
229         }
230     }
231
232     /**
233      * Builds the key string for lookup within the dbobject hashmap.
234      *
235      * @param callingDBObject the calling DBObject
236      * @param fieldName the fieldName to get the key for
237      * @return a string formatted in [callingDBObject].[fieldName]
238      * @throws DBException upon error
239      * <p/>
240      * Modify by Yves Henri AMAIZO <amy_amaizo@compuserve.com>
241      * @since $DatabaseSchema $Date: 2004/11/17 20:48:11 $
242      */

243     protected String JavaDoc getKey(DBObject callingDBObject, String JavaDoc fieldName)
244             throws DBException {
245         FastStringBuffer result = FastStringBuffer.getInstance();
246         String JavaDoc returnValue = null;
247         try {
248             result.append(((JDBCObjectMetaData) callingDBObject.getMetaData()).getTargetTable());
249             result.append(".");
250             result.append(fieldName);
251             returnValue = result.toString();
252         } finally {
253             result.release();
254             result = null;
255         }
256
257         return returnValue;
258     }
259
260 } /* NextNumber */
261
Popular Tags