topicserviceimpl.java
来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 570 行 · 第 1/2 页
JAVA
570 行
/*
* Created on 2007-3-15
* Last modified on 2008-1-1
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.topic;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
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.cache.Cache;
import com.yeqiangwei.cache.CacheFactory;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.dao.TContentDAO;
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.dao.TopicDAO;
import com.yeqiangwei.club.dao.UserDAO;
import com.yeqiangwei.club.model.TContent;
import com.yeqiangwei.club.model.Reply;
import com.yeqiangwei.club.model.Topic;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.ClubRuntimeException;
import com.yeqiangwei.club.param.TopicParameter;
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.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);
private static final Cache<Topic> CACHE_TOPIC = CacheFactory.<Topic>creator(CacheRegion.TOPIC);
//private static final Cache<> CACHE_LIST = CacheFactory.<List<Topic>>creator(CacheRegion.LIST);
@SuppressWarnings("unchecked")
private static final Map<String, Cache> CACHE_MAP = new HashMap<String, Cache>();
private static final LinkedList<Topic> VIEWING_TOPICS = new LinkedList<Topic>();
private static final Map<Integer, Integer> VIEWING_MAP = new HashMap<Integer, Integer>();
@Override
public List<Topic> getViewingTopics() {
return VIEWING_TOPICS;
}
@Override
public void setTopicViewing(Topic topic) {
if(Validator.isEmpty(topic)){
logger.warn("setTopicViewing:Topic is null");
return ;
}
if(VIEWING_TOPICS.size()>=50){
Topic last = VIEWING_TOPICS.removeLast();
if(VIEWING_MAP.get(last.getTopicId())!=null){
VIEWING_MAP.remove(last.getTopicId());
}
}
if(VIEWING_MAP.get(topic.getTopicId())==null){
VIEWING_TOPICS.add(0,topic);
VIEWING_MAP.put(topic.getTopicId(), topic.getTopicId());
}
}
/**
* 帖子列表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_MAP.put(region,cache=CacheFactory.creator(region));
}
return cache;
}
public String getTopicCacheKey(int topicId){
return CacheRegion.TOPIC+":topicId="+topicId;
}
private void updateTopicCache(Topic model){
if(model==null){
return ;
}
Topic cache = CACHE_TOPIC.get(getTopicCacheKey(model.getTopicId()));
if(cache==null){
CACHE_TOPIC.put(getTopicCacheKey(model.getTopicId()),model);
}
else{
TContent tContent = model.getTContent();
if(tContent==null){
tContent = cache.getTContent();
}
model.setTContent(tContent);
CACHE_TOPIC.put(getTopicCacheKey(model.getTopicId()),model);
}
}
@Override
public int updateIsManaged(Topic topic) {
if(Validator.isEmpty(topic)){
return 0;
}
int c = this.getTopicDAO().updateIsManaged(topic.getTopicId(), topic.getIsManaged());
if(c>0){
this.updateTopicCache(topic);
}
return c;
}
/**
* 发帖删除需要更新此cache其他不需要
* @param forumId
* @return
// 和帖子列表CACHE共享实例
private Cache getCountCache(Integer forumId){
return CacheFactory.creator(CacheRegion.TOPIC+"_COUNT:formId="+forumId);
}*/
private static Integer LAST_REPLY_ID;
@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);
this.getTopicDAO().delete(item);
this.getTContentDAO().deleteByTopicId(item.getTopicId());
this.getReplyDAO().deleteByTopicId(item.getTopicId());
this.getRContentDAO().deleteByTopicId(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);
this.getReplyDAO().delete(item);
this.getRContentDAO().deleteByReplyId(item.getReplyId());
}
}
UserDAO userDAO = DAOWrapper.<UserDAO>getSingletonInstance(DAOLocator.USER);
User user = userDAO.findById(userId);
if(!Validator.isEmpty(user)){
user.setTopics(0);
user.setReplys(0);
userDAO.update(user);
}
this.getForumTopicCache(null).clear();
return c;
}
public Topic findNextOrPrevious(int topicId, int forumId, boolean isNext) {
if(topicId>0){
return this.getTopicDAO().findNextOrPrevious(topicId, forumId, isNext);
}
return null;
}
public int delete(List<Integer> ids) throws ClubException{
if(Validator.isEmpty(ids)){
throw new ClubException("List<Integer> is null or List<Integer>.size==0");
}
this.getTContentDAO().deleteByTopicId(ids);
this.getReplyDAO().deleteByTopicId(ids);
this.getRContentDAO().deleteByTopicId(ids);
this.getForumTopicCache(null).clear();
return this.getTopicDAO().delete(ids);
}
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 topic = this.getTopicDAO().findById(id);
TContent tcontent = this.getTContentDAO().findByTopicId(id);
topic.setIsDeleted(isDeleted);
tcontent.setIsDeleted(isDeleted);
this.getTopicDAO().update(topic);
this.getTContentDAO().update(tcontent);
CACHE_TOPIC.remove(this.getTopicCacheKey(topic.getTopicId()));
this.getForumTopicCache(topic.getForumId()).remove(topic.getForumId());
s++;
}
this.getForumTopicCache(null).clear();
return s;
}
public Topic trash(TopicTrashForm form) throws ClubException {
if(form.getTopicId()==0||form.getForumId()==0){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
Topic model = this.getTopicDAO().findById(form.getTopicId());
if(Validator.isEmpty(model)){
throw new ClubException("Topic("+form.getTopicId()+") is null!");
}else{
model.setIsDeleted(form.getIsDeleted());
model.setIsManaged(true);
this.getTopicDAO().update(model);
TContent tcontent = this.getTContentDAO().findByTopicId(form.getTopicId());
tcontent.setIsDeleted(form.getIsDeleted());
this.getTContentDAO().update(tcontent);
CACHE_TOPIC.remove(this.getTopicCacheKey(model.getTopicId()));
this.getForumTopicCache(model.getForumId()).clear();
this.getForumTopicCache(null).clear();
return model;
}
}
public void updateViews(int topicId) throws ClubException {
if(topicId==0){
throw new ClubException("topicId == 0");
}
this.getTopicDAO().updateViews(topicId);
}
public Topic updateBetter(TopicBetterForm form) throws ClubException {
if(form.getTopicId()==0||form.getForumId()==0){
throw new ClubException(MessageUtils.getMessage("error_parameter"));
}
Topic model = this.getTopicDAO().findById(form.getTopicId());
if(Validator.isEmpty(model)){
throw new ClubException(MessageUtils.getMessage("error_update_noid"));
}
else if(model.getBetter()==form.getBetter()){
throw new ClubException(MessageUtils.getMessage("error_post_duplicate"));
}
else{
model.setBetter(form.getBetter());
model.setIsManaged(true);
this.getTopicDAO().update(model);
this.getForumTopicCache(model.getForumId()).clear();
this.updateTopicCache(model);
return model;
}
}
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;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?