001: /**
002: * Copyright (C) 2001 Yasna.com. All rights reserved.
003: *
004: * ===================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * Yasna.com (http://www.yasna.com)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "Yazd" and "Yasna.com" must not be used to
027: * endorse or promote products derived from this software without
028: * prior written permission. For written permission, please
029: * contact yazd@yasna.com.
030: *
031: * 5. Products derived from this software may not be called "Yazd",
032: * nor may "Yazd" appear in their name, without prior written
033: * permission of Yasna.com.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of Yasna.com. For more information
051: * on Yasna.com, please see <http://www.yasna.com>.
052: */package com.Yasna.forum.database;
053:
054: import java.util.Iterator;
055: import java.util.Enumeration;
056: import java.util.Properties;
057: import java.util.ArrayList;
058: import java.util.Date;
059: import java.sql.*;
060: import java.io.*;
061:
062: import com.Yasna.forum.*;
063: import com.Yasna.forum.filter.*;
064: import com.Yasna.util.Cache;
065: import com.Yasna.util.Cacheable;
066: import com.Yasna.util.CacheSizes;
067:
068: /**
069: * Database implementation of the ForumGroup interface. It loads and stores forumGroup
070: * information from the database.
071: *
072: * @see ForumGroup
073: */
074: public class DbForumGroup implements ForumGroup, Cacheable {
075:
076: /** DATABASE QUERIES **/
077: private static final String LOAD_FORUM_GROUP_BY_ID = "SELECT forumGroupID, name, description, creationDate, modifiedDate,grporder FROM yazdForumGroup WHERE forumGroupID=?";
078: private static final String UPDATE_FORUM_GROUP_MODIFIED_DATE = "UPDATE yazdForumGroup SET modifiedDate=? WHERE forumGroupID=?";
079: private static final String ADD_FORUM_GROUP = "INSERT INTO yazdForumGroup(forumGroupID, categoryID, name, description, creationDate, "
080: + "modifiedDate,grporder) VALUES (?,?,?,?,?,?,0)";
081: private static final String SAVE_FORUM_GROUP = "UPDATE yazdForumGroup SET name=?, description=?, creationDate=?, "
082: + "modifiedDate=?,grporder=? WHERE forumGroupID=?";
083:
084: private int id = -1;
085: private String name;
086: private String description;
087: private int categoryID;
088: private int grporder = 0;
089: private java.util.Date creationDate;
090: private java.util.Date modifiedDate;
091:
092: private DbForumFactory factory;
093: private DbCategory category;
094:
095: /**
096: * Loads a forumGroup with the specified id.
097: */
098: protected DbForumGroup(int id, DbCategory category,
099: DbForumFactory factory) throws ForumGroupNotFoundException {
100: this .id = id;
101: this .category = category;
102: this .factory = factory;
103: loadFromDb();
104: }
105:
106: /**
107: * Creats new forumGroup.
108: */
109: protected DbForumGroup(String name, String description,
110: DbCategory category, DbForumFactory factory)
111: throws UnauthorizedException {
112: this .id = DbSequenceManager.nextID("ForumGroup");
113: this .name = name;
114: this .categoryID = category.getID();
115: this .description = description;
116: long now = System.currentTimeMillis();
117: creationDate = new java.util.Date(now);
118: modifiedDate = new java.util.Date(now);
119: this .category = category;
120: this .factory = factory;
121: insertIntoDb();
122: }
123:
124: //FROM THE CATEGORY INTERFACE//
125:
126: public int getID() {
127: return id;
128: }
129:
130: public String getName() {
131: return name;
132: }
133:
134: public int getOrder() {
135: return grporder;
136: }
137:
138: public void setOrder(int param) {
139: this .grporder = param;
140: saveToDb();
141: }
142:
143: public void setName(String name) throws UnauthorizedException {
144: this .name = name;
145: saveToDb();
146: }
147:
148: public String getDescription() {
149: return description;
150: }
151:
152: public void setDescription(String description)
153: throws UnauthorizedException {
154: this .description = description;
155: saveToDb();
156: }
157:
158: public java.util.Date getCreationDate() {
159: return creationDate;
160: }
161:
162: public void setCreationDate(java.util.Date creationDate)
163: throws UnauthorizedException {
164: this .creationDate = creationDate;
165: saveToDb();
166: }
167:
168: public java.util.Date getModifiedDate() {
169: return modifiedDate;
170: }
171:
172: public void setModifiedDate(java.util.Date modifiedDate)
173: throws UnauthorizedException {
174: this .modifiedDate = modifiedDate;
175: saveToDb();
176: }
177:
178: public Iterator forumGroups() {
179: return null;//new DbForumIterator(this, factory);
180: }
181:
182: //FROM THE CACHEABLE INTERFACE//
183:
184: public int getSize() {
185: //Approximate the size of the object in bytes by calculating the size
186: //of each field.
187: int size = 0;
188: size += CacheSizes.sizeOfObject(); //overhead of object
189: size += CacheSizes.sizeOfInt(); //id
190: size += CacheSizes.sizeOfString(name); //name
191: size += CacheSizes.sizeOfString(description); //description
192: size += CacheSizes.sizeOfDate(); //creation date
193: size += CacheSizes.sizeOfDate(); //modified date
194: size += CacheSizes.sizeOfObject(); //save lock
195: size += CacheSizes.sizeOfInt(); //group order
196:
197: return size;
198: }
199:
200: /**
201: * Updates the modified date but doesn't require a security check since
202: * it is a protected method.
203: */
204: protected void updateModifiedDate(java.util.Date modifiedDate) {
205: this .modifiedDate = modifiedDate;
206: Connection con = null;
207: PreparedStatement pstmt = null;
208: try {
209: con = DbConnectionManager.getConnection();
210: pstmt = con
211: .prepareStatement(UPDATE_FORUM_GROUP_MODIFIED_DATE);
212: pstmt.setString(1, "" + modifiedDate.getTime());
213: pstmt.setInt(2, id);
214: pstmt.executeUpdate();
215: } catch (SQLException sqle) {
216: System.err
217: .println("Error in DbCategory:updateModifiedDate()-"
218: + sqle);
219: sqle.printStackTrace();
220: } finally {
221: try {
222: pstmt.close();
223: } catch (Exception e) {
224: e.printStackTrace();
225: }
226: try {
227: con.close();
228: } catch (Exception e) {
229: 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 "
249: + getID()
250: + " could not be loaded from the database.");
251: }
252: id = rs.getInt("forumGroupID");
253: name = rs.getString("name");
254: description = rs.getString("description");
255: this .creationDate = new java.util.Date(Long.parseLong(rs
256: .getString("creationDate").trim()));
257: this .modifiedDate = new java.util.Date(Long.parseLong(rs
258: .getString("modifiedDate").trim()));
259: this .grporder = rs.getInt("grporder");
260: } catch (SQLException sqle) {
261: sqle.printStackTrace();
262: throw new ForumGroupNotFoundException("Category " + getID()
263: + " could not be loaded from the database.");
264: } catch (NumberFormatException nfe) {
265: System.err
266: .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: } finally {
270: try {
271: pstmt.close();
272: } catch (Exception e) {
273: e.printStackTrace();
274: }
275: try {
276: con.close();
277: } catch (Exception e) {
278: e.printStackTrace();
279: }
280: }
281: }
282:
283: /**
284: * Inserts a new record into the database.
285: */
286: private void insertIntoDb() {
287: Connection con = null;
288: PreparedStatement pstmt = null;
289: try {
290: con = DbConnectionManager.getConnection();
291: pstmt = con.prepareStatement(ADD_FORUM_GROUP);
292: pstmt.setInt(1, id);
293: pstmt.setInt(2, categoryID);
294: pstmt.setString(3, name);
295: pstmt.setString(4, description);
296: pstmt.setString(5, Long.toString(creationDate.getTime()));
297: pstmt.setString(6, Long.toString(modifiedDate.getTime()));
298: pstmt.executeUpdate();
299: } catch (SQLException sqle) {
300: System.err.println("Error in DbCategory:insertIntoDb()-"
301: + sqle);
302: sqle.printStackTrace();
303: } finally {
304: try {
305: pstmt.close();
306: } catch (Exception e) {
307: e.printStackTrace();
308: }
309: try {
310: con.close();
311: } catch (Exception e) {
312: e.printStackTrace();
313: }
314: }
315: }
316:
317: /**
318: * Saves group data to the database.
319: */
320: private synchronized void saveToDb() {
321: Connection con = null;
322: PreparedStatement pstmt = null;
323: try {
324: con = DbConnectionManager.getConnection();
325: pstmt = con.prepareStatement(SAVE_FORUM_GROUP);
326: pstmt.setString(1, name);
327: pstmt.setString(2, description);
328: pstmt.setString(3, Long.toString(creationDate.getTime()));
329: pstmt.setString(4, Long.toString(modifiedDate.getTime()));
330: pstmt.setInt(5, this .grporder);
331: pstmt.setInt(6, id);
332: pstmt.executeUpdate();
333: } catch (SQLException sqle) {
334: System.err.println("Error in DbForum:saveToDb()-" + sqle);
335: sqle.printStackTrace();
336: } finally {
337: try {
338: pstmt.close();
339: } catch (Exception e) {
340: e.printStackTrace();
341: }
342: try {
343: con.close();
344: } catch (Exception e) {
345: e.printStackTrace();
346: }
347: }
348: }
349:
350: public Iterator forums() {
351: return new DbForumFactoryIterator(this, factory);
352: }
353:
354: }
|