|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package info.bukova.isspst.reporting;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
@ -16,38 +17,46 @@ public class ReportController {
|
|
|
|
|
private ReportDefinition reportDefinition;
|
|
|
|
|
@Autowired
|
|
|
|
|
private GeneratorFactory factory;
|
|
|
|
|
private static final String ERROR_MESSAGE = "<html><body><b>Generator returned no data!</b><br/>%s</body></html>";
|
|
|
|
|
|
|
|
|
|
private void writeError(OutputStream stream, Throwable e) {
|
|
|
|
|
writeError(stream, e.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void writeError(OutputStream stream, String message) {
|
|
|
|
|
String err = String.format(ERROR_MESSAGE, message);
|
|
|
|
|
try {
|
|
|
|
|
stream.write(err.getBytes(), 0, err.getBytes().length);
|
|
|
|
|
} catch (IOException e1) {
|
|
|
|
|
e1.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequestMapping("report.pdf")
|
|
|
|
|
public void pdfReport(HttpServletResponse response) {
|
|
|
|
|
Generator gen = factory.createGenerator(reportDefinition);
|
|
|
|
|
byte[] data = gen.generate();
|
|
|
|
|
String contentType = "application/pdf";
|
|
|
|
|
final String contentType = "application/pdf";
|
|
|
|
|
|
|
|
|
|
ServletOutputStream os = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
os = response.getOutputStream();
|
|
|
|
|
} catch (IOException e1) {
|
|
|
|
|
e1.printStackTrace();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (reportDefinition.getReport() == null || reportDefinition.getDataSet() == null) {
|
|
|
|
|
// writeError(os, "Definition is null");
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
if (reportDefinition.getReport() == null || reportDefinition.getDataSet() == null) {
|
|
|
|
|
throw new ReportException("Definition is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Generator gen = factory.createGenerator(reportDefinition);
|
|
|
|
|
byte[] data = gen.generate();
|
|
|
|
|
response.setContentType(contentType);
|
|
|
|
|
response.setContentLength(data.length);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
os.write(data, 0, data.length);
|
|
|
|
|
os.flush();
|
|
|
|
|
os.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
// } catch (ReportException e) {
|
|
|
|
|
// writeError(os, e);
|
|
|
|
|
} catch (ReportException e) {
|
|
|
|
|
writeError(os, e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (os != null) {
|
|
|
|
|
try {
|
|
|
|
|