1 package org.mvnforum.phpbb2mvnforum.db.jdbc; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Timestamp ; 8 import java.util.Collection ; 9 10 import org.mvnforum.util.DBUtils; 11 12 import net.myvietnam.mvncore.exception.AssertionException; 13 import net.myvietnam.mvncore.exception.CreateException; 14 import net.myvietnam.mvncore.exception.DatabaseException; 15 import net.myvietnam.mvncore.exception.DuplicateKeyException; 16 import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException; 17 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 18 19 import com.mvnforum.MVNForumConstant; 20 import com.mvnforum.db.DAOFactory; 21 import com.mvnforum.db.WatchBean; 22 import com.mvnforum.db.WatchDAO; 23 24 public class WatchDAOImplJDBC implements WatchDAO { 25 26 public void findByPrimaryKey(int watchID) 27 throws ObjectNotFoundException, DatabaseException { 28 Connection connection = null; 30 PreparedStatement statement = null; 31 ResultSet resultSet = null; 32 StringBuffer sql = new StringBuffer (512); 33 sql.append("SELECT WatchType"); 34 sql.append(" FROM " + TABLE_NAME); 35 sql.append(" WHERE WatchID = ?"); 36 try { 37 connection = DBUtils.getMvnConnection(); 38 statement = connection.prepareStatement(sql.toString()); 39 statement.setInt(1, watchID); 40 resultSet = statement.executeQuery(); 41 if (!resultSet.next()) { 42 throw new ObjectNotFoundException("Cannot find the primary key (" + watchID + ") in table 'Watch'."); 43 } 44 } catch(SQLException sqle) { 45 throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.findByPrimaryKey."); 46 } finally { 47 DBUtils.closeResultSet(resultSet); 48 DBUtils.closeStatement(statement); 49 DBUtils.closeConnection(connection); 50 } 51 } 52 53 public void findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(int memberID, int categoryID, int forumID, 54 int threadID) 55 throws ObjectNotFoundException, DatabaseException { 56 Connection connection = null; 57 PreparedStatement statement = null; 58 ResultSet resultSet = null; 59 StringBuffer sql = new StringBuffer (512); 60 sql.append("SELECT WatchType"); 61 sql.append(" FROM " + TABLE_NAME); 62 sql.append(" WHERE MemberID = ? AND CategoryID = ? AND ForumID = ? AND ThreadID = ?"); 63 try { 64 connection = DBUtils.getMvnConnection(); 65 statement = connection.prepareStatement(sql.toString()); 66 statement.setInt(1, memberID); 67 statement.setInt(2, categoryID); 68 statement.setInt(3, forumID); 69 statement.setInt(4, threadID); 70 resultSet = statement.executeQuery(); 71 if (!resultSet.next()) { 72 throw new ObjectNotFoundException("Cannot find the alternate key [MemberID, CategoryID, ForumID, ThreadID] (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ") in table 'Watch'."); 73 } 74 } catch(SQLException sqle) { 75 throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID."); 76 } finally { 77 DBUtils.closeResultSet(resultSet); 78 DBUtils.closeStatement(statement); 79 DBUtils.closeConnection(connection); 80 } 81 82 } 83 84 public void create(int memberID, int categoryID, int forumID, int threadID, int watchType, int watchOption, 85 int watchStatus, Timestamp watchCreationDate, Timestamp watchLastSentDate, Timestamp watchEndDate) 86 throws IllegalArgumentException , CreateException, DatabaseException, DuplicateKeyException, 87 ForeignKeyNotFoundException { 88 if ((memberID == 0) || (memberID == MVNForumConstant.MEMBER_ID_OF_GUEST)) { 89 throw new IllegalArgumentException ("Cannot add a new watch for Guest (id = " + memberID + ")"); 90 } 91 int notZeroCount = 0; 92 if (categoryID != 0) { 93 notZeroCount++; 94 } 95 if (forumID != 0) { 96 notZeroCount++; 97 } 98 if (threadID != 0) { 99 notZeroCount++; 100 } 101 if (notZeroCount > 1) { 102 throw new IllegalArgumentException ("Cannot add watch with more than 1 element."); 103 } 104 105 try { 108 findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(memberID, categoryID, forumID, threadID); 110 throw new DuplicateKeyException("Alternate key already exists. Cannot create new Watch with the same [MemberID, CategoryID, ForumID, ThreadID] (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ")."); 112 } catch(ObjectNotFoundException e) { 113 } 115 116 try { 117 DAOFactory.getMemberDAO().findByPrimaryKey(memberID); 120 } catch(ObjectNotFoundException e) { 121 throw new ForeignKeyNotFoundException("Foreign key refers to table 'Member' does not exist. Cannot create new Watch."); 122 } 123 124 try { 125 if (categoryID != 0) { 128 DAOFactory.getCategoryDAO().findByPrimaryKey(categoryID); 129 } 130 } catch(ObjectNotFoundException e) { 131 throw new ForeignKeyNotFoundException("Foreign key refers to table 'Category' does not exist. Cannot create new Watch."); 132 } 133 134 try { 135 if (forumID != 0) { 138 DAOFactory.getForumDAO().findByPrimaryKey(forumID); 139 } 140 } catch(ObjectNotFoundException e) { 141 throw new ForeignKeyNotFoundException("Foreign key refers to table 'Forum' does not exist. Cannot create new Watch."); 142 } 143 144 try { 145 if (threadID != 0) { 148 DAOFactory.getThreadDAO().findByPrimaryKey(threadID); 149 } 150 } catch(ObjectNotFoundException e) { 151 throw new ForeignKeyNotFoundException("Foreign key refers to table 'Thread' does not exist. Cannot create new Watch."); 152 } 153 154 Connection connection = null; 155 PreparedStatement statement = null; 156 StringBuffer sql = new StringBuffer (512); 157 sql.append("INSERT INTO " + TABLE_NAME + " (MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate)"); 158 sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 159 try { 160 connection = DBUtils.getMvnConnection(); 161 statement = connection.prepareStatement(sql.toString()); 162 163 statement.setInt(1, memberID); 164 statement.setInt(2, categoryID); 165 statement.setInt(3, forumID); 166 statement.setInt(4, threadID); 167 statement.setInt(5, watchType); 168 statement.setInt(6, watchOption); 169 statement.setInt(7, watchStatus); 170 statement.setTimestamp(8, watchCreationDate); 171 statement.setTimestamp(9, watchLastSentDate); 172 statement.setTimestamp(10, watchEndDate); 173 174 if (statement.executeUpdate() != 1) { 175 throw new CreateException("Error adding a row into table 'Watch'."); 176 } 177 } catch(SQLException sqle) { 178 throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.create."); 179 } finally { 180 DBUtils.closeStatement(statement); 181 DBUtils.closeConnection(connection); 182 } 183 184 } 185 186 public void delete(int watchID) 187 throws DatabaseException, ObjectNotFoundException { 188 190 } 191 192 public void delete_inMember(int memberID) 193 throws DatabaseException { 194 196 } 197 198 public void delete_inCategory(int categoryID) 199 throws DatabaseException { 200 202 } 203 204 public void delete_inForum(int forumID) 205 throws DatabaseException { 206 208 } 209 210 public void delete_inThread(int threadID) 211 throws DatabaseException { 212 214 } 215 216 public void updateLastSentDate(int watchID, Timestamp watchLastSentDate) 217 throws ObjectNotFoundException, DatabaseException { 218 220 } 221 222 public WatchBean getWatch(int watchID) 223 throws ObjectNotFoundException, DatabaseException { 224 return null; 226 } 227 228 public WatchBean getWatch_byAlternateKey_MemberID_CategoryID_ForumID_ThreadID(int memberID, int categoryID, 229 int forumID, int threadID) 230 throws ObjectNotFoundException, DatabaseException { 231 return null; 233 } 234 235 public Collection getWatches() 236 throws DatabaseException { 237 return null; 239 } 240 241 public int getNumberOfWatches() 242 throws AssertionException, DatabaseException { 243 return 0; 245 } 246 247 public int getNumberOfWatches_forMember(int memberID) 248 throws AssertionException, DatabaseException { 249 return 0; 251 } 252 253 public Collection getMemberBeans() 254 throws DatabaseException { 255 return null; 257 } 258 259 public Collection getWatches_forMember(int memberID) 260 throws DatabaseException { 261 return null; 263 } 264 265 public void updateLastSentDate_forMember(int memberID, Timestamp watchLastSentDate) 266 throws ObjectNotFoundException, DatabaseException { 267 269 } 270 271 } 272 | Popular Tags |