topicaction.java

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

JAVA
434
字号
/* 
 * Created on 2007-3-17
 * Last modified on 2007-12-12
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.controller.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.topic.ReplyService;
import com.yeqiangwei.club.service.topic.TopicService;
import com.yeqiangwei.club.model.ManageLog;
import com.yeqiangwei.club.model.Reply;
import com.yeqiangwei.club.model.TContent;
import com.yeqiangwei.club.model.Topic;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.service.user.RuleService;
import com.yeqiangwei.club.service.user.UserLogin;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.service.user.UserSettingsService;
import com.yeqiangwei.club.service.util.BasicInfoService;
import com.yeqiangwei.club.service.util.ManageLogService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.TopicBetterForm;
import com.yeqiangwei.club.controller.form.TopicMoveForm;
import com.yeqiangwei.club.controller.form.TopicPostForm;
import com.yeqiangwei.club.controller.form.TopicTrashForm;
import com.yeqiangwei.club.controller.form.build.TopicBetterBuild;
import com.yeqiangwei.club.controller.form.build.TopicMoveBuild;
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.EnCoder;
import com.yeqiangwei.club.view.util.UrlUtils;
import com.yeqiangwei.io.File;
import com.yeqiangwei.net.URL;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.HttpServletUtils;
import com.yeqiangwei.util.ParamUtils;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.Validator;

public class TopicAction {
	
	private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(TopicAction.class);
	
	public void txtdownload(HttpServletRequest request,HttpServletResponse response) throws IOException{
		boolean permission = true;
		if(UserLogin.getUserId(request)<=0){
			request.setAttribute("message", MessageUtils.getMessage("error_notlogin"));
			HttpServletUtils.forward(request, response, "msg.jsp");
			return ;
		}
		int topicId = ParamUtils.getIntParameter(request, "topicId", 0);
		int part = ParamUtils.getIntParameter(request, "part", 0);
		Topic topic = this.getTopicService().findTopicAndContentById(topicId);
		if(Validator.isEmpty(topic)){
			request.setAttribute("message", MessageUtils.getMessage("error_notfind_topic"));
			HttpServletUtils.forward(request, response, "msg.jsp");
			return ;
		}else{
			topic.setUser(this.getUserService().findById(topic.getUserId()));
		}
		if(Validator.isEmpty(topic.getUser())){
			request.setAttribute("message", MessageUtils.getMessage("error_notfind_user"));
			HttpServletUtils.forward(request, response, "msg.jsp");
			return ;
		}
		double score = this.getRuleService().getElement(topic.getForumId(),RuleService.SCORE,34);
		double money = this.getRuleService().getElement(topic.getForumId(),RuleService.MONEY,34);
		double credit = this.getRuleService().getElement(topic.getForumId(),RuleService.CREDIT,34);
		int views = (int) this.getRuleService().getElement(topic.getForumId(), RuleService.VIEWS,34);
		if(UserLogin.getUser(request).getScore()+score<0){
			permission = false;
			request.setAttribute("message", MessageUtils.getMessage("error_power_score"));
		}
		if(UserLogin.getUser(request).getMoney()+money<0){
			permission = false;
			request.setAttribute("message", MessageUtils.getMessage("error_power_money"));
		}
		if(UserLogin.getUser(request).getCredit()+credit<0){
			permission = false;
			request.setAttribute("message", MessageUtils.getMessage("error_power_credit"));
		}
		if(UserLogin.getUser(request).getViews()+views<0){
			permission = false;
			request.setAttribute("message", MessageUtils.getMessage("error_power_views"));
		}
		if(!permission){
			HttpServletUtils.forward(request, response, "msg.jsp");
			return ;
		}
		TopicParameter param = new TopicParameter();
		param.setPage(1);
		param.setRows(10000);
		param.setTopicId(topicId);
		switch(part){
			case 1:
				param.setUserId(topic.getUserId());
			break;
		}
		List<Reply> list = this.getReplyService().findReplyAndContent(param);
		String txttemplate = null;
		try {
			txttemplate=File.readTxt(this.getBasicInfoService().findOnly().getSitePath()
					+"WEB-INF"+File.separator+"classes"+File.separator+"template.txt", "utf-8");
		} catch (IOException e) {
			logger.error(e.toString());		
		}
		if(Validator.isEmpty(txttemplate)){
			request.setAttribute("message", MessageUtils.getMessage("error_system"));
			HttpServletUtils.forward(request, response, "msg.jsp");
			return ;
		}else{
			txttemplate = StringHelper.replace(txttemplate, "\r", "");
			txttemplate = StringHelper.replace(txttemplate, "\n", "\r\n");
		}
		topic.getTContent().setContent(EnCoder.txtEncoder(topic.getTContent().getContent()));
		topic.getTContent().setContent(StringHelper.replace(topic.getTContent().getContent()
				, "<url>"
				, "(媒体文件请查看原帖:"+UrlUtils.getUrl(UrlUtils.TOPIC, topicId, topic.getForumId(), request)+")"));
		long lenth = 0;
		String tmp = StringHelper.replace(txttemplate, "{title}", topic.getTitle());
		tmp = StringHelper.replace(tmp, "{content}", topic.getTContent().getContent());
		tmp = StringHelper.replace(tmp, "{url}", UrlUtils.getUrl(UrlUtils.TOPIC, topicId, topic.getForumId(), request));
		tmp = StringHelper.replace(tmp, "{userName}", topic.getUserName());
		tmp = StringHelper.replace(tmp, "{userUrl}", UrlUtils.getUrl(UrlUtils.USER, topic.getUserId(), 0, request));
		tmp = StringHelper.replace(tmp, "{datetime}", String.valueOf(topic.getCreateDateTime()));
		tmp = StringHelper.replace(tmp, "{storey}", "楼主");
		lenth = topic.getContentLength();
		StringBuffer sb = new StringBuffer();
		sb.append(tmp);
		int storey = 0;
		for(Reply reply : list){
			storey++;
			lenth += reply.getContentLength();
			reply.getRContent().setContent(EnCoder.txtEncoder(reply.getRContent().getContent()));
			reply.getRContent().setContent(StringHelper.replace(reply.getRContent().getContent()
					, "<url>"
					, "(媒体文件请查看原帖:"+UrlUtils.getUrl(UrlUtils.TOPIC, reply.getTopicId(), reply.getForumId(), request)+")"));
			tmp = StringHelper.replace(txttemplate, "{title}", reply.getTitle());
			tmp = StringHelper.replace(tmp, "{content}", reply.getRContent().getContent());
			tmp = StringHelper.replace(tmp, "{url}", UrlUtils.getUrl(UrlUtils.TOPIC, topicId, topic.getForumId(), request));
			tmp = StringHelper.replace(tmp, "{userName}", reply.getUserName());
			tmp = StringHelper.replace(tmp, "{userUrl}", UrlUtils.getUrl(UrlUtils.USER, reply.getUserId(), 0, request));
			tmp = StringHelper.replace(tmp, "{datetime}", String.valueOf(reply.getCreateDateTime()));
			tmp = StringHelper.replace(tmp, "{storey}", String.valueOf(storey)+"楼");
			sb.append(tmp);
			if(lenth>300000){
				sb.append("\r\n\r\n");
				sb.append("本文可能超过100万字,你需要登录到社区浏览最后面的分页内容:");
				sb.append(UrlUtils.getUrl(UrlUtils.TOPIC, topicId, topic.getForumId(), request));
				break;
			}
		}
		sb.append("\r\n\r\n");
		sb.append("本TXT文档由《");
		sb.append(this.getBasicInfoService().findOnly().getName());
		sb.append("》提供,访问网址:");
		sb.append(this.getBasicInfoService().findOnly().getUrl());
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/octet-stream");//设置为下载
		String filenamedisplay = topic.getTitle();
		if(StringHelper.length(filenamedisplay)>15){
			filenamedisplay = StringHelper.substring(filenamedisplay, 0, 15, filenamedisplay);
		}
		filenamedisplay = URL.urlEncoder(filenamedisplay, "utf-8");
		response.addHeader("Content-Disposition","attachment;filename="+filenamedisplay+".txt");
		PrintWriter out = response.getWriter();
		out.print(sb);		
		out.close();
		try {
			User user = UserLogin.getUser(request);
			this.getUserService().ruleUtils(user, topic.getForumId(), 34);
			this.getUserService().update(user,false);
			User tuser = topic.getUser();
			if(tuser.getUserId()!=user.getUserId()){
				this.getUserService().ruleUtils(tuser, topic.getForumId(), 35);
				this.getUserService().update(tuser,false);
			}
		} catch (ClubException e) {
			logger.error(e.toString());
		}
		ManageLog log = new ManageLog();
		log.setTopicId(topic.getTopicId());
		log.setTitle(topic.getTitle());
		log.setByUserId(UserLogin.getUserId(request));
		log.setByUserName(UserLogin.getUserName(request));
		log.setByUserIp(request.getRemoteAddr());
		log.setCreateDateTime(FormatDateTime.now());
		log.setForumId(topic.getForumId());
		log.setUserName(log.getByUserName());
		log.setUserId(log.getByUserId());
		log.setIsList(true);
		log.setListByUserName(true);
		log.setMemo("下载文章");
		log.setContent(log.getByUserName()+"下载了"+topic.getUserName()+"的文章");
		this.getUserService().ruleUtils(log,topic.getForumId(),34);
		try {
			this.getManageLogService().create(log);
		} catch (ClubException e) {
			logger.error(e.toString());
		}
		if(topic.getUser().getUserId()!=UserLogin.getUserId(request)){
			log = new ManageLog();
			log.setTopicId(topic.getTopicId());
			log.setTitle(topic.getTitle());
			log.setByUserId(UserLogin.getUserId(request));

⌨️ 快捷键说明

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