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

📄 dbforum.java

📁 这是学习Java必须读懂两套源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    {
        //if (type == Forum.THREAD) {
        //   threadsModerated = moderated;
        //}
        //else {
        //    messagesModerated = moderated;
        //}
    }

    public ForumThread createThread(ForumMessage rootMessage)
        throws UnauthorizedException
    {
        //If the forum is moderated, the thread is not automatically
        //approved.
        boolean approved = true;
        return new DbForumThread(rootMessage, approved, this, factory);
    }

    public ForumMessage createMessage(User user)
        throws UnauthorizedException
    {
        //If the forum is moderated, the message is not automatically
        //approved.
        boolean approved = !isModerated(YazdConstants.MESSAGE);
        return new DbForumMessage(user, factory);
    }

    public void addThread(ForumThread thread) throws UnauthorizedException {
        boolean abortTransaction = false;
        boolean supportsTransactions = false;
        //Add message to db
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            supportsTransactions = con.getMetaData().supportsTransactions();
            if (supportsTransactions) {
                con.setAutoCommit(false);
            }

            pstmt = con.prepareStatement(ADD_THREAD);
            pstmt.setInt(1,id);
            pstmt.setInt(2,thread.getID());
            pstmt.executeUpdate();
            pstmt.close();

            //Now, insert the thread into the database.
            ((ForumThreadProxy)thread).insertIntoDb(con);
        }
        catch(Exception e) {
            e.printStackTrace();
            abortTransaction = true;
            return;
        }
        finally {
            try {
                if (supportsTransactions) {
                    if (abortTransaction == true) {
                        con.rollback();
                    }
                    else {
                        con.commit();
                    }
                }
            }
            catch (Exception e) { e.printStackTrace(); }
            try {
                if (supportsTransactions) {
                    con.setAutoCommit(true);
                }
                con.close();
            }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Since we added a thread, update the modified date of this thread.
        updateModifiedDate(thread.getModifiedDate());
    }

    public ForumThread getThread(int threadID) throws
            ForumThreadNotFoundException
    {
        return factory.getThread(threadID, this);
    }

    public void deleteThread(ForumThread thread) throws UnauthorizedException
    {
        //Delete all messages from the thread. Deleting the root
        //message will delete all submessages.
        ForumMessage message = thread.getRootMessage();
        thread.deleteMessage(message);
    }

    protected void deleteThreadRecord(int threadID) {

        //Delete the actual thread
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_THREAD);
            pstmt.setInt(1, threadID);
            pstmt.execute();
        }
        catch( Exception sqle ) {
            System.err.println("Error in DbForum:deleteThread()-" + sqle);
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Now, delete from cache
        Integer threadIDInteger = new Integer(threadID);
        factory.getCacheManager().remove(DbCacheManager.THREAD_CACHE, threadIDInteger);
    }

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

        //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.
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADD_THREAD);
            pstmt.setInt(1,forum.getID());
            pstmt.setInt(2,thread.getID());
            pstmt.executeUpdate();
            pstmt.close();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForum:addThread()-" + sqle);
            return;
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        DbCacheManager cacheManager = factory.getCacheManager();
        SearchIndexer indexer = factory.getSearchIndexer();

        //Remove both forums from cache.
        Integer key = new Integer(this.id);
        cacheManager.remove(DbCacheManager.FORUM_CACHE, key);
        key = new Integer(forum.getID());
        cacheManager.remove(DbCacheManager.FORUM_CACHE, key);

        //Remove thread from cache.
        key = new Integer(thread.getID());
        cacheManager.remove(DbCacheManager.THREAD_CACHE, key);

        //Loop through all messages in thread
        Iterator messages = thread.messages();
        while (messages.hasNext()) {
            ForumMessage message = (ForumMessage)messages.next();
            //Remove each message from cache.
            key = new Integer(message.getID());
            cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key);
            //Remove and re-add every message to the search index.
            indexer.removeFromIndex(message);
            indexer.addToIndex(message);
        }

        // Update the modified date of thread
        Date now = new Date();
        thread.setModifiedDate(now);
        // Update the modified date of forum thread is now in
        forum.setModifiedDate(now);
    }

    public Iterator threads() {
        return new DbForumIterator(this, factory);
    }

    public Iterator threads(int startIndex, int numResults) {
        return new DbForumIterator(this, factory, startIndex, numResults);
    }

    public int getThreadCount() {
        int threadCount = 0;
        // Based on the id in the object, get the thread data from the database:
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(THREAD_COUNT);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            threadCount = rs.getInt(1 /*"threadCount"*/);
        }
        catch( SQLException sqle ) {
            System.err.println("DbForum:getThreadCount() failed: " + sqle);
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return threadCount;
    }

    public int getMessageCount() {
        int messageCount = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(MESSAGE_COUNT);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            messageCount = rs.getInt(1 /*"messageCount"*/);
        }
        catch( SQLException sqle ) {
            System.err.println("DbForum:getMessageCount() failed: " + sqle);
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return messageCount;
    }

    public Query createQuery() {
        return new DbQuery(this, factory);
    }

    public void addUserPermission(User user, int permissionType)
            throws UnauthorizedException
    {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADD_USER_PERM);
            pstmt.setInt(1,id);
            pstmt.setInt(2,user.getID());
            pstmt.setInt(3,permissionType);
            pstmt.execute();
            //Remove user permissions from cache since they've changed.
            factory.getCacheManager().removeUserPerm(
                    new Integer(user.getID()),
                    new Integer(id)
            );
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForum.java:" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    public void removeUserPermission(User user, int permissionType)
            throws UnauthorizedException
    {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(REMOVE_USER_PERM);
            pstmt.setInt(1,id);
            pstmt.setInt(2,user.getID());
            pstmt.setInt(3,permissionType);
            pstmt.execute();
            //Remove user permissions from cache since they've changed.
            factory.getCacheManager().removeUserPerm(
                    new Integer(user.getID()),
                    new Integer(id)
            );
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForum.java:" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    public int[] usersWithPermission(int permissionType)
            throws UnauthorizedException
    {
        int [] users = new int[0];
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(USERS_WITH_PERM);
            pstmt.setInt(1,id);
            pstmt.setInt(2,permissionType);
            ResultSet rs = pstmt.executeQuery();
            ArrayList userList = new ArrayList();
            while (rs.next()) {
                userList.add(new Integer(rs.getInt("userID")));
            }
            users = new int[userList.size()];

⌨️ 快捷键说明

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