⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbforum.java

📁 java开发的一套非常好用的oa系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    public Iterator propertyNames() {
        if (LAZY_PROP_LOADING) {
            if (properties == null) {
                loadPropertiesFromDb();
            }
        }
        return Collections.unmodifiableSet(properties.keySet()).iterator();
    }

    public void addThread(ForumThread thread) {
        boolean abortTransaction = false;
        // Add thread to forum table.
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getTransactionConnection();
            try {
                pstmt = con.prepareStatement(ADD_THREAD);
                pstmt.setLong(1,id);
                pstmt.setLong(2,thread.getID());
                pstmt.executeUpdate();
            }
            finally {
                try {  pstmt.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }

            // Now, 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.
        factory.cacheManager.forumCache.remove(this.id);

        // Expire the userMessageCountCache if the root message was not posted
        // anonymously.
        ForumMessage message = thread.getRootMessage();
        if (!message.isAnonymous()) {
            factory.userMessageCountCache.remove(message.getUser().getID());
        }
    }

    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);
        }

        // Now, delete thread and forum from cache
        factory.cacheManager.threadCache.remove(thread.getID());
        factory.cacheManager.forumCache.remove(this.id);
    }

    public void moveThread(ForumThread thread, Forum forum)
            throws UnauthorizedException
    {
        // Ensure that thread belongs to this forum
        if (thread.getForum().getID() != this.id) {
            throw new IllegalArgumentException("The thread does not belong to this forum.");
        }

        // 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 record. Only the thread table has information about
        // forumID, so we only need to modify that record. The message records
        // underneath the thread can be left alone.
        boolean abortTransaction = false;
        Connection con = null;
        try {
            con = ConnectionManager.getTransactionConnection();
            PreparedStatement pstmt = null;
            try {
                pstmt = con.prepareStatement(ADD_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();

        // Remove both forums from cache.
        cacheManager.forumCache.remove(this.id);
        cacheManager.forumCache.remove(forum.getID());

        // 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.
        newestThreadFilter = ResultFilter.createDefaultThreadFilter();
        newestThreadFilter.setNumResults(1);
        threadIter = forum.threads(newestThreadFilter);
        if (threadIter.hasNext()) {
            ForumThread newestThread = (ForumThread)threadIter.next();
            if (newestThread != null) {
                forum.setModifiedDate(newestThread.getModifiedDate());
            }
        }

        // Remove thread from cache.
        cacheManager.threadCache.remove(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(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 = getThreadBlock(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
            // 1, but can be overriden by setting a Jive property.
            int timeWindow = 1;
            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.roll(Calendar.DAY_OF_YEAR, -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 = getMessageBlock(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 query = getThreadListSQL(resultFilter, true);
        CacheableInt count = (CacheableInt)threadCountCache.get(query);
        // If already in cache, return the count.
        if (count != null) {
            return count.getInt();
        }
        // Otherwise, we have to load the count from the db.
        else {
            int threadCount = 0;
            Connection con = null;
            Statement stmt = null;
            try {
                con = ConnectionManager.getConnection();
                stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                rs.next();
                threadCount = rs.getInt(1);

⌨️ 快捷键说明

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