001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/WatchDAOImplJDBC.java,v 1.4 2007/01/15 10:27:10 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.4 $
005: * $Date: 2007/01/15 10:27:10 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author:
039: */
040: package org.mvnforum.phpbb2mvnforum.db.jdbc;
041:
042: import java.sql.Connection;
043: import java.sql.PreparedStatement;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.sql.Timestamp;
047: import java.util.Collection;
048:
049: import org.mvnforum.util.DBUtils;
050:
051: import net.myvietnam.mvncore.exception.AssertionException;
052: import net.myvietnam.mvncore.exception.CreateException;
053: import net.myvietnam.mvncore.exception.DatabaseException;
054: import net.myvietnam.mvncore.exception.DuplicateKeyException;
055: import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException;
056: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
057:
058: import com.mvnforum.MVNForumConstant;
059: import com.mvnforum.db.DAOFactory;
060: import com.mvnforum.db.WatchBean;
061: import com.mvnforum.db.WatchDAO;
062:
063: public class WatchDAOImplJDBC implements WatchDAO {
064:
065: public void findByPrimaryKey(int watchID)
066: throws ObjectNotFoundException, DatabaseException {
067: Connection connection = null;
068: PreparedStatement statement = null;
069: ResultSet resultSet = null;
070: StringBuffer sql = new StringBuffer(512);
071: sql.append("SELECT WatchType");
072: sql.append(" FROM " + TABLE_NAME);
073: sql.append(" WHERE WatchID = ?");
074: try {
075: connection = DBUtils.getMvnConnection();
076: statement = connection.prepareStatement(sql.toString());
077: statement.setInt(1, watchID);
078: resultSet = statement.executeQuery();
079: if (!resultSet.next()) {
080: throw new ObjectNotFoundException(
081: "Cannot find the primary key (" + watchID
082: + ") in table 'Watch'.");
083: }
084: } catch (SQLException sqle) {
085: throw new DatabaseException(
086: "Error executing SQL in WatchDAOImplJDBC.findByPrimaryKey.");
087: } finally {
088: DBUtils.closeResultSet(resultSet);
089: DBUtils.closeStatement(statement);
090: DBUtils.closeConnection(connection);
091: }
092: }
093:
094: public void findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(
095: int memberID, int categoryID, int forumID, int threadID)
096: throws ObjectNotFoundException, DatabaseException {
097: Connection connection = null;
098: PreparedStatement statement = null;
099: ResultSet resultSet = null;
100: StringBuffer sql = new StringBuffer(512);
101: sql.append("SELECT WatchType");
102: sql.append(" FROM " + TABLE_NAME);
103: sql
104: .append(" WHERE MemberID = ? AND CategoryID = ? AND ForumID = ? AND ThreadID = ?");
105: try {
106: connection = DBUtils.getMvnConnection();
107: statement = connection.prepareStatement(sql.toString());
108: statement.setInt(1, memberID);
109: statement.setInt(2, categoryID);
110: statement.setInt(3, forumID);
111: statement.setInt(4, threadID);
112: resultSet = statement.executeQuery();
113: if (!resultSet.next()) {
114: throw new ObjectNotFoundException(
115: "Cannot find the alternate key [MemberID, CategoryID, ForumID, ThreadID] ("
116: + memberID + ", " + categoryID + ", "
117: + forumID + ", " + threadID
118: + ") in table 'Watch'.");
119: }
120: } catch (SQLException sqle) {
121: throw new DatabaseException(
122: "Error executing SQL in WatchDAOImplJDBC.findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID.");
123: } finally {
124: DBUtils.closeResultSet(resultSet);
125: DBUtils.closeStatement(statement);
126: DBUtils.closeConnection(connection);
127: }
128:
129: }
130:
131: public void create(int memberID, int categoryID, int forumID,
132: int threadID, int watchType, int watchOption,
133: int watchStatus, Timestamp watchCreationDate,
134: Timestamp watchLastSentDate, Timestamp watchEndDate)
135: throws IllegalArgumentException, CreateException,
136: DatabaseException, DuplicateKeyException,
137: ForeignKeyNotFoundException {
138: if ((memberID == 0)
139: || (memberID == MVNForumConstant.MEMBER_ID_OF_GUEST)) {
140: throw new IllegalArgumentException(
141: "Cannot add a new watch for Guest (id = "
142: + memberID + ")");
143: }
144: int notZeroCount = 0;
145: if (categoryID != 0) {
146: notZeroCount++;
147: }
148: if (forumID != 0) {
149: notZeroCount++;
150: }
151: if (threadID != 0) {
152: notZeroCount++;
153: }
154: if (notZeroCount > 1) {
155: throw new IllegalArgumentException(
156: "Cannot add watch with more than 1 element.");
157: }
158:
159: // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
160: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
161: try {
162: //Check if alternate key already exists
163: findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(
164: memberID, categoryID, forumID, threadID);
165: //If so, then we have to throw an exception
166: throw new DuplicateKeyException(
167: "Alternate key already exists. Cannot create new Watch with the same [MemberID, CategoryID, ForumID, ThreadID] ("
168: + memberID
169: + ", "
170: + categoryID
171: + ", "
172: + forumID + ", " + threadID + ").");
173: } catch (ObjectNotFoundException e) {
174: //Otherwise we can go ahead
175: }
176:
177: try {
178: // @todo: modify the parameter list as needed
179: // You may have to regenerate this method if the needed columns dont have attribute 'include'
180: DAOFactory.getMemberDAO().findByPrimaryKey(memberID);
181: } catch (ObjectNotFoundException e) {
182: throw new ForeignKeyNotFoundException(
183: "Foreign key refers to table 'Member' does not exist. Cannot create new Watch.");
184: }
185:
186: try {
187: // @todo: modify the parameter list as needed
188: // You may have to regenerate this method if the needed columns dont have attribute 'include'
189: if (categoryID != 0) {
190: DAOFactory.getCategoryDAO()
191: .findByPrimaryKey(categoryID);
192: }
193: } catch (ObjectNotFoundException e) {
194: throw new ForeignKeyNotFoundException(
195: "Foreign key refers to table 'Category' does not exist. Cannot create new Watch.");
196: }
197:
198: try {
199: // @todo: modify the parameter list as needed
200: // You may have to regenerate this method if the needed columns dont have attribute 'include'
201: if (forumID != 0) {
202: DAOFactory.getForumDAO().findByPrimaryKey(forumID);
203: }
204: } catch (ObjectNotFoundException e) {
205: throw new ForeignKeyNotFoundException(
206: "Foreign key refers to table 'Forum' does not exist. Cannot create new Watch.");
207: }
208:
209: try {
210: // @todo: modify the parameter list as needed
211: // You may have to regenerate this method if the needed columns dont have attribute 'include'
212: if (threadID != 0) {
213: DAOFactory.getThreadDAO().findByPrimaryKey(threadID);
214: }
215: } catch (ObjectNotFoundException e) {
216: throw new ForeignKeyNotFoundException(
217: "Foreign key refers to table 'Thread' does not exist. Cannot create new Watch.");
218: }
219:
220: Connection connection = null;
221: PreparedStatement statement = null;
222: StringBuffer sql = new StringBuffer(512);
223: sql
224: .append("INSERT INTO "
225: + TABLE_NAME
226: + " (MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate)");
227: sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
228: try {
229: connection = DBUtils.getMvnConnection();
230: statement = connection.prepareStatement(sql.toString());
231:
232: statement.setInt(1, memberID);
233: statement.setInt(2, categoryID);
234: statement.setInt(3, forumID);
235: statement.setInt(4, threadID);
236: statement.setInt(5, watchType);
237: statement.setInt(6, watchOption);
238: statement.setInt(7, watchStatus);
239: statement.setTimestamp(8, watchCreationDate);
240: statement.setTimestamp(9, watchLastSentDate);
241: statement.setTimestamp(10, watchEndDate);
242:
243: if (statement.executeUpdate() != 1) {
244: throw new CreateException(
245: "Error adding a row into table 'Watch'.");
246: }
247: } catch (SQLException sqle) {
248: throw new DatabaseException(
249: "Error executing SQL in WatchDAOImplJDBC.create.");
250: } finally {
251: DBUtils.closeStatement(statement);
252: DBUtils.closeConnection(connection);
253: }
254:
255: }
256:
257: public void delete(int watchID) throws DatabaseException,
258: ObjectNotFoundException {
259: // TODO Auto-generated method stub
260:
261: }
262:
263: public void delete_inMember(int memberID) throws DatabaseException {
264: // TODO Auto-generated method stub
265:
266: }
267:
268: public void delete_inCategory(int categoryID)
269: throws DatabaseException {
270: // TODO Auto-generated method stub
271:
272: }
273:
274: public void delete_inForum(int forumID) throws DatabaseException {
275: // TODO Auto-generated method stub
276:
277: }
278:
279: public void delete_inThread(int threadID) throws DatabaseException {
280: // TODO Auto-generated method stub
281:
282: }
283:
284: public void updateLastSentDate(int watchID,
285: Timestamp watchLastSentDate)
286: throws ObjectNotFoundException, DatabaseException {
287: // TODO Auto-generated method stub
288:
289: }
290:
291: public WatchBean getWatch(int watchID)
292: throws ObjectNotFoundException, DatabaseException {
293: // TODO Auto-generated method stub
294: return null;
295: }
296:
297: public WatchBean getWatch_byAlternateKey_MemberID_CategoryID_ForumID_ThreadID(
298: int memberID, int categoryID, int forumID, int threadID)
299: throws ObjectNotFoundException, DatabaseException {
300: // TODO Auto-generated method stub
301: return null;
302: }
303:
304: public Collection getWatches() throws DatabaseException {
305: // TODO Auto-generated method stub
306: return null;
307: }
308:
309: public int getNumberOfWatches() throws AssertionException,
310: DatabaseException {
311: // TODO Auto-generated method stub
312: return 0;
313: }
314:
315: public int getNumberOfWatches_forMember(int memberID)
316: throws AssertionException, DatabaseException {
317: // TODO Auto-generated method stub
318: return 0;
319: }
320:
321: public Collection getMemberBeans() throws DatabaseException {
322: // TODO Auto-generated method stub
323: return null;
324: }
325:
326: public Collection getWatches_forMember(int memberID)
327: throws DatabaseException {
328: // TODO Auto-generated method stub
329: return null;
330: }
331:
332: public void updateLastSentDate_forMember(int memberID,
333: Timestamp watchLastSentDate)
334: throws ObjectNotFoundException, DatabaseException {
335: // TODO Auto-generated method stub
336:
337: }
338:
339: public void updateWatchType(int watchID, int watchType)
340: throws ObjectNotFoundException, DatabaseException {
341: // TODO Auto-generated method stub
342:
343: }
344:
345: }
|