⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replyserviceimpl.java

📁 这是一款最新的野蔷薇论坛源码,有需要的朋友可以尽情下载
💻 JAVA
字号:
/* 
 * Created on 2007-3-23
 * 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.cache.Cache;
import com.yeqiangwei.club.cache.CacheFactory;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.ReContentDAO;
import com.yeqiangwei.club.dao.ReplyDAO;
import com.yeqiangwei.club.dao.TopicDAO;
import com.yeqiangwei.club.dao.model.ReContent;
import com.yeqiangwei.club.dao.model.Reply;
import com.yeqiangwei.club.dao.model.Topic;
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.model.CountersModel;
import com.yeqiangwei.club.service.model.ReContentModel;
import com.yeqiangwei.club.service.model.ReplyModel;
import com.yeqiangwei.club.service.model.UserModel;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.TopicTrashForm;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.Validator;

public class ReplyServiceImpl implements ReplyService {
	
	private static final Logger logger = Logger.getLogger(ReplyServiceImpl.class);
	
	/**
	 * Cache主题分页列表
	 * @param forumId
	 * @return
	 */
	private Cache getTopicCache(Integer forumId){
		return CacheFactory.creator(CacheRegion.TOPIC+"-formId:"+forumId); 
	}
	
	/**
	 * cache 主题回复数
	 * @param forumId
	 * @return
	 */
	private Cache getCache(Integer forumId){
		return CacheFactory.creator(CacheRegion.REPLY+"-formId:"+forumId); 
	}

	public int delete(List<Integer> ids) {
		int s = 0;
		int e = 0;
		int c = this.getReplyDAO().delete(ids);
		this.getReContentDAO().deleteByReplyId(ids);
		if(c>0){
			s++;
		}else{
			e++;
		}
		//this.setMessage(MessageUtils.getMessage(s,e));
		logger.debug(MessageUtils.getMessage(s,e));
		return s;
	}
	
	public int trash(List<Integer> list, boolean isDeleted){
		int s = 0;
		int e = 0;
		for(int i=0; i<list.size(); i++){
			int id = list.get(i);
			Reply item = this.getReplyDAO().findById(id);
			ReContent citem = this.getReContentDAO().findByReplyId(id);
			item.setIsDeleted(isDeleted);
			citem.setIsDeleted(isDeleted);
			this.getReplyDAO().update(item);
			this.getReContentDAO().update(citem);
			if(!Validator.isEmpty(item)){
				s++;
			}else{
				e++;
			}
		}
		logger.debug(MessageUtils.getMessage(s,e));
		return s;
	}

	public ReplyModel trash(TopicTrashForm form) throws ClubException {
		ReplyModel model = null;
		Reply item = this.getReplyDAO().findById(form.getReplyId());
		if(!Validator.isEmpty(item)){
			item.setIsDeleted(form.getIsDeleted());
			item.setIsManaged(true);
			this.getReplyDAO().update(item);
			logger.debug("trash() isDeleted:"+item.getIsDeleted());
			logger.debug("trash() replyId:"+item.getReplyId());
			ReContent citem = this.getReContentDAO().findByReplyId(form.getReplyId());
			citem.setIsDeleted(form.getIsDeleted());
			this.getReContentDAO().update(citem);
			model = new ReplyModel();
			BeanUtils.copyProperties(model,item);
			return model;
		}else{
			//this.setMessage(MessageUtils.getMessage("error"));
			throw new ClubException("ReplyModel("+form.getTopicId()+") is null!");
		}	
	}

	public List<ReplyModel> findReplyAndContent(TopicParameter param) {
		List<ReplyModel> rlist = this.findReplyByTopicId(param);
		List<ReContent> clist = this.getReContentDAO().findByParameter(param);
		if(!Validator.isEmpty(rlist)&&!Validator.isEmpty(clist)){
			for(int i=0; i<rlist.size(); i++){
				ReContent rc = clist.get(i);
				ReContentModel rcm = new ReContentModel();
				BeanUtils.copyProperties(rcm,rc);
				ReplyModel rm = rlist.get(i);
				rm.setReContentModel(rcm);
			}
		}
		return rlist;
	}

	public ReplyModel findReplyAndContentById(int id) {
		ReplyModel model = this.findById(id);
		if(!Validator.isEmpty(model)){
			ReContent ritem = this.getReContentDAO().findByReplyId(id);
			if(!Validator.isEmpty(ritem)){
				ReContentModel rcmodel = new ReContentModel();
				BeanUtils.copyProperties(rcmodel,ritem);
				model.setReContentModel(rcmodel);
				return model;
			}else{
				logger.error("ReContent("+model.getReplyId()+") is null!");
				return null;
			}
		}else{
			return null;
		}
	}
	
	public List<ReplyModel> findReplyByTopicId(TopicParameter param) {
		List<Reply> list = this.getReplyDAO().findByParameter(param);
		List<ReplyModel> rlist = BeanUtils.<Reply,ReplyModel>copyList(list,BeanLocator.REPLYMODEL);
		return rlist;
	}


	public ReplyModel findById(int id) {
		if(id>0){
			Reply item = this.getReplyDAO().findById(id);
			if(!Validator.isEmpty(item)){
				ReplyModel model = new ReplyModel();
				BeanUtils.copyProperties(model,item);
				return model;
			}
		}
		return null;
	}

	public ReplyModel createOrUpdate(ReplyModel model) throws ClubException {
		if(model.getReplyId()>0){
			return this.update(model);
		}else{
			return this.create(model);
		}
	}
	
	private void updateParameter(ReplyModel replyModel) {
		Reply reply = this.getReplyDAO().findById(replyModel.getReplyId());
		if(!Validator.isEmpty(reply)){
			BeanUtils.copyProperties(reply, replyModel);
			this.getReplyDAO().update(reply);
		}
	}
	
	private void treeUtils(ReplyModel model){
        int orderlist = 0;
        int layer = 0;
        int tree = 0;
        int replys = 0;
        //logger.debug("model.getReplyId():"+model.getReplyId());
        if(model.getReplyId()>0){
        	ReplyModel replyModel = this.findById(model.getReplyId());
        	if(!Validator.isEmpty(replyModel)){
        		orderlist = replyModel.getOrderlist();
        		layer = replyModel.getLayer();
        		tree = replyModel.getTree();
        		replys = replyModel.getReplys();//更新前的回复次数
        		replyModel.setReplys(replyModel.getReplys()+1);//更新回复表回复次数
                this.updateParameter(replyModel);
        	}
        }else{
        	replys = this.getTopicService().findById(model.getTopicId()).getReplys();
        }
    	Reply item = new Reply();
    	item.setTopicId(model.getTopicId());
    	item.setReplyId(model.getReplyId());
    	item.setOrderlist(orderlist);
        this.getReplyDAO().updatesOrderlistByTopicId(item);
        if(model.getReplyId()>0){
        	orderlist++;
        }else{
        	orderlist = 1;
        }
        /* 计算树型参数 */
        int tTree = 0;
        if(replys>0){
           tTree = (tree * 2 +1 );
        }else{
           tTree = tree * 2;
        }
        layer += 1;
        model.setLayer(layer);
        model.setOrderlist(orderlist);
        model.setTree(tTree);
        model.setReplys(0);
	}

	public ReplyModel create(ReplyModel model) throws ClubException {
		if(Validator.isEmpty(model)||Validator.isEmpty(model.getTitle())){
			//this.setMessage(super.getMessage("error_empty"));
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}else{
			//logger.debug("回帖时间:"+model.getCreateDateTime());
			this.treeUtils(model);
			//logger.debug("回帖时间:"+model.getCreateDateTime());
			ReContentModel cmodel = model.getReContentModel();
			model.setContentLength(cmodel.getContent().length());
			Reply item = new Reply();
			BeanUtils.copyProperties(item,model);
			if(model.getTitle().length()>100||model.getContentLength()>100000){
				logger.error("too long title: "+model.getTitle());
				logger.error("content.length: "+model.getContentLength());
				//this.setMessage(super.getMessage("error_toolong"));
				throw new ClubException(MessageUtils.getMessage("error_toolong"));
			}
			int replyId = model.getReplyId(); //保持回复创建之前的replyId
			/*
			Topic topic = this.getTopicService().getTopicDAO().findById(model.getTopicId());
			if(model.getReplyId()==0){
				topic.setReplys(topic.getReplys()+1);
			}
			*/
			this.getReplyDAO().create(item);
			BeanUtils.copyProperties(model,item); //PO->BO
			cmodel.setTopicId(item.getTopicId());
			cmodel.setReplyId(item.getReplyId());
			//创建回复的内容
			ReContent citem = new ReContent();
			BeanUtils.copyProperties(citem,cmodel);
			citem = this.getReContentDAO().create(citem);
			BeanUtils.copyProperties(cmodel,citem); //PO->BO
			model.setReContentModel(cmodel);
			/*更新最后回复ID的Cache*/
			this.getTopicService().setLastReplyId(new Integer(model.getReplyId()));
			/* 更新回复次数最后更新时间等 */
			Topic topic = this.getTopicService().getTopicDAO().findById(model.getTopicId());
			if(Validator.isEmpty(topic)){
				throw new ClubException(MessageUtils.getMessage("error_notfind_topic"));
			}else{
				if(replyId==0){
					topic.setReplys(topic.getReplys()+1);
				}
				topic.setLastReplyDateTime(FormatDateTime.now());
				topic.setLastReplyUserName(model.getUserName());
				topic.setLastReplyUserId(model.getUserId());
				topic.setReplyId(model.getReplyId());
				this.getTopicService().getTopicDAO().update(topic);
				this.getTopicCache(model.getForumId()).clear(); //清除主题列表缓存
				this.getTopicCache(null).clear(); //清除主题列表缓存
				/*
				 * 更新被回复的主帖的用户参数
				 */
				UserModel tuser = this.getUserService().findById(topic.getUserId());
				if(!Validator.isEmpty(tuser)){
					this.getUserService().ruleUtils(tuser,model.getForumId(),6); //文章被回复的参数变化
					this.getUserService().update(tuser);
					logger.debug("Topic.userId: "+tuser.getUserId());
				}
			}
			this.getCache(model.getForumId()).clear(); //清除count统计Cache
			/* 回帖用户的积分变动 */
			UserModel user = model.getUserModel();
			user.setReplys(user.getReplys()+1); //回帖次数加1
			this.getUserService().ruleUtils(user,model.getForumId(),3); //根据社区回复制度更新用户参数
			model.setUserModel(user);
			
			/* 更新统计信息 */
			CountersModel counters = new CountersModel();
			counters.setReplys(1);
			counters.setForumId(model.getForumId());
			this.getCountService().doCount(counters);
			//this.setMessage(super.getMessage("success"));
		}
		return model;
	}

	public ReplyModel update(ReplyModel model) throws ClubException {
		if(Validator.isEmpty(model)||Validator.isEmpty(model.getTitle())){
			//this.setMessage(super.getMessage("error_empty"));
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}
		else if(model.getTopicId()==0){
			model = null;
			//this.setMessage("error_system");
			throw new ClubException(MessageUtils.getMessage("error_system"));
		}
		else{
			ReContentModel cmodel = model.getReContentModel();
			model.setContentLength(cmodel.getContent().length());
			Reply item = this.getReplyDAO().findById(model.getReplyId());
			BeanUtils.copyProperties(item,model);
			if(model.getTitle().length()>100||model.getContentLength()>50000){
				//this.setMessage(super.getMessage("error_toolong"));
				throw new ClubException(MessageUtils.getMessage("error_toolong"));
			}
			this.getReplyDAO().create(item);
			BeanUtils.copyProperties(model,item); //PO->BO
			cmodel.setTopicId(item.getTopicId());
			cmodel.setReplyId(item.getReplyId());
			ReContent citem = this.getReContentDAO().findByReplyId(model.getReplyId());
			BeanUtils.copyProperties(citem,cmodel);
			this.getReContentDAO().update(citem);
			BeanUtils.copyProperties(cmodel,citem); //PO->BO
			model.setReContentModel(cmodel);
		}
		return model;
	}

	public int delete(ReplyModel model) {
		return 0;
	}

	public int delete(String[] ids) {
		return 0;
	}

	public List<ReplyModel> findByParameter(TopicParameter param) {
		List<Reply> list = this.getReplyDAO().findByParameter(param);
		List<ReplyModel> mlist = BeanUtils.<Reply,ReplyModel>copyList(list,BeanLocator.REPLYMODEL);
		return mlist;
	}

	public long countByParameter(TopicParameter param) {
		Long ln = (Long) this.getCache(param.getForumId()).get(param.getCacheKeyOfCount());
		Boolean isput = (Boolean) this.getCache(param.getForumId()).get(param.getCacheKeyOfCount()+"-isput");
		if(Validator.isEmpty(ln)&&Validator.isEmpty(isput)){
			long c = this.getReplyDAO().countByParameter(param);
			this.getCache(param.getForumId()).put(param.getCacheKeyOfCount(),new Long(c));
			this.getCache(param.getForumId()).put(param.getCacheKeyOfCount()+"-isput",new Boolean(true));
			return c;
		}else{
			return ln.longValue();
		}
	}

	public ReplyDAO getReplyDAO() {
		return DAOWrapper.<ReplyDAO>getSingletonInstance(DAOLocator.REPLY);
	}

	public ReContentDAO getReContentDAO() {
		return DAOWrapper.<ReContentDAO>getSingletonInstance(DAOLocator.RECONTENT);
	}
	
	public TopicService getTopicService() {
		return ServiceWrapper.<TopicService>getSingletonInstance(ServiceLocator.TOPIC);
	}

	public UserService getUserService() {
		return ServiceWrapper.<UserService>getSingletonInstance(ServiceLocator.USER);
	}

	public CountService getCountService() {
		return ServiceWrapper.<CountService>getSingletonInstance(ServiceLocator.COUNT);
	}

	public TopicDAO getTopicDAO() {
		return DAOWrapper.<TopicDAO>getSingletonInstance(DAOLocator.TOPIC);
	}
	
	public static void main(String args[]){
		com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
		ReplyServiceImpl r = new ReplyServiceImpl();
		ReplyModel model = r.findById(18);
		//r.treeUtils(model);
		System.out.println(model.getTitle());
		/*
		TopicParameter param = new TopicParameter();
		param.setForumId(10);
		param.setTopicId(599);
		param.setPage(1);
		List<ReplyModel> mlist = r.findReplyAndContent(param);
		for(int i=0; i<mlist.size(); i++){
			ReplyModel model = mlist.get(i);
			System.out.println(model.getReContentModel().getContent());
		}
		/*
		ReplyModel rm = new ReplyModel();
		rm.setTitle("ddddddd");
		rm.setTopicId(11);
		
		ReContentModel rc = new ReContentModel();
		rc.setContent("ddddddddddddddddd");
		rm.setReContentModel(rc);
		r.create(rm);
		
		System.out.println(r.getMessage());
		*/
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -