view flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java @ 3121:0b86b005bb9a

FixA: Respect the selected events and reference period correctly. flys-artifacts/trunk@4722 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jun 2012 11:25:21 +0000
parents 0b5a7a2c3724
children
line wrap: on
line source
package de.intevation.flys.artifacts;

import de.intevation.artifactdatabase.data.StateData;

import de.intevation.flys.artifacts.model.fixings.DateRange;

import de.intevation.flys.utils.FLYSUtils;

import gnu.trove.TDoubleArrayList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import org.apache.log4j.Logger;

public class FixationArtifactAccess
{
    private static Logger log = Logger.getLogger(FixationArtifactAccess.class);

    protected FLYSArtifact artifact;

    protected String river;

    protected String calculationMode;

    protected Double from;
    protected Double to;
    protected Double step;

    protected Long start;
    protected Long end;

    protected Integer qSectorStart;
    protected Integer qSectorEnd;

    protected DateRange    referencePeriod;
    protected DateRange [] analysisPeriods;

    protected int [] events;

    protected Boolean preprocessing;

    protected String  function;

    protected double [] qs;

    public FixationArtifactAccess() {
    }

    public FixationArtifactAccess(FLYSArtifact artifact) {
        this.artifact = artifact;
    }

    public FLYSArtifact getArtifact() {
        return artifact;
    }

    public String getRiver() {
        if (river == null) {
            StateData sd = artifact.getData("river");
            if (sd == null) {
                log.warn("missing 'river' value");
                return null;
            }
            river = (String)sd.getValue();
        }
        if (log.isDebugEnabled()) {
            log.debug("river: '" + river + "'");
        }
        return river;
    }

    public String getCalculationMode() {
        if (calculationMode == null) {
            StateData sd = artifact.getData("calculation.mode");
            if (sd == null) {
                log.warn("missing 'calculation.mode' value");
                return null;
            }
            calculationMode = (String)sd.getValue();
        }

        if (log.isDebugEnabled()) {
            log.debug("calculationMode: '" + calculationMode + "'");
        }
        return calculationMode;
    }

    public Double getFrom() {

        if (from == null) {
            StateData sd = artifact.getData("from");
            if (sd == null) {
                log.warn("missing 'from' value");
                return null;
            }
            try {
                from = Double.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("from '" + sd.getValue() + "' is not numeric.");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("from: '" + from + "'");
        }

        return from;
    }

    public Double getTo() {

        if (to == null) {
            StateData sd = artifact.getData("to");
            if (sd == null) {
                log.warn("missing 'to' value");
                return null;
            }
            try {
                to = Double.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("to '" + sd.getValue() + "' is not numeric.");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("to: '" + to + "'");
        }

        return to;
    }

    public Double getStep() {

        if (step == null) {
            StateData sd = artifact.getData("step");
            if (sd == null) {
                log.warn("missing 'step' value");
                return null;
            }
            try {
                step = Double.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("step '" + sd.getValue() + "' is not numeric.");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("step: '" + step + "'");
        }

        return step;
    }

    public Long getStart() {

        if (start == null) {
            StateData sd = artifact.getData("start");
            if (sd == null) {
                log.warn("missing 'start' value");
                return null;
            }
            try {
                start = Long.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("start '" + sd.getValue() + "' is not an integer.");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("start: '" + start + "'");
        }

        return start;
    }

    public Long getEnd() {

        if (end == null) {
            StateData sd = artifact.getData("end");
            if (sd == null) {
                log.warn("missing 'end' value");
                return null;
            }
            try {
                end = Long.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("end '" + sd.getValue() + "' is not an integer.");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("end: '" + end + "'");
        }

        return end;
    }

    public Integer getQSectorStart() {

        if (qSectorStart == null) {
            StateData sd = artifact.getData("q1");
            if (sd == null) {
                log.warn("missing 'q1' value");
                return null;
            }
            try {
                qSectorStart = Integer.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("q1 '" + sd.getValue() + "' is not an integer.");
            }
        }

        return qSectorStart;
    }

    public Integer getQSectorEnd() {

        if (qSectorEnd == null) {
            StateData sd = artifact.getData("q2");
            if (sd == null) {
                log.warn("missing 'q2' value");
                return null;
            }
            try {
                qSectorEnd = Integer.valueOf((String)sd.getValue());
            }
            catch (NumberFormatException nfe) {
                log.warn("q2 '" + sd.getValue() + "' is not an integer.");
            }
        }

        return qSectorEnd;
    }

    public int [] getEvents() {
        if (events == null) {
            StateData sd = artifact.getData("events");
            if (sd == null) {
                log.warn("missing 'events' value");
                return null;
            }
            events = FLYSUtils.intArrayFromString((String)sd.getValue());
        }
        return events;
    }

    public DateRange getReferencePeriod() {
        if (referencePeriod == null) {
            StateData refStart = artifact.getData("ref_start");
            StateData refEnd   = artifact.getData("ref_end");

            if (refStart == null || refEnd == null) {
                log.warn("missing 'ref_start' or 'ref_start' value");
                return null;
            }

            try {
                long rs = Long.parseLong((String)refStart.getValue());
                long re = Long.parseLong((String)refEnd  .getValue());

                if (rs > re) { long t = rs; rs = re; re = t; }

                Date from = new Date(rs);
                Date to   = new Date(re);
                referencePeriod = new DateRange(from, to);
            }
            catch (NumberFormatException nfe) {
                log.warn("ref_start or ref_end is not an integer.");
            }
        }

        return referencePeriod;
    }

    public DateRange [] getAnalysisPeriods() {
        if (analysisPeriods == null) {
            StateData sd = artifact.getData("ana_data");

            if (sd == null) {
                log.warn("missing 'ana_data'");
                return null;
            }

            String data = (String)sd.getValue();
            String[] pairs = data.split("\\s*;\\s*");

            ArrayList<DateRange> aPs = new ArrayList<DateRange>(pairs.length);

            for (int i = 0; i < pairs.length; i++) {
                String[] fromTo = pairs[i].split("\\s*,\\s*");
                if (fromTo.length >= 2) {
                    try {
                        Date from = new Date(Long.parseLong(fromTo[0]));
                        Date to   = new Date(Long.parseLong(fromTo[1]));
                        DateRange aP = new DateRange(from, to);
                        if (!aPs.contains(aP)) {
                            aPs.add(aP);
                        }
                    }
                    catch (NumberFormatException nfe) {
                        log.warn("ana_data contains no long values.", nfe);
                    }
                }
            }

            analysisPeriods = aPs.toArray(new DateRange[aPs.size()]);
        }

        if (log.isDebugEnabled()) {
            for (int i = 0; i < analysisPeriods.length; ++i) {
                DateRange ap = analysisPeriods[i];
                log.debug("analysis period " +
                    ap.getFrom() + " - " + ap.getTo());
            }
        }

        return analysisPeriods;
    }

    public Boolean getPreprocessing() {
        if (preprocessing == null) {
            StateData sd = artifact.getData("preprocessing");
            if (sd == null) {
                log.warn("missing 'preprocessing'");
                return null;
            }
            preprocessing = Boolean.valueOf((String)sd.getValue());
        }
        return preprocessing;
    }

    public String getFunction() {
        if (function == null) {
            StateData sd = artifact.getData("function");
            if (sd == null) {
                log.warn("missing 'function'");
                return null;
            }
            function = (String)sd.getValue();
        }
        if (log.isDebugEnabled()) {
            log.debug("function: " + function);
        }
        return function;
    }

    public double [] getQs() {
        if (qs == null) {
            StateData sd = artifact.getData("qs");
            if (sd == null) {
                log.warn("missing 'qs'");
                return null;
            }
            String [] parts = ((String)sd.getValue()).split("[\\s;]");
            TDoubleArrayList list = new TDoubleArrayList(parts.length);
            for (String part: parts) {
                try {
                    list.add(Double.parseDouble(part));
                }
                catch (NumberFormatException nfe) {
                    log.warn("'" + part + "' is not numeric.");
                }
            }
            qs = list.toNativeArray();
        }
        if (log.isDebugEnabled()) {
            log.debug("qs: " + Arrays.toString(qs));
        }
        return qs;
    }


    public double getCurrentKm() {
        //TODO: get the current km.
        return getFrom().doubleValue();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org