Demo entry 5958965

java

   

Submitted by anonymous on Sep 09, 2016 at 08:14
Language: Java. Code size: 10.5 kB.

package cn.qtone.faultreport.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.List;

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

import org.apache.commons.codec.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.qtone.faultreport.bean.FaultReportFile;
import cn.qtone.faultreport.bean.FaultReportRecord;
import cn.qtone.faultreport.bean.FaultReportRecordQueryParams;
import cn.qtone.faultreport.bean.FaultReportStatus;
import cn.qtone.faultreport.dao.FaultCategoryDao;
import cn.qtone.faultreport.dao.FaultHandlerDao;
import cn.qtone.faultreport.dao.FaultLocationDao;
import cn.qtone.faultreport.dao.FaultReportFileDao;
import cn.qtone.faultreport.dao.FaultReportRecordDao;
import cn.qtone.html5.util.JsonUtil;
import cn.qtone.purview.PermissionUtil;
import qt.base.action.ActionBase;
import qt.utils.template.Page;

@Component
@Scope("prototype")
public class FaultReportRecordAction extends ActionBase {
	private static final long serialVersionUID = -6688957310692331060L;
	private FaultReportRecord faultReportRecord;
	private FaultReportRecordQueryParams faultReportRecordQueryParams;

	@Resource
	private FaultReportRecordDao faultReportRecordDao;
	@Resource
	private FaultReportFileDao faultReportFileDao;
	@Resource
	private FaultCategoryDao faultCategoryDao;
	@Resource
	private FaultLocationDao faultLocationDao;
	@Resource
	private FaultHandlerDao faultHandlerDao;

	public FaultReportRecord getFaultReportRecord() {
		return faultReportRecord;
	}

	public void setFaultReportRecord(FaultReportRecord faultReportRecord) {
		this.faultReportRecord = faultReportRecord;
	}

	public FaultReportRecordQueryParams getFaultReportRecordQueryParams() {
		return faultReportRecordQueryParams;
	}

	public void setFaultReportRecordQueryParams(FaultReportRecordQueryParams faultReportRecordQueryParams) {
		this.faultReportRecordQueryParams = faultReportRecordQueryParams;
	}

	private void decodeFaultReportRecord(FaultReportRecord faultReportRecord) {
		if (faultReportRecord == null) {
			return;
		}
		try {
			if (faultReportRecord.getContent() != null) {
				faultReportRecord.setContent(URLDecoder.decode(faultReportRecord.getContent(), "UTF-8"));
			}
			if (faultReportRecord.getFaultReportFileList() != null && faultReportRecord.getFaultReportFileList().size() > 0) {
				for (FaultReportFile faultReportFile : faultReportRecord.getFaultReportFileList()) {
					if (faultReportFile.getName() != null) {
						faultReportFile.setName(URLDecoder.decode(faultReportFile.getName(), "UTF-8"));
					}
				}
			}
		} catch (UnsupportedEncodingException e) {
		}
	}

	public void editFaultReportRecord() {
		Page page = new Page();
		page.setTemplate("/templates/faultReport/faultReportRecordEdit.html");
		Long id = NumberUtils.createLong(getRequest().getParameter("id"));
		FaultReportRecord faultReportRecord = faultReportRecordDao.findById(id);
		page.put("faultCategoryList", faultCategoryDao.findAll());
		page.put("faultLocationList", faultLocationDao.findAll());
		page.put("faultReportRecord", faultReportRecord == null ? new FaultReportRecord() : faultReportRecord);
		page.put("action", faultReportRecord == null ? "add" : "edit");
		renderView(page);
	}

	public void saveFaultReportRecord() {
		if (faultReportRecord != null) {
			decodeFaultReportRecord(faultReportRecord);
			String action = getRequest().getParameter("action");
			if ("add".equalsIgnoreCase(action)) {
				faultReportRecord.setStatus(FaultReportStatus.未处理.getValue());
				faultReportRecord.setCreatorUserId(Long.valueOf(getCurrentUser().getId()));
				faultReportRecord.setCreateTime(new Date());
			} else {
				faultReportRecord.setLastModifyTime(new Date());
			}
			JsonUtil jsonUtil = new JsonUtil();
			jsonUtil.setSuccess(faultReportRecordDao.save(faultReportRecord, getRequest()));
			faultReportRecord = null;
			renderView(jsonUtil);
		}
	}

	public void deleteFaultReportRecord() {
		Long id = NumberUtils.createLong(getRequest().getParameter("id"));
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.setSuccess(faultReportRecordDao.deleteById(id, getRequest()));
		renderView(jsonUtil);
	}

	public void faultReportRecordDetail() {
		Page page = new Page();
		page.setTemplate("/templates/faultReport/faultReportRecordDetail.html");
		Long id = NumberUtils.createLong(getRequest().getParameter("id"));
		FaultReportRecord faultReportRecord = faultReportRecordDao.findById(id);
		if (faultReportRecord != null) {
			faultReportRecord.setChildren(faultReportRecordDao.findChildren(faultReportRecord.getId(), null));
			Collections.reverse(faultReportRecord.getChildren());

			for (FaultReportRecord f : faultReportRecord.getChildren()) {
				f.setFaultReportFileList(faultReportFileDao.findByFaultReportRecordId(f.getId()));
			}
		} else {
			faultReportRecord = new FaultReportRecord();
		}
		page.put("faultReportRecord", faultReportRecord);
		page.put("faultReportRecordList", faultReportRecord.getChildren());
		renderView(page);
	}

