userreportformcontroller.java
来自「Java的框架」· Java 代码 · 共 470 行 · 第 1/2 页
JAVA
470 行
package mcaps.core.reporting.webapp.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.logging.Log;
import mcap.core.reporting.model.Report;
import mcap.core.reporting.model.ReportParameter;
import mcap.core.reporting.model.SubReport;
import mcap.core.reporting.service.ReportManager;
import mcap.core.reporting.util.JasperReportDesignUtil;
import mcap.core.reporting.util.NameConstants;
import mcaps.core.reporting.webapp.view.JasperReportEnhancedMultiFormatView;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException;
import org.springframework.validation.BindException;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
/**
* Implementation of BaseFormController that handle request to
* generate dynamic report. The request should contain information
* pertaining to the report as well as the data for each report
* parameter(s) in order for the report to be generated.
*
* This controller class does not provide any input validation but
* accept the inputs which has been verified by UserReportValidateFormController
* that are then redirected to a web page before submited to this controller.
*
* The separation of input validation is to facilitate the display of error
* messages on the same web page and display of the report after the validation
* in a new web page.
*
* @author jov
* @date May 18, 2006
* @version 1.0.2.0
*/
public class UserReportFormController extends BaseFormController {
private ReportManager reportManager;
/**
* 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;
}
//===========================================================================================================
//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){
Log.warn("Must set reportManager bean property on " + getClass());
throw new ApplicationContextException(
"Must set reportManager bean property on " + getClass());
}
}
/* (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 (request.getParameter("cancel") != null) {
if (request.getParameter("from") != null) {
if (request.getParameter("from").equals("reportlist")){
return new ModelAndView(new RedirectView(NameConstants.REPORTS_VIEW_URL));
}
}
return new ModelAndView(getSuccessView());
}
//Validation of parameter values against the parameter types
//Is written here instead of onBind() due to the need to redirect to
//the new view with .action extension because return super.processFormSubmission()
//will return based on url with extension .pdf, .html or .xls which will not be
//processed by site mesh
Enumeration paramNames = request.getParameterNames();
StringBuffer msgBuffer = null;
StringBuffer paramValueBuffer = new StringBuffer();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
if ((name.startsWith("_")) && (!name.endsWith("_valueClassName"))) {
String paramName = name.substring(1);
String value = request.getParameter(name);
String valueType = request.getParameter(name + "_valueClassName");
//Save the value of the parameter name and parameter value to be appended
//to the redirect url as query string. The value will be retrieved in
//formBackingObject()
paramValueBuffer.append("&").append(name).append("=").append(value);
//Perform field validation
String msg = validateParameter(value, valueType);
//If message is not null, validation error is found.
//Redirect to the origin page where parameter is submitted.
if (msg != null) {
if (msgBuffer == null){
msgBuffer = new StringBuffer();
msgBuffer.append(getText(msg,
new Object[] { paramName },
request.getLocale()));
}else{
msgBuffer.append("<br>").append(getText(msg,
new Object[] { paramName },
request.getLocale()));
}
}
}
}
if (msgBuffer != null){
String reportName = request.getParameter("reportName");
saveError(request, msgBuffer.toString());
return new ModelAndView(new RedirectView(
new StringBuffer()
.append("userReportForm.action?reportName=")
.append(reportName)
.append(paramValueBuffer.toString()).toString()));
}
return super.processFormSubmission(request, response, command, errors);
}
/* (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;
String format = request.getParameter("format");
Log.info("Report format : " + format);
Map model = new HashMap ();
model.put ("format", format);
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
if ((paramName.startsWith("_")) && (!paramName.endsWith("_valueClassName"))){
String value = request.getParameter(paramName);
String name = paramName.substring(1);
Object obj = null;
if (value != null){
if (value.length() > 0){
obj = getValue(value,request.getParameter(paramName + "_valueClassName"));
}
}
Log.info("Report parameter : " + name + " (" + obj + ")");
Log.debug("Report parameter : " + name + " (" + obj + ")");
model.put (name, obj);
}
}
ServletContext servletContext = getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext (servletContext);
DataSource ds = (DataSource) ctx.getBean("dataSource");
// DataSource ds = (DataSource)jndiTemplate.lookup("java:comp/env/jdbc/" + report.getJndiDataSourceName());
JasperReportEnhancedMultiFormatView view = new JasperReportEnhancedMultiFormatView();
String binaryFile = report.getSourceFile().substring(0,report.getSourceFile().lastIndexOf("."));
binaryFile += ".jasper";
view.setUrl(binaryFile);
Log.debug("Report url : " + binaryFile);
Report detailedReport = this.getReportManager().getReport(report.getName());
//set sub report urls
Properties props = new Properties();
for (int i=0; i < detailedReport.getSubReports().size(); i++){
SubReport subReport = (SubReport)detailedReport.getSubReports().get(i);
if ((subReport.getSourceFile() != null) || (subReport.getSourceFile().length() > 0)){
props.setProperty(subReport.getName(),subReport.getSourceFile());
Log.debug("Report sub report url : " + subReport.getName() + " (" + subReport.getSourceFile() + ")");
}
}
if (props.size() > 0) view.setSubReportUrls(props);
view.setJdbcDataSource(ds);
view.setApplicationContext(this.getApplicationContext());
view.setContentType(getContentType(format));
if (format.equals("html")){
Map imagesMap = new HashMap();
request.getSession().setAttribute("IMAGES_MAP", imagesMap);
Map paramMap = new HashMap();
paramMap.put(JRHtmlExporterParameter.IMAGES_MAP,imagesMap);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?