1 41 package com.mvnforum.db.jdbc; 42 43 import java.sql.*; 44 import java.util.ArrayList ; 45 import java.util.Collection ; 46 47 import com.mvnforum.db.*; 48 import net.myvietnam.mvncore.db.DBUtils; 49 import net.myvietnam.mvncore.exception.*; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 public class PmAttachMessageDAOImplJDBC implements PmAttachMessageDAO { 54 55 private static Log log = LogFactory.getLog(PmAttachMessageDAOImplJDBC.class); 56 57 private static boolean m_dirty = true; 59 60 public PmAttachMessageDAOImplJDBC() { 61 } 62 63 protected static boolean isDirty() { 64 return m_dirty; 65 } 66 67 protected static void setDirty(boolean dirty) { 68 m_dirty = dirty; 69 } 70 71 public void findByPrimaryKey(int messageID, int pmAttachID) 72 throws ObjectNotFoundException, DatabaseException { 73 74 Connection connection = null; 75 PreparedStatement statement = null; 76 ResultSet resultSet = null; 77 StringBuffer sql = new StringBuffer (512); 78 sql.append("SELECT MessageID, PmAttachID"); 79 sql.append(" FROM " + TABLE_NAME); 80 sql.append(" WHERE MessageID = ? AND PmAttachID = ?"); 81 try { 82 connection = DBUtils.getConnection(); 83 statement = connection.prepareStatement(sql.toString()); 84 statement.setInt(1, messageID); 85 statement.setInt(2, pmAttachID); 86 resultSet = statement.executeQuery(); 87 if (!resultSet.next()) { 88 throw new ObjectNotFoundException("Cannot find the primary key (" + messageID + ", " + pmAttachID + ") in table 'PmAttachMessage'."); 89 } 90 } catch(SQLException sqle) { 91 log.error("Sql Execution Error!", sqle); 92 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.findByPrimaryKey."); 93 } finally { 94 DBUtils.closeResultSet(resultSet); 95 DBUtils.closeStatement(statement); 96 DBUtils.closeConnection(connection); 97 } 98 } 99 100 104 public void create(int messageID, int pmAttachID, int relationType, 105 int relationOption, int relationStatus) 106 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 107 108 try { 112 findByPrimaryKey(messageID, pmAttachID); 114 throw new DuplicateKeyException("Primary key already exists. Cannot create new PmAttachMessage with the same [MessageID, PmAttachID] (" + messageID + ", " + pmAttachID + ")."); 116 } catch(ObjectNotFoundException e) { 117 } 119 120 try { 121 DAOFactory.getMessageDAO().findByPrimaryKey(messageID); 124 } catch(ObjectNotFoundException e) { 125 throw new ForeignKeyNotFoundException("Foreign key refers to table 'Message' does not exist. Cannot create new PmAttachMessage."); 126 } 127 128 try { 129 DAOFactory.getPmAttachmentDAO().findByPrimaryKey(pmAttachID); 132 } catch(ObjectNotFoundException e) { 133 throw new ForeignKeyNotFoundException("Foreign key refers to table 'PmAttachment' does not exist. Cannot create new PmAttachMessage."); 134 } 135 136 Connection connection = null; 137 PreparedStatement statement = null; 138 StringBuffer sql = new StringBuffer (512); 139 sql.append("INSERT INTO " + TABLE_NAME + " (MessageID, PmAttachID, RelationType, RelationOption, RelationStatus)"); 140 sql.append(" VALUES (?, ?, ?, ?, ?)"); 141 try { 142 connection = DBUtils.getConnection(); 143 statement = connection.prepareStatement(sql.toString()); 144 145 statement.setInt(1, messageID); 146 statement.setInt(2, pmAttachID); 147 statement.setInt(3, relationType); 148 statement.setInt(4, relationOption); 149 statement.setInt(5, relationStatus); 150 151 if (statement.executeUpdate() != 1) { 152 throw new CreateException("Error adding a row into table 'PmAttachMessage'."); 153 } 154 m_dirty = true; 155 } catch(SQLException sqle) { 156 log.error("Sql Execution Error!", sqle); 157 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.create."); 158 } finally { 159 DBUtils.closeStatement(statement); 160 DBUtils.closeConnection(connection); 161 } 162 } 163 164 public void delete(int messageID, int pmAttachID) 165 throws DatabaseException, ObjectNotFoundException { 166 167 Connection connection = null; 168 PreparedStatement statement = null; 169 StringBuffer sql = new StringBuffer (512); 170 sql.append("DELETE FROM " + TABLE_NAME); 171 sql.append(" WHERE MessageID = ? AND PmAttachID = ?"); 172 173 try { 174 connection = DBUtils.getConnection(); 175 statement = connection.prepareStatement(sql.toString()); 176 statement.setInt(1, messageID); 177 statement.setInt(2, pmAttachID); 178 if (statement.executeUpdate() != 1) { 179 throw new ObjectNotFoundException("Cannot delete a row in table PmAttachMessage where primary key = (" + messageID + ", " + pmAttachID + ")."); 180 } 181 m_dirty = true; 182 } catch(SQLException sqle) { 183 log.error("Sql Execution Error!", sqle); 184 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.delete."); 185 } finally { 186 DBUtils.closeStatement(statement); 187 DBUtils.closeConnection(connection); 188 } 189 } 190 191 public void delete_inMessage(int messageID) 192 throws DatabaseException { 193 194 Connection connection = null; 195 PreparedStatement statement = null; 196 StringBuffer sql = new StringBuffer (512); 197 sql.append("DELETE FROM " + TABLE_NAME); 198 sql.append(" WHERE MessageID = ? "); 199 200 try { 201 connection = DBUtils.getConnection(); 202 statement = connection.prepareStatement(sql.toString()); 203 statement.setInt(1, messageID); 204 statement.executeUpdate(); 205 m_dirty = true; 206 } catch(SQLException sqle) { 207 log.error("Sql Execution Error!", sqle); 208 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.delete_inMessage."); 209 } finally { 210 DBUtils.closeStatement(statement); 211 DBUtils.closeConnection(connection); 212 } 213 } 214 215 219 public Collection getBeans_inMessage(int messageID) 220 throws DatabaseException { 221 222 Connection connection = null; 223 PreparedStatement statement = null; 224 ResultSet resultSet = null; 225 Collection retValue = new ArrayList (); 226 StringBuffer sql = new StringBuffer (512); 227 sql.append("SELECT MessageID, PmAttachID, RelationType, RelationOption, RelationStatus"); 228 sql.append(" FROM " + TABLE_NAME); 229 sql.append(" WHERE MessageID = ?"); 230 try { 232 connection = DBUtils.getConnection(); 233 statement = connection.prepareStatement(sql.toString()); 234 statement.setInt(1, messageID); 235 resultSet = statement.executeQuery(); 236 while (resultSet.next()) { 237 PmAttachMessageBean bean = new PmAttachMessageBean(); 238 bean.setMessageID(resultSet.getInt("MessageID")); 239 bean.setPmAttachID(resultSet.getInt("PmAttachID")); 240 bean.setRelationType(resultSet.getInt("RelationType")); 241 bean.setRelationOption(resultSet.getInt("RelationOption")); 242 bean.setRelationStatus(resultSet.getInt("RelationStatus")); 243 retValue.add(bean); 244 } 245 return retValue; 246 } catch(SQLException sqle) { 247 log.error("Sql Execution Error!", sqle); 248 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getBeans_inMessage."); 249 } finally { 250 DBUtils.closeResultSet(resultSet); 251 DBUtils.closeStatement(statement); 252 DBUtils.closeConnection(connection); 253 } 254 } 255 256 260 public Collection getBeans_inPmAttach(int pmAttachID) 261 throws DatabaseException { 262 263 Connection connection = null; 264 PreparedStatement statement = null; 265 ResultSet resultSet = null; 266 Collection retValue = new ArrayList (); 267 StringBuffer sql = new StringBuffer (512); 268 sql.append("SELECT MessageID, PmAttachID, RelationType, RelationOption, RelationStatus"); 269 sql.append(" FROM " + TABLE_NAME); 270 sql.append(" WHERE PmAttachID = ?"); 271 try { 273 connection = DBUtils.getConnection(); 274 statement = connection.prepareStatement(sql.toString()); 275 statement.setInt(1, pmAttachID); 276 resultSet = statement.executeQuery(); 277 while (resultSet.next()) { 278 PmAttachMessageBean bean = new PmAttachMessageBean(); 279 bean.setMessageID(resultSet.getInt("MessageID")); 280 bean.setPmAttachID(resultSet.getInt("PmAttachID")); 281 bean.setRelationType(resultSet.getInt("RelationType")); 282 bean.setRelationOption(resultSet.getInt("RelationOption")); 283 bean.setRelationStatus(resultSet.getInt("RelationStatus")); 284 retValue.add(bean); 285 } 286 return retValue; 287 } catch(SQLException sqle) { 288 log.error("Sql Execution Error!", sqle); 289 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getBeans_inPmAttach."); 290 } finally { 291 DBUtils.closeResultSet(resultSet); 292 DBUtils.closeStatement(statement); 293 DBUtils.closeConnection(connection); 294 } 295 } 296 297 public int getNumberOfBeans_inMessage(int messageID) 298 throws AssertionException, DatabaseException { 299 300 Connection connection = null; 301 PreparedStatement statement = null; 302 ResultSet resultSet = null; 303 StringBuffer sql = new StringBuffer (512); 304 sql.append("SELECT Count(*)"); 305 sql.append(" FROM " + TABLE_NAME); 306 sql.append(" WHERE MessageID = ?"); 307 try { 308 connection = DBUtils.getConnection(); 309 statement = connection.prepareStatement(sql.toString()); 310 statement.setInt(1, messageID); 311 resultSet = statement.executeQuery(); 312 if (!resultSet.next()) { 313 throw new AssertionException("Assertion in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inMessage."); 314 } 315 return resultSet.getInt(1); 316 } catch(SQLException sqle) { 317 log.error("Sql Execution Error!", sqle); 318 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inMessage."); 319 } finally { 320 DBUtils.closeResultSet(resultSet); 321 DBUtils.closeStatement(statement); 322 DBUtils.closeConnection(connection); 323 } 324 } 325 326 public int getNumberOfBeans_inPmAttach(int pmAttachID) 327 throws AssertionException, DatabaseException { 328 329 Connection connection = null; 330 PreparedStatement statement = null; 331 ResultSet resultSet = null; 332 StringBuffer sql = new StringBuffer (512); 333 sql.append("SELECT Count(*)"); 334 sql.append(" FROM " + TABLE_NAME); 335 sql.append(" WHERE PmAttachID = ?"); 336 try { 337 connection = DBUtils.getConnection(); 338 statement = connection.prepareStatement(sql.toString()); 339 statement.setInt(1, pmAttachID); 340 resultSet = statement.executeQuery(); 341 if (!resultSet.next()) { 342 throw new AssertionException("Assertion in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inPmAttach."); 343 } 344 return resultSet.getInt(1); 345 } catch(SQLException sqle) { 346 log.error("Sql Execution Error!", sqle); 347 throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inPmAttach."); 348 } finally { 349 DBUtils.closeResultSet(resultSet); 350 DBUtils.closeStatement(statement); 351 DBUtils.closeConnection(connection); 352 } 353 } 354 355 } | Popular Tags |