# HG changeset patch # User gernotbelger # Date 1523963996 -7200 # Node ID 460fcc128794bc90af6d102c5248a6b66b61ef19 # Parent 4635872e7117d8829e4307653552be12fc1a8e91 jasper multiple diff -r 4635872e7117 -r 460fcc128794 artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java Tue Apr 17 13:18:56 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java Tue Apr 17 13:19:56 2018 +0200 @@ -9,35 +9,16 @@ */ package org.dive4elements.river.artifacts.common; -import java.io.OutputStream; import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; import org.dive4elements.artifacts.CallMeta; -import org.dive4elements.artifacts.common.utils.Config; import org.dive4elements.river.artifacts.model.CalculationResult; import org.dive4elements.river.artifacts.resources.Resources; -import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; import org.dive4elements.river.exports.AbstractExporter; import au.com.bytecode.opencsv.CSVWriter; -import net.sf.jasperreports.engine.JRDataSource; -import net.sf.jasperreports.engine.JRElement; -import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JRPrintPage; -import net.sf.jasperreports.engine.JasperCompileManager; -import net.sf.jasperreports.engine.JasperExportManager; -import net.sf.jasperreports.engine.JasperFillManager; -import net.sf.jasperreports.engine.JasperPrint; -import net.sf.jasperreports.engine.JasperReport; -import net.sf.jasperreports.engine.design.JasperDesign; -import net.sf.jasperreports.engine.util.JRProperties; -import net.sf.jasperreports.engine.xml.JRSaxParserFactory; -import net.sf.jasperreports.engine.xml.JRXmlLoader; /** * @author Gernot Belger @@ -54,15 +35,14 @@ protected RESULTS data = null; - public RESULTS getData() { + public final RESULTS getData() { return this.data; } @Override - protected void addData(final Object d) { + protected final void addData(final Object d) { /* reset */ this.data = null; - if (d instanceof CalculationResult) { final Object dat = ((CalculationResult) d).getData(); @@ -77,13 +57,13 @@ /** * Formats header with unit and label: msg [unit] (label) */ - protected String msgUnitLabel(final String key, final String unit, final String label) { + protected final String msgUnitLabel(final String key, final String unit, final String label) { final String msg = msg(key); return String.format("%s [%s] (%s)", msg, unit, label); } @Override - protected void writeCSVData(final CSVWriter writer) { + protected final void writeCSVData(final CSVWriter writer) { getLog().info("writeCSVData"); /* fetch calculation results */ @@ -111,23 +91,22 @@ */ protected abstract void writeCSVGlobalMetadata(final CSVWriter writer, final RESULTS results); - protected void writeCSVMetaEntry(final CSVWriter writer, final String message, final Object... messageArgs) { + protected final void writeCSVMetaEntry(final CSVWriter writer, final String message, final Object... messageArgs) { final CallMeta meta = this.context.getMeta(); writer.writeNext(new String[] { Resources.getMsg(meta, message, message, messageArgs) }); } - private void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) { + private final void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) { writeCSVResultMetadata(writer, results, result); - writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance + // wenn resultsmetadata null sind!? keine neue zeile + // writer.writeNext(new String[] { "" }); // break line in den Implementationen, + // weil es sein kann, dass KEINE ResultMetadata geschrieben werden; dann wäre eine Leerzeile überflüssig - // final RiverInfo river = results.getRiver(); - // - // writeCSVHeader(writer, results, river); /* now the value rows */ - final Collection rows = result.getRows(); // war mal SInfoResultRow + final Collection rows = result.getRows(); for (final ResultRow row : rows) { writeCSVRow(writer, results, result, row); } @@ -149,54 +128,7 @@ return formatRow(results, row, ExportMode.csv); } - // protected abstract void tweakDesign() - - @Override - protected void writePDF(final OutputStream outStream) { - getLog().debug("write PDF"); - - final JRDataSource source = createJRData(); - final JRDataSource source2 = createJRData(); - - final String confPath = Config.getConfigDirectory().toString(); - - // FIXME: distinguish between with and without tkh: we need two jasper reports! - - final Map parameters = new HashMap<>(); - parameters.put("ReportTitle", "Exported Data"); - - try { - - // JRProperties.setProperty(JRProperties.COMPILER_XML_VALIDATION, false); - JRProperties.setProperty(JRSaxParserFactory.PROPERTY_REPORT_PARSER_FACTORY, JRReportSaxParserFactory.class.getName()); - - final String jasperPath = confPath + getJasperFile(); - final JasperDesign test = JRXmlLoader.load(jasperPath); - - final JRElement element = test.getColumnHeader().getElementByKey("TEST"); - // element.setWidth(200); - - final JasperReport compiled = JasperCompileManager.compileReport(test); - - final JasperPrint print = JasperFillManager.fillReport(compiled, parameters, source); - // JasperExportManager.exportReportToPdfStream(print, outStream); - - final JasperPrint print2 = JasperFillManager.fillReport(compiled, parameters, source2); - - final List pages = print2.getPages(); - for (final JRPrintPage page : pages) - print.addPage(page); - - JasperExportManager.exportReportToPdfStream(print, outStream); - } - catch (final JRException je) { - getLog().warn("Error generating PDF Report!", je); - } - } - - protected abstract String getJasperFile(); - - private JRDataSource createJRData() { + protected final MetaAndTableJRDataSource createJRData(final AbstractCalculationResults data) { /* fetch calculation results */ final RESULTS results = this.data; @@ -205,16 +137,15 @@ addJRMetaData(source, results); - for (final RESULT result : results.getResults()) { + for (final RESULT result : results.getResults()) addJRTableData(source, results, result); - } return source; } protected abstract void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results); - protected void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) { + private void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) { final Collection rows = result.getRows(); @@ -227,11 +158,8 @@ protected abstract String[] formatRow(RESULTS results, ResultRow row, ExportMode mode); - protected final String[] formatPDFRow(final RESULTS results, final ResultRow row) { - // @Override - // protected String[] formatPDFRow(final FlowDepthDevelopmentCalculationResults results, final ResultRow row) { + private final String[] formatPDFRow(final RESULTS results, final ResultRow row) { return formatRow(results, row, ExportMode.pdf); - // } } protected abstract void writeCSVGlobalMetadataDefaults(final CSVWriter writer, final RESULTS results); diff -r 4635872e7117 -r 460fcc128794 artifacts/src/main/java/org/dive4elements/river/artifacts/common/JasperDesigner.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/common/JasperDesigner.java Tue Apr 17 13:19:56 2018 +0200 @@ -0,0 +1,94 @@ +/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ +package org.dive4elements.river.artifacts.common; + +import net.sf.jasperreports.engine.JRBand; +import net.sf.jasperreports.engine.JRChild; +import net.sf.jasperreports.engine.JRElement; +import net.sf.jasperreports.engine.design.JasperDesign; + +/** + * @author Domenico Nardi Tironi + */ +public class JasperDesigner { + + private final JasperDesign design; + private final MetaAndTableJRDataSource source; + private int startpageOffset = 0; + + public JasperDesigner(final JasperDesign design, final MetaAndTableJRDataSource source) { + this.design = design; + this.source = source; + } + + public void removeColumn(final String key) { + + final JRBand colchildren = this.design.getColumnHeader(); + final JRBand datachildren = this.design.getDetailSection().getBands()[0]; + int width = 0; + if (colchildren != null && datachildren != null) { + + final JRElement elementCol = colchildren.getElementByKey(key); + final JRElement elementData = datachildren.getElementByKey(key); + if (elementCol != null && elementData != null) { + width = elementCol.getWidth() + width; + final int indexCol = colchildren.getChildren().indexOf(elementCol); + final int indexData = datachildren.getChildren().indexOf(elementData); + + elementCol.getElementGroup().getChildren().remove(elementCol); + elementData.getElementGroup().getChildren().remove(elementData); + + for (int j = indexCol; j < colchildren.getChildren().size(); j++) { + final JRChild element = colchildren.getChildren().get(j); + if (element instanceof JRElement) { + ((JRElement) element).setX(((JRElement) element).getX() - width); + } + } + for (int j = indexData; j < datachildren.getChildren().size(); j++) { + final JRChild element = datachildren.getChildren().get(j); + if (element instanceof JRElement) { + ((JRElement) element).setX(((JRElement) element).getX() - width); + } + } + } + + // width auf letzte spalte schlagen + final JRElement elementColLast = colchildren.getElements()[colchildren.getElements().length - 1]; + if (elementColLast != null) { + elementColLast.setWidth(elementColLast.getWidth() + width); + } + final JRElement elementDataLast = datachildren.getElements()[datachildren.getElements().length - 1]; + if (elementDataLast != null) { + elementDataLast.setWidth(elementDataLast.getWidth() + width); + } + } else { + // TODO: LOG + System.out.print("Column not found for key: " + key); + } + + } + + JasperDesign getDesign() { + return this.design; + } + + public MetaAndTableJRDataSource getSource() { + return this.source; + } + + public void setPagesCount(final int pagesCount) { + this.startpageOffset = pagesCount; + } + + public int getPagesCount() { + + return this.startpageOffset; + } +} \ No newline at end of file diff -r 4635872e7117 -r 460fcc128794 artifacts/src/main/java/org/dive4elements/river/artifacts/common/JasperReporter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/common/JasperReporter.java Tue Apr 17 13:19:56 2018 +0200 @@ -0,0 +1,128 @@ +/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ +package org.dive4elements.river.artifacts.common; + +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dive4elements.artifacts.common.utils.Config; + +import net.sf.jasperreports.engine.JRDataSource; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JRExporterParameter; +import net.sf.jasperreports.engine.JasperCompileManager; +import net.sf.jasperreports.engine.JasperFillManager; +import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.JasperReport; +import net.sf.jasperreports.engine.design.JasperDesign; +import net.sf.jasperreports.engine.export.JRPdfExporter; +import net.sf.jasperreports.engine.export.JRPdfExporterParameter; +import net.sf.jasperreports.engine.util.JRProperties; +import net.sf.jasperreports.engine.xml.JRSaxParserFactory; +import net.sf.jasperreports.engine.xml.JRXmlLoader; + +/** + * @author Domenico Nardi Tironi + */ +public class JasperReporter { + + private final List designs = new ArrayList<>(); + + private final Map parameters = new HashMap<>(); + + private final String confPath; + + public JasperReporter() { + this.confPath = Config.getConfigDirectory().toString(); + + this.parameters.put("ReportTitle", "Exported Data"); + } + + public JasperDesigner addReport(final String jasperFile, final MetaAndTableJRDataSource source) throws JRException { + + JRProperties.setProperty(JRSaxParserFactory.PROPERTY_REPORT_PARSER_FACTORY, JRReportSaxParserFactory.class.getName()); + + final String jasperPath = this.confPath + jasperFile; + final JasperDesign design = JRXmlLoader.load(jasperPath); + + final JasperDesigner designer = new JasperDesigner(design, source); + this.designs.add(designer); + + return designer; + } + + public void exportPDF(final OutputStream out) throws JRException { + + final List compiledReports = new ArrayList<>(); + + final List sourcesWithPages = new ArrayList<>(); + + final List jasperPrintList = new ArrayList<>(); + + if (this.designs.size() == 1) { + final JasperDesigner designer = this.designs.get(0); + final JasperDesign jDesign = designer.getDesign(); + + final JasperReport compiled = JasperCompileManager.compileReport(jDesign); + compiledReports.add(compiled); + final MetaAndTableJRDataSource source = designer.getSource(); + final JasperPrint print = JasperFillManager.fillReport(compiled, this.parameters, source); + jasperPrintList.add(print); + } else { + + int totalpagesoffset = 0; + int pagesCount = 0; + + // erstes Rendern, um Seitenzahl zu ermitteln + for (final JasperDesigner designer : this.designs) { + + final JasperReport compiled = JasperCompileManager.compileReport(designer.getDesign()); + compiledReports.add(compiled); + final MetaAndTableJRDataSource source = designer.getSource(); + final JasperPrint print = JasperFillManager.fillReport(compiled, this.parameters, source); + + pagesCount = print.getPages().size(); + designer.setPagesCount(totalpagesoffset); + + totalpagesoffset += pagesCount; + } + totalpagesoffset = totalpagesoffset - pagesCount; // offset darf die letzten Seiten nicht beinhalten! + + // Sources mit pages machen + for (final JasperDesigner designer : this.designs) { + final JRDataSource sourceWithPages = designer.getSource().withPagePageOffsets(designer.getPagesCount(), totalpagesoffset); + sourcesWithPages.add(sourceWithPages); + } + + for (int i = 0; i < compiledReports.size(); i++) { + final JasperReport compiledReport = compiledReports.get(i); + final JRDataSource source = sourcesWithPages.get(i); + final JasperPrint print = JasperFillManager.fillReport(compiledReport, this.parameters, source); + jasperPrintList.add(print); + } + } + doExport(jasperPrintList, out); + } + + private void doExport(final List jasperPrintList, final OutputStream out) throws JRException { + final JRPdfExporter exporter = new JRPdfExporter(); + exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); + exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out); + // this will make a bookmark in the exported PDF for each of the reports + exporter.setParameter(JRPdfExporterParameter.IS_CREATING_BATCH_MODE_BOOKMARKS, Boolean.TRUE); + + exporter.exportReport(); + + } + +} \ No newline at end of file diff -r 4635872e7117 -r 460fcc128794 artifacts/src/main/java/org/dive4elements/river/artifacts/common/MetaAndTableJRDataSource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/common/MetaAndTableJRDataSource.java Tue Apr 17 13:19:56 2018 +0200 @@ -0,0 +1,72 @@ +/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde + * Software engineering by + * Björnsen Beratende Ingenieure GmbH + * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ +package org.dive4elements.river.artifacts.common; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.sf.jasperreports.engine.JRDataSource; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JRField; + +/** + * @author Raimund Renkert + */ +public final class MetaAndTableJRDataSource implements JRDataSource { + + private final List data = new ArrayList<>(); + + private final Map metaData = new HashMap<>(); + + private int index = -1; + + public void addData(final String[] row) { + this.data.add(row); + } + + public void addMetaData(final String key, final String value) { + this.metaData.put(key, value); + } + + @Override + public boolean next() throws JRException { + this.index++; + + return this.index < this.data.size(); + } + + @Override + public Object getFieldValue(final JRField field) throws JRException { + final String fieldName = field.getName(); + + if (fieldName.startsWith("meta:")) + return this.metaData.get(fieldName.substring("meta:".length())); + + if (fieldName.startsWith("data:")) { + final int column = Integer.valueOf(fieldName.substring("data:".length())); + return this.data.get(this.index)[column]; + } + return null; + } + + public JRDataSource withPagePageOffsets(final int startpageoffset, final int totalpagesoffset) { + + final MetaAndTableJRDataSource newSource = new MetaAndTableJRDataSource(); + newSource.data.addAll(this.data); + newSource.metaData.putAll(this.metaData); + + newSource.addMetaData("meta:startpageoffset", "" + startpageoffset); + newSource.addMetaData("meta:totalpagesoffset", "" + totalpagesoffset); + + return newSource; + } +} \ No newline at end of file diff -r 4635872e7117 -r 460fcc128794 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoExporter.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoExporter.java Tue Apr 17 13:18:56 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoExporter.java Tue Apr 17 13:19:56 2018 +0200 @@ -19,9 +19,9 @@ import org.dive4elements.river.artifacts.common.AbstractCommonExporter; import org.dive4elements.river.artifacts.common.GeneralResultType; import org.dive4elements.river.artifacts.common.I18NStrings; +import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource; import org.dive4elements.river.artifacts.resources.Resources; import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; -import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; import org.dive4elements.river.artifacts.sinfo.util.WstInfo; diff -r 4635872e7117 -r 460fcc128794 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/util/MetaAndTableJRDataSource.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/util/MetaAndTableJRDataSource.java Tue Apr 17 13:18:56 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde - * Software engineering by - * Björnsen Beratende Ingenieure GmbH - * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt - * - * This file is Free Software under the GNU AGPL (>=v3) - * and comes with ABSOLUTELY NO WARRANTY! Check out the - * documentation coming with Dive4Elements River for details. - */ -package org.dive4elements.river.artifacts.sinfo.util; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import net.sf.jasperreports.engine.JRDataSource; -import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JRField; - -/** - * @author Raimund Renkert - */ -public final class MetaAndTableJRDataSource implements JRDataSource { - - private final List data = new ArrayList<>(); - - private final Map metaData = new HashMap<>(); - - private int index = -1; - - public void addData(final String[] row) { - this.data.add(row); - } - - public void addMetaData(final String key, final String value) { - this.metaData.put(key, value); - } - - @Override - public boolean next() throws JRException { - this.index++; - - return this.index < this.data.size(); - } - - @Override - public Object getFieldValue(final JRField field) throws JRException { - final String fieldName = field.getName(); - - if (fieldName.startsWith("meta:")) - return this.metaData.get(fieldName.substring("meta:".length())); - - if (fieldName.startsWith("data:")) { - final int column = Integer.valueOf(fieldName.substring("data:".length())); - return this.data.get(this.index)[column]; - } - - return null; - } -} \ No newline at end of file