Skip to content

Instantly share code, notes, and snippets.

@wpivotto
Created October 15, 2014 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpivotto/c779361840c4b2c3c415 to your computer and use it in GitHub Desktop.
Save wpivotto/c779361840c4b2c3c415 to your computer and use it in GitHub Desktop.
FileSystemReportExporter
package br.com.prixma.models;
import java.io.File;
import java.io.IOException;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import org.slf4j.Logger;
import br.com.caelum.vraptor.jasperreports.Report;
import br.com.caelum.vraptor.jasperreports.ReportPathResolver;
import br.com.caelum.vraptor.jasperreports.download.BatchReportsDownload;
import br.com.caelum.vraptor.jasperreports.download.ReportDownload;
import br.com.caelum.vraptor.jasperreports.download.ReportsDownload;
import br.com.caelum.vraptor.jasperreports.exporter.ReportExporter;
import br.com.caelum.vraptor.jasperreports.formats.ExportFormat;
import com.google.common.io.Files;
@RequestScoped
public class FileSystemReportExporter {
@Inject private ReportExporter exporter;
@Inject private ReportPathResolver resolver;
@Inject private Logger log;
public File export(Report report, ExportFormat format) {
return export(new ReportDownload(report, format));
}
public File export(ReportDownload download) {
return write(download.getContent(exporter), download.getFileName());
}
public File export(ReportsDownload download) {
return write(download.getContent(exporter), download.getFileName());
}
public File export(BatchReportsDownload download) {
return write(download.getContent(exporter), download.getFileName());
}
public String getReportGenerationDirectory() {
return resolver.getReportsPath() + File.separator + "tmp" + File.separator;
}
private File write(byte[] content, String fileName) {
File file = new File(getReportGenerationDirectory() + fileName);
try {
Files.write(content, file);
log.debug("File {} created", file.getAbsolutePath());
} catch (IOException e) {
log.error("Error creating report file", e);
}
return file;
}
}
package br.com.prixma.controllers;
import static br.com.caelum.vraptor.jasperreports.formats.ExportFormats.pdf;
import java.io.File;
import javax.inject.Inject;
import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.prixma.models.FileSystemReportExporter;
@Controller
public class ReportController {
@Inject FileSystemReportExporter exporter;
@Get("/report/file")
public File exportToFile() {
File file = exporter.export(new SalesReport(), pdf());
return file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment