📄 topicproxy.java
字号:
/*
* Created on 2007-3-17
* Last modified on 2007-08-22
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.topic;
import java.util.List;
import org.apache.log4j.Logger;
import com.yeqiangwei.club.dao.ContentDAO;
import com.yeqiangwei.club.dao.TopicDAO;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.counter.CountService;
import com.yeqiangwei.club.service.forum.ForumService;
import com.yeqiangwei.club.service.util.ManageLogService;
import com.yeqiangwei.club.service.util.NoAllowedCharService;
import com.yeqiangwei.club.service.model.ContentModel;
import com.yeqiangwei.club.service.model.CountersModel;
import com.yeqiangwei.club.service.model.ForumModel;
import com.yeqiangwei.club.service.model.ManageLogModel;
import com.yeqiangwei.club.service.model.TopicModel;
import com.yeqiangwei.club.service.model.UserModel;
import com.yeqiangwei.club.service.search.SearchFactory;
import com.yeqiangwei.club.service.search.SearchProvider;
import com.yeqiangwei.club.service.security.UserPower;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.util.LocalizeFile;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.TopicBetterForm;
import com.yeqiangwei.club.controller.form.TopicMoveForm;
import com.yeqiangwei.club.controller.form.TopicTrashForm;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.IpUtils;
import com.yeqiangwei.util.Validator;
public class TopicProxy implements TopicService{
private static final Logger logger = Logger.getLogger(TopicProxy.class);
public static TopicService TOPIC_SERVICE;
private static UserPower USER_POWER;
static{
if(TOPIC_SERVICE==null)TOPIC_SERVICE = new TopicServiceImpl();
if(USER_POWER==null)USER_POWER = new UserPower();
}
public TopicModel findNextOrPrevious(int topicId, int forumId, boolean isNext) {
return TOPIC_SERVICE.findNextOrPrevious(topicId, forumId, isNext);
}
public int trash(List<Integer> list, boolean isDeleted) throws ClubException {
return TOPIC_SERVICE.trash(list, isDeleted);
}
public int delete(List<Integer> ids) throws ClubException {
return TOPIC_SERVICE.delete(ids);
}
public TopicModel trash(TopicTrashForm form) throws ClubException {
TopicModel model = TOPIC_SERVICE.trash(form);
if(!Validator.isEmpty(model)){
this.getSearchProvider().deleteIndex(model.getTopicId()); //删除索引
UserModel byuser = form.getUserModel(); //操作人员
UserModel umodel = this.getUserService().findById(form.getUserId()); //帖子作者
if(!Validator.isEmpty(umodel)){ //如果是非过客发表的帖子
ManageLogModel log = new ManageLogModel();
log.setTopicId(model.getTopicId());
log.setTitle(model.getTitle());
if(!Validator.isEmpty(byuser)){
log.setByUserId(byuser.getUserId() );
log.setByUserName(byuser.getUserName());
log.setByUserIp(form.getUserIp());
}
log.setCreateDateTime(FormatDateTime.now());
log.setForumId(model.getForumId());
log.setUserName(umodel.getUserName());
log.setUserId(umodel.getUserId());
log.setIsList(form.getIsList());
log.setListByUserName(form.getListByUserName());
if(form.getIsDeleted()){
this.getUserService().ruleUtils(umodel,model.getForumId(),8); //获取用户积分变动
this.getUserService().update(umodel,false);
this.getUserService().ruleUtils(log,model.getForumId(),8); //获取日志积分变动
log.setMemo("删除文章");
}else{
log.setMemo("恢复文章");
}
StringBuffer temp = new StringBuffer();
temp.append(form.getContent());
log.setContent(temp.toString());
this.getManageLogService().create(log);
}
}
return model;
}
public TopicModel findTopicAndContentById(int id) {
return TOPIC_SERVICE.findTopicAndContentById(id);
}
public int update_forumId(int forumId, int toForumId) throws ClubException{
return TOPIC_SERVICE.update_forumId(forumId, toForumId);
}
public int updateViews(int topicId) throws ClubException {
return TOPIC_SERVICE.updateViews(topicId);
}
public TopicModel updateBetter(TopicBetterForm form) throws ClubException {
TopicModel model = TOPIC_SERVICE.updateBetter(form);
UserModel byuser = form.getUserModel(); //操作人员
UserModel umodel = this.getUserService().findById(form.getUserId()); //帖子作者
if(!Validator.isEmpty(umodel)){
ManageLogModel log = new ManageLogModel();
log.setTopicId(model.getTopicId());
log.setTitle(model.getTitle());
if(!Validator.isEmpty(byuser)){
log.setByUserId(byuser.getUserId() );
log.setByUserName(byuser.getUserName());
log.setByUserIp(form.getUserIp());
}
log.setCreateDateTime(FormatDateTime.now());
log.setForumId(model.getForumId());
log.setUserName(umodel.getUserName());
log.setUserId(umodel.getUserId());
log.setIsList(form.getIsList());
log.setListByUserName(form.getListByUserName());
if(form.getBetter()>0){
this.getUserService().ruleUtils(umodel,model.getForumId(),10); //获取用户积分变动
this.getUserService().update(umodel,false);
this.getUserService().ruleUtils(log,model.getForumId(),10); //获取日志积分变动
log.setMemo("加为精品");
}else{
log.setMemo("解除精品");
}
StringBuffer temp = new StringBuffer();
temp.append(form.getContent());
log.setContent(temp.toString());
this.getManageLogService().create(log);
}
return model;
}
public TopicModel findById(int id) {
return TOPIC_SERVICE.findById(id);
}
public TopicModel createOrUpdate(TopicModel model) throws ClubException {
if(model.getTopicId()==0){
return this.create(model);
}else{
return this.update(model);
}
}
private void localizeFile(TopicModel model){
if(model.getLocalizeImage()==1
&&USER_POWER.canAccess(
model.getUserModel(),model.getForumId(),UserPower.OPERATEPOWER,6))
{ //本地化图片
String temp = model.getContentModel().getContent();
temp = LocalizeFile.localizeImage(temp,LocalizeFile.imagepath(model.getForumId()));
model.getContentModel().setContent(temp);
}
}
/**
* 验证用户登录、权限,以及敏感字符过滤等
* 如果用户没有登录则先去验证用户登录
* @param model
* @return
* @throws ClubException
*/
private void noAllowed(TopicModel model) throws ClubException{
if(!this.getNoAllowedCharService().permissionTopic(model)){
throw new ClubException(MessageUtils.getMessage("error_char_noAllowed"));
}else{
ForumModel forum = this.getForumService().findById(model.getForumId());
if(Validator.isEmpty(forum)){
//this.setMessage(MessageUtils.getMessage("error_empty"));
logger.error("Forum("+model.getForumId()+") is null");
throw new ClubException(MessageUtils.getMessage("error_notfind_forum"));
}else if(forum.getNeedLabel()==(byte)1&&model.getLabelId()==0){
//this.setMessage(MessageUtils.getMessage("error_labelId"));
throw new ClubException(MessageUtils.getMessage("error_labelId"));
}
if(!Validator.isEmpty(model.getUserModel())){
model.setUserId(model.getUserModel().getUserId());
model.setUserName(model.getUserModel().getUserName().trim());
}else{//判断用户是否在页面同时输入了用户名和密码,如果是则请求数据库验证,否则只当作过客提交
UserModel user = null;
if(!Validator.isEmpty(model.getPassword())&&!Validator.isEmpty(model.getUserName()))
{
user = new UserModel();
user.setUserName(model.getUserName());
user.setPassword(model.getPassword());
user = this.getUserService().findByUserNameAndPassword(user,true);
if(Validator.isEmpty(user)){//如果登录失败
logger.debug("登录失败 userName:"+model.getUserName());
logger.debug("登录失败 password:"+model.getPassword());
//this.setMessage(MessageUtils.getMessage("error_login"));
throw new ClubException(MessageUtils.getMessage("error_login"));
}else{
model.setUserId(user.getUserId());
model.setUserName(user.getUserName());
model.setUserModel(user);
}
}else{//如果密码为空则为过客取名
String eip = IpUtils.enAddr(model.getUserIp());
eip = eip.substring(eip.length()-6,eip.length());
model.setUserName("过客"+eip);
}
}
}
}
public TopicModel create(TopicModel model) throws ClubException {
this.noAllowed(model);
this.localizeFile(model);
TOPIC_SERVICE.create(model);
//如果主题创建成功AND用户以登录
UserModel user = model.getUserModel();
user.setTopics(user.getTopics()+1); //回帖次数加1
//logger.debug("User score:"+user.getScore());
this.getUserService().ruleUtils(user,model.getForumId(),2); //根据社区制度更新用户参数
//logger.debug("after utils User score:"+user.getScore());
user = this.getUserService().update(user,false);
//logger.debug("after update User score:"+user.getScore());
model.setUserModel(user);
/*
* 统计相关
*/
CountersModel counters = new CountersModel();
counters.setTopics(1);
counters.setForumId(model.getForumId());
this.getCountService().doCount(counters);
return model;
}
public TopicModel update(TopicModel model) throws ClubException {
/*
* 是否具备编辑他人文章的权限
*/
boolean updateadmin = USER_POWER.canAccess(model.getSessionUserModel()
,model.getForumId()
,UserPower.ADMINPOWER
,0);
/*
* 是否具备编辑被回复过的文章的权限
*/
boolean updatereplyed = USER_POWER.canAccess(model.getSessionUserModel()
,model.getForumId()
,UserPower.OPERATEPOWER
,4);
/*
* 是否具备编辑自己文章的权限
*
*/
boolean updatemy = USER_POWER.canAccess(model.getSessionUserModel()
,model.getForumId()
,UserPower.OPERATEPOWER
,3);
if(!updateadmin){
//先检查文章作者和操作者是否为同一人
if(model.getSessionUserModel()!=null && model.getSessionUserModel().getUserId()== model.getUserId()){
/*
* 如果用户编辑的是自己的文章
* 这个判断放到了ctroller层
*/
logger.debug("model.getUserModel().getUserId(): "+model.getUserModel().getUserId());
logger.debug("model.getUserId(): "+model.getUserId());
if(!updatemy){
throw new ClubException(MessageUtils.getMessage("error_power"));
}
/*
* 判断是否具备编辑被回复过的文章权限
*/
if(model.getReplys()>0&&!updatereplyed){
//this.setMessage(MessageUtils.getMessage("error_update_replyed"));
throw new ClubException(MessageUtils.getMessage("error_update_replyed"));
}
}else{
//this.setMessage(MessageUtils.getMessage("error_power"));
throw new ClubException(MessageUtils.getMessage("error_power"));
}
}
this.localizeFile(model);
return TOPIC_SERVICE.update(model);
}
public int delete(TopicModel model) {
return 0;
}
public int delete(String[] ids) {
return 0;
}
public TopicModel move(TopicMoveForm form) throws ClubException {
UserModel userModel = form.getUserModel();
if(!Validator.isEmpty(userModel)){
form.setMoveUserId(userModel.getUserId());
form.setMoveUserName(userModel.getUserName());
}
TopicModel model = TOPIC_SERVICE.move(form);
if(Validator.isEmpty(model)){
return null;
}else{
UserModel umodel = this.getUserService().findById(model.getUserId());
if(!Validator.isEmpty(umodel)){
this.getUserService().ruleUtils(umodel,model.getForumId(),7);
umodel = this.getUserService().update(umodel,false);
if(!Validator.isEmpty(umodel)){
ManageLogModel log = new ManageLogModel();
log.setTopicId(model.getTopicId());
log.setTitle(model.getTitle());
log.setByUserId(model.getMoveUserId());
log.setByUserName(model.getMoveUserName());
log.setByUserIp(model.getUserIp());
log.setCreateDateTime(FormatDateTime.now());
log.setForumId(model.getForumId());
this.getUserService().ruleUtils(log,model.getForumId(),7);
log.setUserName(model.getUserName());
log.setUserId(model.getUserId());
log.setIsList(form.getIsList());
log.setListByUserName(form.getListByUserName());
log.setMemo("移动文章");
StringBuffer temp = new StringBuffer();
temp.append("文章从 ");
temp.append(this.getForumService(). findById(form.getTheForumId()).getForumName());
temp.append(" 移动到 ");
temp.append(this.getForumService().findById(form.getForumId()).getForumName());
temp.append("\r\n");
temp.append(form.getContent());
log.setContent(temp.toString());
this.getManageLogService().create(log);
}else{
logger.debug("更新用户参数错误 - userName:"+model.getUserName());
}
}
}
return model;
}
public List<TopicModel> findByParameter(TopicParameter param) {
return TOPIC_SERVICE.findByParameter(param);
}
public long countByParameter(TopicParameter param) {
return TOPIC_SERVICE.countByParameter(param);
}
public ManageLogService getManageLogService() {
return ServiceWrapper.<ManageLogService>getSingletonInstance(ServiceLocator.MANAGELOG);
}
public ForumService getForumService() {
return ServiceWrapper.<ForumService>getSingletonInstance(ServiceLocator.FORUM);
}
public UserService getUserService() {
return ServiceWrapper.<UserService>getSingletonInstance(ServiceLocator.USER);
}
/*
public NoAllowedCharService getNoAllowedCharService() {
return ServiceWrapper.<NoAllowedCharService>getSingletonInstance(ServiceLocator.NOALLOWEDCHAR);
}
*/
private CountService getCountService() {
return ServiceWrapper.<CountService>getSingletonInstance(ServiceLocator.COUNT);
}
public TopicDAO getTopicDAO() {
return TOPIC_SERVICE.getTopicDAO();
}
public Integer getLastReplyId() {
return TOPIC_SERVICE.getLastReplyId();
}
public void setLastReplyId(Integer i) {
TOPIC_SERVICE.setLastReplyId(i);
}
/*
public UserPower getUserPower() {
if(Validator.isEmpty(userPower)){
userPower = new UserPower();
}
return userPower;
}
*/
public List<ContentModel> findContentByParameter(TopicParameter param) {
return TOPIC_SERVICE.findContentByParameter(param);
}
public ContentDAO getContentDAO(){
return TOPIC_SERVICE.getContentDAO();
}
private SearchProvider getSearchProvider(){
return SearchFactory.getInstance(SearchProvider.SEARCH_TOPIC);
}
public NoAllowedCharService getNoAllowedCharService(){
return ServiceWrapper.<NoAllowedCharService>getSingletonInstance(ServiceLocator.NOALLOWEDCHAR);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -