# HG changeset patch # User Andre Heinecke # Date 1374146193 -7200 # Node ID 5ecc6d4d73f263c7c58c87bd8d33db998706ce60 # Parent df1140486ba49d025581e91eaf579dd5fa92e84f Add official fixings to Waterlevel CSV Export (issue1384) This searches the collection for staticwqkms artifacts that contain official data and adds that data to the export. The data is filtered by the calculation range and sorted by the calculation direction. diff -r df1140486ba4 -r 5ecc6d4d73f2 artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java --- a/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java Thu Jul 18 13:14:57 2013 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java Thu Jul 18 13:16:33 2013 +0200 @@ -34,6 +34,7 @@ import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JRException; +import org.dive4elements.artifacts.Artifact; import org.dive4elements.artifacts.CallContext; import org.dive4elements.artifacts.CallMeta; import org.dive4elements.artifacts.common.utils.Config; @@ -45,6 +46,7 @@ import org.dive4elements.river.artifacts.FixationArtifact; import org.dive4elements.river.artifacts.D4EArtifact; import org.dive4elements.river.artifacts.WINFOArtifact; +import org.dive4elements.river.artifacts.StaticWQKmsArtifact; import org.dive4elements.river.artifacts.model.CalculationResult; import org.dive4elements.river.artifacts.model.Segment; import org.dive4elements.river.artifacts.model.WQCKms; @@ -70,6 +72,9 @@ public static final String FACET_WST = "wst"; + /* This should be the same as in the StaticWQKmsArtifact */ + public static final String STATICWQKMSNAME = "staticwqkms"; + public static final String CSV_KM_HEADER = "export.waterlevel.csv.header.km"; @@ -134,9 +139,11 @@ public static final String PDF_HEADER_MODE = "export.waterlevel.pdf.mode"; public static final String JASPER_FILE = "export.waterlevel.pdf.file"; - /** The storage that contains all WQKms objects for the different facets.*/ + /** The storage that contains all WQKms objects that are calculated.*/ protected List data; + /** The storage that contains official fixings if available.*/ + protected List officalFixings; public void init(Document request, OutputStream out, CallContext context) { logger.debug("WaterlevelExporter.init"); @@ -153,6 +160,37 @@ { logger.debug("WaterlevelExporter.generate"); + /* Check for official fixings. They should also be included in the + * export but only the calculation result is added with addData */ + + officalFixings = new ArrayList(); + + for (Artifact art: collection.getArtifactsByName(STATICWQKMSNAME, context)) { + if (art instanceof StaticWQKmsArtifact) { + StaticWQKmsArtifact sart = (StaticWQKmsArtifact) art; + if (!sart.isOfficial()) { + continue; + } + + /* Check that we add the data only once */ + WQKms toAdd = sart.getWQKms(); + String newName = toAdd.getName(); + + boolean exists = false; + for (WQKms wqkm: officalFixings) { + /* The same official fixing could be in two + artifacts/outs so let's deduplicate */ + if (wqkm.getName().equals(newName)) { + exists = true; + } + } + if (!exists) { + officalFixings.add(toAdd); + logger.debug("Adding additional offical fixing: " + newName); + } + } + } + if (facet != null && facet.equals(AbstractExporter.FACET_CSV)) { generateCSV(); } @@ -294,13 +332,73 @@ writeCSVMeta(writer); writeCSVHeader(writer, atGauge, isQ); + Double first = Double.NaN; + Double last = Double.NaN; + for (WQKms[] tmp: data) { for (WQKms wqkms: tmp) { wQKms2CSV(writer, wqkms, atGauge, isQ); + double[] firstLast = wqkms.getFirstLastKM(); + if (first.isNaN()) { + /* Initialize */ + first = firstLast[0]; + last = firstLast[1]; + } + if (firstLast[0] > firstLast[1]) { + /* Calculating upstream we assert that it is + * impossible that the direction changes during this + * loop */ + first = Math.max(first, firstLast[0]); + last = Math.min(last, firstLast[1]); + } else if (firstLast[0] < firstLast[1]) { + first = Math.min(first, firstLast[0]); + last = Math.max(last, firstLast[1]); + } else { + first = last = firstLast[0]; + } } } + /* Append the official fixing at the bottom */ + for (WQKms wqkms: officalFixings) { + logger.debug("Exporting official fixing fromKM: " + first + + " toKM: " + last); + /* To handle upstream / downstream and to limit + * the officialFixings to the calculation distance + * we create a new wqkms object here and fill it only + * with the relevant data. */ + if (first.isNaN() || last.isNaN()) { + logger.warn("Exporting official fixing without valid first/last."); + wQKms2CSV(writer, wqkms, atGauge, isQ); + return; + } + int firstIdx = first > last ? wqkms.size() - 1 : 0; + int lastIdx = first > last ? 0 : wqkms.size() -1; + WQKms filtered = new WQKms (wqkms.size()); + filtered.setReferenceSystem(wqkms.getReferenceSystem()); + filtered.setName(wqkms.getName()); + double [] dp = new double [3]; + + if (first > last) { + for (int i = wqkms.size() - 1; i >= 0; i--) { + dp = wqkms.get(i, dp); + if (dp[2] < first && dp[2] > last) { + filtered.add(dp[0], dp[1], dp[2]); + } + } + } else { + for (int i = 0; i < wqkms.size(); i++) { + dp = wqkms.get(i, dp); + if (dp[2] < last && dp[2] > first) { + filtered.add(dp[0], dp[1], dp[2]); + } + } + } + wQKms2CSV(writer, filtered, atGauge, isQ); + } } - +/* + private WQKms filterWQKms (wkqm +*/ protected void writeCSVMeta(CSVWriter writer) { logger.info("WaterlevelExporter.writeCSVMeta");