view artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java @ 8315:d4c501d2c098

Removed obsolete imports.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 24 Sep 2014 12:16:53 +0200
parents 9f5059f4f01a
children 9a5b3079aad4
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.CallContext;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.StaticSQRelationAccess;
import org.dive4elements.river.artifacts.model.FacetTypes;
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
implements FacetTypes
{

    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 computeAdvance(
        D4EArtifact artifact,
        String hash,
        CallContext context,
        List<Facet> facets,
        Object old) {
        StaticSQContainer sqRelations;
        StaticSQRelationAccess access = new StaticSQRelationAccess(artifact);
        String river = access.getRiverName();
        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()) {
            log.debug("parameter: " + p.toString());
            List<StaticSQRelation> relations =
                sqRelations.getRelationsByParameter(p);

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

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

    @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.getRiverName();
            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