	/**
	 * 待处理的记录数
	 */
	public void getUnHandledFaultReportRecordCount() {
		FaultReportRecordQueryParams faultReportRecordQueryParams = new FaultReportRecordQueryParams();
		faultReportRecordQueryParams.setOnlyCanHandle(true);
		faultReportRecordQueryParams.setCurrentUser(getCurrentUser());
		faultReportRecordQueryParams.setPageIndex(null);
		List<FaultReportRecord> list = faultReportRecordDao.findAllForPage(faultReportRecordQueryParams, null, getRequest());

		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.setSuccess(true);
		jsonUtil.setProperty("count", list.size());
		renderView(jsonUtil);
	}

	public void handleFaultReportRecordPage() {
		Page page = new Page();
		page.setTemplate("/templates/faultReport/handleFaultReportRecord.html");
		Long id = NumberUtils.createLong(getRequest().getParameter("id"));
		FaultReportRecord faultReportRecord = faultReportRecordDao.findById(id);
		page.put("faultReportStatusList", FaultReportStatus.values());
		page.put("faultReportRecord", faultReportRecord == null ? new FaultReportRecord() : faultReportRecord);
		renderView(page);
	}

	public void handleFaultReportRecord() {
		if (faultReportRecord != null) {
			decodeFaultReportRecord(faultReportRecord);
			faultReportRecord.setCreatorUserId(Long.valueOf(getCurrentUser().getId()));
			faultReportRecord.setCreateTime(new Date());
			JsonUtil jsonUtil = new JsonUtil();
			jsonUtil.setSuccess(faultReportRecordDao.handle(faultReportRecord));
			faultReportRecord = null;
			renderView(jsonUtil);
		}
	}

	/**
	 * 报障记录
	 */
	public void faultReportRecordIndex() {
		Page page = new Page();
		String listType = getRequest().getParameter("listType");
		if (StringUtils.isBlank(listType)) {
			listType = "all";
		}
		page.put("listType", listType);
		page.setTemplate("/templates/faultReport/faultReportRecordIndex.html");
		page.put("faultCategoryList", faultCategoryDao.findAll());
		page.put("faultLocationList", faultLocationDao.findAll());
		page.put("faultReportStatusList", FaultReportStatus.values());
		page.put("faultHandlerList", faultHandlerDao.findAllHandler());
		renderView(page);
	}

	/**
	 * 报障记录列表
	 */
	public void faultReportRecordList() {
		if (faultReportRecordQueryParams != null) {
			String listType = getRequest().getParameter("listType");
			if ("my".equalsIgnoreCase(listType)) {
				faultReportRecordQueryParams.setReportUserId(Long.valueOf(getCurrentUser().getId()));
			} else if ("handle".equalsIgnoreCase(listType)) {
				faultReportRecordQueryParams.setOnlyCanHandle(true);
			}
			faultReportRecordQueryParams.setCurrentUser(getCurrentUser());
			Page page = new Page();
			page.setTemplate("/templates/faultReport/faultReportRecordList.html");
			page.put("currentUser", getCurrentUser());
			page.put("faultReportRecordList", faultReportRecordDao.findAllForPage(faultReportRecordQueryParams, page, getRequest()));
			renderView(page);
		}
	}

	/**
	 * 下载文件Header 
	 */
	private void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
	        String fileName) {
		// 中文文件名支持
		String encodedfileName = null;
		// 替换空格,否则firefox下有空格文件名会被截断,其他浏览器会将空格替换成+号
		encodedfileName = fileName.trim().replaceAll(" ", "_");
		String agent = request.getHeader("User-Agent");
		boolean isMSIE = (agent != null
		        && (agent.toUpperCase().indexOf("MSIE") != -1 || agent.toUpperCase().indexOf("EDGE") != -1
		                || agent.toUpperCase().indexOf("TRIDENT") != -1));
		if (isMSIE) {
			try {
				encodedfileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				encodedfileName = "";
				e.printStackTrace();
			}
		} else {
			encodedfileName = new String(fileName.getBytes(), Charsets.ISO_8859_1);
		}
		response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
	}

	/**
	 * 下载文件
	 */
	public void downloadFaultReportFile() {
		HttpServletRequest request = getRequest();
		HttpServletResponse response = getResponse();
		try {
			String httpPath = request.getParameter("httpPath");
			String fileName = request.getParameter("fileName");
			if (StringUtils.isNotBlank(httpPath) && StringUtils.isNotBlank(fileName)) {
				String savePath = request.getSession().getServletContext().getRealPath(httpPath);
				File downloadFile = new File(savePath);
				if (downloadFile.exists()) {
					fileName = URLDecoder.decode(fileName, "UTF-8");
					setFileDownloadHeader(request, response, fileName);
					FileUtils.copyFile(downloadFile, response.getOutputStream());
					response.getOutputStream().close();
				} else {
					Page page = new Page();
					page.put("Msg", "服务器上的文件已经被删除,请联系管理员!");
					page.put("URL", PermissionUtil.returnForwardURL("/faultReportRecordIndex.crm"));
					page.setTemplate("/templates/error/alarm.html");
					renderView(page);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

This snippet took 0.05 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).