我的源程序是安道森练功房http://www.andowson.com/的2.2.1版本
2010.12.5接周五,继续研究。
- private void loadForums(ForumDAO fm) //取板块
- {
- List<Forum> list = fm.selectAll(); //从数据表读取所有板块
- Map<String, String> m = (Map<String, String>)cache.get(FQN, RELATION); //读缓存板块分类关系表
- if (m == null) { //缓存中没有
- m = new HashMap<String, String>();
- }
- int lastId = 0; //分类ID初值
- Category category = null;
- String catId = null;
- for (Iterator<Forum> iter = list.iterator(); iter.hasNext(); ) {
- Forum forum = iter.next();
- if (forum.getCategoryId() != lastId) { //不属于当前分类
- if (category != null) {
- cache.add(FQN, catId, category); //将所属分类写入缓存
- }
- lastId = forum.getCategoryId(); //设置当前分类ID
- catId = Integer.toString(forum.getCategoryId());
- category = (Category)cache.get(FQN, catId); //读缓存中有无当前分类实体
- }
- if (category == null) { //前面已将所有分类读入缓存,所以如果缓存中没有则数据表中也没有对应分类,如对应的分类不存在则报错。
- throw new CategoryNotFoundException("Category for forum #" + forum.getId() + " not found");
- }
- String forumId = Integer.toString(forum.getId());
- category.addForum(forum); //将板块加入分类
- m.put(forumId, catId); //M的格式是板块ID,分类ID
- }
- if (category != null) {
- cache.add(FQN, catId, category); //将分类写入缓存
- }
- cache.add(FQN, RELATION, m); //将板块分类关系表写入缓存
- }
private void loadForums(ForumDAO fm) //取板块
{
List<Forum> list = fm.selectAll(); //从数据表读取所有板块
Map<String, String> m = (Map<String, String>)cache.get(FQN, RELATION); //读缓存板块分类关系表
if (m == null) { //缓存中没有
m = new HashMap<String, String>();
}
int lastId = 0; //分类ID初值
Category category = null;
String catId = null;
for (Iterator<Forum> iter = list.iterator(); iter.hasNext(); ) {
Forum forum = iter.next();
if (forum.getCategoryId() != lastId) { //不属于当前分类
if (category != null) {
cache.add(FQN, catId, category); //将所属分类写入缓存
}
lastId = forum.getCategoryId(); //设置当前分类ID
catId = Integer.toString(forum.getCategoryId());
category = (Category)cache.get(FQN, catId); //读缓存中有无当前分类实体
}
if (category == null) { //前面已将所有分类读入缓存,所以如果缓存中没有则数据表中也没有对应分类,如对应的分类不存在则报错。
throw new CategoryNotFoundException("Category for forum #" + forum.getId() + " not found");
}
String forumId = Integer.toString(forum.getId());
category.addForum(forum); //将板块加入分类
m.put(forumId, catId); //M的格式是板块ID,分类ID
}
if (category != null) {
cache.add(FQN, catId, category); //将分类写入缓存
}
cache.add(FQN, RELATION, m); //将板块分类关系表写入缓存
}
我又看了一下category.addForum(forum)过程,
- public void addForum(Forum forum) {
- this.forumsIdMap.put(Integer.valueOf(forum.getId()), forum); //写入板块ID映射
- this.forums.add(forum); //写入板块集合
- }
public void addForum(Forum forum) {
this.forumsIdMap.put(Integer.valueOf(forum.getId()), forum); //写入板块ID映射
this.forums.add(forum); //写入板块集合
}
却意外发现,在category创建的时候已经读入了所有的forum,那为什么在forumRepository的start中又再次读一遍呢?有些奇怪,水平有限,不太明白。
- public Category(Category category) {
- this.name = category.getName();
- this.id = category.getId();
- this.order = category.getOrder();
- this.moderated = category.isModerated();
- for (Iterator<Forum> iter = category.getForums().iterator(); iter.hasNext(); ) {
- Forum forum = new Forum(iter.next());
- this.forumsIdMap.put(Integer.valueOf(forum.getId()), forum);//写入板块ID映射
- this.forums.add(forum); //写入板块集合
- } //读入所有的板块,写入板块集合和板块ID映射
- }
public Category(Category category) {
this.name = category.getName();
this.id = category.getId();
this.order = category.getOrder();
this.moderated = category.isModerated();
for (Iterator<Forum> iter = category.getForums().iterator(); iter.hasNext(); ) {
Forum forum = new Forum(iter.next());
this.forumsIdMap.put(Integer.valueOf(forum.getId()), forum);//写入板块ID映射
this.forums.add(forum); //写入板块集合
} //读入所有的板块,写入板块集合和板块ID映射
}
category.getForums()是读取分类下的当前用户有权处理的所有板块,但让我有些困惑的是,其来源是this.forums。而调用这个过程的Category又是将处理后的结果写入this.forums。难道说,是做一个过滤?将用户无权处理的板块删除出this.forums?
- public Collection<Forum> getForums()
- {
- if (this.forums.size() == 0) { //如果没有,直接返回空
- return this.forums;
- }
- //否则,返回有权处理的板块
- return this.getForums(SessionFacade.getUserSession().getUserId());
- }
public Collection<Forum> getForums()
{
if (this.forums.size() == 0) { //如果没有,直接返回空
return this.forums;
}
//否则,返回有权处理的板块
return this.getForums(SessionFacade.getUserSession().getUserId());
}
- public Collection<Forum> getForums(int userId) //根据用户ID返回有权处理的板块
- {
- PermissionControl pc = SecurityRepository.get(userId); //板限控制
- List<Forum> forums = new ArrayList<Forum>();
- for (Iterator<Forum> iter = this.forums.iterator(); iter.hasNext(); ) {
- Forum forum = iter.next();
- if (pc.canAccess(SecurityConstants.PERM_FORUM, Integer.toString(forum.getId()))) {
- forums.add(forum); //如果有权控制板块,则添加到返回列表
- }
- }
- return forums;