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

📄 dbforumfactory.java

📁 Jive(J道版) Jive(J道版)是在Jive 2.1版本基础上改编而成
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }

        // Loop through all messages from forum2 and delete from cache, then
        // reset entry in the search index.
        long [] messageIDArray = messageIDList.toArray();
        int i;
        for (i=0; i<messageIDArray.length; i++) {
            long messageID = messageIDArray[i];
            try {
                ForumMessage message = cacheManager.messageCache.get(messageID);
                searchManager.removeFromIndex(message);
                searchManager.addToIndex(message);
            }
            catch (Exception e)  {
                System.err.println("Failed to re-index message " + messageIDArray[i]);
                e.printStackTrace();
            }
        }
    }

    public Forum getForum(long forumID) throws ForumNotFoundException {
        return cacheManager.forumCache.get(forumID);
    }

    public Forum getForum(String name) throws ForumNotFoundException {
        return cacheManager.forumCache.get(name);
    }

    public int getForumCount() {
        if (forumCount != -1) {
            return forumCount;
        }
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(FORUM_COUNT);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            forumCount = rs.getInt(1);
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return forumCount;
    }

    public Iterator forums() {
        if (forums == null) {
            LongList forumList = new LongList();
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = ConnectionManager.getConnection();
                pstmt = con.prepareStatement(GET_FORUMS);
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                    forumList.add(rs.getLong(1));
                }
            }
            catch( SQLException sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  pstmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            this.forums = forumList.toArray();
        }
        return new DatabaseObjectIterator(JiveGlobals.FORUM, forums, this);
    }

    public Query createQuery() {
        return null;
        // Note, this a fake method only implemented to meet the requirements
        // of extending the abstract ForumFactory class. It's not possible to
        // directly implement this method since we need authorization
        // information to determine which forums a user has READ access for.
        // The ForumFactoryProxy instead automatically calls the
        // createQuery(Forum []) method with the proper set of forums.
    }

    public Query createQuery(Forum [] forums) {
        return new DbQuery(forums, this);
    }

    public Iterator popularForums() {
        if (popularForums == null) {
            LongList popForums = new LongList(popularForumsNumber);

            Calendar cal = Calendar.getInstance();
            cal.roll(Calendar.DAY_OF_YEAR, -popularForumsWindow);

            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = ConnectionManager.getConnection();
                pstmt = con.prepareStatement(POPULAR_FORUMS);
                pstmt.setString(1, StringUtils.dateToMillis(cal.getTime()));
                ResultSet rs = pstmt.executeQuery();
                for (int i=0; i < popularForumsNumber; i++) {
                    if (!rs.next()) {
                        break;
                    }
                    popForums.add(rs.getLong(1));
                }
                this.popularForums = popForums.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.FORUM, popularForums, this);
    }

    public Iterator popularThreads() {
        if (popularThreads == null) {
            LongList threadIDs = new LongList(popularThreadsNumber);

            Calendar cal = Calendar.getInstance();
            cal.roll(Calendar.DAY_OF_YEAR, -popularThreadsWindow);

            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = ConnectionManager.getConnection();
                pstmt = con.prepareStatement(POPULAR_THREADS);
                pstmt.setString(1, StringUtils.dateToMillis(cal.getTime()));
                ResultSet rs = pstmt.executeQuery();
                for (int i=0; i < popularThreadsNumber; i++) {
                    if (!rs.next()) {
                        break;
                    }
                    threadIDs.add(rs.getLong(1));
                }
                popularThreads = threadIDs.toArray();
            }
            catch( Exception e ) {
                e.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 UserManager getUserManager() {
        return userManager;
    }

    public GroupManager getGroupManager() {
        return groupManager;
    }

    public SearchManager getSearchManager() {
        return searchManager;
    }

    public FilterManager getFilterManager() {
        return filterManager;
    }

    public WatchManager getWatchManager() {
        return watchManager;
    }

    public RewardManager getRewardManager() {
        return rewardManager;
    }

    public PermissionsManager getPermissionsManager() {
        return permissionsManager;
    }

    public ForumMessageFilter [] getAvailableFilters()
    {
        return DbFilterManager.getAvailableFilters();
    }

    public void addFilterClass(String className) throws ClassNotFoundException,
            IllegalArgumentException
    {
        DbFilterManager.addFilterClass(className);
    }

    public ForumPermissions getPermissions(Authorization authorization) {
        long userID = authorization.getUserID();

        return permissionsManager.getFinalUserPerms(-1, userID);
    }

    public boolean hasPermission(int type) {
        return true;
    }

    //OTHER METHODS//

    /**
     * Returns the cache manager object.
     */
    public DatabaseCacheManager getCacheManager() {
        return cacheManager;
    }

    /**
     * Returns the id of the forum with the specified name.
     *
     * @param name the name of the forum to lookup.
     */
    protected static long getForumID(String name) throws ForumNotFoundException {
        Connection con = null;
        PreparedStatement pstmt = null;
        long forumID = -1;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(GET_FORUM_ID);
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            if (!rs.next()) {
                throw new ForumNotFoundException("Forum with name " + name +
                        "does not exist.");
            }
            forumID = rs.getLong(1);
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return forumID;
    }

    /**
     * Returns a thread specified by its id. An exception will be thrown if the
     * thread could not be loaded or if the thread is not in the specified
     * forum. If cache is turned on, it will be used.
     *
     * @param threadID the thread to load.
     * @param forum the forum that the thread belongs to.
     * @throws ForumThreadNotFoundException if the specified thread could not
     *      be loaded.
     */
    protected DbForumThread getThread(long threadID, DbForum forum) throws
            ForumThreadNotFoundException
    {
        DbForumThread thread = cacheManager.threadCache.get(threadID);
        // Make sure the thread is part of the forum.
        if (!(thread.forumID ==  forum.getID())) {
            throw new ForumThreadNotFoundException();
        }
        return thread;
    }

    /**
     * Returns a message based on its id. An exception will be thrown if the
     * message could not be loaded or if the message is not in the specified
     * thread. If cache is turned on, it will be used. All filters will be
     * applied to the message before it is returned.
     *
     * @param messageID the ID of the message to get.
     * @param threadID the ID of the thread the message belongs to.
     * @param forumID the ID of the forum that the message belong to.
     * @return the message specified by <tt>messageID</tt> with all filters
     *      applied.
     */
    protected ForumMessage getMessage(long messageID, long threadID, long forumID)
            throws ForumMessageNotFoundException
    {
        DbForumMessage message = cacheManager.messageCache.get(messageID);
        // Do a security check to make sure the message comes from the thread.
        if (message.threadID != threadID) {
            throw new ForumMessageNotFoundException();
        }
        ForumMessage filterMessage = null;
        // See if the filter values are not already cached.
        if (message.filteredSubject == null) {
            // Apply global filters
            filterMessage = filterManager.applyCacheableFilters(message);
            // Apply forum specific filters if there were no uncacheable filters
            // at the global level.
            if (!filterManager.hasUncacheableFilters()) {
                try {
                    FilterManager fManager = getForum(forumID).getFilterManager();
                    filterMessage =  fManager.applyCacheableFilters(filterMessage);
                }
                catch (Exception e) { }
            }
            // Now, cache those values.
            message.filteredSubject = filterMessage.getSubject();
            message.filteredBody = filterMessage.getBody();
            Hashtable filteredProperties = new Hashtable();
            for (Iterator i=filterMessage.propertyNames(); i.hasNext(); ) {
                String name = (String)i.next();
                filteredProperties.put(name, filterMessage.getProperty(name));
            }
            message.filteredProperties = filteredProperties;
        }
        // Apply uncacheable filters.
        if (filterManager.hasUncacheableFilters()) {
            // Apply global uncachable filters and then all filters for the forum.
            filterMessage = filterManager.applyUncacheableFilters(message);
            try {
                Forum forum = getForum(forumID);
                filterMessage = forum.getFilterManager().applyFilters(filterMessage);
            }
            catch (Exception e) { }
        }
        else {
            // Apply any forum specific uncacheable filters.
            try {
                Forum forum = getForum(forumID);
                filterMessage = forum.getFilterManager().applyUncacheableFilters(message);
            }
            catch (Exception e) { }
        }
        return filterMessage;
    }
}

⌨️ 快捷键说明

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