favoriteforumserviceimpl.java

来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 274 行

JAVA
274
字号
/* 
 * Created on 2007-5-10
 * Last modified on 2008-1-1
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.service.forum;

import java.util.ArrayList;
import java.util.List;

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.FavoriteForumDAO;
import com.yeqiangwei.club.model.FavoriteForum;
import com.yeqiangwei.club.model.Forum;
import com.yeqiangwei.club.model.ManageLog;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.FavoriteParameter;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.model.Topic;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.topic.TopicService;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.service.util.ManageLogService;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.Validator;

public class FavoriteForumServiceImpl extends MessageUtils implements FavoriteForumService{
	
	private static final Logger logger = Logger.getLogger(FavoriteForumServiceImpl.class);

	/**
	 * 为每个版创建一个缓存实例,便于批量清理某个版的收藏缓存
	 * @param forumId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Cache getCache(Integer forumId){
		return CacheFactory.creator(CacheRegion.FAVORITE_FORUM+":forumId="+forumId);
	}
	
	public int delFavorite(int favoriteForumId, int userId) {
		FavoriteForum model = this.findById(favoriteForumId);
		if(!Validator.isEmpty(model)){
			if(model.getUserId()==userId){
				try {
					return this.delete(model);
				} catch (ClubException e) {
					logger.error(e.toString());
				}
			}else{
				return 0;
			}
		}
		return userId;
	}
	
	public List<Forum> findForumByUserId(int userId) {
		List<Forum> list = null;
		FavoriteParameter param = new FavoriteParameter();
		param.setUserId(userId);
		List<FavoriteForum> flist = this.getFavoriteForumDAO().findByParameter(param);
		if(!Validator.isEmpty(flist)){
			list = new ArrayList<Forum>();
			for(int i=0; i<flist.size(); i++){
				FavoriteForum fa = flist.get(i);
				list.add(this.getForumService().findById(fa.getForumId()));
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public FavoriteForum findOnlyByUserIdAndForumId(int userId, int forumId) {
		String key = this.cacheKey(userId,forumId,4);
		FavoriteForum model = (FavoriteForum) getCache(forumId).get(key);
		Boolean isput = (Boolean) getCache(forumId).get(key+"-isput");
		if(Validator.isEmpty(model)&&Validator.isEmpty(isput)){
			model = this.getFavoriteForumDAO().findOnly(userId,forumId);
			getCache(forumId).put(key,model);
			getCache(forumId).put(key+"-isput",new Boolean(true));
		}
		return model;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findUser(FavoriteParameter param) {
		String key = this.cacheKey(param.getUserId(),param.getForumId(),0);
		List<User> ulist = (List<User>) getCache(param.getForumId()).get(key);
		Boolean isput = (Boolean) getCache(param.getForumId()).get(key+"-isput");
		if(Validator.isEmpty(ulist)&&Validator.isEmpty(isput)){
			List<FavoriteForum> list = this.getFavoriteForumDAO().findByParameter(param);
			if(!Validator.isEmpty(list)){
				ulist =  new java.util.ArrayList<User>();
				for(int i=0; i<list.size(); i++){
					FavoriteForum item = list.get(i);
					User user = this.getUserService().findById(item.getUserId());
					ulist.add(user);
				}
			}
			getCache(param.getForumId()).put(key,ulist);
			getCache(param.getForumId()).put(key+"-isput",new Boolean(true));
		}else{
			logger.debug("find users in cache");
		}
		return ulist;
	}

	@SuppressWarnings("unchecked")
	public List<Topic> findTopic(FavoriteParameter param) {
		int rows = param.getRows();
		String key = this.cacheKey(param.getUserId(),param.getForumId(),1);
		List<Topic> topicList = (List<Topic>) getCache(param.getForumId()).get(key);
		Boolean isput = (Boolean) getCache(param.getForumId()).get(key+"-isput");
		if(Validator.isEmpty(topicList)&&Validator.isEmpty(isput)){
			topicList = new java.util.ArrayList<Topic>();
			param.setRows(100);
			List<FavoriteForum> list = this.getFavoriteForumDAO().findByParameter(param);
			if(!Validator.isEmpty(list)){
				List<Integer> forumIdList = new ArrayList<Integer>();
				for(int i=0; i<list.size(); i++){
					FavoriteForum item = list.get(i);
					forumIdList.add(item.getForumId());
				}
				TopicParameter tparam = new TopicParameter();
				tparam.setPage(1);
				tparam.setRows(rows);
				tparam.setIsDeleted(new Boolean(false));
				tparam.setOrderBy(new Byte("2"));
				tparam.setForumIdList(forumIdList);
				topicList = this.getTopicService().findByParameter(tparam);
			}
			getCache(param.getForumId()).put(key,topicList);
			getCache(param.getForumId()).put(key+"-isput",new Boolean(true));
		}
		return topicList;
	}

	public void doFavorite(FavoriteForum model) throws ClubException {
		if(Validator.isEmpty(this.getFavoriteForumDAO().findOnly(model.getUserId(),model.getForumId()))){
			this.getFavoriteForumDAO().create(model);
		}
		getCache(model.getForumId()).clear();
		logger.debug("cache cleared of Favorite forum");
		
		ManageLog log = new ManageLog();
		log.setTopicId(0);
		log.setReplyId(0);
		log.setForumId(model.getForumId());
		log.setTitle("");
		log.setByUserId(model.getUserId());
		log.setByUserName(this.getUserService().findById(model.getUserId()).getUserName());
		log.setByUserIp("127.0.0.1");
		log.setCreateDateTime(FormatDateTime.now());
		this.getUserService().ruleUtils(log,model.getForumId(),30);
		log.setUserName(this.getUserService().findById(model.getUserId()).getUserName());
		log.setUserId(model.getUserId());
		log.setIsList(true);
		log.setListByUserName(true);
		log.setMemo("订阅版面");
		log.setContent("订阅社区版面:"+this.getForumService().findById(model.getForumId()).getForumName());
		this.getManageLogService().create(log);
	}

	public FavoriteForum findById(int id) {
		return this.getFavoriteForumDAO().findById(id);
	}

	public void createOrUpdate(FavoriteForum model) {

	}

	public void create(FavoriteForum model) {

	}

	public void update(FavoriteForum model) {

	}

	public int delete(FavoriteForum item) throws ClubException{
		FavoriteForum model = this.getFavoriteForumDAO().findById(item.getFavoriteForumId());
		getCache(model.getForumId()).remove(cacheKey(model.getUserId(),model.getForumId(),4));
		int c = this.getFavoriteForumDAO().delete(model);
		ManageLog log = new ManageLog();
		log.setTopicId(0);
		log.setReplyId(0);
		log.setForumId(model.getForumId());
		log.setTitle("");
		log.setByUserId(model.getUserId());
		log.setByUserName(this.getUserService().findById(model.getUserId()).getUserName());
		log.setByUserIp("127.0.0.1");
		log.setCreateDateTime(FormatDateTime.now());
		this.getUserService().ruleUtils(log,model.getForumId(),31);
		log.setUserName(this.getUserService().findById(model.getUserId()).getUserName());
		log.setUserId(model.getUserId());
		log.setIsList(true);
		log.setListByUserName(true);
		log.setMemo("取消订阅版面");
		log.setContent("取消订阅社区版面:"+this.getForumService().findById(model.getForumId()).getForumName());
		this.getManageLogService().create(log);
		return c;
	}

	public List<FavoriteForum> findByParameter(FavoriteParameter param) {
		return this.getFavoriteForumDAO().findByParameter(param);
	}
	
	/**
	 * 可以根据用户ID统计收藏的版面数,也可以根据版面ID统计收藏的用户数
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public long countByParameter(FavoriteParameter param) {
		String key = this.cacheKey(param.getUserId(),param.getForumId(),3);
		Long l = (Long) getCache(param.getForumId()).get(key);
		if(Validator.isEmpty(l)){
			l= new Long(this.getFavoriteForumDAO().countByParameter(param));
			if(Validator.isEmpty(l)){
				l = new Long(0);
			}
			getCache(param.getForumId()).put(key,l);
		}
		return l.longValue();
	}
	
	private ForumService getForumService() {
		return ServiceWrapper.<ForumService>getSingletonInstance(ServiceLocator.FORUM);
	}

	public FavoriteForumDAO getFavoriteForumDAO() {
		return DAOWrapper.<FavoriteForumDAO>getSingletonInstance(DAOLocator.FAVORITEFORUM);
	}

	public UserService getUserService() {
		return ServiceWrapper.getSingletonInstance(ServiceLocator.USER);
	}
	
	public TopicService getTopicService() {
		return ServiceWrapper.<TopicService>getSingletonInstance(ServiceLocator.TOPIC);
	}
	
	/**
	 * 
	 * @param userId
	 * @param forumId
	 * @param type 0缓存用户 1缓存文章 3统计相关总数 4查询某用户是否收藏过指定版面
	 * @return
	 */
	public String cacheKey(Integer userId, Integer forumId, int type){
		StringBuffer sb = new StringBuffer();
		sb.append("FavoriteForum-");
		sb.append(userId);
		sb.append("-");
		sb.append(forumId);
		sb.append("-");
		sb.append(type);
		return sb.toString();
	}

	private ManageLogService getManageLogService() {
		return  ServiceWrapper.<ManageLogService>getSingletonInstance(ServiceLocator.MANAGELOG);
	}
}

⌨️ 快捷键说明

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