dbforummessage.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,052 行 · 第 1/3 页
JAVA
1,052 行
} catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Inserts a new property into the datatabase. */ private void insertPropertyIntoDb(String name, String value, Connection con) { PreparedStatement pstmt = null; try { pstmt = con.prepareStatement(INSERT_PROPERTY); pstmt.setLong(1, id); pstmt.setString(2, name); pstmt.setString(3, value); pstmt.executeUpdate(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Updates a property value in the database. */ private void updatePropertyInDb(String name, String value) { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(UPDATE_PROPERTY); pstmt.setString(1, value); pstmt.setString(2, name); pstmt.setLong(3, id); pstmt.executeUpdate(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Deletes a property from the db. */ private synchronized void deletePropertyFromDb(String name) { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(DELETE_PROPERTY); pstmt.setLong(1, id); pstmt.setString(2, name); pstmt.execute(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Moves an existing message and all of its children from an existing thread to a new thread. * Usually, this will be done when a new thread is created from an existing message and its * children. * * @param newThread the new thread the message and its children will belong to. * @param parentMessageID the new parent message ID of this message. * @param con a database connection to use for SQL operations. * @throws SQLException if an error occurs. */ protected void moveMessage(DbForumThread newThread, long parentMessageID, Connection con) throws SQLException { DbForumThread oldThread = null; try { oldThread = factory.cacheManager.getForumThread(this.threadID); } catch (Exception e) { e.printStackTrace(); } // Get a list of all child messages. LongList idList = new LongList(); Iterator children = oldThread.treeWalker().recursiveChildren(this); java.util.Date modifiedDate = newThread.getModifiedDate(); while (children.hasNext()) { ForumMessage child = (ForumMessage)children.next(); idList.add(child.getID()); // Find the most recent modified date out of all of the children. if (child.getModifiedDate().after(modifiedDate)) { modifiedDate = child.getModifiedDate(); } } long [] childIDs = idList.toArray(); // Update the threadID and forumID of all messages. StringBuffer sql = new StringBuffer(); sql.append("UPDATE jiveMessage SET forumID=?, threadID=? WHERE messageID IN("); sql.append(this.getID()); // Append ID's of all children. for (int i=0; i<childIDs.length; i++) { sql.append(", ").append(childIDs[i]); } sql.append(")"); PreparedStatement pstmt = null; try { pstmt = con.prepareStatement(sql.toString()); pstmt.setLong(1, newThread.forumID); pstmt.setLong(2, newThread.getID()); pstmt.execute(); pstmt.close(); // Now, update the parentMessageID of this message. pstmt = con.prepareStatement(UPDATE_PARENT_ID); if (parentMessageID == -1) { pstmt.setNull(1, Types.NUMERIC); } else { pstmt.setLong(1, parentMessageID); } pstmt.setLong(2, this.id); pstmt.execute(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } // Set the modified date of the new thread. newThread.setModifiedDate(modifiedDate); // Clear the caches of both threads. newThread.clearCache(); oldThread.clearCache(); // Expire individual message caches. factory.cacheManager.messageCache.remove(new Long(id)); for (int i=0; i<childIDs.length; i++) { factory.cacheManager.messageCache.remove(new Long(childIDs[i])); } } /** * Loads message and user data from the database. */ private void loadFromDb() throws ForumMessageNotFoundException { // Based on the id in the object, get the message data from the database. Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(LOAD_MESSAGE); pstmt.setLong(1, id); ResultSet rs = pstmt.executeQuery(); if (!rs.next()) { throw new ForumMessageNotFoundException("Message " + id + " could not be loaded from the database."); } this.threadID = rs.getLong(1); this.forumID = rs.getLong(2); this.userID = rs.getLong(3); if (rs.wasNull()) { userID = -1; } this.subject = rs.getString(4); // The Oracle JDBC driver is lame and returns the String "null" for // null columns. if (rs.wasNull()) { this.subject = null; } // Use the connection manager to get the body field. this.body = ConnectionManager.getLargeTextField(rs, 5); this.moderationValue = rs.getInt(6); this.rewardPoints = rs.getInt(7); // We trim() the dates before trying to parse them because some // databases pad with extra characters when returning the data. this.creationDate = new java.util.Date(Long.parseLong(rs.getString(8).trim())); this.modifiedDate = new java.util.Date(Long.parseLong(rs.getString(9).trim())); // Load any extended message properties. if (!LAZY_PROP_LOADING) { pstmt.close(); properties = new Hashtable(); pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt.setLong(1, id); rs = pstmt.executeQuery(); while(rs.next()) { // Add in name, value as a new property. properties.put(rs.getString(1), rs.getString(2)); } } } catch( SQLException sqle ) { throw new ForumMessageNotFoundException( "Message with id " + id + " could not be loaded from the database." ); } catch (NumberFormatException nfe) { System.err.println("WARNING: In DbForumMessage.loadFromDb() -- there " + "was an error parsing the dates returned from the database. Ensure " + "that they're being stored correctly."); throw new ForumMessageNotFoundException( "Message with id " + id + " could not be loaded from the database." ); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Inserts a new message into the database. A connection object must * be passed in. The connection must be open when passed in, and will * remain open when passed back. This method allows us to make insertions * be transactional. * * @param con an open Connection used to insert the thread to the db. * @param thread the ForumThread the message is being added to. */ protected void insertIntoDb(DbForumThread thread, long parentMessageID, Connection con) throws SQLException { // Set the message threadID, forumID this.threadID = thread.getID(); // Get forumID and moderation value. Forum forum = thread.getForum(); this.forumID = forum.getID(); // If the moderation value hasn't already been set, use the default. if (moderationValue == Integer.MIN_VALUE) { moderationValue = forum.getModerationDefaultMessageValue(); } // Now, insert the message to the db. PreparedStatement pstmt = null; try { pstmt = con.prepareStatement(INSERT_MESSAGE); pstmt.setLong(1, id); if (parentMessageID == -1) { pstmt.setNull(2, Types.NUMERIC); } else { pstmt.setLong(2, parentMessageID); } pstmt.setLong(3, threadID); pstmt.setLong(4, thread.forumID); if (userID == -1) { pstmt.setNull(5, Types.NUMERIC); } else { pstmt.setLong(5, userID); } pstmt.setString(6, subject); // Use the connection manager to set the body field. ConnectionManager.setLargeTextField(pstmt, 7, body); pstmt.setInt(8, moderationValue); pstmt.setInt(9, rewardPoints); pstmt.setString(10, StringUtils.dateToMillis(creationDate)); pstmt.setString(11, StringUtils.dateToMillis(modifiedDate)); pstmt.executeUpdate(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } // We're done inserting the message, so now save any extended // properties to the database. // Now, insert properties into database for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) { String name = (String)i.next(); String value = (String)properties.get(name); insertPropertyIntoDb(name, value, con); } // Since we're done inserting the object to the database, it is ready // for future saves. readyToSave = true; } /** * Saves message data to the database. */ private synchronized void saveToDb() { boolean abortTransaction = false; Connection con = null; try { con = ConnectionManager.getTransactionConnection(); saveToDb(con); } catch( Exception e ) { e.printStackTrace(); abortTransaction = true; } finally { ConnectionManager.closeTransactionConnection(con, abortTransaction); } } /** * Implementation of saveToDb that takes a Connection. This is useful for * participating in transactions. */ private synchronized void saveToDb(Connection con) throws SQLException { PreparedStatement pstmt = null; try { pstmt = con.prepareStatement(SAVE_MESSAGE); pstmt.setString(1, subject); // Use the connection manager to set the body field. ConnectionManager.setLargeTextField(pstmt, 2, body); pstmt.setInt(3, moderationValue); pstmt.setInt(4, rewardPoints); pstmt.setString(5, StringUtils.dateToMillis(creationDate)); pstmt.setString(6, StringUtils.dateToMillis(modifiedDate)); pstmt.setLong(7, id); pstmt.executeUpdate(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?