Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 9005:460fcc128794
jasper multiple
author | gernotbelger |
---|---|
date | Tue, 17 Apr 2018 13:19:56 +0200 |
parents | e3b3ce32c825 |
children |
line wrap: on
line source
/** 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.Collection; import org.apache.log4j.Logger; import org.dive4elements.artifacts.CallMeta; import org.dive4elements.river.artifacts.model.CalculationResult; import org.dive4elements.river.artifacts.resources.Resources; import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; import org.dive4elements.river.exports.AbstractExporter; import au.com.bytecode.opencsv.CSVWriter; /** * @author Gernot Belger */ public abstract class AbstractCommonExporter<RESULT extends AbstractCalculationResult, RESULTS extends AbstractCalculationResults<RESULT>> extends AbstractExporter { /** The storage that contains the current calculation result. */ protected static enum ExportMode { pdf, csv } /** The log used in this exporter. */ protected abstract Logger getLog(); protected RESULTS data = null; public final RESULTS getData() { return this.data; } @Override protected final void addData(final Object d) { /* reset */ this.data = null; if (d instanceof CalculationResult) { final Object dat = ((CalculationResult) d).getData(); if (dat != null) { @SuppressWarnings("unchecked") final RESULTS result = (RESULTS) dat; this.data = result; } } } /** * Formats header with unit and label: msg [unit] (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 final void writeCSVData(final CSVWriter writer) { getLog().info("writeCSVData"); /* fetch calculation results */ final RESULTS results = this.data; /* write as csv */ writeCSVGlobalMetadata(writer, results); writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance final RiverInfo river = results.getRiver(); // FIXME :with comment if not first result writeCSVHeader(writer, results, river); writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance for (final RESULT result : results.getResults()) { writeCSVResult(writer, results, result); writer.writeNext(new String[] { "" }); // break line HERE after each resultset } } protected abstract void writeCSVHeader(final CSVWriter writer, final RESULTS results, final RiverInfo river); /** * Add metadata that is once written to the top of the file. */ protected abstract void writeCSVGlobalMetadata(final CSVWriter writer, final RESULTS results); 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 final void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) { writeCSVResultMetadata(writer, results, result); // 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 /* now the value rows */ final Collection<ResultRow> rows = result.getRows(); for (final ResultRow row : rows) { writeCSVRow(writer, results, result, row); } } /** * Add metadata that is written once per result set. */ protected abstract void writeCSVResultMetadata(CSVWriter writer, RESULTS results, RESULT result); protected void writeCSVRow(final CSVWriter writer, final RESULTS results, final RESULT result, final ResultRow row) { getLog().debug("writeCSVFlowDepthRow"); final String[] formattedRow = formatCSVRow(results, row); writer.writeNext(formattedRow); } protected final String[] formatCSVRow(final RESULTS results, final ResultRow row) { return formatRow(results, row, ExportMode.csv); } protected final MetaAndTableJRDataSource createJRData(final AbstractCalculationResults<RESULT> data) { /* fetch calculation results */ final RESULTS results = this.data; final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); addJRMetaData(source, results); for (final RESULT result : results.getResults()) addJRTableData(source, results, result); return source; } protected abstract void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results); private void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) { final Collection<ResultRow> rows = result.getRows(); for (final ResultRow row : rows) { final String[] formattedRow = formatPDFRow(results, row); source.addData(formattedRow); } } protected abstract String[] formatRow(RESULTS results, ResultRow row, ExportMode mode); 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); protected abstract void addJRMetaDataDefaults(final MetaAndTableJRDataSource source, final RESULTS results); }