view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java @ 4255:670e98f5a441

Fixed leak while merging facets. The ThemeList that is used by OutputHelper to sort the Facets for an Output now uses a list to store the ManagedFacets. The correct order is made up by sorting the List using Collections.sort() function of the Java JDK. Therfore, the ManagedFacet class implements the Comparable interface. The return value of its compareTo(other) method depends on the value of the 'position' field.
author Ingo Weinzierl <weinzierl.ingo@googlemail.com>
date Thu, 25 Oct 2012 14:01:46 +0200
parents 49cb65d5932d
children 2c6e571f366a
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Element;

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.WINFOArtifact;
import de.intevation.flys.artifacts.access.HistoricalDischargeAccess;
import de.intevation.flys.artifacts.access.HistoricalDischargeAccess.EvaluationMode;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.HistoricalDischargeCurveFacet;
import de.intevation.flys.artifacts.model.HistoricalDischargeData;
import de.intevation.flys.artifacts.model.HistoricalDischargeDifferenceFacet;
import de.intevation.flys.artifacts.model.HistoricalDischargeFacet;
import de.intevation.flys.artifacts.model.HistoricalDischargeWQFacet;
import de.intevation.flys.artifacts.model.HistoricalWQKms;
import de.intevation.flys.artifacts.model.HistoricalWQTimerange;
import de.intevation.flys.artifacts.model.ReportFacet;
import de.intevation.flys.artifacts.model.WQKms;
import de.intevation.flys.artifacts.model.WQTimerange;
import de.intevation.flys.artifacts.resources.Resources;
import de.intevation.flys.model.TimeInterval;


/**
 * State to calculate historical discharge curves.
 * 
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class HistoricalDischargeComputeState extends DefaultState implements
    FacetTypes {

    private static final Logger logger = Logger
        .getLogger(HistoricalDischargeComputeState.class);

    public static final String DEFAULT_UNIT = "cm";
    public static final String I18N_WQ_CURVE_FACET_RANGE = "historical_discharge.wq.curve_range";
    public static final String I18N_WQ_CURVE_FACET_SINCE = "historical_discharge.wq.curve_since";

    @Override
    protected void appendItems(Artifact artifact, ElementCreator creator,
        String name, CallContext context, Element select) {
        // TODO IMPLEMENT ME
    }

    @Override
    public Object computeAdvance(FLYSArtifact artifact, String hash,
        CallContext context, List<Facet> facets, Object old) {
        logger.debug("HistoricalDischargeComputeState.computeAdvance");

        WINFOArtifact winfo = (WINFOArtifact) artifact;

        CalculationResult res = old instanceof CalculationResult ? (CalculationResult) old
            : winfo.getHistoricalDischargeData();

        if (facets == null) {
            return res;
        }

        if (res.getReport().hasProblems()) {
            facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id));
        }

        HistoricalDischargeData data = (HistoricalDischargeData) res.getData();
        HistoricalDischargeAccess access = new HistoricalDischargeAccess(
            artifact);

        WQTimerange[] wqts = (WQTimerange[]) data.getWQTimeranges();
        if (wqts != null && wqts.length > 0) {
            facets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE,
                hash, id));

            facets.add(new DataFacet(PDF, "PDF data", ComputeType.ADVANCE,
                hash, id));

            prepareFacets(facets, wqts, access);
        }

        WQKms[] wqs = (WQKms[]) data.getWQs();
        if (wqs != null && wqs.length > 0) {
            prepareWQFacets(context, facets, wqs, access, hash);
        }

        return res;
    }

    protected void prepareFacets(List<Facet> facets, WQTimerange[] wqts,
        HistoricalDischargeAccess access) {
        int i = 0;

        for (WQTimerange wqt : wqts) {
            logger.debug("Prepare facet for: " + wqt.getName());

            EvaluationMode evalMode = access.getEvaluationMode();
            if (evalMode == EvaluationMode.W) {
                facets.add(new HistoricalDischargeFacet(i,
                    HISTORICAL_DISCHARGE_Q, createFacetTitle(wqt)));

                if (wqt instanceof HistoricalWQTimerange) {
                    logger
                        .debug("Create another facet for historical differences.");

                    // TODO CREATE BETTER TITLE FOR FACETS
                    facets.add(new HistoricalDischargeDifferenceFacet(i,
                        HISTORICAL_DISCHARGE_Q_DIFF, "DIFF: " + wqt.getName()));
                }
            }
            else {
                facets.add(new HistoricalDischargeFacet(i,
                    HISTORICAL_DISCHARGE_W, createFacetTitle(wqt)));

                if (wqt instanceof HistoricalWQTimerange) {
                    logger
                        .debug("Create another facet for historical differences.");

                    // TODO CREATE BETTER TITLE FOR FACETS
                    facets.add(new HistoricalDischargeDifferenceFacet(i,
                        HISTORICAL_DISCHARGE_W_DIFF, "DIFF: " + wqt.getName()));
                }
            }

            i++;
        }
    }

    protected void prepareWQFacets(CallContext cc, List<Facet> facets,
        WQKms[] wqs, HistoricalDischargeAccess access, String hash) {
        double[] ws = access.getWs();
        double[] qs = access.getQs();

        for (int k = 0; k < ws.length; k++) {
            facets.add(new HistoricalDischargeWQFacet(
                HISTORICAL_DISCHARGE_WQ_W, "W=" + ws[k], ws[k]));
        }

        for (int k = 0; k < qs.length; k++) {
            facets.add(new HistoricalDischargeWQFacet(
                HISTORICAL_DISCHARGE_WQ_W, "Q=" + ws[k], qs[k]));
        }

        int idx = 0;
        for (WQKms wqkms : wqs) {
            String title = null;
            if (wqkms instanceof HistoricalWQKms) {
                title = createDischargeCurveTitle(cc, (HistoricalWQKms) wqkms);
            }
            else {
                title = wqkms.getName();
            }

            facets.add(new HistoricalDischargeCurveFacet(idx,
                HISTORICAL_DISCHARGE_WQ_CURVE, title, ComputeType.ADVANCE,
                getID(), hash));
            idx++;
        }
    }

    protected String createDischargeCurveTitle(CallContext cc,
        HistoricalWQKms wqkms) {
        TimeInterval timeInterval = wqkms.getTimeInterval();

        if (timeInterval.getStopTime() != null) {
            return Resources.format(cc.getMeta(), I18N_WQ_CURVE_FACET_RANGE,
                I18N_WQ_CURVE_FACET_RANGE, timeInterval.getStartTime(),
                timeInterval.getStopTime());
        }
        else {
            return Resources.format(cc.getMeta(), I18N_WQ_CURVE_FACET_SINCE,
                I18N_WQ_CURVE_FACET_SINCE, timeInterval.getStartTime());
        }
    }

    protected String createFacetTitle(WQTimerange wqt) {
        String name = wqt.getName();

        return name != null && name.indexOf("W") >= 0 ? createFacetTitleW(wqt)
            : createFacetTitleQ(wqt);
    }

    protected String createFacetTitleW(WQTimerange wqt) {
        String name = wqt.getName();
        return name + " " + DEFAULT_UNIT;
    }

    protected String createFacetTitleQ(WQTimerange wqt) {
        return wqt.getName();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org