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.artifacts.states;
ingo@2191:
felix@4928: import java.text.NumberFormat;
felix@4928:
ingo@2215: import java.util.List;
ingo@2191:
ingo@4232: import org.apache.log4j.Logger;
ingo@2191:
teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet;
teichmann@5914:
teichmann@5831: import org.dive4elements.artifacts.Artifact;
teichmann@5831: import org.dive4elements.artifacts.CallContext;
teichmann@5914:
teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
teichmann@5914:
teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact;
teichmann@5914:
teichmann@5914: import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess.EvaluationMode;
teichmann@5914:
teichmann@5831: import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess;
teichmann@5914:
teichmann@5914: import org.dive4elements.river.artifacts.model.Calculation6;
teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult;
teichmann@5831: import org.dive4elements.river.artifacts.model.DataFacet;
teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes;
teichmann@5831: import org.dive4elements.river.artifacts.model.HistoricalDischargeData;
teichmann@5831: import org.dive4elements.river.artifacts.model.HistoricalDischargeDifferenceFacet;
teichmann@5831: import org.dive4elements.river.artifacts.model.HistoricalDischargeFacet;
teichmann@5831: import org.dive4elements.river.artifacts.model.HistoricalDischargeWQFacet;
teichmann@5831: import org.dive4elements.river.artifacts.model.HistoricalWQTimerange;
teichmann@5831: import org.dive4elements.river.artifacts.model.ReportFacet;
teichmann@5831: import org.dive4elements.river.artifacts.model.WQTimerange;
teichmann@5914:
teichmann@5831: import org.dive4elements.river.artifacts.resources.Resources;
teichmann@5914:
teichmann@5914: import org.w3c.dom.Element;
ingo@2191:
ingo@2191: /**
felix@3047: * State to calculate historical discharge curves.
teichmann@4736: *
ingo@2191: * @author Ingo Weinzierl
ingo@2191: */
felix@4927: public class HistoricalDischargeComputeState
teichmann@5914: extends DefaultState
teichmann@5914: implements FacetTypes
teichmann@5914: {
teichmann@8202: private static final Logger log = Logger
ingo@4232: .getLogger(HistoricalDischargeComputeState.class);
ingo@2215:
ingo@4144: public static final String DEFAULT_UNIT = "cm";
ingo@4144:
ingo@2191: @Override
ingo@4232: protected void appendItems(Artifact artifact, ElementCreator creator,
ingo@4232: String name, CallContext context, Element select) {
ingo@2191: // TODO IMPLEMENT ME
ingo@2191: }
ingo@2215:
ingo@2215: @Override
teichmann@5867: public Object computeAdvance(D4EArtifact artifact, String hash,
ingo@4232: CallContext context, List facets, Object old) {
teichmann@8202: log.debug("HistoricalDischargeComputeState.computeAdvance");
ingo@2215:
teichmann@5914: HistoricalDischargeAccess access =
teichmann@5914: new HistoricalDischargeAccess(artifact);
ingo@2215:
teichmann@5914: CalculationResult res = old instanceof CalculationResult
teichmann@5914: ? (CalculationResult)old
teichmann@5914: : new Calculation6(access).calculate();
ingo@2215:
ingo@2215: if (facets == null) {
ingo@2215: return res;
ingo@2215: }
ingo@2215:
ingo@2250: if (res.getReport().hasProblems()) {
ingo@2250: facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id));
ingo@2250: }
ingo@2250:
ingo@4241: HistoricalDischargeData data = (HistoricalDischargeData) res.getData();
ingo@4241:
ingo@4241: WQTimerange[] wqts = (WQTimerange[]) data.getWQTimeranges();
ingo@4241: if (wqts != null && wqts.length > 0) {
ingo@4241: facets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE,
ingo@4241: hash, id));
ingo@4241:
ingo@4241: facets.add(new DataFacet(PDF, "PDF data", ComputeType.ADVANCE,
ingo@4241: hash, id));
ingo@4241:
ingo@4241: prepareFacets(facets, wqts, access);
ingo@2228: }
ingo@2215:
tom@8462: prepareWQFacets(context, facets, access, hash);
ingo@2215:
ingo@2215: return res;
ingo@2215: }
ingo@2228:
teichmann@5914: protected void prepareFacets(
teichmann@5914: List facets,
teichmann@5914: WQTimerange[] wqts,
teichmann@5914: HistoricalDischargeAccess access
teichmann@5914: ) {
ingo@2228: int i = 0;
ingo@2228:
ingo@4232: for (WQTimerange wqt : wqts) {
teichmann@8202: log.debug("Prepare facet for: " + wqt.getName());
ingo@2228:
ingo@4232: EvaluationMode evalMode = access.getEvaluationMode();
ingo@4232: if (evalMode == EvaluationMode.W) {
ingo@4232: facets.add(new HistoricalDischargeFacet(i,
ingo@4232: HISTORICAL_DISCHARGE_Q, createFacetTitle(wqt)));
ingo@2311:
ingo@4232: if (wqt instanceof HistoricalWQTimerange) {
tom@8856: log.debug(
tom@8856: "Create another facet for historical differences.");
ingo@2311:
felix@5335: // TODO CREATE BETTER TITLE FOR FACETS (issue1180)
ingo@4232: facets.add(new HistoricalDischargeDifferenceFacet(i,
tom@8856: HISTORICAL_DISCHARGE_Q_DIFF,
tom@8856: "DIFF: " + wqt.getName()));
ingo@4232: }
ingo@4232: }
ingo@4232: else {
ingo@4232: facets.add(new HistoricalDischargeFacet(i,
ingo@4232: HISTORICAL_DISCHARGE_W, createFacetTitle(wqt)));
ingo@4232:
ingo@4232: if (wqt instanceof HistoricalWQTimerange) {
tom@8856: log.debug(
tom@8856: "Create another facet for historical differences.");
ingo@4232:
ingo@4232: // TODO CREATE BETTER TITLE FOR FACETS
ingo@4232: facets.add(new HistoricalDischargeDifferenceFacet(i,
tom@8856: HISTORICAL_DISCHARGE_W_DIFF,
tom@8856: "DIFF: " + wqt.getName()));
ingo@4232: }
ingo@2311: }
ingo@2311:
ingo@2311: i++;
ingo@2228: }
ingo@2228: }
ingo@4144:
ingo@4241: protected void prepareWQFacets(CallContext cc, List facets,
tom@8462: HistoricalDischargeAccess access, String hash) {
ingo@4241: double[] ws = access.getWs();
ingo@4241: double[] qs = access.getQs();
ingo@4241:
felix@4928: NumberFormat format = NumberFormat.getInstance(
felix@4928: Resources.getLocale(cc.getMeta()));
ingo@4241: for (int k = 0; k < ws.length; k++) {
ingo@4260: facets.add(new HistoricalDischargeWQFacet(k,
tom@8856: HISTORICAL_DISCHARGE_WQ_W,
tom@8856: "W=" + format.format(ws[k]),
tom@8856: ComputeType.ADVANCE,
tom@8856: hash,
tom@8856: getID(),
tom@8856: ws[k]));
ingo@4241: }
ingo@4241:
ingo@4241: for (int k = 0; k < qs.length; k++) {
ingo@4260: facets.add(new HistoricalDischargeWQFacet(k,
tom@8856: HISTORICAL_DISCHARGE_WQ_Q,
tom@8856: "Q=" + format.format(qs[k]),
tom@8856: ComputeType.ADVANCE,
tom@8856: hash,
tom@8856: getID(),
tom@8856: qs[k]));
ingo@4241: }
ingo@4241: }
ingo@4241:
felix@5339: /** Create string for facets name/description. */
ingo@4144: protected String createFacetTitle(WQTimerange wqt) {
ingo@4144: String name = wqt.getName();
ingo@4144:
ingo@4232: return name != null && name.indexOf("W") >= 0 ? createFacetTitleW(wqt)
ingo@4144: : createFacetTitleQ(wqt);
ingo@4144: }
ingo@4144:
ingo@4144: protected String createFacetTitleW(WQTimerange wqt) {
ingo@4144: String name = wqt.getName();
ingo@4144: return name + " " + DEFAULT_UNIT;
ingo@4144: }
ingo@4144:
ingo@4144: protected String createFacetTitleQ(WQTimerange wqt) {
ingo@4144: return wqt.getName();
ingo@4144: }
ingo@2191: }
ingo@2191: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :