view artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.java @ 9104:07d51fd4864c

Added metadata subtitle to all chart export
author gernotbelger
date Tue, 29 May 2018 11:35:44 +0200
parents
children 1cc7653ca84f
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.exports;

import javax.xml.xpath.XPathConstants;

import org.dive4elements.artifacts.ArtifactNamespaceContext;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.RangeAccess;
import org.dive4elements.river.artifacts.access.RiverAccess;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.w3c.dom.Document;

/**
 * @author Gernot Belger
 */
// FIXME: this class is intended to contain all duplicate code from ChartGenerator and ChartGenerator2; who will clean
// up this mess...?
abstract class AbstractChartGenerator implements OutGenerator {
    private static final String XPATH_CHART_EXPORT = "/art:action/art:attributes/art:export/@art:value";

    // TODO: move real code here
    protected abstract D4EArtifact getArtifact();

    /** The CallContext object. */
    // TODO: move real code here
    protected abstract CallContext getContext();

    /** The document of the incoming out() request. */
    // TODO: move real code here
    protected abstract Document getRequest();

    /**
     * Adds a metadata sub-title to the chart if it gets exported
     */
    protected final void addMetadataSubtitle(final JFreeChart chart) {
        if (isExport()) {
            final String text = ChartExportHelper.createMetadataSubtitle(getArtifact(), getContext(), getRiverName());
            chart.addSubtitle(new TextTitle(text));
        }
    }

    /**
     * This method returns the export flag specified in the <i>request</i> document
     * or <i>false</i> if no export is specified in <i>request</i>.
     */
    protected final boolean isExport() {
        final Boolean export = (Boolean) XMLUtils.xpath(getRequest(), XPATH_CHART_EXPORT, XPathConstants.BOOLEAN, ArtifactNamespaceContext.INSTANCE);

        return export == null ? false : export;
    }

    protected final String getRiverName() {
        return new RiverAccess(getArtifact()).getRiver().getName();
    }

    protected final String getRiverUnit() {
        return new RiverAccess(getArtifact()).getRiver().getWstUnit().getName();
    }

    protected final double[] getRange() {
        final D4EArtifact flys = getArtifact();

        final RangeAccess rangeAccess = new RangeAccess(flys);
        return rangeAccess.getKmRange();
    }
}

http://dive4elements.wald.intevation.org