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

📄 articlebiz.java

📁 一个不错的bbs论坛系统.对初学者很有帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.yhbbs.article.biz;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;

import com.yhbbs.article.bean.ArticleDtoIm;
import com.yhbbs.article.bean.ArticleIm;
import com.yhbbs.article.bean.ContentIm;
import com.yhbbs.article.dao.ArticleDaoIm;
import com.yhbbs.article.itface.Article;
import com.yhbbs.article.itface.ArticleDao;
import com.yhbbs.article.itface.ArticleDto;
import com.yhbbs.article.itface.Content;
import com.yhbbs.article.search.biz.BbsWriterIm;
import com.yhbbs.bbs.bean.WealthDtoIm;
import com.yhbbs.bbs.biz.BbsPropBiz;
import com.yhbbs.bbs.biz.BbsStatBiz;
import com.yhbbs.bbs.biz.WealthBiz;
import com.yhbbs.bbs.itface.Parameter;
import com.yhbbs.forum.bean.ForumArtPostIm;
import com.yhbbs.forum.biz.ForumBiz;
import com.yhbbs.forum.itface.ForumArtPost;
import com.yhbbs.user.biz.UserBiz;
import com.yhbbs.utils.Constants;
import com.yhbbs.utils.DateUtils;
import com.yhbbs.vote.biz.VoteBiz;

/**
 * <p>Title:论坛帖子相关操作</p>
 * <li>	论坛帖子的相关操作
 * <p>Company: www.yyhweb.com</p>
 * <br><b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YHBBS-2.0
 */
public class ArticleBiz {
	
	private static Logger bbslog = Logger.getLogger(ArticleBiz.class);
	
	private static ArticleDao artdao = ArticleDaoIm.getInstance();
	
	/** 取得帖子标题(用于导航)
	 * @param artId 帖子Id
	 * @return 帖子标题
	 */
	public static String getArtTitle(int artId){
		
		try {
			return artdao.getArtTitle(artId);
		}
		catch(SQLException e) {
			e.toString();
		}
		return "";
	}
	
	/** 取得帖子发表时间(用于判断用户是否可再编辑帖子)
	 * @param artId 帖子Id
	 * @return true:可以 false:不可以
	 */
	public static boolean isEditAgain(int artId){
		// 系统设置的编辑帖子过期时间
		Parameter parameter = BbsPropBiz.getParameter();
		if(parameter.getArtEdit()==0)
			return false;
		
		int hours = parameter.getArtHour();
		if(hours==0)
			return true;
		
		long pt = 0;
		long ht = hours*3600000l;
		long ct = System.currentTimeMillis();
		try {
			pt = artdao.getArtPostTime(artId).getTime();
			return (ct-pt-ht)<0;
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke isEditAgain(int artId):\n" + e.toString());
		}
		return false;
	}
	
