📄 topicbuilder.java
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的,如有技术问题请与本人联系! */package biz.tbuy.bbs;import biz.tbuy.common.Utils;import java.sql.Timestamp;import java.util.Date;import java.util.List;/** * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b> * <a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b> * <a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class TopicBuilder extends Builder{ private TopicModel _topic; // 当前操作的topic private AuthTopic _authenticator; // 该topic所拥有的权限 public TopicBuilder(TopicModel topic, AuthTopic authenticator) { _topic = topic; _authenticator = authenticator; } /** * 获得当前builder所操作的topic * @return topic */ public TopicModel getTopic() { return _topic; } /** * 获取当前用户相对于该topic的所有权限 * @return authenticator */ public AuthTopic getAuthenticator() { return _authenticator; } // ------------------------------------------------------------ topic post /** * 发表主题文章,将主题插入数据库中,并返回主题的主键值:t_num * @return key,返回所插入主题的主键值"t_num",如果发表失败, * 则返回 -1; */ public int post() { if (!isPost()) return -1; // 如果没有权限发表相应的主题类型 String[] type = getBBSApplication().getTopicTypes().get(_topic.getType()); if (type == null) { _topic.setType(BBSGroups.TYPE_TOPIC); } else { int level = Integer.parseInt(type[0]); if (_authenticator.getPower() < level) { Utils.addErrorMessage(getBundle(), "缺少权限,你无权发表该主题类型!"); return -1; } } // 如果BBS已经关闭了主题背景音乐 if (!getBBSApplication().isBgsound()) { _topic.setMusic(-1); } int key = TopicAction.addTopicWithKey(_topic); _topic.setNum(key); return key; } // ------------------------------------------------------------ topic display /** * 获取用户是否有权限查看该贴子 * @return true or false */ public boolean isViewTopic() { return _authenticator.isTopicView(); } /** * 判断用户是否有权限修改指定的主题的内容 * @return true or false */ public boolean isDisplay() { if (!_authenticator.isTopicUpdateContent()) { Utils.addErrorMessage(getBundle(), "权限不足,你无法编辑该贴子!"); if (_topic.getBeLock()) { Utils.addErrorMessage(getBundle(), "该贴已锁!"); } return false; } return true; } // ------------------------------------------------------------ topic update /** * 更新topic的主体内容及引用出处,同时更新最后修改者及修改时间 * @param content 要更新的内容,可为null * @param from 要更新的文章出处,可为null * @return true or false; */ public boolean updateContent(String content, String from) { if (!_authenticator.isTopicUpdateContent()) { Utils.addErrorMessage(getBundle(), "对不起,你无权限修改该文章!"); return false; } if (from != null) _topic.setFrom(from); if (content != null) _topic.setContent(content); _topic.setEditByUser(getVisitor().getUser().getId()); _topic.setEditByDate(new Timestamp(new Date().getTime())); return update(); } /** * 更新文章的阅读权限, * @return */ public boolean updateAuth(int authType, List<String> authGroups) { if (!_authenticator.isTopicUpdateAuth()) { Utils.addErrorMessage(getBundle(), "更改阅读权限时,遇到权限不足!"); return false; } // 设置阅读类型(如果选择了“特定用户组”同设置所选择的用户组) _topic.setAuthType(authType); if (authType == BBSGroups.READER_AUTH_GROUPS && !authGroups.isEmpty()) { StringBuffer groupsStr = new StringBuffer(); for (String group : authGroups) { groupsStr.append(group + ","); } _topic.setAuthGroups(authGroups.toString()); } return update(); } /** * 更新主题类型 * @return true or false */ public boolean updateType(int type) { String[] oldType = getBBSApplication().getTopicTypes().get(_topic.getType()); String[] newType = getBBSApplication().getTopicTypes().get(type); if (oldType == null || newType == null) { Utils.addErrorMessage(getBundle(), "主题类型错误!"); return false; } int oldLevel = Integer.parseInt(oldType[0]); int newLevel = Integer.parseInt(newType[0]); if (_authenticator.getPower() < oldLevel || _authenticator.getPower() < newLevel) { Utils.addErrorMessage(getBundle(), "缺少权限,你无权更新该主题类型!"); return false; } _topic.setType(type); return update(); } /** * 对该topic进行锁定或解锁 * @param lock true为锁定,false为解锁 * @return true or false */ public boolean displayLock(boolean lock) { if (!_authenticator.isTopicLock()) { Utils.addErrorMessage(getBundle(), "对不起,你无权限锁定/解锁该贴!"); return false; } _topic.setBeLock(lock); return update(); } /** * 让主题的总体回复量增加number数量 * @param number * @return true 更新成功,否则false; */ public boolean growTotalReply(int number) { _topic.setTotalReply(_topic.getTotalReply() + number); return update(); } /** * 更新主题的背景音乐信息 * @param soundId 新的音乐信息id, -1 表示取消背景音乐 * @return true or false; */ public boolean changeBgsound(int soundId) { if (!_authenticator.isTopicBgsound()) { Utils.addErrorMessage(getBundle(), "修改失败!可能系统已经关闭了背景音乐功能."); return false; } _topic.setMusic(soundId); return update(); } // ------------------------------------------------------------ topic move /** * 将主题移动到另一个讨论区 * @param forumId 目标讨论区 * @return true or false */ public boolean move(int forumId) { if (!_authenticator.isTopicMove()) { Utils.addErrorMessage(getBundle(), "对不起,你无权限移动该主题!"); return false; } ForumBuilder fBuilder = ForumFactory.newBuilder(forumId); return fBuilder.receiveTopic(_topic); } // ------------------------------------------------------------ topic revoke /** * 将Topic移入回收站,并不真正删除该Topic * @return true 如果更新成功,否则返回false; */ public boolean revoke() { if (!_authenticator.isTopicRevoke()) { Utils.addErrorMessage(getBundle(), "对不起,你无权限删除该贴子!"); return false; } _topic.setBeRevoke(true); return TopicAction.revokeTopicById(_topic.getNum()); } // ------------------------------------------------------------ topic uploadFile /** * 处理上传文件信息,将fileModel信息加入数据库中,该file信息将会与当前topic相关联 * @param file * @return true or false */ public boolean uploadFile(FileModel file) { if (!isUpload()) return false; if (file == null) return false; return FileAction.addFile(file); } // ------------------------------------------------------------ topic private /** * 更新当前builder所操作的topic * @return true 如果更新成功,否则false */ private boolean update() { return TopicAction.updateTopic(_topic); } // ------------------------------------------------------------ authenticator /** * 检查是否允许发表主题 * @return true false */ private boolean isPost() { if (!_authenticator.isPost()) { Utils.addErrorMessage(getBundle(), "你所选择的讨论区已经禁止发表主题!或者你可能缺少权限"); return false; } else { return true; } } /** * 检查是否允许上传附件 * @return true false */ private boolean isUpload() { if (!_authenticator.isUpload()) { Utils.addErrorMessage(getBundle(), "该讨论区禁止上传附件!或者你可能缺少权限"); return false; } else { return true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -