view flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/FixDeltaWtGenerator.java @ 4446:05a54b4d579d

Remove AbstractFixGenerator and use new KMIndexProcessor instead Don't use same base class for minfo and fixings artifacts. Instead use the new KMIndexProcessor to generate the same output for the same data types and facets.
author Björn Ricks <bjoern.ricks@intevation.de>
date Wed, 07 Nov 2012 14:34:13 +0100
parents 975f608dd254
children acfd48384835
line wrap: on
line source
package de.intevation.flys.exports.fixings;

import de.intevation.artifactdatabase.state.ArtifactAndFacet;
import de.intevation.artifacts.CallContext;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.access.FixAnalysisAccess;
import de.intevation.flys.artifacts.model.DateRange;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.QWDDateRange;
import de.intevation.flys.artifacts.model.fixings.QWD;
import de.intevation.flys.artifacts.resources.Resources;
import de.intevation.flys.exports.TimeseriesChartGenerator;
import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
import de.intevation.flys.jfree.FLYSAnnotation;
import de.intevation.flys.jfree.StyledDomainMarker;
import de.intevation.flys.jfree.StyledTimeSeries;
import de.intevation.flys.jfree.StyledValueMarker;
import de.intevation.flys.utils.ThemeUtil;

import java.io.OutputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.data.time.Day;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.w3c.dom.Document;


/**
 * Generator for Delta W(t) charts.
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class FixDeltaWtGenerator
extends      TimeseriesChartGenerator
implements   FacetTypes
{
    /** Private logger. */
    private static Logger logger =
            Logger.getLogger(FixDeltaWtGenerator.class);

    public static final String I18N_CHART_TITLE =
            "chart.fix.deltawt.title";

    public static final String I18N_CHART_SUBTITLE =
            "chart.fix.deltawt.subtitle";

    public static final String I18N_XAXIS_LABEL =
            "chart.fix.deltawt.xaxis.label";

    public static final String I18N_YAXIS_LABEL =
            "chart.fix.deltawt.yaxis.label";

    public static final String I18N_YAXIS_SECOND_LABEL =
            "chart.fix.deltawt.yaxis.second.label";


    public static enum YAXIS {
        dW(0);
        protected int idx;
        private YAXIS(int c) {
            idx = c;
        }
    }


    private FLYSArtifact artifact = null;


    @Override
    protected YAxisWalker getYAxisWalker() {
        return new YAxisWalker() {
            @Override
            public int length() {
                return YAXIS.values().length;
            }

            @Override
            public String getId(int idx) {
                YAXIS[] yaxes = YAXIS.values();
                return yaxes[idx].toString();
            }
        };
    }


    @Override
    protected String getDefaultChartTitle() {
        return msg(I18N_CHART_TITLE, I18N_CHART_TITLE);
    }


    @Override
    protected String getChartTitle() {
        return Resources.format(
                context.getMeta(),
                I18N_CHART_TITLE,
                "",
                FixChartGenerator
                .getCurrentKmFromRequest(request).doubleValue());
    }


    @Override
    protected String getDefaultChartSubtitle() {
        FixAnalysisAccess access = new FixAnalysisAccess(artifact);
        DateRange dateRange = access.getDateRange();
        DateRange refRange  = access.getReferencePeriod();
        return Resources.format(
                context.getMeta(),
                I18N_CHART_SUBTITLE,
                "",
                access.getRiver(),
                dateRange.getFrom(),
                dateRange.getTo(),
                refRange.getFrom(),
                refRange.getTo());
    }


    @Override
    protected String getDefaultXAxisLabel() {
        return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL);
    }

    @Override
    protected String getDefaultYAxisLabel(int pos) {
        if (pos == 0) {
            return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL);
        }
        else if (pos == 1) {
            return msg(I18N_YAXIS_SECOND_LABEL, I18N_YAXIS_SECOND_LABEL);
        }
        else {
            return "NO TITLE FOR Y AXIS: " + pos;
        }
    }


    @Override
    public void doOut(
            ArtifactAndFacet artifactFacet,
            Document         theme,
            boolean          visible
            ) {
        String name = artifactFacet.getFacetName();
        logger.debug("FixDeltaWtGenerator.doOut: " + name);
        logger.debug("Theme description is: " + artifactFacet.getFacetDescription());

        this.artifact = (FLYSArtifact)artifactFacet.getArtifact();

        if (name.contains(FIX_SECTOR_AVERAGE_DWT)) {
            doSectorAverageOut(
                    (FLYSArtifact) artifactFacet.getArtifact(),
                    artifactFacet.getData(context),
                    artifactFacet.getFacetDescription(),
                    theme,
                    visible);
        }
        else if (name.equals(FIX_REFERENCE_EVENTS_DWT)) {
            doReferenceEventsOut(
                    (FLYSArtifact) artifactFacet.getArtifact(),
                    artifactFacet.getData(context),
                    artifactFacet.getFacetDescription(),
                    theme,
                    visible);
        }
        else if (name.equals(FIX_ANALYSIS_EVENTS_DWT)) {
            doAnalysisEventsOut(
                    (FLYSArtifact) artifactFacet.getArtifact(),
                    artifactFacet.getData(context),
                    artifactFacet.getFacetDescription(),
                    theme,
                    visible);
        }
        else if (name.equals(FIX_DEVIATION_DWT)) {
            doDeviationOut(
                    (FLYSArtifact) artifactFacet.getArtifact(),
                    artifactFacet.getData(context),
                    artifactFacet.getFacetDescription(),
                    theme,
                    visible);
        }
        else if (name.equals(FIX_ANALYSIS_PERIODS_DWT)) {
            doAnalysisPeriodsOut(
                    (FLYSArtifact) artifactFacet.getArtifact(),
                    artifactFacet.getData(context),
                    artifactFacet.getFacetDescription(),
                    theme,
                    visible);
        }
        else if (name.equals(FIX_REFERENCE_PERIOD_DWT)) {
            doReferencePeriodsOut(
                    (FLYSArtifact) artifactFacet.getArtifact(),
                    artifactFacet.getData(context),
                    artifactFacet.getFacetDescription(),
                    theme,
                    visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints (artifactFacet.getData(context),
                    artifactFacet,
                    theme, visible, YAXIS.dW.idx);
        }
        else {
            logger.warn("doOut(): unknown facet name: " + name);
            return;
        }
    }


    protected void doReferencePeriodsOut(
            FLYSArtifact artifact,
            Object       data,
            String       desc,
            Document     theme,
            boolean      visible)
    {
        logger.debug("doReferencePeriodsOut()");

        if (visible) {
            FixAnalysisAccess access = new FixAnalysisAccess(artifact);
            DateRange refRange  = access.getReferencePeriod();

            RegularTimePeriod start = new Day(refRange.getFrom());
            RegularTimePeriod end = new Day(refRange.getTo());
            StyledDomainMarker marker = new StyledDomainMarker(
                    start.getMiddleMillisecond(),
                    end.getMiddleMillisecond(),
                    theme);
            domainMarker.add(marker);
        }
    }


    protected void doSectorAverageOut(
            FLYSArtifact artifact,
            Object       data,
            String       desc,
            Document     theme,
            boolean      visible)
    {
        logger.debug("doSectorAverageOut(): description = " + desc);

        QWDDateRange qwd = (QWDDateRange) data;
        TimeSeriesCollection tsc = new TimeSeriesCollection();
        TimeSeries        series = new StyledTimeSeries(desc, theme);

        if (qwd == null || qwd.qwd == null || qwd.dateRange == null) {
            return;
        }
        RegularTimePeriod rtp = new Day(qwd.qwd.getDate());
        double value = qwd.qwd.getDeltaW();

        // Draw a line spanning the analysis time.
        series.add(rtp, value);
        rtp = new Day(qwd.dateRange.getFrom());
        series.addOrUpdate(rtp, value);
        rtp = new Day(qwd.dateRange.getTo());
        series.addOrUpdate(rtp, value);

        tsc.addSeries(series);

        addAxisDataset(tsc, 0, visible);

        if (visible && ThemeUtil.parseShowLineLabel(theme)) {
            List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>();
            XYTextAnnotation anno = new CollisionFreeXYTextAnnotation(
                    "\u0394 W(t) [cm] " + (float)Math.round(qwd.qwd.getDeltaW() * 10000) / 10000,
                    tsc.getXValue(0, 0),
                    qwd.qwd.getDeltaW());
            textAnnos.add(anno);

            FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme);
            flysAnno.setTextAnnotations(textAnnos);
            addAnnotations(flysAnno);
        }
    }


    protected void doAnalysisEventsOut(
            FLYSArtifact artifact,
            Object       data,
            String       desc,
            Document     theme,
            boolean      visible
            ) {
        logger.debug("doAnalysisEventsOut: desc = " + desc);

        QWD qwd = (QWD) data;
        doQWDEventsOut(qwd, desc, theme, visible);
    }


    protected void doQWDEventsOut(QWD qwd, String desc, Document theme, boolean visible)
    {
        TimeSeriesCollection tsc = new TimeSeriesCollection();

        TimeSeries   series = new StyledTimeSeries(desc, theme);
        TimeSeries interpol = new StyledTimeSeries(desc + "interpol", theme);

        if (qwd == null) {
            logger.debug("doQWDEventsOut: qwd == null");
            return;
        }

        Map<Integer, int[]> annoIdxMap = new HashMap<Integer, int[]>();

        int idxInterpol = 0;
        int idxRegular = 0;
        RegularTimePeriod rtp = new Day(qwd.getDate());
        double value =  qwd.getDeltaW();
        boolean interpolate = qwd.getInterpolated();
        if (interpolate) {
            if(interpol.addOrUpdate(rtp, value) == null) {
                annoIdxMap.put(
                        0,
                        new int[]{1, idxInterpol});
                idxInterpol++;
            }
        }
        else {
            if(series.addOrUpdate(rtp, value) == null) {
                annoIdxMap.put(
                        0,
                        new int[]{0, idxRegular});
                idxRegular++;
            }
        }

        tsc.addSeries(series);
        tsc.addSeries(interpol);
        addAxisDataset(tsc, 0, visible);
        addAttribute(desc + "interpol", "interpolate");
        addAttribute(desc, "outline");

        doQWDTextAnnotations(annoIdxMap, tsc, qwd, theme, visible);
    }


    /**
     * @param annoIdxMap map of index in qwds to series/data item indices in tsc.
     */
    protected void doQWDTextAnnotations(Map<Integer, int[]> annoIdxMap,
            TimeSeriesCollection tsc, QWD qwd, Document theme,
            boolean visible) {
        logger.debug("doQWDTextAnnotation()");

        if (!visible || !ThemeUtil.parseShowPointLabel(theme)) {
            logger.debug("doQWDTextAnnotation: annotation not visible");
            return;
        }

        Locale locale = Resources.getLocale(context.getMeta());
        NumberFormat nf = NumberFormat.getInstance(locale);

        List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>();

        for (int[] idxs: annoIdxMap.values()) {

            double x = tsc.getXValue(idxs[0], idxs[1]);

            XYTextAnnotation anno = new CollisionFreeXYTextAnnotation(
                    nf.format(qwd.getQ()) + " m\u00B3/s",
                    x,
                    qwd.getDeltaW());
            textAnnos.add(anno);
        }

        FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme);
        flysAnno.setTextAnnotations(textAnnos);
        addAnnotations(flysAnno);
    }


    protected void doReferenceEventsOut(
            FLYSArtifact artifact,
            Object       data,
            String       desc,
            Document     theme,
            boolean      visible
            ) {
        logger.debug("doReferenceEventsOut: desc = " + desc);

        QWD qwd = (QWD) data;
        doQWDEventsOut(qwd, desc, theme, visible);
    }


    protected void doDeviationOut(
            FLYSArtifact artifact,
            Object       data,
            String       desc,
            Document     theme,
            boolean      visible
            ) {
        logger.debug("doDeviationOut: desc = " + desc);

        if (data == null || !visible) {
            logger.debug("no standard deviation");
            return;
        }
        double[] value = (double[]) data;
        StyledDomainMarker lower = new StyledDomainMarker((value[0] * -1), 0, theme);
        StyledDomainMarker upper = new StyledDomainMarker(0, value[0], theme);

        valueMarker.add(lower);
        valueMarker.add(upper);
    }


    protected void doAnalysisPeriodsOut(
            FLYSArtifact artifact,
            Object       data,
            String       desc,
            Document     theme,
            boolean      visible)
    {
        DateRange[] ranges = (DateRange[]) data;
        if (ranges == null || !visible) {
            return;
        }
        for (int i = 0; i < ranges.length; i++) {
            logger.debug("creating domain marker");
            RegularTimePeriod start = new Day(ranges[i].getFrom());
            RegularTimePeriod end = new Day(ranges[i].getTo());
            StyledDomainMarker marker =
                    new StyledDomainMarker(start.getMiddleMillisecond(),
                            end.getMiddleMillisecond(), theme);
            marker.useSecondColor(i % 2 == 0);
            domainMarker.add(marker);
        }
        logger.debug("domainmarkers: " + domainMarker.size());
    }


    @Override
    public void init(Document request, OutputStream out, CallContext context) {
        super.init(request, out, context);

        Double currentKm = FixChartGenerator.getCurrentKmFromRequest(request);

        if (logger.isDebugEnabled()) {
            logger.debug("currentKm = " + currentKm);
        }

        context.putContextValue("currentKm", currentKm);

        StyledValueMarker marker = new StyledValueMarker(0, request);
        valueMarker.add(marker);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org