forumjsp.java

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

JAVA
795
字号
/* 
 * Created on 2007-2-17
 * Last modified on 2007-12-14
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.view.jsp;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.yeqiangwei.club.param.CounterParameter;
import com.yeqiangwei.club.param.FavoriteParameter;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.counter.CountService;
import com.yeqiangwei.club.service.forum.ForumService;
import com.yeqiangwei.club.service.topic.ReplyService;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.topic.TopicService;
import com.yeqiangwei.club.service.user.UserLogin;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.model.Counters;
import com.yeqiangwei.club.model.ForumLabel;
import com.yeqiangwei.club.model.Forum;
import com.yeqiangwei.club.model.ModelsOfForum;
import com.yeqiangwei.club.model.Reply;
import com.yeqiangwei.club.model.Topic;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.forum.FavoriteForumService;
import com.yeqiangwei.club.service.forum.ForumLabelService;
import com.yeqiangwei.club.service.forum.ForumResults;
import com.yeqiangwei.club.service.forum.ForumTemplate;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.PaginationHelper;
import com.yeqiangwei.club.util.Utils;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.HttpServletUtils;
import com.yeqiangwei.util.IpUtils;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.club.controller.form.ForumEditForm;
import com.yeqiangwei.club.controller.form.ForumLabelForm;
import com.yeqiangwei.club.controller.form.TopicPostForm;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.view.model.CountersView;
import com.yeqiangwei.club.view.model.ForumView;
import com.yeqiangwei.club.view.model.ModelsOfForumView;
import com.yeqiangwei.club.view.model.ReplyView;
import com.yeqiangwei.club.view.model.TopicView;
import com.yeqiangwei.club.view.model.UserView;
import com.yeqiangwei.club.view.util.ModelsOfForumToView;
import com.yeqiangwei.club.view.util.UrlUtils;
import com.yeqiangwei.club.view.util.UserSetUtils;
import com.yeqiangwei.util.ParamUtils;

public class ForumJsp extends BaseJsp{
	
	private static final Logger logger = Logger.getLogger(ForumJsp.class);
	
	private ForumTemplate forumTemplate;
	
	private String pagination = "";
	
	private String forumStyle;
	
	private String threadStyle;
	
	private String title;
	
	private int storey = 0;
	
	private int forumId = 0;
	
	private int lastpage = 1; //帖子回复的最后一页
	
	private GroupOfForumJsp groupOfForumJsp;
	
	private int page = 1;
	
	private int fpage = 1;
	
	private int maxpage = 0;

	public String getIndentStr(int layer){
		StringBuffer sb = new StringBuffer("");
		for(int i=0; i<layer; i++){
    		sb.append("&nbsp;");
    	}
		return sb.toString();
	}
	

	public String topicflag(){
		StringBuffer sb = new StringBuffer();
		String url = (UrlUtils.getUrl(UrlUtils.TOPIC,ParamUtils.getIntAuto(request,"topicId"),0,request));
		sb.append("<a href=\"");
		sb.append(this.getUserService().getBasicInfo().getUrl());
		sb.append("\" target=\"_blank\">");
		sb.append(this.getUserService().getBasicInfo().getName());
		sb.append("</a>&nbsp;&nbsp;<a href=\"");
		sb.append(url);
		sb.append("\" target=\"_blank\">");
		sb.append(url);
		sb.append("</a>");
		return sb.toString();
	}

	public ForumJsp(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		forumId = ParamUtils.getIntParameter(request,"forumId",0);
		page = ParamUtils.getIntParameter(request,"page",1);
		fpage = ParamUtils.getIntParameter(request,"fpage",1);
		lastpage = ParamUtils.getIntParameter(request,"lpage",1);
		maxpage = this.getUserService().getBasicInfo().getMaxpage();
		if(maxpage>0&&fpage>maxpage){
			fpage = maxpage;
			request.setAttribute("message","系统只提供最多"+maxpage+"页的文章列表,如果您需要找到更多内容请使用社区搜索功能!");
		}
	}
	
	public int getSotrey(int listnum){
		storey++;
		//int page = ParamUtils.getIntParameter(request,"page",1);
		if(storey==1&&page>1){
			storey = ((page-1)*listnum);
		}
		return storey;
	}
	
	/**
	 * 回复楼层
	 * @return
	 */
	public int getStorey(){
		return this.getStorey(request);
	}
	
	public int getStorey(HttpServletRequest request){
		int listnum = 0;
		Forum fmodel = this.getForumService().findById(forumId);
		if(!Validator.isEmpty(fmodel)){
			listnum = fmodel.getReplyListNumber(); 
		}
		return this.getSotrey(listnum);
	}

	public CountersView findCounter(){
		CounterParameter param = new CounterParameter();
		param.setForumId(forumId);
		param.setYmd(FormatDateTime.formatDateTimeToInt("yyyyMMdd"));
		CountersView view = new CountersView();
		Counters model = this.getCountService().findOnlyByParameter(param);
		if(!Validator.isEmpty(model)){
			BeanUtils.copyProperties(view,model);
		}
		return view;
	}
	
	public List<ForumView> findAll(){
		List<Forum> mlist = this.getForumTemplate().findAll();
		return BeanUtils.copyList(mlist, BeanLocator.FORUMVIEW);
	}
	
	public ForumLabelForm getForumLabelForm(){
		int id = ParamUtils.getIntParameter(request,"labelId");
		ForumLabelForm f = new ForumLabelForm();
		ForumLabel model = null;
		if(id>0){
			model = this.getForumLabelService().findById(id);
		}
		if(!Validator.isEmpty(model)){
			BeanUtils.copyProperties(f,model);
		}else{
			Object object = ParamUtils.getAttribute(request,"ForumLabelForm");
			if(!Validator.isEmpty(object)){
				f = (ForumLabelForm) object;
			}
		}
		return f;
	}
	
	public ForumEditForm getForumEditForm(){
		ForumEditForm f = new ForumEditForm();
		Forum model = null;
		if(forumId>0){
			model = this.getForumService().findById(forumId);
		}
		if(!Validator.isEmpty(model)){
			BeanUtils.copyProperties(f,model);
		}else{
			Object object = ParamUtils.getAttribute(request,"ForumEditForm");
			if(!Validator.isEmpty(object)){
				f = (ForumEditForm) object;
			}
		}
		return f;
	}
	
	public ForumView getForumView(){
		ForumView v = null;
		Forum m = this.getForumService().findById(forumId);
		if(!Validator.isEmpty(m)){
			v = new ForumView();
			BeanUtils.copyProperties(v,m);
		}
		return v;
	}
	
	private boolean cookieLabel(){
		boolean bea = false;
		StringBuffer sb = new StringBuffer();
		sb.append("f");
		sb.append(forumId);
		if(ParamUtils.getCookieValue(request,sb.toString(),null)==null){
			bea = true;
			ParamUtils.setCookieValue(response,sb.toString(),"1",0);
		}
		return bea;
	}
	
	public ModelsOfForumView getModelsOfForumView(){
		Counters counters = new Counters();
		counters.setHits(1);
		counters.setForumId(forumId);
		if(this.cookieLabel()){
			User user = UserLogin.getUser(request);
			if(!Validator.isEmpty(user)){
				switch(user.getSex()){
					case 1:
					counters.setBoys(1);
					break;
					
					case 2:
					counters.setGirls(1);
					break;
					
					default:
					counters.setGirls(1);
				}
			}else{
				counters.setOthers(1);
			}
		}
		if(!Utils.isSpider(request)){
			try {
				this.getCountService().doCount(counters);
			} catch (ClubException e) {
				logger.error(e.toString());
			}
		}
		return this.getModelsOfForumView(forumId);
	}
	
	@SuppressWarnings("unchecked")
	public ModelsOfForumView getModelsOfForumView(int forumId){
		ModelsOfForumView modelsOfForumView = new ModelsOfForumView();
		ModelsOfForum modelsOfForum = this.getForumService().getModelsOfForum(forumId);			
		if(!Validator.isEmpty(modelsOfForum)){
			ModelsOfForumToView modelsOfForumToView = new ModelsOfForumToView();
			modelsOfForumView = modelsOfForumToView.toView(modelsOfForum);
			if(Validator.isEmpty(modelsOfForum.getForum())){
				logger.debug("未找到相关版面信息 forumId:"+forumId);
				HttpServletUtils.redirect(response,"404.html");
				return null;
			}
		}else{
			logger.debug("未找到相关版面 forumId:"+forumId);
			HttpServletUtils.redirect(response,"404.html");
			return null;
		}
		return modelsOfForumView;
	}
	
	public List<TopicView> findTopicByForumId(){
		int drows = 20;
		if(!Validator.isEmpty(this.getForumService().findById(forumId))){
			drows = this.getForumService().findById(forumId).getTopicListNumber(); //版面默认显示的文章数
		}
		int rows = ParamUtils.getIntParameter(request,"rows",drows);
		if(rows>100){
			rows = drows;
		}
		
		TopicParameter param = new TopicParameter();
		param.setIsDeleted(false);
		param.setForumId(forumId);
		param.setPage(fpage);
		param.setRows(rows);
		param.setOrderBy((byte)1);
		byte better = ParamUtils.getByteParameter(request,"better");
		int labelId = ParamUtils.getIntParameter(request,"labelId");
		if(better>0){
			param.setBetter(new Byte(better));
		}
		if(labelId>0){
			param.setLabelId(labelId);
		}
		long total = this.getTopicService().countByParameter(param);
		int lastpage = PaginationHelper.getLastpage(total, param.getRows());
		if(param.getPage()>lastpage){
			param.setPage(lastpage);
		}
		List<Topic> list = this.getTopicService().findByParameter(param);
		StringBuffer url = new StringBuffer();
		url.append("f");
		url.append(forumId);
		url.append("b");
		url.append(better);
		url.append("l");
		url.append(labelId);
		url.append("fp");
		this.setPagination(PaginationHelper.getPagination(url.toString(),param.getPage(),param.getRows(),total,5));
		if(!Validator.isEmpty(list)){
			return BeanUtils.<Topic,TopicView>copyList(list,BeanLocator.TOPICVIEW);
		}else{
			return null;
		}
	}
	
	public TopicPostForm getTopicPostForm(){
		TopicPostForm topicPostForm = (TopicPostForm) request.getAttribute("TopicPostForm");
		if(Validator.isEmpty(topicPostForm)){
			topicPostForm = new TopicPostForm();
		}
		User user =  UserLogin.getUser(request);
		if(!Validator.isEmpty(user)){
			topicPostForm.setUserId(user.getUserId());
			topicPostForm.setUserName(user.getUserName());
			topicPostForm.setPassword(user.getPassword());
		}else{
			String eip = IpUtils.enAddr(request.getRemoteAddr());
			eip = eip.substring(eip.length()-6,eip.length());
			topicPostForm.setUserName("过客"+eip);
		}
		return topicPostForm;
	}
	
	
	public Object findTopicAndContentById(){
		TopicView tv = null;
		ReplyView rv = null;
		int topicId = ParamUtils.getIntAuto(request,"topicId");
		int rid = ParamUtils.getIntAuto(request,"replyId");
		String act = ParamUtils.getStringParameter(request,"act","");
		if((rid>0&&topicId==0)
				||(rid>0&&(act.equals("addreply")||act.equals("trashreply")||act.equals("editreply")))
			){//回复内容
			Reply model = this.getReplyService().findReplyAndContentById(rid);
			if(!Validator.isEmpty(model)){
				rv = new ReplyView();
				BeanUtils.copyProperties(rv,model.getRContent());
				BeanUtils.copyProperties(rv,model);
				if(model.getForumId()!=forumId){
					HttpServletUtils.redirect(response,UrlUtils.getUrl(UrlUtils.REPLY, rv, request));
					return null;
				}
				User user = this.getUserService().findById(rv.getUserId());
				if(user!=null){
					UserView userView = new UserView();
					BeanUtils.copyProperties(userView, user);
					rv.setUserView(userView);
				}else{
					rv.setUserView( new UserView());
				}
				request.setAttribute("topicId",rv.getTopicId());
				request.setAttribute(UserSetUtils.getThreadStyleName(request),"1");
				if(request.getRequestURI().equals("/club/thread.jsp")){
					//如果是帖子页则记住浏览风格
					UserSetUtils.update(request,response,UserSetUtils.THREAD_STYLE,"1");
				}
				if(act.equals("editreply")){
					if(rv.getIsDeleted()){
						HttpServletUtils.redirect(response,"404.html");
						return null;
					}
					TopicPostForm form = (TopicPostForm)request.getAttribute("TopicPostForm");
					if(!Validator.isEmpty(form)){
						BeanUtils.copyProperties(rv,form);
					}
					else{
						form = new TopicPostForm();
						BeanUtils.copyProperties(form,rv);
					}
					request.setAttribute("TopicPostForm",form);
				}
			}else{
				HttpServletUtils.redirect(response,"404.html");
				return null;
			}
			
		}
		else if(!act.equals("addtopic")){//取主题
			Topic model = this.getTopicService().findTopicAndContentById(topicId);

⌨️ 快捷键说明

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