KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > database > DbSequenceManager


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

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.database;
108
109 import com.Yasna.forum.PropertyManager;
110
111 import java.util.HashMap JavaDoc;
112 import java.sql.*;
113
114 /**
115  * Manages sequences of unique ID's that get stored in the database. Database
116  * support for sequences varies widely; some don't support them at all. So,
117  * we handle unique ID generation with synchronized counters.<p>
118  *
119  * Selecting the initial ID for each sequence is trivial: we simply query the
120  * database to get the last highest unique ID, and then add 1 to that.<p>
121  *
122  * This method only works if a single instance of Yazd is pointed at a database.
123  * Otherwise, the unique ID's will stop being unique. :) A refined class that
124  * deals with this is coming soon.
125  */

126 public class DbSequenceManager {
127
128     private static final String JavaDoc NEXT_FORUM_ID = "SELECT max(forumID) FROM yazdForum";
129     private static final String JavaDoc NEXT_THREAD_ID = "SELECT max(threadID) FROM yazdThread";
130     private static final String JavaDoc NEXT_MESSAGE_ID = "SELECT max(messageID) FROM yazdMessage";
131     private static final String JavaDoc NEXT_GROUP_ID = "SELECT max(groupID) FROM yazdGroup";
132     private static final String JavaDoc NEXT_USER_ID = "SELECT max("+SystemProperty.getProperty("User.Column.UserID")+") FROM "+SystemProperty.getProperty("User.Table");
133     private static final String JavaDoc NEXT_CATEGORY_ID = "SELECT max(categoryID) FROM yazdCategory";
134     private static final String JavaDoc NEXT_FORUM_GROUP_ID = "SELECT max(forumGroupID) FROM yazdForumGroup";
135     /**
136      * Singleton type access to the class.
137      */

138     private static DbSequenceManager sequenceManager = null;
139
140     /**
141      * Lock so that we can synchronize during initialization.
142      */

143     private static Object JavaDoc initLock = new Object JavaDoc();
144
145     /**
146      * Returns the next ID of the specified type.
147      *
148      * @param type the type of unique ID.
149      * @return the next unique ID of the specified type.
150      */

151     public static int nextID(String JavaDoc type) {
152         if (sequenceManager == null) {
153             synchronized(initLock) {
154                 if (sequenceManager == null) {
155                     sequenceManager = new DbSequenceManager();
156                 }
157             }
158         }
159         return sequenceManager.nextUniqueID(type);
160     }
161
162     private HashMap JavaDoc uniqueIDCounters;
163     private HashMap JavaDoc keys;
164
165     /**
166      * Creates a new DbSequenceManager and initializes all of the counters.
167      */

168     public DbSequenceManager() {
169         keys = new HashMap JavaDoc();
170         keys.put("Forum",NEXT_FORUM_ID);
171         keys.put("ForumThread",NEXT_THREAD_ID);
172         keys.put("ForumMessage",NEXT_MESSAGE_ID);
173         keys.put("Group",NEXT_GROUP_ID);
174         keys.put("User",NEXT_USER_ID);
175         keys.put("Category",NEXT_CATEGORY_ID);
176         keys.put("ForumGroup",NEXT_FORUM_GROUP_ID);
177
178         uniqueIDCounters = new HashMap JavaDoc();
179
180         //Forum counter.
181
uniqueIDCounters.put("Forum", new Counter(getNextDbID((String JavaDoc)keys.get("Forum"))));
182         //ForumThread counter.
183
uniqueIDCounters.put("ForumThread", new Counter(getNextDbID((String JavaDoc)keys.get("ForumThread"))));
184         //ForumMessage counter.
185
uniqueIDCounters.put("ForumMessage", new Counter(getNextDbID((String JavaDoc)keys.get("ForumMessage"))));
186         //Group counter.
187
uniqueIDCounters.put("Group", new Counter(getNextDbID((String JavaDoc)keys.get("Group"))));
188         //User counter.
189
uniqueIDCounters.put("User", new Counter(getNextDbID((String JavaDoc)keys.get("User"))));
190         uniqueIDCounters.put("Category", new Counter(getNextDbID((String JavaDoc)keys.get("Category"))));
191         uniqueIDCounters.put("ForumGroup", new Counter(getNextDbID((String JavaDoc)keys.get("ForumGroup"))));
192     }
193
194     /**
195      * Provides the next available unique ID for a particular object type.
196      * Essentially this provides for the functionality of an auto-increment
197      * database field. Valid counter names are Forum, ForumThread, ForumMessage,
198      * Group, and User.
199      * <p>
200      * Those that are integrating Yazd into existing tables should be sure
201      * that the table names match up so that the correct starting unique
202      * id can be determined.
203      */

204     public int nextUniqueID(String JavaDoc counterName) {
205         if(Boolean.valueOf(PropertyManager.getProperty("Sequence.UseDatabase")).booleanValue()){
206             return getNextDbID((String JavaDoc)keys.get(counterName))+1;
207         } else {
208             Counter counter = (Counter)uniqueIDCounters.get(counterName);
209             return counter.next();
210         }
211     }
212
213     /**
214      * Do a lookup to see what the next available unique ID is for a
215      * particular table. Yazd uses the convention of always making the name of
216      * the column that holds the unique id be called id, so this works.
217      */

218     private int getNextDbID(String JavaDoc query) {
219         int currentID = 0;
220         Connection con = null;
221         PreparedStatement pstmt = null;
222
223         try {
224             con = DbConnectionManager.getConnection();
225             pstmt = con.prepareStatement(query);
226             ResultSet rs = pstmt.executeQuery();
227             rs.next();
228             currentID = rs.getInt(1);
229         }
230         catch( Exception JavaDoc sqle ) {
231             System.err.println("Error in DbSequenceManager:getNextDbID()-" + sqle);
232             sqle.printStackTrace();
233         }
234         finally {
235             try { pstmt.close(); }
236             catch (Exception JavaDoc e) { e.printStackTrace(); }
237             try { con.close(); }
238             catch (Exception JavaDoc e) { e.printStackTrace(); }
239         }
240         //If the table is empty, start with id 0
241
if (currentID < 0) {
242             currentID = 0;
243         }
244         return currentID;
245     }
246
247     /**
248      * Internal class to keep track of the current count of a unique id.
249      */

250     private final class Counter {
251
252         private int count;
253
254         public Counter(int currentCount) {
255             count = currentCount;
256         }
257
258         public final synchronized int next() {
259             return (++count);
260         }
261     }
262 }
263
Popular Tags