KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.Yasna.forum.database;
55
56 import java.util.Iterator JavaDoc;
57 import java.util.Enumeration JavaDoc;
58 import java.util.Properties JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Date JavaDoc;
61 import java.sql.*;
62 import java.io.*;
63
64 import com.Yasna.forum.*;
65 import com.Yasna.forum.filter.*;
66 import com.Yasna.util.Cache;
67 import com.Yasna.util.Cacheable;
68 import com.Yasna.util.CacheSizes;
69
70 /**
71  * Database implementation of the ForumGroup interface. It loads and stores forumGroup
72  * information from the database.
73  *
74  * @see ForumGroup
75  */

76 public class DbForumGroup implements ForumGroup, Cacheable {
77
78     /** DATABASE QUERIES **/
79     private static final String JavaDoc LOAD_FORUM_GROUP_BY_ID =
80         "SELECT forumGroupID, name, description, creationDate, modifiedDate,grporder FROM yazdForumGroup WHERE forumGroupID=?";
81     private static final String JavaDoc UPDATE_FORUM_GROUP_MODIFIED_DATE =
82         "UPDATE yazdForumGroup SET modifiedDate=? WHERE forumGroupID=?";
83     private static final String JavaDoc ADD_FORUM_GROUP =
84         "INSERT INTO yazdForumGroup(forumGroupID, categoryID, name, description, creationDate, " +
85         "modifiedDate,grporder) VALUES (?,?,?,?,?,?,0)";
86     private static final String JavaDoc SAVE_FORUM_GROUP =
87         "UPDATE yazdForumGroup SET name=?, description=?, creationDate=?, " +
88         "modifiedDate=?,grporder=? WHERE forumGroupID=?";
89
90     private int id = -1;
91     private String JavaDoc name;
92     private String JavaDoc description;
93     private int categoryID;
94     private int grporder=0;
95     private java.util.Date JavaDoc creationDate;
96     private java.util.Date JavaDoc modifiedDate;
97
98     private DbForumFactory factory;
99     private DbCategory category;
100
101     /**
102      * Loads a forumGroup with the specified id.
103      */

104     protected DbForumGroup(int id, DbCategory category, DbForumFactory factory)
105             throws ForumGroupNotFoundException
106     {
107         this.id = id;
108         this.category = category;
109         this.factory = factory;
110         loadFromDb();
111     }
112
113     /**
114      * Creats new forumGroup.
115      */

116     protected DbForumGroup(String JavaDoc name, String JavaDoc description, DbCategory category, DbForumFactory factory)
117             throws UnauthorizedException
118     {
119         this.id = DbSequenceManager.nextID("ForumGroup");
120         this.name = name;
121         this.categoryID = category.getID();
122         this.description = description;
123         long now = System.currentTimeMillis();
124         creationDate = new java.util.Date JavaDoc(now);
125         modifiedDate = new java.util.Date JavaDoc(now);
126         this.category = category;
127         this.factory = factory;
128         insertIntoDb();
129     }
130
131     //FROM THE CATEGORY INTERFACE//
132

133     public int getID() {
134         return id;
135     }
136
137     public String JavaDoc getName() {
138         return name;
139     }
140     public int getOrder(){
141         return grporder;
142     }
143     public void setOrder(int param){
144         this.grporder = param;
145         saveToDb();
146     }
147     public void setName(String JavaDoc name) throws UnauthorizedException {
148         this.name = name;
149         saveToDb();
150     }
151
152     public String JavaDoc getDescription() {
153         return description;
154     }
155
156     public void setDescription(String JavaDoc description) throws UnauthorizedException
157     {
158         this.description = description;
159         saveToDb();
160     }
161
162     public java.util.Date JavaDoc getCreationDate() {
163         return creationDate;
164     }
165
166     public void setCreationDate(java.util.Date JavaDoc creationDate)
167             throws UnauthorizedException
168     {
169        this.creationDate = creationDate;
170        saveToDb();
171     }
172
173     public java.util.Date JavaDoc getModifiedDate() {
174         return modifiedDate;
175     }
176
177     public void setModifiedDate(java.util.Date JavaDoc modifiedDate)
178             throws UnauthorizedException
179     {
180         this.modifiedDate = modifiedDate;
181         saveToDb();
182     }
183     public Iterator JavaDoc forumGroups() {
184         return null;//new DbForumIterator(this, factory);
185
}
186
187
188     //FROM THE CACHEABLE INTERFACE//
189

190     public int getSize() {
191         //Approximate the size of the object in bytes by calculating the size
192
//of each field.
193
int size = 0;
194         size += CacheSizes.sizeOfObject(); //overhead of object
195
size += CacheSizes.sizeOfInt(); //id
196
size += CacheSizes.sizeOfString(name); //name
197
size += CacheSizes.sizeOfString(description); //description
198
size += CacheSizes.sizeOfDate(); //creation date
199
size += CacheSizes.sizeOfDate(); //modified date
200
size += CacheSizes.sizeOfObject(); //save lock
201
size += CacheSizes.sizeOfInt(); //group order
202

203         return size;
204     }
205
206     /**
207      * Updates the modified date but doesn't require a security check since
208      * it is a protected method.
209      */

210     protected void updateModifiedDate(java.util.Date JavaDoc modifiedDate) {
211         this.modifiedDate = modifiedDate;
212         Connection con = null;
213         PreparedStatement pstmt = null;
214         try {
215             con = DbConnectionManager.getConnection();
216             pstmt = con.prepareStatement(UPDATE_FORUM_GROUP_MODIFIED_DATE);
217             pstmt.setString(1, ""+modifiedDate.getTime());
218             pstmt.setInt(2, id);
219             pstmt.executeUpdate();
220         }
221         catch( SQLException sqle ) {
222             System.err.println("Error in DbCategory:updateModifiedDate()-" + sqle);
223             sqle.printStackTrace();
224         }
225         finally {
226             try { pstmt.close(); }
227             catch (Exception JavaDoc e) { e.printStackTrace(); }
228             try { con.close(); }
229             catch (Exception JavaDoc e) { e.printStackTrace(); }
230         }
231     }
232
233
234     /**
235      * Loads group data from the database.
236      */

237     private void loadFromDb() throws ForumGroupNotFoundException {
238         Connection con = null;
239         PreparedStatement pstmt = null;
240         try {
241             con = DbConnectionManager.getConnection();
242             //See if we should load by categoryID or by name
243

244             pstmt = con.prepareStatement(LOAD_FORUM_GROUP_BY_ID);
245             pstmt.setInt(1, id);
246             ResultSet rs = pstmt.executeQuery();
247             if( !rs.next() ) {
248                 throw new ForumGroupNotFoundException("Category " + getID() +
249                     " could not be loaded from the database.");
250             }
251             id = rs.getInt("forumGroupID");
252             name = rs.getString("name");
253             description = rs.getString("description");
254             this.creationDate =
255                 new java.util.Date JavaDoc(Long.parseLong(rs.getString("creationDate").trim()));
256             this.modifiedDate =
257                 new java.util.Date JavaDoc(Long.parseLong(rs.getString("modifiedDate").trim()));
258             this.grporder = rs.getInt("grporder");
259         }
260         catch( SQLException sqle ) {
261             sqle.printStackTrace();
262             throw new ForumGroupNotFoundException("Category " + getID() +
263                 " could not be loaded from the database.");
264         }
265         catch (NumberFormatException JavaDoc nfe) {
266             System.err.println("WARNING: In DbCAtegory.loadFromDb() -- there " +
267                 "was an error parsing the dates returned from the database. Ensure " +
268                 "that they're being stored correctly.");
269         }
270         finally {
271             try { pstmt.close(); }
272             catch (Exception JavaDoc e) { e.printStackTrace(); }
273             try { con.close(); }
274             catch (Exception JavaDoc e) { e.printStackTrace(); }
275         }
276     }
277
278     /**
279      * Inserts a new record into the database.
280      */

281     private void insertIntoDb() {
282         Connection con = null;
283         PreparedStatement pstmt = null;
284         try {
285             con = DbConnectionManager.getConnection();
286             pstmt = con.prepareStatement(ADD_FORUM_GROUP);
287             pstmt.setInt(1,id);
288             pstmt.setInt(2,categoryID);
289             pstmt.setString(3,name);
290             pstmt.setString(4,description);
291             pstmt.setString(5, Long.toString(creationDate.getTime()));
292             pstmt.setString(6, Long.toString(modifiedDate.getTime()));
293             pstmt.executeUpdate();
294         }
295         catch( SQLException sqle ) {
296             System.err.println("Error in DbCategory:insertIntoDb()-" + sqle);
297             sqle.printStackTrace();
298         }
299         finally {
300             try { pstmt.close(); }
301             catch (Exception JavaDoc e) { e.printStackTrace(); }
302             try { con.close(); }
303             catch (Exception JavaDoc e) { e.printStackTrace(); }
304         }
305     }
306
307     /**
308      * Saves group data to the database.
309      */

310     private synchronized void saveToDb() {
311         Connection con = null;
312         PreparedStatement pstmt = null;
313         try {
314             con = DbConnectionManager.getConnection();
315             pstmt = con.prepareStatement(SAVE_FORUM_GROUP);
316             pstmt.setString(1, name);
317             pstmt.setString(2, description);
318             pstmt.setString(3, Long.toString(creationDate.getTime()));
319             pstmt.setString(4, Long.toString(modifiedDate.getTime()));
320             pstmt.setInt(5,this.grporder);
321             pstmt.setInt(6, id);
322             pstmt.executeUpdate();
323         }
324         catch( SQLException sqle ) {
325             System.err.println("Error in DbForum:saveToDb()-" + sqle);
326             sqle.printStackTrace();
327         }
328         finally {
329             try { pstmt.close(); }
330             catch (Exception JavaDoc e) { e.printStackTrace(); }
331             try { con.close(); }
332             catch (Exception JavaDoc e) { e.printStackTrace(); }
333         }
334     }
335
336     public Iterator JavaDoc forums() {
337         return new DbForumFactoryIterator(this, factory);
338     }
339
340 }
341
Popular Tags