replyserviceimpl.java
来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 350 行
JAVA
350 行
/*
* Created on 2007-3-23
* Last modified on 2008-1-1
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.topic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.yeqiangwei.cache.Cache;
import com.yeqiangwei.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.RContentDAO;
import com.yeqiangwei.club.dao.ReplyDAO;
import com.yeqiangwei.club.model.RContent;
import com.yeqiangwei.club.model.Reply;
import com.yeqiangwei.club.model.Topic;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.ClubRuntimeException;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.user.UserService;
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);
@SuppressWarnings("unchecked")
private static Map<String, Cache> CACHE_MAP = new HashMap<String, Cache>();
/**
* Cache主题分页列表
* @param forumId
* @return
*/
@SuppressWarnings("unchecked")
private Cache getForumTopicCache(Integer forumId){
String region = CacheRegion.FORUM_TOPIC+":formId="+forumId;
Cache cache = CACHE_MAP.get(region);
if(cache==null){
cache = CacheFactory.creator(region);
CACHE_MAP.put(region,cache);
}
return cache;
}
/**
* cache 主题回复数
* @param forumId
* @return
*/
@SuppressWarnings("unchecked")
private Cache getCache(Integer forumId){
String region = CacheRegion.FORUM_REPLY+":formId="+forumId;
Cache cache = CACHE_MAP.get(region);
if(cache==null){
cache = CacheFactory.creator(region);
CACHE_MAP.put(region,cache);
}
return cache;
}
public int updateIsManaged(Reply reply){
return this.getReplyDAO().updateIsManaged(reply.getReplyId(), reply.getIsManaged());
}
public int delete(List<Integer> ids) {
this.getReplyDAO().delete(ids);
return this.getRContentDAO().deleteByReplyId(ids);
}
public int trash(List<Integer> list, boolean isDeleted){
int s = 0;
for(int i=0; i<list.size(); i++){
int id = list.get(i);
Reply reply = this.getReplyDAO().findById(id);
RContent rContent = this.getRContentDAO().findByReplyId(id);
reply.setIsDeleted(isDeleted);
rContent.setIsDeleted(isDeleted);
this.getReplyDAO().update(reply);
this.getRContentDAO().update(rContent);
s++;
}
return s;
}
public Reply trash(TopicTrashForm form) throws ClubException {
Reply item = this.getReplyDAO().findById(form.getReplyId());
if(!Validator.isEmpty(item)){
item.setIsDeleted(form.getIsDeleted());
item.setIsManaged(true);
this.getReplyDAO().update(item);
RContent citem = this.getRContentDAO().findByReplyId(form.getReplyId());
citem.setIsDeleted(form.getIsDeleted());
this.getRContentDAO().update(citem);
return item;
}else{
throw new ClubException(MessageUtils.getMessage("error"));
}
}
public List<Reply> findReplyAndContent(TopicParameter param) {
List<?> list = (List<?>)this.getReplyDAO().findReplyAndContents(param);
List<Reply> replylist = new ArrayList<Reply>();
if(!Validator.isEmpty(list)){
for(int i=0; i<list.size(); i++){
Object[] obj = (Object[]) list.get(i);
Reply reply = (Reply) obj[0];
RContent rContent = (RContent) obj[1];
reply.setRContent(rContent);
replylist.add(reply);
}
}
return replylist;
}
public Reply findReplyAndContentById(int id) {
Reply model = this.findById(id);
if(!Validator.isEmpty(model)){
RContent rContent = this.getRContentDAO().findByReplyId(id);
if(rContent!=null){
model.setRContent(rContent);
return model;
}
}
return null;
}
public List<Reply> findReplyByTopicId(TopicParameter param) {
return this.getReplyDAO().findByParameter(param);
}
public Reply findById(int id) {
if(id>0){
return this.getReplyDAO().findById(id);
}
return null;
}
public void createOrUpdate(Reply model) throws ClubException {
if(model.getReplyId()>0){
this.update(model);
}else{
this.create(model);
}
}
private void updateParameter(Reply reply) throws ClubException {
this.getReplyDAO().update(reply);
}
private void treeUtils(Reply reply) throws ClubException{
int orderlist = 0;
int layer = 0;
int tree = 0;
int replys = 0;
//logger.debug("model.getReplyId():"+model.getReplyId());
if(reply.getReplyId()>0){
Reply replyModel = this.findById(reply.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(reply.getTopicId()).getReplys();
}
Reply item = new Reply();
item.setTopicId(reply.getTopicId());
item.setReplyId(reply.getReplyId());
item.setOrderlist(orderlist);
this.getReplyDAO().updatesOrderlistByTopicId(item);
if(reply.getReplyId()>0){
orderlist++;
}else{
orderlist = 1;
}
/* 计算树型参数 */
int tTree = 0;
if(replys>0){
tTree = (tree * 2 +1 );
}else{
tTree = tree * 2;
}
layer += 1;
reply.setLayer(layer);
reply.setOrderlist(orderlist);
reply.setTree(tTree);
reply.setReplys(0);
}
public void create(Reply reply) throws ClubException {
if(Validator.isEmpty(reply)||Validator.isEmpty(reply.getTitle())){
throw new ClubException(MessageUtils.getMessage("error_empty"));
}else{
this.treeUtils(reply);
RContent reContent = reply.getRContent();
reply.setContentLength(reContent.getContent().length());
if(reply.getTitle().length()>100||reply.getContentLength()>100000){
logger.error("content.length: "+reply.getContentLength());
throw new ClubException(MessageUtils.getMessage("error_toolong"));
}
int replyId = reply.getReplyId(); //保持回复创建之前的replyId
this.getReplyDAO().create(reply);
if(reply.getReplyId()==0){
throw new ClubRuntimeException(MessageUtils.getMessage("error_system"));
}
reContent.setTopicId(reply.getTopicId());
reContent.setReplyId(reply.getReplyId());
this.getRContentDAO().create(reContent);
/*更新最后回复ID的Cache*/
this.getTopicService().setLastReplyId(new Integer(reply.getReplyId()));
/* 更新回复次数最后更新时间等 */
Topic topic = this.getTopicService().getTopicDAO().findById(reply.getTopicId());
if(Validator.isEmpty(topic)){
throw new ClubRuntimeException(MessageUtils.getMessage("error_notfind_topic"));
}else{
if(replyId==0){
topic.setReplys(topic.getReplys()+1);
}
topic.setLastReplyDateTime(FormatDateTime.now());
topic.setLastReplyUserName(reply.getUserName());
topic.setLastReplyUserId(reply.getUserId());
topic.setReplyId(reply.getReplyId());
this.getTopicService().getTopicDAO().update(topic);
this.getTopicService().getTopicCache().remove(getTopicService().getTopicCacheKey(topic.getTopicId()));
this.getForumTopicCache(reply.getForumId()).clear(); //清除主题列表缓存
this.getForumTopicCache(null).clear(); //清除主题列表缓存
this.getCache(reply.getForumId()).clear(); //清除count统计Cache
/*
* 更新被回复的主帖的用户参数
*/
reply.setTopic(topic);
User tuser = this.getUserService().findById(topic.getUserId());
if(!Validator.isEmpty(tuser)){
try{
this.getUserService().ruleUtils(tuser,reply.getForumId(),6); //文章被回复的参数变化
this.getUserService().update(tuser);
}catch(ClubException e){
logger.error(e.toString());
}
}
/* 回帖用户的积分变动 */
User user = reply.getUser();
if(!Validator.isEmpty(user)){
user.setReplys(user.getReplys()+1); //回帖次数加1
this.getUserService().ruleUtils(user,reply.getForumId(),3); //根据社区回复制度更新用户参数
try{
this.getUserService().update(user);
}catch(ClubException e){
logger.error(e.toString());
}
}
}
}
}
public void update(Reply model) throws ClubException {
if(Validator.isEmpty(model)||Validator.isEmpty(model.getTitle())){
throw new ClubException(MessageUtils.getMessage("error_empty"));
}
else if(model.getTopicId()==0){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
else{
RContent reContent = model.getRContent();
model.setContentLength(reContent.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().update(item);
BeanUtils.copyProperties(model,item); //PO->BO
reContent.setTopicId(item.getTopicId());
reContent.setReplyId(item.getReplyId());
RContent citem = this.getRContentDAO().findByReplyId(model.getReplyId());
BeanUtils.copyProperties(citem,reContent);
this.getRContentDAO().update(citem);
BeanUtils.copyProperties(reContent,citem); //PO->BO
model.setRContent(reContent);
}
}
public int delete(Reply model) {
return 0;
}
public int delete(String[] ids) {
return 0;
}
public List<Reply> findByParameter(TopicParameter param) {
return this.getReplyDAO().findByParameter(param);
}
@SuppressWarnings("unchecked")
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{
if(Validator.isEmpty(ln)){
return 0;
}
return ln.longValue();
}
}
public ReplyDAO getReplyDAO() {
return DAOWrapper.<ReplyDAO>getSingletonInstance(DAOLocator.REPLY);
}
private RContentDAO getRContentDAO() {
return DAOWrapper.<RContentDAO>getSingletonInstance(DAOLocator.R_CONTENT);
}
private TopicService getTopicService() {
return ServiceWrapper.<TopicService>getSingletonInstance(ServiceLocator.TOPIC);
}
private UserService getUserService() {
return ServiceWrapper.<UserService>getSingletonInstance(ServiceLocator.USER);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?