reportwizardcontroller.java
来自「Java的框架」· Java 代码 · 共 1,066 行 · 第 1/3 页
JAVA
1,066 行
FileUtil.copy(reportSourceFile,newFile);
}
catch (IOException e) {
Log.warn("Error createTempFile. " + e.getMessage());
errors.reject (NameConstants.ERROR_UPLOAD_FAIL,
new Object[] { sourceFileName },
"Error while uploading report template.");
}
//Compile the report template to jasper file
compileReportTemplate(reportName, fullNewTempFileName, errors);
}
}
}
}
/**
* Set the roles to the report object
* @param request The HTTP request object
* @param report The report object
*/
private void handlerReportRoles(HttpServletRequest request, Report report){
String[] roles = request.getParameterValues ("selectedRoles");
report.getRoles ().clear ();
if (roles != null) {
// 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));
}
}
}
/**
* Converts the absolute source file name to a relative path
* @param absoluteSourceFileName
* @return
*/
private String toRelativePath(String absoluteSourceFileName){
if (absoluteSourceFileName == null) throw new NullPointerException("Absolute source file name is null");
//Change all '\' to '/'
absoluteSourceFileName = absoluteSourceFileName.replaceAll("\\\\","/");
String baseDir = this.getBaseDir();
//Spilt the file name into two using the base dir, this will enable the retrieval
//of source file name in the end part of the string array
//Eg: C:\Tomcat\webapp\prrm\WEB-INF\reports\temp\ReportName\report.xml
//Split using BaseDir which is '\WEB-INF\reports'
//After splitting : [0] C:\Tomcat\webapp\prrm [1] \temp\ReportName\report.xml
//Then join back the BaseDir with \temp\ReportName\report.xml
String[] part = absoluteSourceFileName.split(baseDir);
int index = part.length-1;
String relPath = new StringBuffer().append(baseDir).append(part[index]).toString();
Log.debug("toRelativePath : " + relPath);
return relPath;
}
/**
* Construct the absolute path using the relative source file name
* @param relativeSourceFileName The relative source file name
* @return The absolute path of the source file name
*/
private String toAbsolutePath(String relativeSourceFileName){
if (relativeSourceFileName == null) throw new NullPointerException("Relative source file name is null");
String absPath = getServletContext().getRealPath(relativeSourceFileName);
Log.debug("toAbsolutePath : " + absPath);
return absPath;
}
/**
* Gets the a redirect model and view when the cancal button or finish button is clicked.
* @param request The HTTP request object
* @param command The command object
* @return The redirect model and view object
* @throws ScheduleException The schedule exception
*/
private ModelAndView getRedirectView(HttpServletRequest request, Object command){
return new ModelAndView (new RedirectView(NameConstants.REPORTS_VIEW_URL));
}
/**
* Get the report object. This method enable the getReport by handling the
* DataRetrievalFailureException exception.
* @param reportName The name of the report.
* @return The report object.
*/
private Report getReport(String reportName){
Report report = null;
try{
report = this.getReportManager ().getReport(reportName);
}catch (DataRetrievalFailureException e){
//Do nothing; DataRetrievalFailureException is thrown when getReport returns null
}
return report;
}
/**
* Utility method for returning the common multipart file from the HTTP request object
* using the filename identifier.
* @param request The HTTP request object
* @param fileName The identifier to locate the common multipart file from the HTTP request object
* @return The common multi part file
*/
private CommonsMultipartFile getCommonFile(HttpServletRequest request, String fileName){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
return (CommonsMultipartFile)multipartRequest.getFile(fileName);
}
/**
* Construct the report folder path using the combination of full report base directory
* and the report name
* @param reportName The report name
* @return The report folder path
*/
private String getReportFolderPath(String reportName){
return new StringBuffer()
.append(fullBaseDir).append(File.separator)
.append(reportName).toString();
}
/**
* Constructs the temporary folder path which consists of the combination of
* full report base directory path, the temporary folder, temporary id and also the
* report name
* @param request The HttpServletRequest object that is used to get the temporary id
* @param reportName The report name
* @return The temporary folder path
*/
private String getTempFolderPath(HttpServletRequest request, String reportName){
return new StringBuffer()
.append(fullBaseDir).append(File.separator)
.append("temp").append(File.separator)
.append(getTempID(request)).append("_").append(reportName).toString();
}
/**
* Constructs the temporary file name based on the temporray folder path and
* file name
* @param tempFolderPath The temporary folder path
* @param fileName The name of the file
* @return The temporary file name which is the combination of temporary path and file name
*/
private String getTempFileName(String tempFolderPath, String fileName){
return new StringBuffer()
.append(tempFolderPath).append(File.separator)
.append(fileName).toString();
}
/**
* Get the temporary id from the request object
* @param request
* @return
*/
private String getTempID(HttpServletRequest request){
String tempID = request.getParameter(NameConstants.REPORT_TEMPID);
if (tempID == null || tempID.length() == 0){
Log.warn("Null or empty Temporary ID");
}
return tempID;
}
/**
* Set the request attribute.
* @param request The HTTP request object
* @param attributeID The key for the request attribute
*/
private void setRequestAttribute(HttpServletRequest request, String attributeID){
Log.debug("setRequestAttribute");
String attribute = request.getParameter(attributeID);
if (attribute == null || attribute.length() == 0){
if (attributeID.equals(NameConstants.REPORT_TEMPID)){
request.setAttribute(attributeID,new Long(new Date().getTime()));
Log.debug("setRequestAttribute (New Temp ID)");
}
}else{
request.setAttribute(attributeID,attribute);
}
}
/**
* Convert the temporary report file name to the actual report file name.
* This is usually done when the user wants to commit the report to the database.
* @param report The report object
* @param request The HttpServletRequest object
*/
private void setActualReportFileName(Report report, HttpServletRequest request){
String reportSourceFile = report.getSourceFile();
String tempID = getTempID(request);
reportSourceFile = reportSourceFile.replaceAll("\\\\","/")
.replaceFirst(tempID + "_","")
.replaceFirst("/temp","");
report.setSourceFile(reportSourceFile);
}
/**
* Convert the temporary sub report file name to the actual sub report file name.
* This is usually done when the user wants to commit the sub report to the database.
* @param subReport The sub report object
* @param request The HttpServletRequest object
*/
private void setActualSubReportFileName(SubReport subReport, HttpServletRequest request){
String subRptSourceFile = subReport.getSourceFile();
String tempID = getTempID(request);
subRptSourceFile = subRptSourceFile.replaceAll("\\\\","/")
.replaceFirst(tempID + "_","")
.replaceFirst("/temp","");
subReport.setSourceFile(subRptSourceFile);
}
/**
* Creates the temporary file into the temporary folder path using the common file object.
* @param tempFolderPath The temporory folder
* @param commonFile The common file object
* @param errors The binding error object for populating error information to the user, if
* error was found
* @return The file name of the temporary file created
*/
private String createTempFile(String tempFolderPath, CommonsMultipartFile commonFile, BindException errors){
String oriFileName = commonFile.getOriginalFilename();
String fileName = getTempFileName(tempFolderPath,oriFileName);
//Create the temporary report template file
try {
File reportFile = new File(fileName);
writeToFileSystem(commonFile, reportFile);
}catch (Exception e) {
Log.warn("Error createTempFile. " + e.getMessage());
errors.reject (NameConstants.ERROR_UPLOAD_FAIL,
new Object[] { oriFileName },
"Error while uploading report template.");
}
return fileName;
}
/**
* Write the uploaded report template or sub report template file to the filesystem.
* @param commonFile The common file object being uploaded
* @param reportFile The report file object used to write into the filesystem
* @throws Exception
*/
private void writeToFileSystem(CommonsMultipartFile commonFile, File reportFile) throws Exception{
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 ();
}
/**
* Rename the previous report source file property to the new report source file property
* due to the change of report name since that source file is constructed based on report
* name.
* @param report The report object
* @param request The HttpServletRequest object
* @param oldReportName The previous report name
*/
private void renameReportFileName(Report report, HttpServletRequest request, String oldReportName){
String newReportName = report.getName();
String reportSourceFile = report.getSourceFile();
String tempID = getTempID(request);
reportSourceFile = reportSourceFile.replaceAll("\\\\","/")
.replaceFirst(tempID + "_" + oldReportName, tempID + "_" + newReportName);
report.setSourceFile(reportSourceFile);
}
/**
* Deletes the temporary directory
* @param request The HttpServletRequest object
* @param reportName The report name
*/
private void deleteTempDirectory(HttpServletRequest request, String reportName){
File reportDir = new File(getTempFolderPath(request, reportName));
if (reportDir.exists()){
if (!FileUtil.deleteDir(reportDir,true)){
Log.warn("Fail to delete temporary directory");
}
}
}
/**
* Compile the report template (jrxml) to the jasper binary file.
* @param reportName The name of the report
* @param fileName The full path file name of the report template
* @param errors The binding error object
*/
private void compileReportTemplate(String reportName, String fileName, BindException errors){
String jasperFileName = fileName.substring(0,fileName.lastIndexOf(".")) + ".jasper";
File jasperFile = new File(jasperFileName);
if (jasperFile.exists()) jasperFile.delete();
try{
JasperCompileManager.compileReportToFile(fileName,jasperFileName);
}catch (JRException e) {
errors.reject(NameConstants.ERROR_COMPILE_REPORT,new Object[]{reportName},"Error while compiling report template " + reportName );
}
}
/**
* To commit the uploaded report template and sub report templates. This will cause
* the uploaded report template to be moved from the temp folder to the base folder.
* However for edit operation, there might be no files to be committed if there is no
* uploaded new reports.
*
* @param report The report object
* @return The result of comitting the uploaded report template
*/
private boolean commitReportTemplates(HttpServletRequest request, Report report){
String reportName = report.getName();
//Temp directory that contains the latest uploaded template
File tempReportDir = new File(getTempFolderPath(request, reportName));
//Temp directory always exists for add operation
//But edit operation will depends on whether there is any new uploaded templates
//Check if temporary directory exists, if it is not, no files to be committed and return true.
if (!tempReportDir.exists()){
Log.info("Temporary directory not found. No files to be commited.");
return true;
}
//Actual directory that contains the commited uploaded template
//Directory will not exists on add operation, but exists on edit operation
File reportDir = new File(getReportFolderPath(reportName));
if (reportDir.exists()) {
//If add operation, check for exisiting actual report directory before rename the temp dir
if (StringUtils.equals (request.getParameter ("method"), "add")){
Log.warn("Unable to commit files. A directory with the same name already exists in the file system.");
return false;
}
//If edit operation, remove the actual report directory before rename the temp dir
if (StringUtils.equals (request.getParameter ("method"), "edit")){
FileUtil.deleteDir(reportDir, true);
Log.info("Deleted the actual report directory to replace with new templates.");
}
}
// Move file to new directory by renaming the temp directory
return tempReportDir.renameTo(reportDir);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?