⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topicserviceimpl.java

📁 野蔷薇论坛源码 java 自己看看吧。 学习用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			this.update(model);
		}else{
			this.create(model);
		}
	}

	public void 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
			try {
				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();
				}else{
					throw new ClubException(MessageUtils.getMessage("error_system"));
				}
			} catch (DAOException e) {
				logger.error(e.toString());
				throw new ClubException(MessageUtils.getMessage("error_system"));
			}
		}
	}

	public void 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"));
			}
			try {
				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();
			} catch (DAOException e) {
				logger.error(e.toString());
				throw new ClubException(MessageUtils.getMessage("error_system"));
			}
		}
	}

	public int delete(TopicModel model) throws ClubException {
		if(model.getTopicId()<=0){
			return 0;
		}
		Topic item = new Topic();
		BeanUtils.copyProperties(item,model);
		this.getCountCache(model.getForumId()).clear();
		this.getCache(model.getForumId()).clear();
		this.getCache(null).clear();
		try {
			this.getContentDAO().deleteByTopicId(model.getTopicId());
			TopicParameter param = new TopicParameter();
			param.setTopicId(model.getTopicId());
			param.setPage(1);
			param.setRows(1000000);
			this.getReplyDAO().deleteByTopicId(model.getTopicId());
			this.getReContentDAO().deleteByTopicId(model.getTopicId());
			this.getContentDAO().deleteByTopicId(model.getTopicId());
			this.getCache(null).clear();
			this.getCache(model.getForumId()).clear();
			return this.getTopicDAO().delete(item);
		} catch (DAOException e) {
			logger.error(e.toString());
			throw new ClubException(MessageUtils.getMessage("error_system"));
		}
	}

	/**
	 * 移动文章
	 * @throws ClubException 
	 */
	public TopicModel move(TopicMoveForm form) throws ClubException {
		int originalForumId = form.getOriginalForumId();
		int toForumId = form.getToForumId();
		int topicId = form.getTopicId();
		if(toForumId==0 || topicId==0){
			logger.error("move error: forumId="+toForumId+" && topicId="+topicId);
			throw new ClubException(MessageUtils.getMessage("error_notfind_forum"));
		}
		TopicModel model = null;
		Topic item = this.getTopicDAO().findById(form.getTopicId());
		if(Validator.isEmpty(item)){
			throw new ClubException(MessageUtils.getMessage("error_notfind_topic"));
		}else{
			logger.debug("updated forumId:"+form.getForumId()+" to "+ toForumId);
			item.setForumId(toForumId);
			item.setOriginalForumId(originalForumId);
			item.setMoveUserId(form.getMoveUserId());
			item.setMoveUserName(form.getMoveUserName());
			item.setMoveDateTime(FormatDateTime.now());
			item.setIsManaged(true); //标记文章被管理过
			try {
				this.getTopicDAO().update(item);
			} catch (DAOException e) {
				logger.error(e.toString());
				throw new ClubException(MessageUtils.getMessage("error_system"));
			}
			model = new TopicModel();
			BeanUtils.copyProperties(model,item);
			this.getCache(form.getForumId()).clear();
			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){
			try {
				return this.getTopicDAO().update_forumId(forumId, toForumId);
			} catch (DAOException e) {
				logger.error(e.toString());
				throw new ClubException(MessageUtils.getMessage("error_system"));
			}
		}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());
		}
		*/
	}

	@Override
	public List<TopicModel> 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("{", "");
		StringBuffer keys = new StringBuffer();
		//List<TopicModel> 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<TopicModel> 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;
	}


}	

⌨️ 快捷键说明

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