📄 topicserviceimpl.java
字号:
/*
* Created on 2007-3-15
* Last modified on 2007-11-3
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.topic;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.thesaurus.AnalyzerUtils;
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.UserDAO;
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.dao.model.User;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.DAOException;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.model.ContentModel;
import com.yeqiangwei.club.service.model.TopicModel;
import com.yeqiangwei.club.service.search.AnalyzerFactory;
import com.yeqiangwei.club.service.search.SearchFactory;
import com.yeqiangwei.club.service.search.SearchParameter;
import com.yeqiangwei.club.service.search.SearchProvider;
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);
}
@Override
public int deleteByUserId(int userId) throws ClubException {
if(userId<=0){
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
int c = 0;
TopicParameter param = new TopicParameter();
param.setPage(1);
param.setRows(100000);
param.setUserId(userId);
List<Topic> list = this.getTopicDAO().findByParameter(param);
if(!Validator.isEmpty(list)){
for(int i=0; i<list.size(); i++){
Topic item = list.get(i);
try {
this.getTopicDAO().delete(item);
this.getContentDAO().deleteByTopicId(item.getTopicId());
this.getReplyDAO().deleteByTopicId(item.getTopicId());
this.getReContentDAO().deleteByTopicId(item.getTopicId());
} catch (DAOException e) {
logger.error(e.toString());
}
logger.debug("delete topicId:"+item.getTopicId());
}
}
List<Reply> rlist = this.getReplyDAO().findByParameter(param);
if(!Validator.isEmpty(rlist)){
for(int i=0; i<rlist.size(); i++){
Reply item = rlist.get(i);
try {
this.getReplyDAO().delete(item);
this.getReContentDAO().deleteByReplyId(item.getReplyId());
} catch (DAOException e) {
logger.error(e.toString());
}
}
}
UserDAO userDAO = DAOWrapper.<UserDAO>getSingletonInstance(DAOLocator.USER);
User user = userDAO.findById(userId);
if(!Validator.isEmpty(user)){
user.setTopics(0);
user.setReplys(0);
try {
userDAO.update(user);
} catch (DAOException e) {
logger.error(e.toString());
}
}
this.getCache(null).clear();
return c;
}
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 LAST_REPLY_ID;
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 = 0;
try {
c = this.getTopicDAO().delete(ids);
this.getContentDAO().deleteByTopicId(ids);
this.getReplyDAO().deleteByTopicId(ids);
this.getReContentDAO().deleteByTopicId(ids);
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_system"));
}
this.getCache(null).clear();
return c;
}
public int trash(List<Integer> list, boolean isDeleted) throws ClubException{
if(Validator.isEmpty(list)){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
int s = 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);
try {
this.getTopicDAO().update(item);
this.getContentDAO().update(citem);
} catch (DAOException e1) {
logger.error(e1.toString());
}
s++;
}
this.getCache(null).clear();
return s;
}
public TopicModel trash(TopicTrashForm form) throws ClubException {
if(form.getTopicId()==0||form.getForumId()==0){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
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);
try {
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;
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_system"));
}
}
}
public void updateViews(int topicId) throws ClubException {
if(topicId==0){
throw new ClubException("topicId == 0");
}
try {
this.getTopicDAO().updateViews(topicId);
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_system"));
}
}
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);
try {
this.getTopicDAO().update(item);
TopicModel model = new TopicModel();
BeanUtils.copyProperties(model,item);
this.getCache(model.getForumId()).clear();
return model;
} catch (DAOException e) {
logger.error(e.toString());
throw new ClubException(MessageUtils.getMessage("error_system"));
}
}
}
public Integer getLastReplyId(){
if(Validator.isEmpty(LAST_REPLY_ID)){
logger.debug("get lastReplyId from DB");
Reply item = this.getReplyDAO().findLastReply();
if(!Validator.isEmpty(item)){
LAST_REPLY_ID = new Integer(item.getReplyId());
}else{
LAST_REPLY_ID = new Integer(0);
}
}
return LAST_REPLY_ID;
}
public void setLastReplyId(Integer i){
LAST_REPLY_ID = 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);
try {
this.getTopicDAO().update(topic);
} catch (DAOException e) {
logger.error(e.toString());
}
return null;
}else{
ContentModel contentModel = new ContentModel();
BeanUtils.copyProperties(contentModel,item);
model.setContentModel(contentModel);
}
return model;
}
public void createOrUpdate(TopicModel model) throws ClubException {
if(model.getTopicId()>0){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -