📄 topicserviceimpl.java
字号:
/*
* Created on 2007-3-15
* 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.ContentDAO;
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.Content;
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.model.ContentModel;
import com.yeqiangwei.club.service.model.TopicModel;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.BeanUtils;
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.Validator;
public class TopicServiceImpl implements TopicService{
private static final Logger logger = Logger.getLogger(TopicServiceImpl.class);
/**
* 帖子列表cache
* @param forumId
* @return
*/
private Cache getCache(Integer forumId){
return CacheFactory.creator(CacheRegion.TOPIC+"-formId:"+forumId);
}
public TopicModel findNextOrPrevious(int topicId, int forumId, boolean isNext) {
Topic item = this.getTopicDAO().findNextOrPrevious(topicId, forumId, isNext);
if(!Validator.isEmpty(item)){
TopicModel model = new TopicModel();
BeanUtils.copyProperties(model,item);
return model;
}
return null;
}
/**
* 发帖删除需要更新此cache其他不需要
* @param forumId
* @return
*/
private Cache getCountCache(Integer forumId){
return CacheFactory.creator(CacheRegion.TOPIC+"-count-formId:"+forumId);
}
private static Integer lastReplyId;
public int delete(List<Integer> ids) throws ClubException{
if(Validator.isEmpty(ids)){
throw new ClubException("List<Integer> is null or List<Integer>.size==0");
}
int c = this.getTopicDAO().delete(ids);
this.getContentDAO().deleteByTopicId(ids);
this.getReplyDAO().deleteByTopicId(ids);
this.getReContentDAO().deleteByTopicId(ids);
//this.setMessage(MessageUtils.getMessage(c,0));
this.getCache(null).clear();
return c;
}
public int trash(List<Integer> list, boolean isDeleted) throws ClubException{
if(Validator.isEmpty(list)){
throw new ClubException("List<Integer> is null or List<Integer>.size==0");
}
int s = 0;
int e = 0;
for(int i=0; i<list.size(); i++){
int id = list.get(i);
Topic item = this.getTopicDAO().findById(id);
Content citem = this.getContentDAO().findByTopicId(id);
item.setIsDeleted(isDeleted);
citem.setIsDeleted(isDeleted);
item = this.getTopicDAO().update(item);
this.getContentDAO().update(citem);
if(!Validator.isEmpty(item)){
s++;
}else{
e++;
}
}
//this.setMessage(MessageUtils.getMessage(s,e));
this.getCache(null).clear();
return s;
}
public TopicModel trash(TopicTrashForm form) throws ClubException {
if(form.getTopicId()==0||form.getForumId()==0){
throw new ClubException("form.getTopicId()==0||form.getForumId()==0");
}
Topic item = this.getTopicDAO().findById(form.getTopicId());
//this.getTopicDAO().findById(form.getTopicId());
if(Validator.isEmpty(item)){
throw new ClubException("TopicModel("+form.getTopicId()+") is null!");
}else{
item.setIsDeleted(form.getIsDeleted());
item.setIsManaged(true);
this.getTopicDAO().update(item);
Content citem = this.getContentDAO().findByTopicId(form.getTopicId());
citem.setIsDeleted(form.getIsDeleted());
this.getContentDAO().update(citem);
TopicModel model = new TopicModel();
BeanUtils.copyProperties(model,item);
this.getCache(model.getForumId()).clear();
this.getCache(null).clear();
this.getCache(null).clear();
return model;
}
}
public int updateViews(int topicId) throws ClubException {
if(topicId==0){
throw new ClubException("topicId == 0");
}
return this.getTopicDAO().updateViews(topicId);
}
public TopicModel updateBetter(TopicBetterForm form) throws ClubException {
if(form.getTopicId()==0||form.getForumId()==0){
throw new ClubException("form.getTopicId()==0||form.getForumId()==0");
}
Topic item = this.getTopicDAO().findById(form.getTopicId());
if(Validator.isEmpty(item)){
throw new ClubException("TopicModel("+form.getTopicId()+") is null!");
}else{
item.setBetter(form.getBetter());
item.setIsManaged(true);
this.getTopicDAO().update(item);
TopicModel model = new TopicModel();
BeanUtils.copyProperties(model,item);
return model;
}
}
public Integer getLastReplyId(){
if(Validator.isEmpty(lastReplyId)){
logger.debug("get lastReplyId from DB");
Reply item = this.getReplyDAO().findLastReply();
if(!Validator.isEmpty(item)){
lastReplyId = new Integer(item.getReplyId());
}else{
lastReplyId = new Integer(0);
}
}else{
logger.debug("get lastReplyId from Cache");
}
return lastReplyId;
}
public void setLastReplyId(Integer i){
lastReplyId = i;
}
public TopicModel findById(int id) {
if(id>0){
Topic item = this.getTopicDAO().findById(id);
if(Validator.isEmpty(item)){
////this.setMessage(super.getMessage("error_notfind"));
return null;
}else{
TopicModel model = new TopicModel();
BeanUtils.copyProperties(model,item);
return model;
}
}else{
return null;
}
}
/**
* 根据文章ID查找文章标题表和文章内容表
*/
public TopicModel findTopicAndContentById(int id){
TopicModel model = this.findById(id);
if(Validator.isEmpty(model)){
//this.setMessage(super.getMessage("error_notfind"));
return null;
}
Content item = this.getContentDAO().findByTopicId(model.getTopicId());
if(Validator.isEmpty(item)){
//this.setMessage(super.getMessage("error_notfind"));
logger.error("找不到内容的文章,标题为:"+model.getTitle()+", ID:"+model.getTopicId());
/*
* 找不到内容的标题直接标记删除
*/
model.setIsDeleted(true);
Topic topic = this.getTopicDAO().findById(id);
BeanUtils.copyProperties(topic,model);
this.getTopicDAO().update(topic);
return null;
}else{
ContentModel contentModel = new ContentModel();
BeanUtils.copyProperties(contentModel,item);
model.setContentModel(contentModel);
}
return model;
}
public TopicModel createOrUpdate(TopicModel model) throws ClubException {
if(model.getTopicId()>0){
return this.update(model);
}else{
return this.create(model);
}
}
public TopicModel create(TopicModel model) throws ClubException {
if(Validator.isEmpty(model)
|| Validator.isEmpty(model.getTitle())
|| Validator.isEmpty(model.getContentModel())
|| Validator.isEmpty(model.getContentModel().getContent())
){
throw new ClubException(MessageUtils.getMessage("error_empty"));
}
else if(model.getTitle().length()>200||model.getContentLength()>50000){
throw new ClubException(MessageUtils.getMessage("error_toolong"));
}
else{
/*
* 如果页面从过客身份登录,且认证成功,则重新设置用户名和ID
*/
if(model.getUserId()==0&&!Validator.isEmpty(model.getUserModel())){
model.setUserId(model.getUserModel().getUserId());
model.setUserName(model.getUserModel().getUserName());
}
model.setReplyId(this.getLastReplyId());//获取社区最大ID
ContentModel cmodel = model.getContentModel();
model.setContentLength(cmodel.getContent().length());
model.setLastReplyDateTime(model.getCreateDateTime());
model.setLastReplyUserName("");
Topic item = new Topic();
BeanUtils.copyProperties(item,model); //BO->PO
this.getTopicDAO().create(item);
if(item.getTopicId()>0){ //如果主题添加成功则向内容表添加
BeanUtils.copyProperties(model,item); //PO->BO
cmodel.setTopicId(item.getTopicId());
Content citem = new Content();
BeanUtils.copyProperties(citem,cmodel); //BO->PO
this.getContentDAO().create(citem);
BeanUtils.copyProperties(cmodel,citem); //PO->BO
model.setContentModel(cmodel);
this.getCountCache(model.getForumId()).clear();
this.getCache(model.getForumId()).clear();
this.getCache(null).clear();
return model;
}else{
throw new ClubException(MessageUtils.getMessage("error_system"));
}
}
}
public TopicModel update(TopicModel model) throws ClubException {
if(Validator.isEmpty(model)
|| Validator.isEmpty(model.getUserName())
|| Validator.isEmpty(model.getTitle())
|| Validator.isEmpty(model.getContentModel())
){
throw new ClubException(MessageUtils.getMessage("error_empty"));
}else{
ContentModel cmodel = model.getContentModel();
model.setContentLength(cmodel.getContent().length());
model.setLastReplyDateTime(model.getCreateDateTime());
model.setLastReplyUserName("");
Topic item = this.getTopicDAO().findById(model.getTopicId());
BeanUtils.copyProperties(item,model); //BO->PO
if(model.getTitle().length()>200||model.getContentLength()>50000){
//this.setMessage(MessageUtils.getMessage("error_toolong"));
throw new ClubException(MessageUtils.getMessage("error_toolong"));
}
this.getTopicDAO().update(item);
cmodel.setTopicId(item.getTopicId());
Content citem = this.getContentDAO().findByTopicId(item.getTopicId());
BeanUtils.copyProperties(citem,cmodel); //BO->PO
this.getContentDAO().update(citem);
BeanUtils.copyProperties(model,item); //PO->BO
BeanUtils.copyProperties(cmodel,citem); //PO->BO
model.setContentModel(cmodel);
this.getCache(model.getForumId()).clear();
this.getCache(null).clear();
return model;
}
}
public int delete(TopicModel model) {
Topic item = new Topic();
BeanUtils.copyProperties(item,model);
this.getCountCache(model.getForumId()).clear();
this.getCache(model.getForumId()).clear();
this.getCache(null).clear();
return this.getTopicDAO().delete(item);
}
/**
* 移动文章
* @throws ClubException
*/
public TopicModel move(TopicMoveForm form) throws ClubException {
int originalForumId = form.getOriginalForumId();
int forumId = form.getForumId();
int topicId = form.getTopicId();
if(forumId==0 || topicId==0){
//this.setMessage(MessageUtils.getMessage("error_system"));
logger.error("移动主题出错:版面ID或文章ID等于0! forumId:"+forumId+" // topicId:"+topicId);
throw new ClubException("forumId==0 || topicId==0");
}
TopicModel model = null;
Topic item = this.getTopicDAO().findById(form.getTopicId());
if(Validator.isEmpty(item)){
//this.setMessage(MessageUtils.getMessage("error_notfind"));
throw new ClubException("TopicModel("+form.getTopicId()+") is null!");
}else{
item.setForumId(forumId);
item.setOriginalForumId(originalForumId);
item.setMoveUserId(form.getMoveUserId());
item.setMoveUserName(form.getMoveUserName());
item.setMoveDateTime(FormatDateTime.now());
item.setIsManaged(true); //标记文章被管理过
this.getTopicDAO().update(item);
model = new TopicModel();
BeanUtils.copyProperties(model,item);
this.getCache(model.getForumId()).clear();
this.getCache(null).clear();
}
return model;
}
@SuppressWarnings("unchecked")
public List<TopicModel> findByParameter(TopicParameter param){
List<TopicModel> mlist = (List<TopicModel>) this.getCache(param.getForumId()).get(param.getCacheKeyOfList());
if(Validator.isEmpty(mlist)){
List<Topic> list = this.getTopicDAO().findByParameter(param);
if(!Validator.isEmpty(list)){
mlist = BeanUtils.<Topic,TopicModel>copyList(list,BeanLocator.TOPICMODEL);
this.getCache(param.getForumId()).put(param.getCacheKeyOfList(),mlist);
}
}
return mlist;
}
public List<ContentModel> findContentByParameter(TopicParameter param) {
List<Content> list = this.getContentDAO().findByParameter(param);
if(!Validator.isEmpty(list)){
List<ContentModel> mlist = BeanUtils.<Content,ContentModel>copyList(list,BeanLocator.CONTENTMODEL);
return mlist;
}else{
return null;
}
}
/**
* 合并论坛帖子
* @throws ClubException
*/
public int update_forumId(int forumId, int toForumId) throws ClubException {
if(forumId>0&&toForumId>0){
return this.getTopicDAO().update_forumId(forumId, toForumId);
}else{
throw new ClubException("forumId==0 || toForumId==0");
}
/*
if(c>0){
//this.setMessage(MessageUtils.getMessage("success"));
}else{
//this.setMessage(MessageUtils.getMessage("error"));
}
*/
}
public long countByParameter(TopicParameter param) {
Long ln = (Long) this.getCountCache(param.getForumId()).get(param.getCacheKeyOfCount());
if(Validator.isEmpty(ln)){
long c = this.getTopicDAO().countByParameter(param);
this.getCountCache(param.getForumId()).put(param.getCacheKeyOfCount(),new Long(c));
return c;
}else{
return ln.longValue();
}
}
public TopicDAO getTopicDAO() {
return DAOWrapper.<TopicDAO>getSingletonInstance(DAOLocator.TOPIC);
}
public ContentDAO getContentDAO() {
return DAOWrapper.<ContentDAO>getSingletonInstance(DAOLocator.CONTENT);
}
public ReContentDAO getReContentDAO() {
return DAOWrapper.<ReContentDAO>getSingletonInstance(DAOLocator.RECONTENT);
}
public ReplyDAO getReplyDAO() {
return DAOWrapper.<ReplyDAO>getSingletonInstance(DAOLocator.REPLY);
}
public static void main(String args[]){
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
TopicServiceImpl t = new TopicServiceImpl();
TopicParameter param = new TopicParameter();
param.setIsDeleted(false);
param.setOrderBy(new Byte((byte) 4));
param.setRows(new Integer(200));
param.setMinId(new Integer(19000));
System.out.println(t.countByParameter(param));
/*
long cc = t.getContentDAO().countByParameter(param);
long tc = t.getTopicDAO().countByParameter(param);
System.out.println("内容总数:"+cc);
System.out.println("标题总数:"+tc);
long rcc = t.getReContentDAO().countByParameter(param);
long rtc = t.getReplyDAO().countByParameter(param);
System.out.println("回复内容总数:"+rcc);
System.out.println("回复标题总数:"+rtc);
*/
/*
List<TopicModel> list= t.findByParameter(param);
for(int i=0; i<list.size(); i++){
TopicModel m = list.get(i);
System.out.println(m.getTitle());
}
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -