view artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java @ 7236:a9bd4a23a852

(issue1474) Add datacage loading for SQ Relations
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 02 Oct 2013 17:04:23 +0200
parents af13ceeba52a
children a56fe3bc6700
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.states.sq;

import java.text.DateFormat;
import java.util.List;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;

import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.StaticSQRelationAccess;
import org.dive4elements.river.artifacts.model.sq.StaticSQContainer;
import org.dive4elements.river.artifacts.model.sq.StaticSQFactory;
import org.dive4elements.river.artifacts.model.sq.StaticSQRelation;
import org.dive4elements.river.artifacts.states.StaticState;

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

public class SQStaticState
extends StaticState
{

    private static final Logger log =
        Logger.getLogger(SQStaticState.class);

    private static final String FACET_DESCRIPTION =
        "facet.sq_relation.static_data";

    public SQStaticState() {
        super();
    }

    public SQStaticState(String name) {
        super(name);
    }

    @Override
    public Object computeInit(
        D4EArtifact artifact,
        String       hash,
        Object       context,
        CallMeta     meta,
        List<Facet>  facets
    ) {
        StaticSQContainer sqRelations;

        String id_string = artifact.getDataAsString("ids");

        int static_id = -1;
        String static_desc = null;

        if (id_string != null && !id_string.isEmpty()) {
            String[] id_parts = id_string.split(";");
            static_id = Integer.parseInt(id_parts[0]);
            if (id_parts.length > 1) {
                static_desc = id_parts[1];
            }
        }

        if (static_id != -1) {
            // If the next line fails a traceback is the best debug output anyhow
            sqRelations = StaticSQFactory.getDistinctRelation(static_id);
            log.debug("Got a distinct relation" + sqRelations);
        } else {
            StaticSQRelationAccess access = new StaticSQRelationAccess(artifact);
            String river = access.getRiver();
            String measurementStation = access.getMeasurementStation();

            int ms = -1;
            try {
                ms = Integer.parseInt(measurementStation);
            }
            catch (NumberFormatException nfe) {
                log.error("Unparseable measurement station: " + measurementStation);
                return null;
            }
            log.debug("Parsed measurement station: " + ms);

            sqRelations = StaticSQFactory.getSQRelations(river, ms);
        }

        DateFormat df = new SimpleDateFormat("yyyy");

        for (StaticSQRelation.Parameter p: StaticSQRelation.Parameter.values()) {

            List<StaticSQRelation> relations =
                sqRelations.getRelationsByParameter(p);

            if (!relations.isEmpty()) {
                int count = 0;

                for (StaticSQRelation relation : relations) {
                    String name = "sq_" + p.toString().toLowerCase() + "_curve";
                    String desc = static_desc == null ?
                        Resources.getMsg(meta,
                            FACET_DESCRIPTION,
                            FACET_DESCRIPTION,
                            new Object[] {
                                df.format(relation.getStartTime()),
                                df.format(relation.getStopTime())}) :
                            static_desc;
                    facets.add(new StaticSQRelationFacet(
                        count,
                        name,
                        desc,
                        relation));
                    count++;
                }
            }
        }
        return null;
    }
}

http://dive4elements.wald.intevation.org