topicserviceimpl.java

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

JAVA
570
字号
	public void setLastReplyId(Integer i){
		LAST_REPLY_ID = i;
	}

	public Topic findById(int id) {
		if(id>0){
			Topic model = CACHE_TOPIC.get(getTopicCacheKey(id));
			if(model==null){
				model = this.getTopicDAO().findById(id);
			}
			this.updateTopicCache(model);
			return model;
		}else{
			return null;
		}
	}
	
	/**
	 * 根据文章ID查找文章标题表和文章内容表
	 */
	public Topic findTopicAndContentById(int id){
		Topic model = CACHE_TOPIC.get(getTopicCacheKey(id));
		if(model==null){
			model = this.findById(id);
		}
		if(Validator.isEmpty(model)){
			return null;
		}else{
			if(model.getTContent()==null){
				TContent tContent = this.getTContentDAO().findByTopicId(model.getTopicId());
				if(Validator.isEmpty(tContent)){
					logger.error("找不到内容的文章,标题为:"+model.getTitle()+", ID:"+model.getTopicId());
					model.setIsDeleted(true);
					this.getTopicDAO().update(model);
					return null;
				}else{
					model.setTContent(tContent);
				}
				this.updateTopicCache(model);
			}			
		}
		return model;		
	}

	public void createOrUpdate(Topic model) throws ClubException {
		if(model.getTopicId()>0){
			this.update(model);
		}else{
			this.create(model);
		}
	}

	public void create(Topic model) throws ClubException {
		if(Validator.isEmpty(model)
				|| Validator.isEmpty(model.getTitle())
				|| Validator.isEmpty(model.getTContent())
				|| Validator.isEmpty(model.getTContent().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.getUser())){
				model.setUserId(model.getUser().getUserId());
				model.setUserName(model.getUser().getUserName());
			}
			model.setReplyId(this.getLastReplyId());//获取社区最大ID
			///TContent tcontent = model.getTContent();
			model.setContentLength(model.getTContent().getContent().length());
			model.setLastReplyDateTime(model.getCreateDateTime());
			model.setLastReplyUserName("");
			this.getTopicDAO().create(model);
			if(model.getTopicId()<=0){ //如果主题添加成功则向内容表添加
				throw new ClubRuntimeException(MessageUtils.getMessage("error_system"));
			}
			model.getTContent().setTopicId(model.getTopicId());
			this.getTContentDAO().create(model.getTContent());
			this.getForumTopicCache(model.getForumId()).clear();
			this.getForumTopicCache(null).clear();
			this.updateTopicCache(model);
		}
	}

	public void update(Topic model) throws ClubException {
		if(Validator.isEmpty(model)
				|| Validator.isEmpty(model.getUserName())
				|| Validator.isEmpty(model.getTitle())
				|| Validator.isEmpty(model.getTContent())
		){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}else{
			model.setContentLength(model.getTContent().getContent().length());
			model.setLastReplyDateTime(model.getCreateDateTime());
			model.setLastReplyUserName("");
			Topic topic = this.getTopicDAO().findById(model.getTopicId());
			BeanUtils.copyProperties(topic, model); //BO->PO
			if(model.getTitle().length()>200||model.getContentLength()>50000){
				throw new ClubException(MessageUtils.getMessage("error_toolong"));
			}
			this.getTopicDAO().update(topic);
			TContent tContent = this.getTContentDAO().findByTopicId(topic.getTopicId());
			BeanUtils.copyProperties(tContent,model.getTContent());
			this.getTContentDAO().update(tContent);
			this.getForumTopicCache(model.getForumId()).clear();
			this.getForumTopicCache(null).clear();
			this.updateTopicCache(model);
		}
	}

	public int delete(Topic model) throws ClubException {
		if(Validator.isEmpty(model)||model.getTopicId()<=0){
			throw new ClubException(MessageUtils.getMessage("error_empty"));
		}
		this.getTContentDAO().deleteByTopicId(model.getTopicId());
		TopicParameter param = new TopicParameter();
		param.setTopicId(model.getTopicId());
		param.setPage(1);
		param.setRows(1000000);
		this.getReplyDAO().deleteByTopicId(model.getTopicId());
		this.getRContentDAO().deleteByTopicId(model.getTopicId());
		this.getTContentDAO().deleteByTopicId(model.getTopicId());
		this.getForumTopicCache(null).clear();
		this.getForumTopicCache(model.getForumId()).clear();
		CACHE_TOPIC.remove(this.getTopicCacheKey(model.getTopicId()));
		return this.getTopicDAO().delete(model);
	}

	/**
	 * 移动文章
	 * @throws ClubException 
	 */
	public Topic move(TopicMoveForm form) throws ClubException {
		int originalForumId = form.getOriginalForumId();
		int toForumId = form.getToForumId();
		int topicId = form.getTopicId();
		if(toForumId==0 || topicId==0){
			throw new ClubException(MessageUtils.getMessage("error_notfind_forum"));
		}
		Topic topic = this.getTopicDAO().findById(form.getTopicId());
		topic.setForumId(toForumId);
		topic.setOriginalForumId(originalForumId);
		topic.setMoveUserId(form.getMoveUserId());
		topic.setMoveUserName(form.getMoveUserName());
		topic.setMoveDateTime(FormatDateTime.now());
		topic.setIsManaged(true); //标记文章被管理过
		this.getTopicDAO().update(topic);
		this.getReplyDAO().updateForumIdByTopicId(topic.getTopicId(),topic.getForumId());
		this.getForumTopicCache(form.getForumId()).clear();
		this.getForumTopicCache(topic.getForumId()).clear();
		this.getForumTopicCache(null).clear();
		this.updateTopicCache(topic);
		return topic;
	}
	
	@SuppressWarnings("unchecked")
	public List<Topic> findByParameter(TopicParameter param){
		List<Topic> list = (List<Topic>) this.getForumTopicCache(param.getForumId()).get(param.getCacheKeyOfList());
		if(Validator.isEmpty(list)){
			list = this.getTopicDAO().findByParameter(param);
			if(!Validator.isEmpty(list)){
				this.getForumTopicCache(param.getForumId()).put(param.getCacheKeyOfList(),list);
			}
		}
		return list;
	}
	
	public List<TContent> findContentByParameter(TopicParameter param) {
		return this.getTContentDAO().findByParameter(param);
	}
	
	/**
	 * 合并论坛帖子
	 * @throws ClubException 
	 */
	public int update_forumId(int forumId, int toForumId) throws ClubException {
		if(forumId>0&&toForumId>0){
			this.getForumTopicCache(forumId).clear();
			this.getForumTopicCache(toForumId).clear();
			CACHE_TOPIC.clear();
			this.getReplyDAO().update_forumId(forumId, toForumId);
			return this.getTopicDAO().update_forumId(forumId, toForumId);
		}else{
			throw new ClubException(MessageUtils.getMessage("error_param"));
		}
	}

	@SuppressWarnings("unchecked")
	public long countByParameter(TopicParameter param) {
		Long ln = (Long) this.getForumTopicCache(param.getForumId()).get(param.getCacheKeyOfCount());
		if(Validator.isEmpty(ln)){
			long c = this.getTopicDAO().countByParameter(param);
			this.getForumTopicCache(param.getForumId()).put(param.getCacheKeyOfCount(),new Long(c));
			return c;
		}else{
			return ln.longValue();
		}
	}
	
	public TopicDAO getTopicDAO() {
		return DAOWrapper.<TopicDAO>getSingletonInstance(DAOLocator.TOPIC);
	}

	public TContentDAO getTContentDAO() {
		return DAOWrapper.<TContentDAO>getSingletonInstance(DAOLocator.T_CONTENT);
	}
	
	public RContentDAO getRContentDAO() {
		return DAOWrapper.<RContentDAO>getSingletonInstance(DAOLocator.R_CONTENT);
	}
	
	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));
	}

	@Override
	public List<Topic> searchTopic(String title, int page, int rows) {
		if(Validator.isEmpty(title)){
			return null;
		}
		String key = title.replace("[", "").replace("]", "").replace("\"", "")
			.replace("'", "").replace("`", "").replace("'", "")
			.replace("(", "").replace(")", "").replace("?", "")
			.replace("AND", " ").replace("TO", " ").replace("}", "")
			.replace("+", " ").replace("-", " ").replace("$", "")
			.replace("#", " ").replace("@", " ").replace("%", "")
			.replace("NOT", " ").replace("OR", "").replace(":","").replace(";","")
			.replace(">", "").replace("<", "").replace("{", "").replace("&", "")
			.replace("*", "").replace("^", "").replace("/", " ").replace("\\", " ")
			.replace("-", "").replace("+", "").replace("=", "").replace("_", " ")
			.replace("!", "");
		StringBuffer keys = new StringBuffer();
		//List<Topic> list = null;
		Token[] tokens;
		try {
			tokens = AnalyzerUtils.tokensFromAnalysis(AnalyzerFactory.getAnalyzer(), key);
			if(!Validator.isEmpty(tokens)){
				for (int i = 0; i < tokens.length; i++) {
					String temp = tokens[i].termText();
					//if(temp.length()>2){
						keys.append(temp);
						keys.append(" ");
					//}
					if(i>5){
						break;
					}
				}
				Map<String, String> filter = new HashMap<String,String>();
				filter.put("title", title);
				SearchProvider<Topic> searcher = SearchFactory.getInstance(SearchProvider.SEARCH_TOPIC);
				SearchParameter param = new SearchParameter();
				param.setKeys(keys.toString());
				param.setPage(new Integer(page));
				param.setRows(new Integer(rows));
				param.setHighlight(false);
				param.setFilter(filter);
				return searcher.results(param); 
			}
		} catch (IOException e) {
			logger.error(e.toString());
		}
		return null;
	}

	@Override
	public Cache<Topic> getTopicCache() {
		return CACHE_TOPIC;
	}
}	

⌨️ 快捷键说明

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