# HG changeset patch # User gernotbelger # Date 1523553370 -7200 # Node ID 50cc99579a46202aae8f83f87a1c412875d296d6 # Parent e3b3ce32c8252148d695c8b128082d422b8c1a19 Work on uinfo diff -r e3b3ce32c825 -r 50cc99579a46 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoCalculationResult.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoCalculationResult.java Thu Apr 12 19:15:42 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoCalculationResult.java Thu Apr 12 19:16:10 2018 +0200 @@ -8,83 +8,29 @@ * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.artifacts.sinfo.common; - -import java.io.Serializable; -import java.util.ArrayList; + import java.util.Collection; -import java.util.Collections; -import java.util.List; - +import org.dive4elements.river.artifacts.common.AbstractCalculationResult; +import org.dive4elements.river.artifacts.common.ResultRow; import org.dive4elements.river.artifacts.sinfo.util.WstInfo; - -import gnu.trove.TDoubleArrayList; + /** * @author Gernot Belger */ -public abstract class AbstractSInfoCalculationResult implements Serializable { +public abstract class AbstractSInfoCalculationResult extends AbstractCalculationResult { private static final long serialVersionUID = 1L; - - private final Collection rows; - - private final String label; - + private final WstInfo wst; - public AbstractSInfoCalculationResult(final String label, final WstInfo wst, final Collection rows) { - this.label = label; - this.wst = wst; - this.rows = new ArrayList<>(rows); + public AbstractSInfoCalculationResult(final String label, final WstInfo wst, final Collection< ResultRow> rows) { + super(label, rows); + this.wst = wst; } - public final String getLabel() { - return this.label; - } public final WstInfo getWst() { return this.wst; - } - - public boolean isEmpty() { - return this.rows.isEmpty(); - } - - public final void addRow(final SInfoResultRow resultRow) { - this.rows.add(resultRow); - } - - public final Collection getRows() { - return Collections.unmodifiableCollection(this.rows); - } - - public final double[][] getStationPoints(final SInfoResultType type) { - - final TDoubleArrayList xPoints = new TDoubleArrayList(this.rows.size()); - final TDoubleArrayList yPoints = new TDoubleArrayList(this.rows.size()); - - for (final SInfoResultRow row : this.rows) { - - final double station = row.getDoubleValue(SInfoResultType.station); - final double value = row.getDoubleValue(type); - - xPoints.add(station); - yPoints.add(value); - } - - return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() }; - } - - protected final List getValues(final SInfoResultType type) { - - final List values = new ArrayList<>(); - - for (final SInfoResultRow row : this.rows) { - @SuppressWarnings("unchecked") - final TYPE value = (TYPE) row.getValue(type); - values.add(value); - } - - return values; - } + } } \ No newline at end of file diff -r e3b3ce32c825 -r 50cc99579a46 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoCalculationResults.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoCalculationResults.java Thu Apr 12 19:15:42 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoCalculationResults.java Thu Apr 12 19:16:10 2018 +0200 @@ -9,69 +9,23 @@ */ package org.dive4elements.river.artifacts.sinfo.common; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; + import org.apache.commons.lang.math.DoubleRange; -import org.dive4elements.river.artifacts.model.Calculation; +import org.dive4elements.river.artifacts.common.AbstractCalculationResults; + import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; /** * @author Gernot Belger */ -public abstract class AbstractSInfoCalculationResults implements Serializable { - - private static final long serialVersionUID = 1L; - - private final List results = new ArrayList<>(); - - private final String calcModeLabel; - - private final String user; - - private final RiverInfo river; - - private final DoubleRange calcRange; - - public AbstractSInfoCalculationResults(final String calcModeLabel, final String user, final RiverInfo river, final DoubleRange calcRange) { - this.calcModeLabel = calcModeLabel; - this.user = user; - this.river = river; - this.calcRange = calcRange; - } - - public final String getCalcModeLabel() { - return this.calcModeLabel; - } +public abstract class AbstractSInfoCalculationResults extends AbstractCalculationResults { - public final String getUser() { - return this.user; - } - - public final RiverInfo getRiver() { - return this.river; - } - - public final DoubleRange getCalcRange() { - return this.calcRange; - } - - public final void addResult(final RESULT result, final Calculation problems) { - /* we assume error reporting was already done in this case */ - if (result == null) - return; - - if (result.isEmpty()) { - problems.addProblem("abstractsinfocalculationresults.emptyResult", result.getLabel()); - return; - } - - this.results.add(result); - } - - public final List getResults() { - return Collections.unmodifiableList(this.results); - } + private static final long serialVersionUID = 1L; + + //TODO: einen sinnvollen Grund finden, warum es diese Klasse noch geben muss, oder LÖSCHEN! + public AbstractSInfoCalculationResults(final String calcModeLabel, final String user, final RiverInfo river, final DoubleRange calcRange) { + super(calcModeLabel, user, river, calcRange); //TODO: Split! + } + } \ No newline at end of file diff -r e3b3ce32c825 -r 50cc99579a46 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 Thu Apr 12 19:15:42 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractSInfoExporter.java Thu Apr 12 19:16:10 2018 +0200 @@ -9,40 +9,28 @@ */ package org.dive4elements.river.artifacts.sinfo.common; -import java.io.OutputStream; import java.text.DateFormat; import java.text.NumberFormat; -import java.util.Collection; import java.util.Date; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; import org.apache.commons.lang.math.DoubleRange; -import org.apache.log4j.Logger; -import org.dive4elements.artifacts.CallMeta; -import org.dive4elements.artifacts.common.utils.Config; import org.dive4elements.river.FLYS; -import org.dive4elements.river.artifacts.model.CalculationResult; +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.resources.Resources; -import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings; 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; -import org.dive4elements.river.exports.AbstractExporter; import au.com.bytecode.opencsv.CSVWriter; -import net.sf.jasperreports.engine.JRDataSource; -import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JasperExportManager; -import net.sf.jasperreports.engine.JasperFillManager; -import net.sf.jasperreports.engine.JasperPrint; /** * @author Gernot Belger */ -public abstract class AbstractSInfoExporter> extends AbstractExporter { +public abstract class AbstractSInfoExporter> extends AbstractCommonExporter { private static final String CSV_META_HEADER_SOUNDING = "sinfo.export.flow_depth.csv.meta.header.sounding"; @@ -50,191 +38,12 @@ private static final String CSV_META_HEADER_SOUNDING_TYPE = "sinfo.export.flow_depth.csv.meta.header.sounding.type"; - private static final String CSV_META_HEADER_SOUNDING_EVALUATOR = "sinfo.export.flow_depth.csv.meta.header.sounding.evaluator"; - private static final String CSV_META_HEADER_SOUNDING_PRJ = "sinfo.export.flow_depth.csv.meta.header.sounding.prj"; private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel"; private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel.original"; - /** The storage that contains the current calculation result. */ - private RESULTS data = null; - - protected abstract Logger getLog(); - - public 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; - - final RiverInfo river = results.getRiver(); - - /* write as csv */ - writeCSVGlobalMetadata(writer, results); - writeCSVHeader(writer, results, river); - - for (final RESULT result : results.getResults()) { - writeCSVResult(writer, results, result); - } - } - - 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) }); - } - - protected final void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) { - - writeCSVResultMetadata(writer, results, result); - - /* nwo the value rows */ - final Collection rows = result.getRows(); - for (final SInfoResultRow 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 final void writeCSVRow(final CSVWriter writer, final RESULTS results, final RESULT result, final SInfoResultRow row) { - getLog().debug("writeCSVFlowDepthRow"); - - final String[] formattedRow = formatCSVRow(results, result, row); - writer.writeNext(formattedRow); - } - - protected abstract String[] formatCSVRow(RESULTS results, RESULT result, final SInfoResultRow row); - - @Override - protected final void writePDF(final OutputStream outStream) { - getLog().debug("write PDF"); - - final JRDataSource source = 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 { - final String jasperPath = confPath + getJasperFile(); - - final JasperPrint print = JasperFillManager.fillReport(jasperPath, parameters, source); - JasperExportManager.exportReportToPdfStream(print, outStream); - } - catch (final JRException je) { - getLog().warn("Error generating PDF Report!", je); - } - } - - protected abstract String getJasperFile(); - - private JRDataSource createJRData() { - - /* 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); - - protected final void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) { - - final Collection rows = result.getRows(); - - for (final SInfoResultRow row : rows) { - - final String[] formattedRow = formatPDFRow(results, row); - source.addData(formattedRow); - } - } - - protected abstract String[] formatPDFRow(RESULTS results, final SInfoResultRow row); - - protected final void writeCSVGlobalMetadataDefaults(final CSVWriter writer, final AbstractSInfoCalculationResults results) { - - final String calcModeLabel = results.getCalcModeLabel(); - final RiverInfo river = results.getRiver(); - final DoubleRange calcRange = results.getCalcRange(); - - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_RESULT, msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel); - - // "# FLYS-Version: " - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_VERSION, msg(SInfoI18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION); - - // "# Bearbeiter: " - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_USER, msg(SInfoI18NStrings.CSV_META_USER_LABEL), results.getUser()); - - // "# Datum der Erstellung: " - final Locale locale = Resources.getLocale(this.context.getMeta()); - final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_CREATION, msg(SInfoI18NStrings.CSV_META_CREATION_LABEL), df.format(new Date())); - - // "# Gewässer: " - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_RIVER, msg(SInfoI18NStrings.CSV_META_RIVER_LABEL), river.getName()); - - // "# Höhensystem des Flusses: " - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); - - // "# Ort/Bereich (km): " - writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_RANGE, msg(SInfoI18NStrings.CSV_META_RANGE_LABEL), - getKmFormatter().format(calcRange.getMinimumDouble()), getKmFormatter().format(calcRange.getMaximumDouble())); - } - protected final void writeCSVSoundingMetadata(final CSVWriter writer, final BedHeightInfo sounding) { writeCSVSoundingMetadata(writer, sounding, CSV_META_HEADER_SOUNDING); } @@ -248,7 +57,7 @@ // "# Aufnahmeart: " writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_TYPE, sounding.getType()); // "# Auswerter: " - writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_EVALUATOR, sounding.getEvaluationBy()); + writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_EVALUATOR, sounding.getEvaluationBy()); // "# Lagesystem: " writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_PRJ, sounding.getLocationSystem()); // "# Höhensystem: " @@ -277,33 +86,95 @@ writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_YEAR, Integer.toString(year)); } - protected final void addJRMetaDataDefaults(final MetaAndTableJRDataSource source, final AbstractSInfoCalculationResults results) { + @Override + protected void writeCSVGlobalMetadataDefaults(final CSVWriter writer, final RESULTS results) { + + final String calcModeLabel = results.getCalcModeLabel(); + final RiverInfo river = results.getRiver(); + final DoubleRange calcRange = results.getCalcRange(); + + writeCSVMetaEntry(writer, I18NStrings.CSV_META_HEADER_RESULT, msg(I18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel); + + // "# FLYS-Version: " + writeCSVMetaEntry(writer, I18NStrings.CSV_META_VERSION, msg(I18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION); + + // "# Bearbeiter: " + writeCSVMetaEntry(writer, I18NStrings.CSV_META_USER, msg(I18NStrings.CSV_META_USER_LABEL), results.getUser()); + + // "# Datum der Erstellung: " + final Locale locale = Resources.getLocale(this.context.getMeta()); + final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); + writeCSVMetaEntry(writer, I18NStrings.CSV_META_CREATION, msg(I18NStrings.CSV_META_CREATION_LABEL), df.format(new Date())); + + // "# Gewässer: " + writeCSVMetaEntry(writer, I18NStrings.CSV_META_RIVER, msg(I18NStrings.CSV_META_RIVER_LABEL), river.getName()); + + // "# Höhensystem des Flusses: " + writeCSVMetaEntry(writer, I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); + + if (calcRange != null) { + // "# Ort/Bereich (km): " + writeCSVMetaEntry(writer, I18NStrings.CSV_META_RANGE, msg(I18NStrings.CSV_META_RANGE_LABEL), getKmFormatter().format(calcRange.getMinimumDouble()), + getKmFormatter().format(calcRange.getMaximumDouble())); + } + } + + @Override + protected void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results) { + + /* general metadata */ + this.addJRMetaDataDefaults(source, results); final RiverInfo river = results.getRiver(); final String wstUnitName = river.getWstUnit(); - source.addMetaData("header", msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL)); - source.addMetaData("calcMode", results.getCalcModeLabel()); - - source.addMetaData("version_label", msg(SInfoI18NStrings.CSV_META_VERSION_LABEL)); - source.addMetaData("version", FLYS.VERSION); - - source.addMetaData("user_label", msg(SInfoI18NStrings.CSV_META_USER_LABEL)); - source.addMetaData("user", results.getUser()); - - final Locale locale = Resources.getLocale(this.context.getMeta()); - final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); - source.addMetaData("date_label", msg(SInfoI18NStrings.CSV_META_CREATION_LABEL)); - source.addMetaData("date", df.format(new Date())); - - source.addMetaData("river_label", msg(SInfoI18NStrings.CSV_META_RIVER_LABEL)); + source.addMetaData("river_label", msg(I18NStrings.CSV_META_RIVER_LABEL)); source.addMetaData("river", river.getName()); source.addMetaData("river_unit", wstUnitName); final DoubleRange calcRange = results.getCalcRange(); final NumberFormat kmFormatter = getKmFormatter(); final String rangeValue = String.format("%s - %s", kmFormatter.format(calcRange.getMinimumDouble()), kmFormatter.format(calcRange.getMaximumDouble())); - source.addMetaData("range_label", msg(SInfoI18NStrings.CSV_META_RANGE_LABEL)); + source.addMetaData("range_label", msg(I18NStrings.CSV_META_RANGE_LABEL)); source.addMetaData("range", rangeValue); + + /* column headings */ + // source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta())); // moved to + // super + + // TODO: move to subs + /* column headings */ + source.addMetaData("flowdepth_header", SInfoResultType.flowdepth.getPdfHeader(this.context.getMeta())); + source.addMetaData("flowdepth_tkh_header", SInfoResultType.flowdepthtkh.getPdfHeader(this.context.getMeta())); + source.addMetaData("tkh_header", SInfoResultType.tkh.getPdfHeader(this.context.getMeta())); + source.addMetaData("waterlevel_header", SInfoResultType.waterlevel.getPdfHeader(this.context.getMeta())); + source.addMetaData("discharge_header", SInfoResultType.discharge.getPdfHeader(this.context.getMeta())); + source.addMetaData("waterlevel_name_header", SInfoResultType.waterlevelLabel.getPdfHeader(this.context.getMeta())); + source.addMetaData("gauge_header", SInfoResultType.gaugeLabel.getPdfHeader(this.context.getMeta())); + source.addMetaData("bedheight_header", SInfoResultType.meanBedHeight.getPdfHeader(this.context.getMeta())); + source.addMetaData("sounding_name_header", SInfoResultType.soundingLabel.getPdfHeader(this.context.getMeta())); + source.addMetaData("location_header", SInfoResultType.location.getPdfHeader(this.context.getMeta())); } + + @Override + protected void addJRMetaDataDefaults(final MetaAndTableJRDataSource source, final RESULTS results) { + + source.addMetaData("header", msg(I18NStrings.CSV_META_HEADER_RESULT_LABEL)); + source.addMetaData("calcMode", results.getCalcModeLabel()); + + source.addMetaData("version_label", msg(I18NStrings.CSV_META_VERSION_LABEL)); + source.addMetaData("version", FLYS.VERSION); + + source.addMetaData("user_label", msg(I18NStrings.CSV_META_USER_LABEL)); + source.addMetaData("user", results.getUser()); + + final Locale locale = Resources.getLocale(this.context.getMeta()); + final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); + source.addMetaData("date_label", msg(I18NStrings.CSV_META_CREATION_LABEL)); + source.addMetaData("date", df.format(new Date())); + + /* column headings */ + source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta())); + } + } \ No newline at end of file diff -r e3b3ce32c825 -r 50cc99579a46 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractTkhCalculationResult.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractTkhCalculationResult.java Thu Apr 12 19:15:42 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractTkhCalculationResult.java Thu Apr 12 19:16:10 2018 +0200 @@ -12,6 +12,7 @@ import java.util.Collection; import java.util.List; +import org.dive4elements.river.artifacts.common.ResultRow; import org.dive4elements.river.artifacts.sinfo.tkhcalculation.SoilKind; import org.dive4elements.river.artifacts.sinfo.util.WstInfo; @@ -26,7 +27,7 @@ private final boolean hasTkh; - public AbstractTkhCalculationResult(final String label, final WstInfo wst, final boolean hasTkh, final Collection rows) { + public AbstractTkhCalculationResult(final String label, final WstInfo wst, final boolean hasTkh, final Collection rows) { super(label, wst, rows); this.hasTkh = hasTkh; diff -r e3b3ce32c825 -r 50cc99579a46 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultFacet.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultFacet.java Thu Apr 12 19:15:42 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultFacet.java Thu Apr 12 19:16:10 2018 +0200 @@ -45,7 +45,7 @@ final CalculationResult res = (CalculationResult) flys.compute(context, this.hash, this.stateId, this.type, false); - final AbstractSInfoCalculationResults data = (AbstractSInfoCalculationResults) res.getData(); + final AbstractSInfoCalculationResults data = (AbstractSInfoCalculationResults) res.getData(); return data.getResults().get(this.index); } diff -r e3b3ce32c825 -r 50cc99579a46 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java Thu Apr 12 19:15:42 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java Thu Apr 12 19:16:10 2018 +0200 @@ -10,15 +10,12 @@ package org.dive4elements.river.artifacts.sinfo.common; import java.text.NumberFormat; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; import org.apache.commons.lang.StringUtils; import org.dive4elements.artifacts.CallContext; -import org.dive4elements.artifacts.CallMeta; +import org.dive4elements.river.artifacts.common.AbstractResultType; +import org.dive4elements.river.artifacts.common.I18NStrings; import org.dive4elements.river.artifacts.resources.Resources; -import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings; import org.dive4elements.river.artifacts.sinfo.tkhcalculation.SoilKind; import org.dive4elements.river.utils.Formatter; import org.dive4elements.river.utils.RiverUtils; @@ -28,22 +25,18 @@ * * @author Gernot Belger */ -public enum SInfoResultType { +public abstract class SInfoResultType extends AbstractResultType { - station(SInfoI18NStrings.UNIT_KM, SInfoI18NStrings.CSV_KM_HEADER) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } + private SInfoResultType(final String unit, final String csvHeader) { + super(unit, csvHeader, csvHeader); + } - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getWaterlevelKM(context); - } - }, + private SInfoResultType(final String unit, final String csvHeader, final String pdfHeader) { + super(unit, csvHeader, pdfHeader); + } - waterlevel(null, SInfoI18NStrings.CSV_WATERLEVEL_HEADER,"sinfo.export.flow_depth.pdf.header.waterlevel") { + public static final SInfoResultType waterlevel = new SInfoResultType(null, SInfoI18NStrings.CSV_WATERLEVEL_HEADER, + "sinfo.export.flow_depth.pdf.header.waterlevel") { @Override public String exportValue(final CallContext context, final Object value) { final double doubleValue = asDouble(value); @@ -56,9 +49,9 @@ // digits. return Formatter.getFlowDepth(context); } - }, + }; - waterlevelLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LABEL_HEADER) { + public static final SInfoResultType waterlevelLabel = new SInfoResultType(I18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LABEL_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { return exportStringValue(value); @@ -68,9 +61,9 @@ protected NumberFormat createFormatter(final CallContext context) { throw new UnsupportedOperationException(); } - }, + }; - discharge(SInfoI18NStrings.UNIT_CUBIC_M, SInfoI18NStrings.CSV_DISCHARGE_HEADER) { + public static final SInfoResultType discharge = new SInfoResultType(I18NStrings.UNIT_CUBIC_M, SInfoI18NStrings.CSV_DISCHARGE_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { final double doubleValue = asDouble(value); @@ -82,9 +75,10 @@ protected NumberFormat createFormatter(final CallContext context) { return Formatter.getWaterlevelQ(context); } - }, + }; - meanBedHeight(null, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER_SHORT) { + public static final SInfoResultType meanBedHeight = new SInfoResultType(null, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, + SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER_SHORT) { @Override public String exportValue(final CallContext context, final Object value) { final double doubleValue = asDouble(value); @@ -95,9 +89,9 @@ protected NumberFormat createFormatter(final CallContext context) { return Formatter.getMeanBedHeight(context); } - }, + }; - soundingLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_SOUNDING_HEADER) { + public static final SInfoResultType soundingLabel = new SInfoResultType(I18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_SOUNDING_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { return exportStringValue(value); @@ -107,9 +101,22 @@ protected NumberFormat createFormatter(final CallContext context) { throw new UnsupportedOperationException(); } - }, + }; + public static final SInfoResultType flowdepthDevelopmentPerYear = new SInfoResultType(I18NStrings.UNIT_CM_A, + SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_PER_YEAR_HEADER) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } - flowdepthmin(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MIN_HEADER) { + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getFlowDepthDevelopmentPerYear(context); + } + }; + + public static final SInfoResultType flowdepthmin = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MIN_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { final double doubleValue = asDouble(value); @@ -120,9 +127,9 @@ protected NumberFormat createFormatter(final CallContext context) { return Formatter.getFlowDepth(context); } - }, + }; - flowdepthmax(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MAX_HEADER) { + public static final SInfoResultType flowdepthmax = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MAX_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { final double doubleValue = asDouble(value); @@ -133,9 +140,9 @@ protected NumberFormat createFormatter(final CallContext context) { return Formatter.getFlowDepth(context); } - }, + }; - gaugeLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_GAUGE_HEADER) { + public static final SInfoResultType gaugeLabel = new SInfoResultType(I18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_GAUGE_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { return exportStringValue(value); @@ -145,9 +152,9 @@ protected NumberFormat createFormatter(final CallContext context) { throw new UnsupportedOperationException(); } - }, + }; - location(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LOCATION_HEADER) { + public static final SInfoResultType location = new SInfoResultType(I18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LOCATION_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { return exportStringValue(value); @@ -157,9 +164,10 @@ protected NumberFormat createFormatter(final CallContext context) { throw new UnsupportedOperationException(); } - }, + }; - soilkind(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_TKHKIND_HEADER, "sinfo.export.tkh.pdf.header.tkhkind") { + public static final SInfoResultType soilkind = new SInfoResultType(I18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_TKHKIND_HEADER, + "sinfo.export.tkh.pdf.header.tkhkind") { @Override public String exportValue(final CallContext context, final Object value) { @@ -176,173 +184,9 @@ protected NumberFormat createFormatter(final CallContext context) { throw new UnsupportedOperationException(); } - }, - - flowdepth(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HEADER) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getFlowDepth(context); - } - }, - - d50(null, null) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - throw new UnsupportedOperationException(); - } - }, - - velocity(null, null) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - throw new UnsupportedOperationException(); - } - }, - - tau(null, null) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - throw new UnsupportedOperationException(); - } - }, - - tkh(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_TKH_HEADER, SInfoI18NStrings.CSV_TKH_HEADER_SHORT) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getTkh(context); - } - }, - - tkhup(SInfoI18NStrings.UNIT_CM, null) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - throw new UnsupportedOperationException(); - } - }, + }; - tkhdown(SInfoI18NStrings.UNIT_CM, null) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - throw new UnsupportedOperationException(); - } - }, - - flowdepthtkh(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTHTKH_HEADER) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getFlowDepth(context); - } - }, - - flowdepthDevelopment(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_HEADER, SInfoI18NStrings.PDF_FLOWDEPTH_DEVELOPMENT_HEADER) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getTkh(context); - } - }, - flowdepthDevelopmentPerYear(SInfoI18NStrings.UNIT_CM_A, SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_PER_YEAR_HEADER) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getFlowDepthDevelopmentPerYear(context); - } - }, - waterlevelDifference(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_WATERLEVEL_DIFFERENCE_HEADER, "sinfo.export.pdf.header.waterlevel.difference") { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getTkh(context); - } - }, - bedHeightDifference(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_DIFFERENCE_HEADER, "sinfo.export.pdf.header.mean_bed_height.difference") { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getTkh(context); - } - }, - flowdepthCurrent(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_CURRENT_HEADER) { - @Override - public String exportValue(final CallContext context, final Object value) { - final double doubleValue = asDouble(value); - return exportDoubleValue(context, doubleValue); - } - - @Override - protected NumberFormat createFormatter(final CallContext context) { - return Formatter.getFlowDepth(context); - } - }, - flowdepthHistorical(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HISTORICAL_HEADER) { + public static final SInfoResultType flowdepth = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HEADER) { @Override public String exportValue(final CallContext context, final Object value) { final double doubleValue = asDouble(value); @@ -355,82 +199,173 @@ } }; - /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ - private final Map formatters = new HashMap<>(); - - private final String unit; - - private final String csvHeader; - - private final String pdfHeader; - - private SInfoResultType(final String unit, final String csvAndPdfHeader) { - this(unit, csvAndPdfHeader, csvAndPdfHeader); - } - - private SInfoResultType(final String unit, final String csvHeader, final String pdfHeader) { - this.unit = unit; - this.csvHeader = csvHeader; - this.pdfHeader = pdfHeader; - } - - public abstract String exportValue(final CallContext context, final Object value); - - protected final String exportStringValue(final Object value) { - - if (value == null) - return StringUtils.EMPTY; - - if (!(value instanceof String)) - throw new IllegalStateException(); - - return (String) value; - } - - protected final double asDouble(final Object value) { - if (value == null) - return Double.NaN; - - if (!(value instanceof Number)) - throw new IllegalStateException(); + public static final SInfoResultType d50 = new SInfoResultType(null, null) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } - final Number number = (Number) value; - return number.doubleValue(); - } - - protected final String exportDoubleValue(final CallContext context, final double value) { - if (Double.isNaN(value)) - return StringUtils.EMPTY; - - final NumberFormat formatter = getFormatter(context); - return formatter.format(value); - } - - private NumberFormat getFormatter(final CallContext context) { - final CallMeta meta = context.getMeta(); - final Locale locale = Resources.getLocale(meta); - - if (!this.formatters.containsKey(locale)) - this.formatters.put(locale, createFormatter(context)); + @Override + protected NumberFormat createFormatter(final CallContext context) { + throw new UnsupportedOperationException(); + } + }; - return this.formatters.get(locale); - } - - protected abstract NumberFormat createFormatter(CallContext context); + public static final SInfoResultType velocity = new SInfoResultType(null, null) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } - public final String getCsvHeader() { - return this.csvHeader; - } - - public final String getCsvHeader(final CallMeta meta) { - return Resources.getMsg(meta, this.csvHeader, this.csvHeader); - } + @Override + protected NumberFormat createFormatter(final CallContext context) { + throw new UnsupportedOperationException(); + } + }; - public final String getPdfHeader(final CallMeta meta) { - return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); - } + public static final SInfoResultType tau = new SInfoResultType(null, null) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } - public final String getUnit() { - return this.unit; - } + @Override + protected NumberFormat createFormatter(final CallContext context) { + throw new UnsupportedOperationException(); + } + }; + + public static final SInfoResultType tkh = new SInfoResultType(I18NStrings.UNIT_CM, SInfoI18NStrings.CSV_TKH_HEADER, SInfoI18NStrings.CSV_TKH_HEADER_SHORT) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getTkh(context); + } + }; + + public static final SInfoResultType tkhup = new SInfoResultType(I18NStrings.UNIT_M, null) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + throw new UnsupportedOperationException(); + } + }; + + public static final SInfoResultType tkhdown = new SInfoResultType(I18NStrings.UNIT_M, null) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + throw new UnsupportedOperationException(); + } + }; + + public static final SInfoResultType flowdepthtkh = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTHTKH_HEADER) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getFlowDepth(context); + } + }; + + public static final SInfoResultType flowdepthDevelopment = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_HEADER, + SInfoI18NStrings.PDF_FLOWDEPTH_DEVELOPMENT_HEADER) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getTkh(context); + } + }; + + public static final SInfoResultType depthDevelopmentPerYear = new SInfoResultType(I18NStrings.UNIT_CM_A, + SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_PER_YEAR_HEADER) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getFlowDepthDevelopmentPerYear(context); + } + }; + public static final SInfoResultType waterlevelDifference = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_WATERLEVEL_DIFFERENCE_HEADER, + "sinfo.export.pdf.header.waterlevel.difference") { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getTkh(context); + } + }; + public static final SInfoResultType bedHeightDifference = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_DIFFERENCE_HEADER, + "sinfo.export.pdf.header.mean_bed_height.difference") { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getTkh(context); + } + }; + public static final SInfoResultType flowdepthCurrent = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_CURRENT_HEADER) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getFlowDepth(context); + } + }; + public static final SInfoResultType flowdepthHistorical = new SInfoResultType(I18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HISTORICAL_HEADER) { + @Override + public String exportValue(final CallContext context, final Object value) { + final double doubleValue = asDouble(value); + return exportDoubleValue(context, doubleValue); + } + + @Override + protected NumberFormat createFormatter(final CallContext context) { + return Formatter.getFlowDepth(context); + } + }; + } \ No newline at end of file