teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
teichmann@5863: * Software engineering by Intevation GmbH
teichmann@5863: *
teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3)
teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the
teichmann@5994: * documentation coming with Dive4Elements River for details.
teichmann@5863: */
teichmann@5863:
teichmann@5831: package org.dive4elements.river.exports;
sascha@730:
sascha@730: import java.io.OutputStream;
sascha@730: import java.io.OutputStreamWriter;
sascha@730: import java.io.IOException;
sascha@730:
sascha@730: import org.w3c.dom.Document;
sascha@730:
sascha@730: import org.apache.log4j.Logger;
sascha@730:
teichmann@5831: import org.dive4elements.artifacts.Artifact;
teichmann@5831: import org.dive4elements.artifacts.CallContext;
sascha@730:
teichmann@5831: import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
teichmann@5831: import org.dive4elements.artifactdatabase.state.Settings;
sascha@730:
teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact;
teichmann@5831:
teichmann@5831: import org.dive4elements.river.artifacts.model.WQ;
teichmann@5867: import org.dive4elements.river.collections.D4EArtifactCollection;
teichmann@5831:
teichmann@5831: import org.dive4elements.river.model.Gauge;
teichmann@5831: import org.dive4elements.river.model.River;
teichmann@5831: import org.dive4elements.river.model.TimeInterval;
teichmann@5865: import org.dive4elements.river.utils.RiverUtils;
teichmann@5831: import org.dive4elements.river.artifacts.access.RangeAccess;
ingo@1658:
sascha@730: public class ATExporter
sascha@730: implements OutGenerator
sascha@730: {
sascha@730: private static Logger logger = Logger.getLogger(ATExporter.class);
sascha@730:
sascha@730: public static final String DEFAULT_ENCODING = "UTF-8";
sascha@730:
sascha@730: protected WQ data;
sascha@730: protected CallContext context;
sascha@730: protected OutputStream out;
teichmann@5867: protected D4EArtifact master;
sascha@730:
teichmann@5867: protected D4EArtifactCollection collection;
ingo@3422:
ingo@3422:
sascha@730: public ATExporter() {
sascha@730: }
sascha@730:
sascha@730: @Override
sascha@730: public void init(Document request, OutputStream out, CallContext context) {
sascha@730: this.context = context;
sascha@730: this.out = out;
sascha@730: }
sascha@730:
ingo@3422:
sascha@730: @Override
sascha@730: public void setMasterArtifact(Artifact master) {
teichmann@5867: this.master = (D4EArtifact) master;
sascha@730: }
sascha@730:
sascha@730: @Override
teichmann@5867: public void setCollection(D4EArtifactCollection collection) {
ingo@3422: this.collection = collection;
ingo@3422: }
ingo@3422:
ingo@3422: @Override
ingo@1684: public void doOut(
felix@1944: ArtifactAndFacet artifactf,
ingo@1684: Document attr,
ingo@1684: boolean visible
ingo@1684: ) {
felix@1944: data = (WQ)artifactf.getData(context);
sascha@730: }
sascha@730:
sascha@730: @Override
sascha@730: public void generate() throws IOException {
sascha@730:
sascha@730: if (data == null) {
sascha@730: logger.debug("no W/Q data");
sascha@730: return;
sascha@730: }
sascha@730:
teichmann@5865: River river = RiverUtils.getRiver(master);
teichmann@6101: RangeAccess rangeAccess = new RangeAccess(master);
felix@4849: double[] kms = rangeAccess.getLocations();
ingo@1658:
rrenkert@4938: Gauge gauge = river.determineGaugeByPosition(kms[0]);
teichmann@4948: if (Math.abs(kms[0] - gauge.getStation().doubleValue()) < 1e-4) {
rrenkert@4941: // at gauge.
rrenkert@4941: TimeInterval interval =
rrenkert@4941: gauge.fetchMasterDischargeTable().getTimeInterval();
aheinecke@6304: ATWriter.write(
aheinecke@6304: data,
rrenkert@4941: new OutputStreamWriter(out, DEFAULT_ENCODING),
rrenkert@4941: context.getMeta(),
rrenkert@4941: river.getName(),
rrenkert@4941: kms[0],
rrenkert@4941: gauge.getName(),
rrenkert@4941: gauge.getDatum(),
rrenkert@4941: interval.getStartTime(),
aheinecke@6304: river.getWstUnit().getName());
rrenkert@4941: }
rrenkert@4941: else {
rrenkert@4941: // at km
aheinecke@6304: ATWriter.write(
aheinecke@6304: data,
rrenkert@4941: new OutputStreamWriter(out),
rrenkert@4941: context.getMeta(),
rrenkert@4941: river.getName(),
rrenkert@4941: kms[0],
rrenkert@4941: null,
rrenkert@4941: null,
rrenkert@4941: null,
aheinecke@6304: river.getWstUnit().getName());
rrenkert@4941: }
rrenkert@4941:
sascha@730: }
ingo@1979:
ingo@1979:
ingo@1979: /**
ingo@1979: * Returns an instance of EmptySettings currently!
ingo@1979: *
ingo@1979: * @return an instance of EmptySettings.
ingo@1979: */
ingo@2047: @Override
ingo@1979: public Settings getSettings() {
ingo@1979: return new EmptySettings();
ingo@1979: }
ingo@2047:
ingo@2047:
ingo@2047: /**
ingo@2047: * This method is not implemented!
ingo@2047: *
ingo@2047: * @param settings A settings object.
ingo@2047: */
ingo@2047: @Override
ingo@2047: public void setSettings(Settings settings) {
ingo@2047: // do nothing here
ingo@2047: }
sascha@730: }
sascha@730: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :