replyaction.java

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

JAVA
201
字号
/* 
 * Created on 2007-3-23
 * Last modified on 2007-11-9
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.controller.action;

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

import org.apache.log4j.Logger;

import com.yeqiangwei.club.service.topic.ReplyService;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.model.RContent;
import com.yeqiangwei.club.model.Reply;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.user.UserLogin;
import com.yeqiangwei.club.service.user.UserSettingsService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.TopicPostForm;
import com.yeqiangwei.club.controller.form.TopicTrashForm;
import com.yeqiangwei.club.controller.form.build.TopicPostBuild;
import com.yeqiangwei.club.controller.form.build.TopicTrashBuild;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.view.util.UrlUtils;
import com.yeqiangwei.util.HttpServletUtils;
import com.yeqiangwei.util.ParamUtils;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.Validator;

public class ReplyAction {
	
	private static final Logger logger = Logger.getLogger(ReplyAction.class);
	
	public void delete(HttpServletRequest request,HttpServletResponse response){
		
	}

	private String forwardUrl(String act, int topicId, int forumId){
		StringBuffer sb = new StringBuffer("/club/topicAdmin.jsp?forumId=");
		sb.append(forumId);
		sb.append("&topicId=");
		sb.append(topicId);
		sb.append("&act=");
		sb.append(act);
		return sb.toString();
	}
	
	public void trash(HttpServletRequest request,HttpServletResponse response){
		TopicTrashForm topicTrashForm = new TopicTrashBuild(request).building();
		try {
			this.getReplyService().trash(topicTrashForm);
			request.setAttribute("message",MessageUtils.getMessage("success"));
		} catch (ClubException e) {
			request.setAttribute("message",e.getMessage());
		}
		HttpServletUtils.forward(request,response,forwardUrl("trashreply",topicTrashForm.getTopicId(),topicTrashForm.getForumId()));
	}
	
	private void autoTitle(TopicPostForm topicPostForm){
		if(Validator.isEmpty(topicPostForm.getTitle())){
			String content = topicPostForm.getContent();
			content = StringHelper.substring(content,0,500,"");
			content = StringHelper.ubbPattern(content,"\\[(.*?)\\](.*?)\\[/(.*?)\\]","","");
			content = StringHelper.ubbPattern(content,"\\[(.*?)\\]","","");
			content = StringHelper.ubbPattern(content,"<(.*?)>","","");
			if(content.length()>0){
				content = content.replace("\r\n","{r-n}");
				if(content.startsWith("{r-n}")){
					content = StringHelper.substring(content,5,content);
				}
				int n = content.indexOf("{r-n}");
				if(n==-1||n>30){
					n = 30;
				}
				String str = StringHelper.substring(content,0,n,"");
				int i1 = str.length();
				int i2 = topicPostForm.getContent().length();
				if(i1==i2){
					topicPostForm.setContent("");
				}
				else if(!Validator.isEmpty(str)){
					str+="...";
				}
				topicPostForm.setTitle(str);
			}
		}
	}
	
	public void createOrUpdate(HttpServletRequest request,HttpServletResponse response, String act)
	{
		TopicPostForm topicPostForm = new TopicPostBuild(request).building();
		this.autoTitle(topicPostForm);
		if(Validator.isEmpty(topicPostForm.getTitle())&&Validator.isEmpty(topicPostForm.getContent()))
		{
			request.setAttribute("message",MessageUtils.getMessage("error_empty"));
			this.errorForward(request,response,topicPostForm);
			return;
		}
		else if(topicPostForm.getTitle().length()>100||topicPostForm.getContentLength()>50000){
			request.setAttribute("message",MessageUtils.getMessage("error_toolong"));
			this.errorForward(request,response,topicPostForm);
			return;
		}
		Reply reply = null;
		RContent rContent = null;
		//User user = UserLogin.getUser(request);
		try {
			if(act.equals("addreply")){
				reply = new Reply();
				rContent = new RContent();
				BeanUtils.copyProperties(reply,topicPostForm);
				BeanUtils.copyProperties(rContent,topicPostForm);
				reply.setRContent(rContent);
				reply.setUser(UserLogin.getUser(request)); //from session online user
				reply.setSessionUser(UserLogin.getUser(request));
				
				String temp = ParamUtils.getCookieValue(request, "r_title", "");
				if(!Validator.isEmpty(temp)&&temp.equalsIgnoreCase(reply.getTitle())){
					request.setAttribute("message","系统怀疑您在灌水...");
					this.errorForward(request,response,topicPostForm);
					return;
				}
				this.getReplyService().create(reply);
				ParamUtils.setCookieValue(response, "r_title", reply.getTitle(), 30);
			}else if(act.equals("editreply")){
				reply = this.getReplyService().findReplyAndContentById(topicPostForm.getReplyId());
				rContent = reply.getRContent();
				topicPostForm.setUserId(reply.getUserId());
				topicPostForm.setUserName(reply.getUserName());
				topicPostForm.setCreateDateTime(reply.getCreateDateTime());
				topicPostForm.setUserIp(reply.getUserIp());
				BeanUtils.copyProperties(reply,topicPostForm);
				BeanUtils.copyProperties(rContent,topicPostForm);
				reply.setRContent(rContent);
				reply.setUser(UserLogin.getUser(request));
				reply.setSessionUser(UserLogin.getUser(request));
				this.getReplyService().update(reply);
			}
			User user = reply.getUser();
			if(!Validator.isEmpty(user) && Validator.isEmpty(UserLogin.getUser(request)))
			{
				UserLogin.setCookies(request,response,user,0);
			}
			topicPostForm.setTopicId(reply.getTopicId());
			topicPostForm.setReplyId(reply.getReplyId());
			request.setAttribute("TopicPostForm",topicPostForm);
			request.setAttribute("returnUrl",UrlUtils.getUrl(UrlUtils.TOPIC,topicPostForm.getTopicId(),topicPostForm.getForumId(),request));
			byte postForward = this.getUserSettingscService().getUserSettings(user).getPostForward();
			StringBuffer sb = new StringBuffer();
			switch(postForward){
				case 1://返回版面
					sb.append(UrlUtils.getUrl(UrlUtils.FORUM,topicPostForm.getForumId(),0,request));
					HttpServletUtils.redirect(response,sb.toString());
					break;
				case 2://返回发表的主题
					sb.append(UrlUtils.getUrl(UrlUtils.TOPIC,topicPostForm.getTopicId(),reply.getForumId(),request));
					HttpServletUtils.redirect(response,sb.toString());
					break;
				default: //返回界面让用户选择
					sb.append("/club/posted.jsp?forumId=");
					sb.append(reply.getForumId());
					sb.append("&better=");
					sb.append(ParamUtils.getStringParameter(request, "better","0"));
					sb.append("&labelId=");
					sb.append(ParamUtils.getStringParameter(request, "labelId","0"));
					sb.append("&fpage=");
					sb.append(ParamUtils.getStringParameter(request, "fpage","1"));
					sb.append("&topicId=");
					sb.append(reply.getTopicId());
					sb.append("&replyId=");
					sb.append(reply.getReplyId());
					sb.append("&act=");
					sb.append(ParamUtils.getStringParameter(request,"act"));
					request.setAttribute("msg","success");
					HttpServletUtils.forward(request,response,sb.toString());
			}
		} catch (ClubException e) {
			logger.error(e.toString());
			request.setAttribute("message",e.getMessage());
			this.errorForward(request,response,topicPostForm);
		}
	}
	
	private void errorForward(HttpServletRequest request,HttpServletResponse response, TopicPostForm topicPostForm){
		request.setAttribute("TopicPostForm",topicPostForm);
		HttpServletUtils.forward(request,response,"/club/post.jsp");
	}
	
	public UserSettingsService getUserSettingscService() {
		return ServiceWrapper.<UserSettingsService>getSingletonInstance(ServiceLocator.USERSETTINGS);
	}

	public ReplyService getReplyService() {
		return ServiceWrapper.<ReplyService>getSingletonInstance(ServiceLocator.REPLY);
	}	
}

⌨️ 快捷键说明

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