view artifacts/src/main/java/org/dive4elements/river/artifacts/QSectorArtifact.java @ 7471:fff862f4ef76

Experimental caching of datacage recommendations. The respective hook is called a lot and running the datacage over and over again when loading data can be expensive. So the generated recommendations are cached for some time. Hopefully this improves the overall speed of loading data from the datacage.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 30 Oct 2013 15:26:21 +0100
parents af13ceeba52a
children 8d5ca5175038
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.artifacts;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;

import org.dive4elements.artifactdatabase.state.Facet;

import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.ArtifactFactory;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.artifacts.CallContext;

import org.dive4elements.river.artifacts.model.GaugeFinder;
import org.dive4elements.river.artifacts.model.GaugeFinderFactory;
import org.dive4elements.river.artifacts.model.GaugeRange;
import org.dive4elements.river.artifacts.model.NamedDouble;

import org.dive4elements.river.artifacts.services.FixingsKMChartService;

import org.dive4elements.river.artifacts.states.DefaultState;

import org.dive4elements.river.artifacts.resources.Resources;


/**
 * Artifact to produce sector markers.
 */
public class QSectorArtifact
extends      StaticD4EArtifact
{
    /** The logger for this class. */
    private static Logger logger = Logger.getLogger(QSectorArtifact.class);

    /** The name of the artifact. */
    public static final String ARTIFACT_NAME = "qsector";


    /**
     * Trivial Constructor.
     */
    public QSectorArtifact() {
        logger.debug("QSectorArtifact.QSectorArtifact()");
    }


    /**
     * Gets called from factory, to set things up.
     */
    @Override
    public void setup(
        String          identifier,
        ArtifactFactory factory,
        Object          context,
        CallMeta        callMeta,
        Document        data)
    {
        logger.debug("QSectorArtifact.setup");
        super.setup(identifier, factory, context, callMeta, data);
        initialize(null, context, callMeta);
    }


    /** Return the name of this artifact. */
    public String getName() {
        return ARTIFACT_NAME;
    }


    /** Get list of NamedDouble s (QSectors). */
    public Object getQSectors(double km, CallContext context) {

        String river = getDataAsString("river");
        List<NamedDouble> qsectors = new ArrayList<NamedDouble>();

        GaugeFinderFactory ggf = GaugeFinderFactory.getInstance();
        GaugeFinder        gf  = ggf.getGaugeFinder(river);

        if (gf == null) {
            logger.warn("No gauge finder found for river '" + river + "'");
            return null;
        }

        GaugeRange gr = gf.find(km);
        if (gr == null) {
            logger.debug("No gauge range found for km "
                + km + " on river " + river + ".");
            return null;
        }

        if (logger.isDebugEnabled()) {
            logger.debug(gr);
        }

        for (int i = 0; i < FixingsKMChartService.I18N_Q_SECTOR_BOARDERS.length; ++i) {
            String key   = FixingsKMChartService.I18N_Q_SECTOR_BOARDERS[i];
            String def   = FixingsKMChartService.DEFAULT_Q_SECTOR_BORDERS[i];
            String label = Resources.getMsg(context.getMeta(), key, def);

            qsectors.add(new NamedDouble(label, gr.getSectorBorder(i)));
        }

        return qsectors;
    }


    /** Setup state and facet. */
    @Override
    protected void initialize(Artifact artifact, Object context, CallMeta meta) {
        logger.debug("QSectorArtifact.initialize");
        List<Facet> fs = new ArrayList<Facet>();

        D4EArtifact flys = (D4EArtifact) artifact;
        importData(flys, "river");

        DefaultState state = (DefaultState) getCurrentState(context);
        state.computeInit(this, hash(), context, meta, fs);
        if (!fs.isEmpty()) {
            logger.debug("Facets to add in QSectorArtifact.initialize .");
            addFacets(getCurrentStateId(), fs);
        }
        else {
            logger.debug("No facets to add in QSectorArtifact.initialize ("
                + state.getID() + ").");
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org