	/** 取得某回复帖子的主题帖子Id
	 * @param artId 帖子Id
	 * @return 发表帖子用户Id
	 */
	public static int getReplayParentId(int artId){
		
		try {
			return artdao.getReplayParentId(artId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getReplayParentId(int artId):\n" + e.toString());
		}
		return 0;
	}
	
	/** 取得发表帖子用户Id
	 * @param artId 帖子Id
	 * @return 发表帖子用户Id
	 */
	public static int getArtUserId(int artId){
		
		try {
			return artdao.getArtUserId(artId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getArtUserId(int artId):\n" + e.toString());
		}
		return 0;
	}
	
	/** 取得帖子最大Id
	 * @return 帖子最大Id
	 */
	public static int getArtMaxId(){
		
		try {
			return (artdao.getMaxArtId()+1);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getMaxArtId():\n" + e.toString());
		}
		return 1;
	}
	
	/** 根据帖子Id取得帖子Dto(无内容)
	 * @param artId 帖子Id
	 * @return 一个帖子
	 */
	public static ArticleDto getArticle(int artId){
		ArticleDto article = null;
		try {
			article = artdao.getArticle(artId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getArticle(artId):\n" + e.toString());
		}
		if(article==null)
			article = new ArticleDtoIm();
		return article;
	}
	
	/** 发表文章,根据parent判断是主题贴还是回帖
	 * <li>若是回复贴更新主题贴信息
	 * @param article 帖子信息
	 * @param content 帖子内容
	 * @param forum 需要更新的论坛信息
	 * @return 一个帖子
	 */
	public static boolean postArticle(Article article,Content content,ForumArtPost forum){
		boolean flag = false;
		WealthDtoIm wealth = null;
		HashMap<String, Comparable> userMap = null;
		int artId = article.getId();
		int userId = article.getUserid();
		int parent = article.getParentid();
		try {
			flag = artdao.addArticle(article);					   // 添加帖子信息 
			if(flag){
				flag = artdao.addContent(content);				  // 添加帖子内容
			}
			if(flag){											 // 更新帖子\论坛\系统\用户相关信息
				if(parent==0){									// 主题贴
					wealth = (WealthDtoIm) WealthBiz.getPostArtWth();
					wealth.setId(userId);
					UserBiz.postArticle(wealth,1);            // 更新用户财富和帖子数目
					ForumBiz.forumPostArt(forum,true);       // 更新论坛信息
					BbsStatBiz.upArtCount(true);            // 更新系统统计信息
				}else{									   // 回复贴
					wealth = (WealthDtoIm) WealthBiz.getRepArtWth();
					wealth.setId(userId);
					UserBiz.postArticle(wealth,2);		  // 更新用户财富和帖子数目
					userMap = new HashMap<String, Comparable>();
					userMap.put("artId",parent);
					userMap.put("userId",userId);
					userMap.put("userName",article.getUser());
					artdao.replayArt(userMap);	 		 // 更新主题帖子信息 
					ForumBiz.forumPostArt(forum,false); // 更新论坛信息
					BbsStatBiz.upArtCount(false);	   // 更新系统统计信息
				}
				article.setContent(content);		  // 该帖子建立索引
				BbsWriterIm.indexArticle(article);
			}else{
				artdao.delArticle(artId,false);
				bbslog.error("Post an article unsuccessfully!");
			}
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke postArticle(article,content,forum):\n" + e.toString());
		}
		return flag;
	}
	
	/** 更新文章,如果该文章是论坛的最新文章则需要更新其标题.
	 * @param article 帖子信息
	 * @param content 帖子内容
	 * @return true:更新成功 false:更新失败
	 */
	public static boolean updateArticle(Article article,Content content){
		boolean flag = true;
		int artId = article.getId();
		int forumId = article.getForum();
		String title = article.getTitle();
		try {
			flag = artdao.editArticle(article);
			if(flag){
				if(artId==ForumBiz.getForumLArtId(forumId))
					ForumBiz.upForumLArtTitle(forumId,title);
				flag = artdao.editContent(content);
				article.setContent(content);		  // 更新该帖子索引
				BbsWriterIm.updateIndex(article);
			}else{
				bbslog.error("Edit artilce unsuccessfully!The article id is:"+artId);
			}
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke updateArticle(Article article,Content content):\n" + e.toString());
		}
		return flag;
	}
	
	/** 删除帖子,论坛统计和系统统计都要更新
	 * @param artId 帖子Id
	 * @param userId 帖子作者Id
	 * @param forumId 帖子所在论坛Id
	 * @param flag true:主题帖 false:回复贴
	 * @return true:删除成功 false:删除失败
	 */
	public static boolean deleteArticle(int artId,int userId,int forumId,boolean flag){
		String isVote = "";
		boolean del = false;
		boolean isToday = isTodayArt(artId);/** 判断是否为今日帖子 */
		WealthDtoIm wealth = null;
		try {
			
			isVote =getArticle(artId).getIsvote();
			del = artdao.delArticle(artId,flag);
			if(del){
				if(flag){
					wealth = (WealthDtoIm) WealthBiz.getDelArtWth();
					wealth.setId(userId);
					UserBiz.postArticle(wealth,4);
				}else{
					wealth = (WealthDtoIm) WealthBiz.getDelArtWth();
					wealth.setId(userId);
					UserBiz.postArticle(wealth,5);
				}
				
				BbsStatBiz.deleteArticle(flag);
				if(!isVote.equals("0")){ // 删除投票贴还需要删除其投票用户等信息
					VoteBiz.deleteVote(artId);
				}
				if(artId==ForumBiz.getForumLArtId(forumId)) // 已经删除的文章是本论坛的最新文章
					upForumLastArt(forumId,flag,isToday);
				else
					ForumBiz.delArticle(forumId, flag,isToday);    // 更新原论坛统计信息(主要是帖子数目的更新)
				  
				BbsWriterIm.deleteArt(artId);  // 删除该帖子索引
			}else{
				bbslog.error("Delete artilce unsuccessfully!The article id is:"+artId);
			}	
			
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke deleteArticle(int artId,boolean flag):\n" + e.toString());
		}
		return del;
	}
	
	/** 当删除或移动的文章是某论坛最新文章时,需再次更新论坛最新文章信息
	 * @param forumId 论坛Id
	 * @param flag true:主题帖 false:回复贴
	 * @param istoday true:今日帖 false:非今日贴
	 */
	private static void upForumLastArt(int forumId,boolean flag,boolean istoday){
		try{
			Article lastArt = artdao.getForumLastArt(forumId);// 取得论坛最新文章更新论坛信息
			ForumArtPost fArtDel = new ForumArtPostIm();    
			String title = "";
			int titlelength = Constants.titlelength;
			fArtDel.setId(forumId);
			if(lastArt==null)
				return;
			if(lastArt.getParentid()==0){		  //  这个地方的处理非常重要,如果删除回复贴需要根据parentId取得主题贴的标题
				title = lastArt.getTitle();
				fArtDel.setLastartid(lastArt.getId());
			}else{
				title = getArtTitle(lastArt.getParentid());
				fArtDel.setLastartid(lastArt.getParentid());
			}
			if(title.length()<=titlelength)
				fArtDel.setLastart(title);
			else
				fArtDel.setLastart(title.substring(0,titlelength)+"···");
			
			fArtDel.setLastuserid(lastArt.getUserid());
			fArtDel.setLastuser(lastArt.getUser());
			fArtDel.setLasttime(lastArt.getPosttime());
			ForumBiz.forumDelArt(fArtDel,flag,istoday);
		}catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke upForumLastArt(int forumId,boolean flag):\n" + e.toString());
		}
	}
	/** 根据帖子Id取得帖子(有内容)
	 * @param artId 帖子Id
	 * @return 一个帖子
	 */
	public static Article getArtContent(int artId){
		
		try {
			return artdao.getArtContent(artId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getArtContent(artId):\n" + e.toString());
		}
		return new ArticleIm();
	}
	/** 根据帖子Id取得帖子内容(共搜索时用)
	 * @param artId 帖子Id
	 * @return 帖子内容
	 */
	public static Content getContent(int artId){
		
		try {
			return artdao.getContent(artId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getContent(int artId):\n" + e.toString());
		}
		return new ContentIm();
	}
	
	/** 取得主题帖子的回复贴列表
	 * @param artMap 帖子Id,分页起始位置和大小
	 * @return 回复贴列表
	 */
	public static List getArtReplay(HashMap artMap){
		
		try {
			return artdao.getArtReplay(artMap);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getArtReplay(artMap):\n" + e.toString());
		}
		return null;
	}
	
	/** 取得主题帖子的回复贴总数
	 * @param artId 帖子Id
	 * @return 回复贴总数
	 */
	public static int getArtReplayC(int artId){
		
		try {
			return artdao.getArtReplayC(artId);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getArtReplayC(artId):\n" + e.toString());
		}
		return 0;
	}
	
	/** 判断某用户是否回复了某帖
	 * @param userMap 帖子Id和用户Id
	 * @return boolean 是:true 否:false
	 */
	public static boolean isUserRepArt(HashMap userMap){
		
		try {
			return artdao.isUserRepArt(userMap);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke isUserRepArt(userMap):\n" + e.toString());
		}
		return false;
	}
	/** 取得论坛最新帖子列表
	 * @param size 显示的总数
	 * @return 最新帖子列表
	 */
	public static List getNewArticle(int size){
		
		try {
			return artdao.getNewArticle(size);
		}
		catch(SQLException e) {
			bbslog.error("Throws a SqlException when invoke getNewArticle(int size):\n" + e.toString());
		}
		return null;
	}
	/** 取得论坛最新精华帖子列表
	 * @param size 显示的总数
	 * @return 最新精华帖子列表
	 */

⌨️ 快捷键说明

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