dbforum.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,659 行 · 第 1/5 页

JAVA
1,659 行
字号
    public Iterator propertyNames() {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return Collections.unmodifiableSet(properties.keySet()).iterator();    }    public ForumCategory getForumCategory() {        try {            return factory.cacheManager.getForumCategory(categoryID);        }        catch (ForumCategoryNotFoundException e) {            e.printStackTrace();            return null;        }    }    public void addThread(ForumThread thread) {        boolean abortTransaction = false;        Connection con = null;        try {            con = ConnectionManager.getTransactionConnection();            // Insert the thread into the database. Depending on if this            // method was called internally or not, the thread object might            // be wrapped by a proxy or not.            if (thread instanceof ForumThreadProxy) {                ForumThreadProxy proxyThread = (ForumThreadProxy)thread;                DbForumThread dbThread = (DbForumThread)proxyThread.getProxiedForumThread();                dbThread.insertIntoDb(this, con);            }            else {                DbForumThread dbThread = (DbForumThread)thread;                dbThread.insertIntoDb(this, con);            }            // Check the moderation value of the thread. If the value is above            // the visible threshold for the forum, then update the modified            // date of the forum. Otherwise, we'll wait to update the modified            // date to when the thread is moderated to be visible.            if (thread.getModerationValue() >= getModerationMinThreadValue()) {                updateModifiedDate(thread.getModifiedDate().getTime(), con);            }        }        catch(Exception e) {            e.printStackTrace();            abortTransaction = true;        }        finally {            ConnectionManager.closeTransactionConnection(con, abortTransaction);        }        // Thread count has changed, so remove the forum from cache.        this.clearCache();        // Expire the global popular objects lists.        factory.expirePopularObjects();        // Update the userMessageCountCache if the root message was not posted        // anonymously. There is a small risk that updating the cache this way        // will temporarily make the count incorrect, but the speed improvement        // justifies it.        ForumMessage message = thread.getRootMessage();        if (!message.isAnonymous()) {            Cache msgCountCache = factory.cacheManager.userMessageCountCache;            Long userID = new Long(message.getUser().getID());            Integer msgCount;            // If the value is in cache, update it.            if ((msgCount = (Integer)msgCountCache.get(userID)) != null) {                msgCount = new Integer(msgCount.intValue() + 1);                msgCountCache.put(userID, msgCount);            }        }    }    public ForumThread getThread(long threadID) throws ForumThreadNotFoundException {        return factory.getThread(threadID, this);    }    public void deleteThread(ForumThread thread) {        boolean abortTransaction = false;        Connection con = null;        try {            con = ConnectionManager.getTransactionConnection();            DbForumThread dbThread = (DbForumThread)getThread(thread.getID());            deleteThread(dbThread, con);        }        catch (Exception e) {            e.printStackTrace();            abortTransaction = true;        }        finally {            ConnectionManager.closeTransactionConnection(con, abortTransaction);        }        // Delete the thread from the search index.        factory.getSearchManager().removeFromIndex(thread);        // Now, delete thread from cache.        factory.cacheManager.threadCache.remove(new Long(thread.getID()));        // Clear forum data caches.        this.clearCache();    }    public void moveThread(ForumThread thread, Forum otherForum) {        // Ensure that thread belongs to this forum        if (thread.getForum().getID() != this.id) {            throw new IllegalArgumentException("The thread does not belong to this forum.");        }        // Get a handle on the actual db object.        DbForum forum = null;        try {            forum = factory.cacheManager.getForum(otherForum.getID());        }        catch (ForumNotFoundException fnfe) {            fnfe.printStackTrace();        }        // Read all messageIDs of the thread into an array so that we can expire        // each of them later.        ResultFilter ignoreModerationFilter = ResultFilter.createDefaultMessageFilter();        ignoreModerationFilter.setModerationRangeMin(Integer.MIN_VALUE+1);        LongList messageIDList = new LongList();        Iterator iter = thread.messages(ignoreModerationFilter);        while (iter.hasNext()) {            long messageID = ((ForumMessage)iter.next()).getID();            messageIDList.add(messageID);        }        // Modify the SQL records.        boolean abortTransaction = false;        Connection con = null;        try {            con = ConnectionManager.getTransactionConnection();            PreparedStatement pstmt = null;            try {                pstmt = con.prepareStatement(MOVE_THREAD);                pstmt.setLong(1, forum.getID());                pstmt.setLong(2, thread.getID());                pstmt.executeUpdate();                pstmt.close();                // Move all messages in thread to new forum.                pstmt = con.prepareStatement(MOVE_MESSAGES);                pstmt.setLong(1, forum.getID());                pstmt.setLong(2, thread.getID());                pstmt.executeUpdate();            }            finally {                try {  pstmt.close(); }                catch (Exception e) { e.printStackTrace(); }            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();            abortTransaction = true;            return;        }        finally {            ConnectionManager.closeTransactionConnection(con, abortTransaction);        }        DatabaseCacheManager cacheManager = factory.cacheManager;        SearchManager searchManager = factory.getSearchManager();        // Clear both forum's data caches.        this.clearCache();        forum.clearCache();        // Update the last modified date of both forums to the most recently        // updated thread (this may have changed during the move thread operation).        ResultFilter newestThreadFilter = ResultFilter.createDefaultThreadFilter();        newestThreadFilter.setNumResults(1);        Iterator threadIter = threads(newestThreadFilter);        if (threadIter.hasNext()) {            ForumThread newestThread = (ForumThread)threadIter.next();            if (newestThread != null) {                setModifiedDate(newestThread.getModifiedDate());            }        }        // Updated modified date of other forum.        threadIter = forum.threads(newestThreadFilter);        if (threadIter.hasNext()) {            ForumThread newestThread = (ForumThread)threadIter.next();            if (newestThread != null) {                forum.setModifiedDate(newestThread.getModifiedDate());            }        }        // Remove thread from cache.        try {            DbForumThread dbThread = cacheManager.getForumThread(thread.getID());            dbThread.clearCache();        }        catch (ForumThreadNotFoundException e) {            e.printStackTrace();        }        cacheManager.threadCache.remove(new Long(thread.getID()));        // Loop through all messages in thread and delete from cache, reset        // entry in the search index to new thread.        long [] messageIDArray = messageIDList.toArray();        for (int i=0; i<messageIDArray.length; i++) {            long messageID = messageIDArray[i];            cacheManager.messageCache.remove(new Long(messageID));            try {                ForumMessage message = thread.getMessage(messageID);                searchManager.removeFromIndex(message);                searchManager.addToIndex(message);            }            catch (ForumMessageNotFoundException e)  { }        }    }    public com.jivesoftware.forum.ForumThreadIterator threads() {        return threads(DEFAULT_THREAD_FILTER);    }    public ForumThreadIterator threads(ResultFilter resultFilter) {        String query = getThreadListSQL(resultFilter, false);        long [] threadBlock = getBlock(query.toString(), resultFilter.getStartIndex());        int startIndex = resultFilter.getStartIndex();        int endIndex;        // If number of results is set to inifinite, set endIndex to the total        // number of threads in the forum.        if (resultFilter.getNumResults() == ResultFilter.NULL_INT) {            endIndex = (int)getThreadCount(resultFilter);        }        else {            endIndex = resultFilter.getNumResults() + startIndex;        }        return new ForumThreadBlockIterator(threadBlock, query.toString(),                startIndex, endIndex, this.id, factory);    }    public Iterator popularThreads() {        if (popularThreads == null) {            // Determine the max number of threads that will be in the list.            // Default is 4, but can be overriden by setting a Jive property.            int threadNumber = 4;            try {                String number = JiveGlobals.getJiveProperty("popularThreads.number");                if (number != null) {                    threadNumber = Integer.parseInt(number);                }            }            catch (Exception e) { }            // Determine the number of days to consider for the list. Default is            // 24 hours, but can be overriden by setting a Jive property.            int timeWindow = 24;            try {                String window = JiveGlobals.getJiveProperty("popularThreads.timeWindow");                if (window != null) {                    timeWindow = Integer.parseInt(window);                }            }            catch (Exception e) { }            LongList popThreads = new LongList(threadNumber);            Calendar cal = Calendar.getInstance();            cal.add(Calendar.HOUR_OF_DAY, -timeWindow);            Connection con = null;            PreparedStatement pstmt = null;            try {                con = ConnectionManager.getConnection();                // If Oracle, use the special SQL that includes Oracle hints.                // Use the regular query for all other databases.                if (ConnectionManager.getDatabaseType() ==                        ConnectionManager.DatabaseType.ORACLE)                {                    pstmt = con.prepareStatement(POPULAR_THREADS_ORACLE);                }                else {                    pstmt = con.prepareStatement(POPULAR_THREADS);                }                pstmt.setString(1, StringUtils.dateToMillis(cal.getTime()));                pstmt.setLong(2, this.id);                ResultSet rs = pstmt.executeQuery();                for (int i=0; i < threadNumber; i++) {                    if (!rs.next()) {                        break;                    }                    popThreads.add(rs.getLong(1));                }                this.popularThreads = popThreads.toArray();            }            catch( SQLException sqle ) {                sqle.printStackTrace();            }            finally {                try {  pstmt.close(); }                catch (Exception e) { e.printStackTrace(); }                try {  con.close();   }                catch (Exception e) { e.printStackTrace(); }            }        }        return new DatabaseObjectIterator(JiveGlobals.THREAD, popularThreads,                this);    }    public Iterator messages() {        return messages(DEFAULT_MESSAGE_FILTER);    }    public Iterator messages(ResultFilter resultFilter) {        String query = getMessageListSQL(resultFilter, false);        long [] messageBlock = getBlock(query.toString(), resultFilter.getStartIndex());        int startIndex = resultFilter.getStartIndex();        int endIndex;        // If number of results is set to inifinite, set endIndex to the total        // number of threads in the forum.        if (resultFilter.getNumResults() == ResultFilter.NULL_INT) {            endIndex = (int)getMessageCount(resultFilter);        }        else {            endIndex = resultFilter.getNumResults() + startIndex;        }        return new ForumMessageBlockIterator(messageBlock, query.toString(),                startIndex, endIndex, this.id, factory, -1);    }    public int getThreadCount() {        return getThreadCount(DEFAULT_THREAD_FILTER);    }    public int getThreadCount(ResultFilter resultFilter) {        String sql = getThreadListSQL(resultFilter, true);        QueryCacheKey key = new QueryCacheKey(JiveGlobals.FORUM, id, sql, -1);        return getCount(key);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?