reportwizardcontroller.java

来自「Java的框架」· Java 代码 · 共 1,066 行 · 第 1/3 页

JAVA
1,066
字号
	 */
	protected ModelAndView processCancel (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
	throws Exception{
		
		Report report = (Report)command;
		String reportName = report.getName();
		Log.info("Cancel request for report " + reportName);
		if (reportName != null){
			Log.info("Delete temporary files for " + reportName);
			deleteTempDirectory(request, reportName);
		}
		return getRedirectView(request,command);
	}
	
	/**
	 * Method disallows duplicate form submission. Typically used to prevent
	 * duplicate insertion of entities into the datastore. Shows a new form with
	 * an error message.
	 * @param request HTTP request object
	 * @param response HTTP response object
	 * @return Model and View object
	 * @throws Exception
	 */
	protected ModelAndView disallowDuplicateFormSubmission (
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		
		BindException errors = getErrorsForNewForm (request);
		errors.reject (NameConstants.ERROR_DUPLICATE_SUBMISSION,
		"Duplicate form submission");
		return showForm (request, response,errors);
	}
	
	/**
	 * Function that sets the request attribute before calling the superclass showForm method.
	 * @param request HTTP request object
	 * @param response HTTP response object
	 * @return Model and View object
	 * @throws Exception
	 */
	private ModelAndView showFormEx(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception{
		setRequestAttribute(request,NameConstants.REPORT_TEMPID);
		return super.showForm(request,response,errors);
	}
	
	/**
	 * REMOVED DUE TO ERROR WITH LAZY LOADING. THIS FUNCTION WILL CAUSE THE
	 * STALE COMMAND OBJECT THUS ERROR WHEN EDITTING THE REPORT 
	 * Perform general validation. 
	 * Checks whether the report with the same name already exists if it is an add operation.
	 * Checks whether the report exists if it is an edit operation.
	 * This checks ensure that there will be no problem when 2 user creating the report with the 
	 * same name at the same time and one commit the changes first.
	 * @param request HTTP request object
	 * @param report The report object.
	 * @param errors Errors object
	 * @return The result of the validation
	private boolean performGeneralValidation(HttpServletRequest request, Report report, 
			BindException errors){
		//	Validation for any existing report
		Report tempReport = getReport(report.getName());
		//Check if an existing report already exists; if true show duplicate report error
		if (StringUtils.equals (request.getParameter ("method"), "add")) {
			if (tempReport != null){
				errors.reject(NameConstants.ERROR_DUPLICATE_REPORT, 
						new Object[] {report.getName()}, "Duplicate report");						
			}
		}else{
			//Check if an existing report already exists; if false show report not found for edit
			if (tempReport == null){
				errors.reject(NameConstants.ERROR_REPORT_NOTFOUND, 
						new Object[] {report.getName()}, "Non-existing report");						
			}
		}
		return errors.getErrorCount() == 0;
	}
	 */
	
	/**
	 * Perform data validation on page 0.
	 * Checks the validity of the uploaded report template.
	 * @param request HTTP request object
	 * @param report The report object.
	 * @param errors Errors object
	 * @return The result of the validation
	 */
	private boolean performPage0Validation(HttpServletRequest request, Report report, 
			BindException errors){		
		//Process report template
		CommonsMultipartFile commonFile = getCommonFile(request,"reportFile");
		
		//If source file not null AND empty 
		//AND
		//common file not null OR size = 0
		if ((report.getSourceFile() != null && 
				report.getSourceFile().length() > 0) &&
				(commonFile == null || commonFile.getSize() == 0)){
			return true;
		}
		
		if (commonFile == null || commonFile.getSize() == 0){
			Log.debug("Common file is null");
			errors.reject(NameConstants.ERROR_RPT_TEMPLATE_NULL,"Report template is null");
			return false;
		}
		String oriFileName = commonFile.getOriginalFilename();
		Log.debug("Report file name : " + oriFileName);
		
		//Validate report template
		try {
			JasperDesign design = JasperReportDesignUtil.instance.getJasperDesign(commonFile.getInputStream());
			if (!JasperReportDesignUtil.instance.isValidReportDesign(design)){
				errors.rejectValue("sourceFile",NameConstants.ERROR_INVALID_REPORT,new Object[]{oriFileName},"Invalid report template.");
			}
		}
		catch (JRException e) {
			Log.warn(e.getMessage());
			errors.rejectValue("sourceFile",NameConstants.ERROR_LOAD_REPORT,new Object[]{oriFileName},"Error while loading report template.");
		}
		catch (IOException e) {
			Log.warn(e.getMessage());
			errors.rejectValue("sourceFile",NameConstants.ERROR_UPLOAD_FAIL,new Object[]{oriFileName},"Error while uploading report template.");
		}			
		return errors.getErrorCount() == 0;
	}
	
	/**
	 * Perform data validation on page 1.
	 * Checks the validity of the uploaded sub report template.
	 * @param request HTTP request object
	 * @param report The report object.
	 * @param errors Errors object
	 * @return The result of the data validation
	 */
	private boolean performPage1Validation(HttpServletRequest request, Report report, 
			BindException errors){
		
		List subReports = report.getSubReports();
		for (int i = 0; i < subReports.size(); i++){
			SubReport subReport = (SubReport)subReports.get(i);
			String subReportName = subReport.getName();
			CommonsMultipartFile subRptCommonFile = getCommonFile(request, subReportName + "_file");

			//If no file was loaded and the sub report source was not null, then skip validation
			String subReportSourceFile = subReport.getSourceFile();
			if ((subReportSourceFile != null && 
					subReportSourceFile.length() > 0) &&
					(subRptCommonFile == null || subRptCommonFile.getSize() == 0)){
				return true;
			}

			if (subRptCommonFile == null || subRptCommonFile.getSize() == 0){
				Log.debug("Common file is null");
				errors.reject(NameConstants.ERROR_SUBREPORT_TEMPLATE_NULL,"Sub report template is null");		
				return false;
			}
			try {
				JasperDesign subRptDesign = JasperReportDesignUtil.instance.getJasperDesign(subRptCommonFile.getInputStream());
				if (!JasperReportDesignUtil.instance.isValidReportDesign(subRptDesign)){
					errors.reject(NameConstants.ERROR_INVALID_SUBREPORT,new Object[]{subReportName},"Sub report template " + subReportName + " is invalid");						
					return false;
				}							
			}catch (JRException e) {
				Log.warn(e.getMessage());
				errors.reject(NameConstants.ERROR_LOADSUBREPORT,new Object[]{subReportName},"Error while loading sub report template " + subReportName );
				return false;
			}catch (IOException e) {
				Log.warn(e.getMessage());
				errors.rejectValue("sourceFile",NameConstants.ERROR_UPLOAD_FAIL,new Object[]{subReportName},"Error while uploading report template" + subReportName);
				return false;
			}
		}
		return errors.getErrorCount() == 0;
	}
	
	/**
	 * Perform data binding on page 0.
	 * Creates the template file in a temporary directory. File will be moved to
	 * report repository upon processFinish() else will be deleted when processCancel()
	 * is called. 
	 * @param request HTTP request object
	 * @param report The report object.
	 * @param errors Errors object
	 */
	private void onBindPage0(HttpServletRequest request, Report report, 
			BindException errors){		
		
		String reportName = report.getName();
		
		//Get the temp folder path based on the report name as in the input form
		String tempFolderPath = getTempFolderPath(request, reportName);
		File tempFolder = new File(tempFolderPath);
		
		//Handle the report name changed
		handleReportNameChanged(request, report, tempFolder);
		//Handle the report template upload
		handleReportTemplateUpload(request, report, errors, tempFolder);
		//Process roles
		handlerReportRoles(request, report);
	}
	
	
	/**
	 * Perform validation on page 1.
	 * Checks the validity of the uploaded sub report template.
	 * @param request HTTP request object
	 * @param report The report object.
	 * @param errors Errors object
	 */
	private void onBindPage1(HttpServletRequest request, Report report, 
			BindException errors){
		
		List subReports = report.getSubReports();
		if (subReports.size() > 0){
			String reportName = report.getName();
			String tempFolderPath = new StringBuffer().append(getTempFolderPath(request, reportName)).append(File.separator).append(NameConstants.SUBREPORT_DIR).toString();
			File tempFolder = new File(tempFolderPath);
			if (!tempFolder.exists()){
				tempFolder.mkdirs();
			}				
			for (int i = 0; i < subReports.size(); i++){
				SubReport subReport = (SubReport)subReports.get(i);
				String subReportName = subReport.getName();
				CommonsMultipartFile subRptCommonFile = getCommonFile(request, subReportName + "_file");
				if (subRptCommonFile != null && subRptCommonFile.getSize() > 0){
					//Create the temporary file
					String fullTempFileName = createTempFile(tempFolderPath, subRptCommonFile, errors);
					//Compile the report template to jasper file
					compileReportTemplate(subReportName, fullTempFileName, errors);
					//Set the source file property
					subReport.setSourceFile(toRelativePath(fullTempFileName));
				}else{
					//If no report uploaded, use the existing report
					String fullSubRptSourceFile = this.getServletContext().getRealPath(subReport.getSourceFile());
					Log.debug("No new sub report template uploaded. Copy exisiting file " + fullSubRptSourceFile + " to " + tempFolder.getAbsolutePath());
					File subRptSourceFile = new File(fullSubRptSourceFile);
					if (subRptSourceFile.exists()){
						String sourceFileName = subRptSourceFile.getName();
						String fullNewTempFileName = this.getTempFileName(tempFolder.getAbsolutePath(), sourceFileName);
						Log.debug("New temp sub report source file name : " + fullNewTempFileName);
						File newFile = new File(fullNewTempFileName);
						try {
							FileUtil.copy(subRptSourceFile,newFile);
						}
						catch (IOException e) {
							Log.warn("Error create sub report temporary file. " + 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);
					}
				}
			}		
		}
	}
	
	/**
	 * Get the previous report name based on the request parameter. If previous 
	 * report name is not null, and the report name has been changed, rename the 
	 * temp folder according to the newly changed report name.
	 * This is to facilitate the rename of report information when duplicate report was found
	 * when two users trying to create the report with the same name.
	 * @param request The HttpServletRequest object
	 * @param report The report object
	 * @param tempFolder The temporary folder
	 */
	private void handleReportNameChanged(HttpServletRequest request, Report report, File tempFolder){
		//Get the previous report name based on the request parameter
		//If previous report name is not null, and the report name has been changed,
		//rename the temp folder according to the newly changed report name.
		//This is to facilitate the rename of report information when duplicate report was found
		//when two users trying to create the report with the same name.
		String reportName = report.getName();
		String paramReportName = request.getParameter("reportName");
		if (paramReportName != null && paramReportName.length() > 0){
			if (!reportName.equals(paramReportName)){
				//Rename the temp folder 
				String oldTempFolderPath = getTempFolderPath(request, paramReportName);
				File oldTempFolder = new File(oldTempFolderPath);
				if (oldTempFolder.exists()) oldTempFolder.renameTo(tempFolder);
				//Rename report source file property
				renameReportFileName(report, request, paramReportName);
				//Rename sub report parent name to the new report name
				List subReports = report.getSubReports();
				for (int i = 0; i < subReports.size(); i++){
					SubReport subReport = (SubReport)subReports.get(i);
					subReport.setParentName(reportName);
				}
			}
		}
	}
	
	/**
	 * Process the report template upload. If uploaded file is found, the file will be uploaded into 
	 * a temporary directory. If the temporary directory already exists, clear the temporary directory.
	 * For edit operation, there might not be any uploaded template, in case if there isnt any uploaded
	 * file, the exisiting report template will be used. That is a copy of the existing file will be made
	 * to the temporary folder so that during commit of the edit operation, the existing folder will be
	 * replaced with the temporary folder.
	 * @param request The HTTP request object.
	 * @param report The report object.
	 * @param errors The binding error object.
	 * @param tempFolder The temporary folder.
	 */
	private void handleReportTemplateUpload(HttpServletRequest request, Report report, BindException errors, File tempFolder){
		//2. 
		//(a) If first time upload, the temp folder does not exists, create the directory
		//(b) If not first time add, clear the temp directory
		//If there is a report template uploaded (common file != null && common file size > 0)
		//(c) Re-upload the report template 
		//(d) Update the report source file property accordingly
		//(e) Update the sub report source file property accordingly
		//If no report uploaded, use the existing report
		//(a) Copy the exisitng report template from original directory to temp dir only if edit operation
		//(b) If add operation, dont need to copy
		String reportName = report.getName();
		CommonsMultipartFile commonFile = getCommonFile(request,"reportFile");
		//(a)If first time upload, the temp folder does not exists, create the directory
		if (!tempFolder.exists()){
			tempFolder.mkdirs();
			//(b)If not first time add, clear the temp directory
		}else{
			//If temp folder already exists, report template was previously uploaded, but user navigate back to
			//Page 0 to do upload again, delete the existing report template and also the subreports (all in the folder). 
			if (commonFile != null && commonFile.getSize() > 0){							
				FileUtil.deleteDir(tempFolder,false);
			}
		}
		//If there is a report template uploaded (common file != null && common file size > 0)
		if (commonFile != null && commonFile.getSize() > 0){							
			//(c) Re-upload the report template
			//Create the temporary file
			String fullTempFileName = createTempFile(tempFolder.getAbsolutePath(), commonFile, errors);
			//Compile the report template to jasper file
			compileReportTemplate(reportName, fullTempFileName, errors);
			//This is to handle the user navigate from Page 1 back to Page 0 and upload the report template again
			//Clearing the sub reports will enable the sub report to be reloaded in getTargetPage
			if (report.getSubReports().size() > 0) report.getSubReports().clear();
			//Set the source file property
			report.setSourceFile(toRelativePath(fullTempFileName));
		}else{
			//If no report uploaded, use the existing report	
			//If edit operation
			if (report.getSourceFile().indexOf("/WEB-INF/reports/temp") < 0){
				String fullReportSourceFile = this.getServletContext().getRealPath(report.getSourceFile());
				Log.debug("No new report template uploaded. Copy exisiting file " + fullReportSourceFile + " to " + tempFolder.getAbsolutePath());
				File reportSourceFile = new File(fullReportSourceFile);
				if (reportSourceFile.exists()){
					String sourceFileName = reportSourceFile.getName();
					String fullNewTempFileName = this.getTempFileName(tempFolder.getAbsolutePath(), sourceFileName);
					Log.debug("New temp report source file name : " + fullNewTempFileName);
					File newFile = new File(fullNewTempFileName);
					try {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?