reportformcontroller.java
来自「Java的框架」· Java 代码 · 共 542 行 · 第 1/2 页
JAVA
542 行
package mcaps.core.reporting.webapp.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.logging.Log;
import mcap.core.reporting.model.Report;
import mcap.core.reporting.model.SubReport;
import mcap.core.reporting.service.ReportException;
import mcap.core.reporting.service.ReportManager;
import mcap.core.reporting.util.JasperReportDesignUtil;
import mcap.core.reporting.util.NameConstants;
import mcap.core.user.service.RoleManager;
import mcap.core.util.FileUtil;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.design.JasperDesign;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
/**
* @deprecated The class is no longer be used. It is replaced by the
* mcaps.core.reporting.webapp.controller.ReportWizardController to
* provide better support for advanced report template editing.
*
* Implementation of BaseFormController that interacts with the
* ReportManager to handle request to query database and generate
* dynamic report.
*
* @author jov
* @date Jan 12, 2006
* @version 1.0.1.0
*/
public class ReportFormController extends BaseFormController implements InitializingBean{
private ReportManager reportManager;
private RoleManager roleManager;
private String reportDir;
private String fullReportDir;
/**
* Returns the reportDir.
* @return String
*/
public String getReportDir () {
return reportDir;
}
/**
* Sets the reportDir.
* @param reportDir The reportDir to set.
*/
public void setReportDir (String reportDir) {
this.reportDir = reportDir;
}
/**
* Returns the reportManager.
* @return ReportManager
*/
public ReportManager getReportManager () {
return reportManager;
}
/**
* Sets the reportManager.
* @param reportManager The reportManager to set.
*/
public void setReportManager (ReportManager reportManager) {
this.reportManager = reportManager;
}
/**
* Returns the roleManager.
* @return RoleManager
*/
public RoleManager getRoleManager () {
return roleManager;
}
/**
* Sets the roleManager.
* @param roleManager The roleManager to set.
*/
public void setRoleManager (RoleManager roleManager) {
this.roleManager = roleManager;
}
//===========================================================================================================
//INITIALIZING BEAN IMPLEMENTATION
//===========================================================================================================
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
* Invoked by a BeanFactory after it has set all bean properties supplied. This allows
* the bean instance to perform initialization only possible when all bean properties
* have been set and to throw an exception in the event of misconfiguration.
*/
public void afterPropertiesSet() throws Exception {
if (reportManager == null)
throw new ApplicationContextException(
"Must set reportManager bean property on " + getClass());
if (roleManager == null)
throw new ApplicationContextException(
"Must set roleManager bean property on " + getClass());
System.setProperty(
"jasper.reports.compile.class.path",
this.getServletContext().getRealPath("/WEB-INF/lib/jasperreports-0.6.8.jar")
+ System.getProperty("path.separator")
+ this.getServletContext().getRealPath("/WEB-INF/classes/"));
String rootDir = getServletContext().getRealPath("/");
if (this.reportManager.getRootDir() == null)
this.reportManager.setRootDir(rootDir);
fullReportDir = getServletContext().getRealPath(reportDir);
File file = new File(fullReportDir);
if (!file.exists()){
file.mkdir();
}
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
* Checks for an action that should be executed without respect to binding errors, like a cancel action.
*/
public ModelAndView processFormSubmission (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
// if the request is "Cancel"
if (request.getParameter ("cancel") != null) {
if (!StringUtils.equals (request.getParameter ("from"), "list")) {
return new ModelAndView (getCancelView ());
}
else {
return new ModelAndView (getSuccessView ());
}
}
return super.processFormSubmission (request, response, command, errors);
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.BaseCommandController#onBind(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
*/
protected void onBind(HttpServletRequest request, Object command,
BindException errors) {
Report report = (Report)command;
//Checks whether or not file already exists and
//whether the report template is valid
if (StringUtils.equals (request.getParameter ("method"), "add")) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile commonFile = (CommonsMultipartFile)multipartRequest.getFile("reportFile");
String oriFileName = commonFile.getOriginalFilename();
String fileName = new StringBuffer()
.append(fullReportDir).append(File.separator)
.append(report.getName()).append(File.separator)
.append(oriFileName).toString();
File reportFile = new File(fileName);
if (reportFile.exists ()) {
errors.rejectValue ("sourceFile", "errors.filename.exists",
new Object[] { oriFileName },
"A file with the same name already exists.");
}else{
try {
JasperDesign design = JasperReportDesignUtil.instance.getJasperDesign(commonFile.getInputStream());
if (!JasperReportDesignUtil.instance.isValidReportDesign(design)){
errors.rejectValue("report.sourceFile","errors.invalid.report",new Object[]{oriFileName},"Invalid report template.");
}
}
catch (JRException e) {
errors.rejectValue("report.sourceFile","errors.loadreport.fail",new Object[]{oriFileName},"Error while loading report template.");
}
catch (IOException e) {
errors.rejectValue("report.sourceFile","errors.upload.fail",new Object[]{oriFileName},"Error while uploading report template.");
}
}
}
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
* Next to be called if processFormSubmission method called the
* super.processFormSubmission
*/
public ModelAndView onSubmit (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Report report = (Report) command;
Locale locale = request.getLocale ();
if (request.getParameter ("delete") != null) {
//Delete report and subreport(s) from file system
File reportDir = new File(this.getServletContext()
.getRealPath(new StringBuffer().append(this.reportDir)
.append(File.separator).append(report.getName()).toString()));
if (reportDir.exists()){
FileUtil.deleteDir(reportDir,true);
}
this.getReportManager ().removeReport (report.getName());
saveMessage (request, getText ("report.deleted", report.getName(),
locale));
return new ModelAndView (getSuccessView ());
}
else {
report.getSubReports().clear();
//Process report
Log.info("Process Report");
File file = processReport(request,response,command,errors);
if (file != null){
Log.info("New uploaded file found.");
report.setSourceFile(new StringBuffer().append(this.reportDir).append(File.separator)
.append(report.getName()).append(File.separator)
.append(file.getName()).toString());
Log.info("New uploaded file : " + report.getSourceFile());
}else{
Log.info("No uploaded file found");
if (report.getSourceFile() != null){
String fileName = getServletContext().getRealPath(report.getSourceFile());
file = new File(fileName);
}
Log.info("Create file object from existing file");
}
//Process sub report - Check if any subreport exists
Log.info("Process sub report");
List subReportParam = this.getReportManager().getSubReportParam(file);
Log.info("No. of sub reports found : " + subReportParam.size());
if (subReportParam.size() > 0){
Log.info("Create sub report entry");
for(int i=0; i < subReportParam.size(); i++){
SubReport subreport = new SubReport(report.getName(),(String)subReportParam.get(i));
if (StringUtils.equals (request.getParameter ("method"), "edit")) {
Log.info("Process Sub Report");
File subfile = processSubReport(subreport,request,response,command,errors);
if (subfile != null){
Log.info("New uploaded sub report file found");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?