reportformcontroller.java
来自「Java的框架」· Java 代码 · 共 542 行 · 第 1/2 页
JAVA
542 行
StringBuffer sb = new StringBuffer();
sb.append(this.reportDir).append(File.separator)
.append(report.getName()).append(File.separator)
.append(NameConstants.SUBREPORT_DIR).append(File.separator)
.append(subfile.getName());
subreport.setSourceFile(sb.toString());
Log.info("New uploaded sub report file : " + subreport.getSourceFile());
}else{
Log.info("No uploaded sub report file found");
String existingFileName = request.getParameter(subreport.getName()+ "_old");
if (existingFileName != null){
subreport.setSourceFile(existingFileName);
Log.info("User existing sub report file : " + subreport.getSourceFile());
}
}
}
Log.info("Add sub report entry : " + subreport.getName() + ";" + subreport.getParentName() + ";" + subreport.getSourceFile());
report.addSubReport(subreport);
}
}else{
report.setEnabled(true);
}
//process roles
String[] roles = request.getParameterValues ("selectedRoles");
if (roles != null) {
Log.info("Assign Report Roles : " + roles.toString());
// for some reason, Spring seems to hang on to the roles in
// the User object, even though isSessionForm() == false
report.getRoles ().clear ();
for (int i = 0; i < roles.length; i++) {
String roleName = roles[i];
report.addRole (this.getRoleManager().getRole (roleName));
}
}
else {
report.getRoles ().clear ();
}
//try saving the report information into database.
try {
if (StringUtils.equals (request.getParameter ("method"), "edit")){
report.setEnabled(true);
}
this.getReportManager ().saveReport (report);
}
catch (ReportException e) {
errors.rejectValue ("name", "errors.existing.report", new Object[] {
report.getName()}, "duplicate report");
return showForm (request, response, errors);
}
if (StringUtils.equals (request.getParameter ("method"), "add")) {
saveMessage (request, getText ("report.saved", report.getName(), locale));
}else{
saveMessage (request, getText ("report.updated", report.getName(), locale));
}
return new ModelAndView(new RedirectView("reportForm.action?method=edit&reportName=" + report.getName()));
}
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.AbstractFormController#showForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.validation.BindException)
* Calling in case of validation errors, to show the form view again.
*/
protected ModelAndView showForm (HttpServletRequest request,
HttpServletResponse response, BindException errors) throws Exception {
// prevent ordinary users from calling a GET on reportForm.html
// unless a bind error exists.
if ((request.getRequestURI ().indexOf ("reportForm") > -1)
&& (!request.isUserInRole(mcap.core.user.util.NameConstants.ADMIN_ROLE)
&& (errors.getErrorCount () == 0) &&
(request.getRemoteUser () != null))) {
response.sendError (HttpServletResponse.SC_FORBIDDEN);
return null;
}
return super.showForm (request, response, errors);
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
* Retrieve a backing object for the current form from the given request.
*/
protected Object formBackingObject (HttpServletRequest request)
throws Exception {
Report report = null;
if (StringUtils.equals (request.getParameter("method"), "add")) {
report = new Report ();
}else {
report = this.getReportManager ().getReport (RequestUtils.getStringParameter(request, "reportName"));
}
return report;
}
/**
* Process the report file being uploaded
* @param request HTTP request object
* @param response HTTP response object
* @param command command object
* @param errors errors object
* @return the uploaded report file
* @throws Exception
*/
private File processReport (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Report report = (Report)command;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile commonFile = (CommonsMultipartFile) multipartRequest
.getFile ("reportFile");
Log.info("Is Null Uploaded File : " + (commonFile == null));
if (commonFile != null) {
if (commonFile.getOriginalFilename().length () > 0) {
String oriFileName = commonFile.getOriginalFilename ();
String fullUploadPath = new StringBuffer()
.append(fullReportDir).append(File.separator)
.append(report.getName()).toString();
File dir = new File(fullUploadPath);
if (!dir.exists()){
Log.info("Dir not exists (makeDir) : " + fullUploadPath);
dir.mkdirs();
}else{
Log.info("Dir not exists (deleteDir) : " + fullUploadPath);
FileUtil.deleteDir(dir,false);
}
String fileName = new StringBuffer()
.append(fullUploadPath).append(File.separator)
.append(oriFileName).toString();
File reportFile = new File(fileName);
if (!reportFile.exists ()) {
try {
InputStream stream = commonFile.getInputStream ();
FileOutputStream bos = new FileOutputStream (reportFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read (buffer, 0, 8192)) != -1) {
bos.write (buffer, 0, bytesRead);
}
bos.close ();
stream.close ();
}
catch (Exception e) {
errors.rejectValue ("sourceFile", "errors.upload.fail",
new Object[] { oriFileName },
"Error while uploading report template.");
}
String jasperFileName = reportFile.getPath().substring(0,reportFile.getPath().lastIndexOf(".")) + ".jasper";
File jasperFile = new File(jasperFileName);
// If compiled file is not found, then compile XML template
if (jasperFile.exists())jasperFile.delete();
JasperCompileManager.compileReportToFile(reportFile.getPath(),jasperFileName);
return reportFile;
}
else {
errors.rejectValue ("sourceFile", "errors.filename.exists",
new Object[] { oriFileName },
"A file with the same name already exists.");
}
}
}
return null;
}
/**
* Process a particular sub report file being uploaded accroding to the
* sub report object parameter
* @param subreport the sub report object that contains the sub report information
* @param request HTTP request object
* @param response HTTP response object
* @param command command object
* @param errors errors object
* @return the uploaded sub report file
* @throws Exception
*/
private File processSubReport (SubReport subreport,HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Report report = (Report)command;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile commonFile = (CommonsMultipartFile) multipartRequest
.getFile (subreport.getName() + "_file");
Log.info("Is Null Uploaded File : " + (commonFile == null));
if (commonFile != null) {
if (commonFile.getOriginalFilename().length () > 0) {
String oriFileName = commonFile.getOriginalFilename ();
String fullUploadPath = new StringBuffer()
.append(fullReportDir).append(File.separator)
.append(report.getName()).append(File.separator)
.append(NameConstants.SUBREPORT_DIR).toString();
File dir = new File(fullUploadPath);
if (!dir.exists()){
dir.mkdirs();
} else{
String oldFileName = request.getParameter(subreport.getName()+ "_old");
Log.info("Existing sub report file : " + oldFileName);
if (oldFileName != null){
if (oldFileName.startsWith("/")){
File f = new File(getServletContext().getRealPath(oldFileName));
if (f.exists()) {
f.delete();
}
String jasperFileName = oldFileName.substring(0,oldFileName.lastIndexOf(".")) + ".jasper";
f = new File(getServletContext().getRealPath(jasperFileName));
if (f.exists()) {
f.delete();
}
}
}
}
String fileName = new StringBuffer().append(fullUploadPath).append(File.separator).append(oriFileName).toString();
Log.info("New uploaded sub report file : " + fileName);
File reportFile = new File(fileName);
if (!reportFile.exists ()) {
try {
InputStream stream = commonFile.getInputStream ();
FileOutputStream bos = new FileOutputStream (reportFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read (buffer, 0, 8192)) != -1) {
bos.write (buffer, 0, bytesRead);
}
bos.close ();
stream.close ();
}
catch (Exception e) {
errors.reject ("errors.upload.fail",
new Object[] { oriFileName },
"Error while uploading report template.");
}
String jasperFileName = reportFile.getPath().substring(0,reportFile.getPath().lastIndexOf(".")) + ".jasper";
File jasperFile = new File(jasperFileName);
// If compiled file is not found, then
// compile XML template
if (jasperFile.exists())jasperFile.delete();
JasperCompileManager.compileReportToFile(reportFile.getPath(),jasperFileName);
return reportFile;
}
else {
errors.reject ("errors.filename.exists",
new Object[] { oriFileName },
"A file with the same name already exists.");
}
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?