Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package info.joseluismartin.gui.report;
  
 
 import java.io.File;
 import java.util.Map;
 
 
 
This class is used to generate and display reports. Combine it with info.joseluismartin.util.processor.FileProcessor

Author(s):
Jose A. Corbacho
 
 public abstract class ReportManager {

apache common log
 
 	private static final Log log = LogFactory.getLog(ReportManager.class);
 	private DataSource dataSource;

Default ctor.
 
 	public ReportManager(){
 	}

Display a report in outputType using the service and available ids to create the data source

Parameters:
report report to display
filter filter to use when query data to persistent service
sortPropertyName sort property name
sortOrder sort order (ASC, DESC)
service persistent service to use
outputType report output type (pdf, xml)
 
 	public void showReport(Report reportPersistentService<ObjectSerializableserviceObject filterString sortPropertyNamePage.Order sortOrderString outputTypethrows Exception{
 		PageJRDatasourceAdapter dataSource = new PageJRDatasourceAdapter(true);
 		Page<Objectpage = new Page<Object>(10, 0, sortPropertyNamesortOrder);
 		page.setFilter(filter);
 		page.setPageableDataSource(service);
 		dataSource.setPage(page);
 		
 		try {
 			st.processFile(reportoutputTypedataSource);
 		} catch (IOException e) {
 			.error(e);
 		}
 
 	}


Displays the report in a new window as a file

Parameters:
report the report to be displayed
Throws:
info.joseluismartin.reporting.ReportingException
 
 	public void showReport(Report reportDataSource dataSourceString outputTypethrows ReportingException {
 		Connection conn = null;
 		try {
 			conn = dataSource.getConnection();
 			boolean continueWithReport = st.preprocessFile(report);
 			if (continueWithReportst.processFile(reportoutputTypeconn);
 		} catch (Exception e) {
 			.error(e);
 		} finally {
 			if (conn != null)
 				try {
 					conn.close();
catch (SQLException e) {
				}
		}
	}
	public static String getPrefix(String fileName) {
		String name = fileName != null ? fileName : "";
		int index = name.lastIndexOf('.');
		return index == -1 ? name : name.substring(0, name.lastIndexOf('.'));
	}
	public static String getSuffix(String fileName) {
		String name = fileName != null ? fileName : "";
		int index = name.lastIndexOf('.');
		return index == -1 ? "" : name.substring(index);
	}
Implementation of FileStrategy using a JRDataSource as report data source

Author(s):
Jose A. Corbacho
		public void setReportDataSource(FileProcessor fpObject obj) {
		}
	}

Implementation of FileStrategy using a Connection as report data source

Author(s):
Jose A. Corbacho
		public void setReportDataSource(FileProcessor fpObject obj) {
		}		
	}

Strategy to process the jasper file. Different strategies depending on the system used to retrieve the data (JRDataSource, java.sql.Connection)

Author(s):
Jose A. Corbacho
	public abstract class ProcessFileStrategy {
		private Map<StringObjectparameters = new HashMap<StringObject>();
		private boolean interactive = true;
Set the data source in this file processor.

Parameters:
fp
obj
		public abstract void setReportDataSource(FileProcessor fpObject obj);

Pre-process file. In case this report includes params, display it. The fact a report has params can be determined at the moment of registering it the system. At this point, it could be possible to have this information thus making this process faster

Parameters:
report report to process
		public boolean preprocessFile(Report report){
				.debug("ReportManager. Preprocess file: hasQuery -> " + report.getHasQuery());
			if (report.getHasQuery()) {
				JasperReport jasperReport = report.newJasperReport();
						.debug("Parameters in jasperReport");
				Map<StringJRParameterjrParameters = new HashMap<StringJRParameter>();
				for (JRParameter param : jasperReport.getParameters()) {
					if (!param.isSystemDefined() && param.isForPrompting()){
							.debug("Param to fill from paramEntry: " + param.getName());
						jrParameters.put(param.getName(), param);					
					}
				}
				if (!jrParameters.isEmpty() && ) {
					if (!showParameterDialog(jrParameters)) return false;
				}
			}
			return true;
		}
		private boolean showParameterDialog(Map<StringJRParameterjrParameters){
			dialog.setParameters(jrParameters);
			dialog.initialize();
			dialog.setVisible(true);
			if (dialog.isCanceled()) return false;
			this. = dialog.getReturnValues();
			return true;
		}

Process the report with this reportDataSource. The data source can be JRDataSource or Connection

Parameters:
report
outputType
reportDataSource
Throws:
info.joseluismartin.reporting.ReportingException
java.io.IOException
		public void processFile(Report reportString outputTypeObject reportDataSource ) throws ReportingExceptionIOException{
			if (report == null || report.getData() == null)
				return;
			File file;
			String suffix = getSuffix(report.getFileName());
			try {
				file = File.createTempFile(getPrefix(report.getFileName()), suffix);
				FileUtils.writeByteArrayToFile(filereport.getData());
				if (".zip".equalsIgnoreCase(suffix)) {
					String dir = System.getProperty("java.io.tmpdir") + "/" + getPrefix(report.getFileName());
					ZipFileUtils.unzip(new ZipFile(file), dir);
					File dirFile = new File(dir);
					Iterator<Fileiter = FileUtils.iterateFiles(dirFilenew String[] {"jrxml""jasper"}, false);
					while (iter.hasNext()) {
						file = iter.next();
						suffix = getSuffix(file.getName());
						break;
					}
				}
				// TODO In case more file types are allowed in the system, create Factory
				if (".jasper".equals(suffix)){
				}
				else if (".jrxml".equals(suffix)){
				}
				else {
					throw new ReportingException("Process not yet implemented for file type " + suffix);
				}
				// Process the file. This method sets its rawData attribute to the result of the processing.
				.processFile(fileoutputTypereport.getHasQuery());
				if ( && Desktop.isDesktopSupported()) {
					Desktop desktop = Desktop.getDesktop();
					File outputFile;
					try {
						outputFile = File.createTempFile(getPrefix(report.getFileName()), "." + outputType);
						outputFile.deleteOnExit();
						// Create the file with the raw data provided by the file processor 
						desktop.open(outputFile);
catch (IOException e) {
						throw new ReportingException("No ha sido posible abrir el fichero del informe: " + report.getFileName(), e);
					}
				}
catch (IOException e) {
				throw new ReportingException("No ha sido posible abrir el fichero del informe: " + report.getFileName(), e);
			}			
		}

Returns:
the interactive
		public boolean isInteractive() {
			return ;
		}

Parameters:
interactive the interactive to set
		public void setInteractive(boolean interactive) {
			this. = interactive;
		}

Returns:
the parameters
		public Map<StringObjectgetParameters() {
			return ;
		}

Parameters:
parameters the parameters to set
		public void setParameters(Map<StringObjectparameters) {
			this. = parameters;
		}

Returns:
the fileProcessor
		}

Parameters:
fileProcessor the fileProcessor to set
		public void setFileProcessor(FileProcessor fileProcessor) {
			this. = fileProcessor;
		}
	}

Returns:
the dataSource
		return ;
	}

Parameters:
dataSource the dataSource to set
	public void setDataSource(DataSource dataSource) {
		this. = dataSource;
	}

Parameters:
parentFolder
	public ReportFileResolver(File parentFolder) {
		super(parentFolder);
	}

	public File resolveFile(String fileName) {
		return super.resolveFile(FilenameUtils.getName((fileName)));
	}
New to GrepCode? Check out our FAQ X