diff --git a/src/main/java/info/bukova/isspst/Constants.java b/src/main/java/info/bukova/isspst/Constants.java index 039d67cb..93a4599d 100644 --- a/src/main/java/info/bukova/isspst/Constants.java +++ b/src/main/java/info/bukova/isspst/Constants.java @@ -58,6 +58,7 @@ public class Constants { public final static String DYNAMIC_REPORT_NAME = "Tabulková sestava"; public final static ReportMapping REPORTS[] = { - new ReportMapping(MOD_ADDRESSBOOK, new Report("Adresní karty", "address")) + new ReportMapping(MOD_ADDRESSBOOK, new Report("Adresní karty", "address")), + new ReportMapping(MOD_ADDRESSBOOK, new Report("Adresna", "address", false, true)) }; } diff --git a/src/main/java/info/bukova/isspst/reporting/DynamicGenerator.java b/src/main/java/info/bukova/isspst/reporting/DynamicGenerator.java index 79c5acc4..b2117879 100644 --- a/src/main/java/info/bukova/isspst/reporting/DynamicGenerator.java +++ b/src/main/java/info/bukova/isspst/reporting/DynamicGenerator.java @@ -53,11 +53,9 @@ public class DynamicGenerator implements Generator { try { rb.addColumn(StringUtils.localize(col), col, clazz, 30, false); } catch (ColumnBuilderException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new ReportException(e); } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new ReportException(e); } } } @@ -72,11 +70,8 @@ public class DynamicGenerator implements Generator { JasperReport report = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), definition.getParams()); return JasperRunManager.runReportToPdf(report, definition.getParams(), ds); } catch (JRException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new ReportException(e); } - - return null; } private Class colClass(String col) { diff --git a/src/main/java/info/bukova/isspst/reporting/PredefinedGenerator.java b/src/main/java/info/bukova/isspst/reporting/PredefinedGenerator.java index d50fd5c6..81a1f5f6 100644 --- a/src/main/java/info/bukova/isspst/reporting/PredefinedGenerator.java +++ b/src/main/java/info/bukova/isspst/reporting/PredefinedGenerator.java @@ -31,8 +31,7 @@ public class PredefinedGenerator implements Generator { JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "Cp1250"); bytes = JasperRunManager.runReportToPdf(report, definition.getParams(), new JRBeanCollectionDataSource(definition.getDataSet()));; } catch (JRException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new ReportException(e); } return bytes; diff --git a/src/main/java/info/bukova/isspst/reporting/Report.java b/src/main/java/info/bukova/isspst/reporting/Report.java index 93dd580c..19b2a757 100644 --- a/src/main/java/info/bukova/isspst/reporting/Report.java +++ b/src/main/java/info/bukova/isspst/reporting/Report.java @@ -7,11 +7,17 @@ public class Report { private String name; private String jasperFile; private boolean hasSettings; + private boolean singleRecord; public Report() { hasSettings = false; + singleRecord = false; } + /** + * @param name + * @param jasperFile + */ public Report(String name, String jasperFile) { this(); this.type = ReportType.DEFINED; @@ -19,10 +25,26 @@ public class Report { this.jasperFile = jasperFile; } + /** + * @param name + * @param jasperFile + * @param hasSettings + */ public Report(String name, String jasperFile, boolean hasSettings) { this(name, jasperFile); this.hasSettings = hasSettings; } + + /** + * @param name + * @param jasperFile + * @param hasSettings + * @param singleRecord + */ + public Report(String name, String jasperFile, boolean hasSettings, boolean singleRecord) { + this(name, jasperFile, hasSettings); + this.singleRecord = singleRecord; + } public ReportType getType() { return type; @@ -56,4 +78,12 @@ public class Report { this.hasSettings = hasSettings; } + public boolean isSingleRecord() { + return singleRecord; + } + + public void setSingleRecord(boolean singleRecord) { + this.singleRecord = singleRecord; + } + } diff --git a/src/main/java/info/bukova/isspst/reporting/ReportController.java b/src/main/java/info/bukova/isspst/reporting/ReportController.java index bf86f0f8..18888238 100644 --- a/src/main/java/info/bukova/isspst/reporting/ReportController.java +++ b/src/main/java/info/bukova/isspst/reporting/ReportController.java @@ -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 = "Generator returned no data!
%s"; - @RequestMapping("report.pdf") - public void pdfReport(HttpServletResponse response) { - Generator gen = factory.createGenerator(reportDefinition); - byte[] data = gen.generate(); - String contentType = "application/pdf"; - - ServletOutputStream os = null; - + 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 { - os = response.getOutputStream(); + stream.write(err.getBytes(), 0, err.getBytes().length); } catch (IOException e1) { e1.printStackTrace(); - return; } + } + + @RequestMapping("report.pdf") + public void pdfReport(HttpServletResponse response) { + final String contentType = "application/pdf"; -// if (reportDefinition.getReport() == null || reportDefinition.getDataSet() == null) { -// writeError(os, "Definition is null"); -// return; -// } + ServletOutputStream os = null; - response.setContentType(contentType); - response.setContentLength(data.length); - try { + os = response.getOutputStream(); + + 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); + 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 { diff --git a/src/main/java/info/bukova/isspst/reporting/ReportException.java b/src/main/java/info/bukova/isspst/reporting/ReportException.java new file mode 100644 index 00000000..1f82b317 --- /dev/null +++ b/src/main/java/info/bukova/isspst/reporting/ReportException.java @@ -0,0 +1,20 @@ +package info.bukova.isspst.reporting; + +import info.bukova.isspst.services.IsspstException; + +public class ReportException extends IsspstException { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public ReportException(Throwable cause) { + super("Reporting exception: ", cause); + } + + public ReportException(String message) { + super(message); + } + +} diff --git a/src/main/java/info/bukova/isspst/services/IsspstException.java b/src/main/java/info/bukova/isspst/services/IsspstException.java index b0a89439..449f45c2 100644 --- a/src/main/java/info/bukova/isspst/services/IsspstException.java +++ b/src/main/java/info/bukova/isspst/services/IsspstException.java @@ -4,7 +4,7 @@ public class IsspstException extends RuntimeException { private static final long serialVersionUID = 1L; - private final static String MSG = "RsFaktura Exception. "; + private final static String MSG = "IS Exception. "; public IsspstException() { super(MSG); diff --git a/src/main/java/info/bukova/isspst/ui/ListViewModel.java b/src/main/java/info/bukova/isspst/ui/ListViewModel.java index f739efd9..ccafe836 100644 --- a/src/main/java/info/bukova/isspst/ui/ListViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/ListViewModel.java @@ -301,6 +301,7 @@ public class ListViewModel { Map params = new HashMap(); params.put("reports", service.getReports()); params.put("data", dataList); + params.put("singleObject", dataBean); Window win = (Window) Executions.createComponents("/app/reporting/reportDialog.zul", null, params); win.doModal(); } diff --git a/src/main/java/info/bukova/isspst/ui/reporting/ReportDialogVM.java b/src/main/java/info/bukova/isspst/ui/reporting/ReportDialogVM.java index 9e525e3b..98873a23 100644 --- a/src/main/java/info/bukova/isspst/ui/reporting/ReportDialogVM.java +++ b/src/main/java/info/bukova/isspst/ui/reporting/ReportDialogVM.java @@ -21,17 +21,21 @@ public class ReportDialogVM { private Report selected; @WireVariable private ReportDefinition reportDefinition; + private List dataList; + private Object singleObject; @Init public void init(@ExecutionArgParam("reports") List reports, - @ExecutionArgParam("data") List data) { + @ExecutionArgParam("data") List data, + @ExecutionArgParam("singleObject") Object singleObject) { this.reports = reports; if (data != null && data.size() > 0 && data.get(0).getClass() != reportDefinition.gatDataClass()) { reportDefinition.clear(); } - reportDefinition.setDataSet(data); + dataList = data; + this.singleObject = singleObject; } public List getReports() { @@ -50,6 +54,11 @@ public class ReportDialogVM { public void setSelected(Report selected) { this.selected = selected; reportDefinition.setReport(selected); + if (selected.isSingleRecord()) { + reportDefinition.setSingleObject(singleObject); + } else { + reportDefinition.setDataSet(dataList); + } } @Command @@ -72,4 +81,12 @@ public class ReportDialogVM { } } + public Object getSingleObject() { + return singleObject; + } + + public void setReportDefinition(ReportDefinition reportDefinition) { + this.reportDefinition = reportDefinition; + } + } diff --git a/src/main/webapp/app/reporting/reportDialog.zul b/src/main/webapp/app/reporting/reportDialog.zul index 6a38a10c..85a40846 100644 --- a/src/main/webapp/app/reporting/reportDialog.zul +++ b/src/main/webapp/app/reporting/reportDialog.zul @@ -8,7 +8,7 @@