學習 jForum筆記 四ForumAction 發現用戶認證模板 .

    public synchronized static void start(final ForumDAO forumDAO, final CategoryDAO categoryDAO, final ConfigDAO configModel)
    {
        instance = new ForumRepository();
       
        if (cache.get(FQN, LOADED) == null) {  //未讀入緩存
            instance.loadCategories(categoryDAO); //取分類
            instance.loadForums(forumDAO);       //取板塊
            instance.loadMostUsersEverOnline(configModel); //取最高在線人數
            instance.loadUsersInfo ();  //取用戶信息
           
            final Integer totalMessages = (Integer)cache.get(FQN, TOTAL_MESSAGES); //取緩存信息數
           
            if (totalMessages == null) {
                cache.add(FQN, TOTAL_MESSAGES, Integer.valueOf(0)); //設置緩存信息數爲零
            }
           
            cache.add(FQN, LOADED, "1"); //已讀入緩存標記
        }
    }

 

還剩下loadUserInfo()沒分析了。

 

    private void loadUsersInfo() //取用戶信息
    {   //直接從數據表讀取並寫入緩存
        UserDAO udao = DataAccessDriver.getInstance().newUserDAO();
        cache.add(FQN, LAST_USER, udao.getLastUserInfo ());
        cache.add(FQN, TOTAL_USERS, Integer.valueOf(udao.getTotalUsers ()));
    }

 

在net.jforum.dao.generic/GenericUserDAO.java中

 

    public User getLastUserInfo()
    {  //取最後註冊的用戶姓名與ID
        PreparedStatement p = null;
        ResultSet rs = null;
        try {
            User user = new User();
            p = JForumExecutionContext.getConnection().prepareStatement(
                    SystemGlobals.getSql("UserModel.lastUserRegistered")); //取SQL語句
            rs = p.executeQuery();
            rs.next();
            user.setUsername(rs.getString("username"));  //用戶名
            user.setId(rs.getInt("user_id"));  //用戶ID
            return user;
        }
        catch (SQLException e) {
            throw new DatabaseException(e);
        }
        finally {
            DbUtils.close(rs, p);
        }
    }

 

    public int getTotalUsers()
    {  //取總註冊人數
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = JForumExecutionContext.getConnection().prepareStatement(
                    SystemGlobals.getSql("UserModel.totalUsers"));  //取SQL語句
            return this.getTotalUsersCommon (preparedStatement); //防異常處理
        }
        catch (SQLException e) {
            throw new DatabaseException(e);
        }
        finally {
            DbUtils.close(preparedStatement);
        }
    }

 

    protected int getTotalUsersCommon(PreparedStatement p) throws SQLException//防異常處理
    {
        ResultSet rs = p.executeQuery();
        int total = 0;
        if (rs.next()) {
            total = rs.getInt(1);
        }
        rs.close();
        p.close();
        return total;
    }

 

小結:

根據ForumRepository的start,其動作爲:取所有分類,取當前用戶有權處理的所有板塊,取最高在線人數,取最後註冊用戶信息及註冊總人數,取緩存信息數。

 

取所有分類的語句在:“CategoryModel.selectAll”

取所有板塊的語句在:“ForumModel.selectAll”

取最後註冊的用戶姓名與ID語句在: "UserModel.lastUserRegistered"

取總註冊人數語句在: "UserModel.totalUsers")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章