学习 jForum笔记 二 ForumAction

来源:互联网 发布:佐治亚理工本科知乎 编辑:程序博客网 时间:2024/06/05 06:46

我的源程序是安道森练功房http://www.andowson.com/的2.2.1版本

2010.12.5接周五,继续研究。

Java代码 复制代码 收藏代码
  1. private void loadForums(ForumDAO fm) //取板块  
  2. {   
  3.     List<Forum> list = fm.selectAll(); //从数据表读取所有板块  
  4.        
  5.     Map<String, String> m = (Map<String, String>)cache.get(FQN, RELATION); //读缓存板块分类关系表  
  6.     if (m == null) { //缓存中没有  
  7.         m = new HashMap<String, String>();   
  8.     }   
  9.        
  10.     int lastId = 0;   //分类ID初值  
  11.     Category category = null;   
  12.     String catId = null;   
  13.   
  14.     for (Iterator<Forum> iter = list.iterator(); iter.hasNext(); ) {   
  15.         Forum forum = iter.next();   
  16.            
  17.         if (forum.getCategoryId() != lastId) {  //不属于当前分类  
  18.             if (category != null) {   
  19.                 cache.add(FQN, catId, category); //将所属分类写入缓存  
  20.             }   
  21.                
  22.             lastId = forum.getCategoryId();  //设置当前分类ID  
  23.             catId = Integer.toString(forum.getCategoryId());   
  24.             category = (Category)cache.get(FQN, catId); //读缓存中有无当前分类实体  
  25.         }   
  26.            
  27.         if (category == null) { //前面已将所有分类读入缓存,所以如果缓存中没有则数据表中也没有对应分类,如对应的分类不存在则报错。  
  28.             throw new CategoryNotFoundException("Category for forum #" + forum.getId() + " not found");   
  29.         }   
  30.            
  31.         String forumId = Integer.toString(forum.getId());   
  32.         category.addForum(forum); //将板块加入分类  
  33.         m.put(forumId, catId); //M的格式是板块ID,分类ID  
  34.     }   
  35.        
  36.     if (category != null) {   
  37.         cache.add(FQN, catId, category);   //将分类写入缓存  
  38.     }   
  39.        
  40.     cache.add(FQN, RELATION, m);  //将板块分类关系表写入缓存  
  41. }  
 

我又看了一下category.addForum(forum)过程,

Java代码 复制代码 收藏代码
  1. public void addForum(Forum forum) {   
  2.     this.forumsIdMap.put(Integer.valueOf(forum.getId()), forum);  //写入板块ID映射  
  3.     this.forums.add(forum); //写入板块集合  
  4. }  

 却意外发现,在category创建的时候已经读入了所有的forum,那为什么在forumRepository的start中又再次读一遍呢?有些奇怪,水平有限,不太明白。

Java代码 复制代码 收藏代码
  1. public Category(Category category) {   
  2.     this.name = category.getName();   
  3.     this.id = category.getId();   
  4.     this.order = category.getOrder();   
  5.     this.moderated = category.isModerated();   
  6.        
  7.     for (Iterator<Forum> iter = category.getForums().iterator(); iter.hasNext(); ) {   
  8.         Forum forum = new Forum(iter.next());    
  9.         this.forumsIdMap.put(Integer.valueOf(forum.getId()), forum);//写入板块ID映射  
  10.         this.forums.add(forum); //写入板块集合  
  11.     } //读入所有的板块,写入板块集合和板块ID映射   
  12. }  

 category.getForums()是读取分类下的当前用户有权处理的所有板块,但让我有些困惑的是,其来源是this.forums。而调用这个过程的Category又是将处理后的结果写入this.forums。难道说,是做一个过滤?将用户无权处理的板块删除出this.forums?

Java代码 复制代码 收藏代码
  1. public Collection<Forum> getForums()   
  2. {   
  3.     if (this.forums.size() == 0) { //如果没有,直接返回空  
  4.         return this.forums;   
  5.     }   
  6.        //否则,返回有权处理的板块   
  7.     return this.getForums(SessionFacade.getUserSession().getUserId());   
  8. }  
 
Java代码 复制代码 收藏代码
  1. public Collection<Forum> getForums(int userId)  //根据用户ID返回有权处理的板块  
  2. {   
  3.     PermissionControl pc = SecurityRepository.get(userId); //板限控制  
  4.     List<Forum> forums = new ArrayList<Forum>();   
  5.   
  6.     for (Iterator<Forum> iter = this.forums.iterator(); iter.hasNext(); ) {   
  7.         Forum forum = iter.next();   
  8.         if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer.toString(forum.getId()))) {   
  9.             forums.add(forum); //如果有权控制板块,则添加到返回列表  
  10.         }   
  11.     }   
  12.        
  13.     return forums;   
原创粉丝点击