view artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadCalculate.java @ 7502:f2d97537f48a

issue1658: Handle new Facet-Types for SedimentLoad.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 01 Nov 2013 16:29:40 +0100
parents a56fe3bc6700
children b8faaac71507
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.minfo;

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

import org.apache.log4j.Logger;
import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.artifactdatabase.state.FacetActivity;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.SedimentLoadAccess;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.DataFacet;
import org.dive4elements.river.artifacts.model.FacetTypes;
import org.dive4elements.river.artifacts.model.ReportFacet;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoad;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadCalculation;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFacet;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFactory;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadResult;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadUnknownFacet;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.states.DefaultState;
import org.dive4elements.river.utils.DateGuesser;

/** State in which Sediment Load(s) are calculated/retrieved. */
public class SedimentLoadCalculate
extends DefaultState
implements FacetTypes
{

    private static final long serialVersionUID = 1L;

    private static final Logger logger = Logger
        .getLogger(SedimentLoadCalculate.class);

    public static final String I18N_FACET_SEDIMENTLOAD_COARSE = "facet.sedimentload.coarse";
    public static final String I18N_FACET_SEDIMENTLOAD_SAND = "facet.sedimentload.sand";
    public static final String I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE = "facet.sedimentload.fine_middle";
    public static final String I18N_FACET_SEDIMENTLOAD_SUSPSAND = "facet.sedimentload.susp_sand";
    public static final String I18N_FACET_SEDIMENTLOAD_SUSPSANDBED = "facet.sediemntload.susp_sand_bed";
    public static final String I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT = "facet.sedimentload.susp_sediment";
    public static final String I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD = "facet.sedimentload.total_load";
    public static final String I18N_FACET_SEDIMENTLOAD_TOTAL = "facet.sedimentload.total";

    private transient SedimentLoadAccess access;

    static {
        // Active/deactivate facets.
        FacetActivity.Registry.getInstance().register(
            "minfo",
            new FacetActivity() {
                @Override
                public Boolean isInitialActive(
                    Artifact artifact,
                    Facet    facet,
                    String   output
                ) {
                    String name = facet.getName();
                    if (name.equals(SEDIMENT_LOAD_TA_COARSE) ||
                        name.equals(SEDIMENT_LOAD_TA_FINEMIDDLE) ||
                        name.equals(SEDIMENT_LOAD_TA_SAND) ||
                        name.equals(SEDIMENT_LOAD_TA_SUSP_SAND) ||
                        name.equals(SEDIMENT_LOAD_TA_SUSP_SEDIMENT) ||
                        name.equals(SEDIMENT_LOAD_TA_SUSP_SAND_BED) ||
                        name.equals(SEDIMENT_LOAD_M3A_COARSE) ||
                        name.equals(SEDIMENT_LOAD_M3A_FINEMIDDLE) ||
                        name.equals(SEDIMENT_LOAD_M3A_SAND) ||
                        name.equals(SEDIMENT_LOAD_M3A_SUSP_SAND) ||
                        name.equals(SEDIMENT_LOAD_M3A_SUSP_SEDIMENT) ||
                        name.equals(SEDIMENT_LOAD_M3A_SUSP_SAND_BED)){
                        return Boolean.FALSE;
                    }
                    else if (name.equals(SEDIMENT_LOAD_TA_UNKNOWN)
                        || name.equals(SEDIMENT_LOAD_M3A_UNKNOWN)) {
                        D4EArtifact d4e = (D4EArtifact)artifact;
                        SedimentLoadUnknownFacet f =
                            (SedimentLoadUnknownFacet)
                                d4e.getNativeFacet(facet, null);
                        SedimentLoad load =
                            (SedimentLoad)f.getData(artifact, null);
                        SedimentLoadAccess access =
                            new SedimentLoadAccess(d4e);
                        List<int[]> dates = new ArrayList<int[]>();
                        if (access.getYearEpoch().equals("year")) {
                            dates.add(access.getPeriod());
                        }
                        else {
                            int[][] epochs = access.getEpochs();
                            for (int i = 0; i < epochs.length; i++) {
                                dates.add(epochs[i]);
                            }
                        }
                        for (int[] date: dates) {
                            try {
                                Date s =
                                    DateGuesser.guessDate(String.valueOf(date[0]));
                                Date e =
                                    DateGuesser.guessDate(String.valueOf(date[1]));
                                if (!(s.after(load.getEnd()) ||
                                      e.before(load.getStart()))) {
                                    return Boolean.TRUE;
                                }
                            }
                            catch (IllegalArgumentException iae) {
                                return Boolean.FALSE;
                            }
                        }
                        return Boolean.FALSE;
                    }
                    else {
                        return null;
                    }
                }
            });
    }

    @Override
    public Object computeAdvance(D4EArtifact artifact, String hash,
        CallContext context, List<Facet> facets, Object old) {
        logger.debug("SedimentLoadCalculate.computeAdvance");

        List<Facet> newFacets = new ArrayList<Facet>();

        if (access == null) {
            access = new SedimentLoadAccess(artifact);
        }

        CalculationResult res = old instanceof CalculationResult ? (CalculationResult) old
            : new SedimentLoadCalculation().calculate(access);

        if (facets == null || res == null) {
            return res;
        }

        SedimentLoadResult[] results = (SedimentLoadResult[]) res.getData();

        if (results == null || results.length == 0) {
            logger.warn("Calculation computed no results!");
            return res;
        }

        String type = access.getYearEpoch();
        if (type.equals("year")) {
            generateYearFacets(context, newFacets, results, getID(), hash);
        }
        else if (type.equals("epoch")) {
            generateEpochFacets(context, newFacets, results, getID(), hash);
        }
        else if (type.equals("off_epoch")) {
            generateOffEpochFacets(context, newFacets, results, getID(), hash);
        }
        logger.debug("Created " + newFacets.size() + " new Facets.");

        String river = access.getRiverName();
        SedimentLoad[] unknown =
            SedimentLoadFactory.getSedimentLoadUnknown(river,
                access.getUnit().replace("_per_","/"), type);

        boolean isUnitTA = access.getUnit().startsWith("t");

        if (res.getReport().hasProblems()) {
            newFacets.add(new ReportFacet(ComputeType.ADVANCE, hash, id));
        }

        for (int i = 0; i < unknown.length; i++) {
            newFacets.add(new SedimentLoadUnknownFacet(
                i,
                (isUnitTA)? SEDIMENT_LOAD_TA_UNKNOWN:SEDIMENT_LOAD_M3A_UNKNOWN,
                unknown[i].getDescription(),
                ComputeType.ADVANCE,
                getID(),
                hash));
        }

        newFacets.add(
            new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));

        facets.addAll(newFacets);

        return res;
    }

    protected void generateYearFacets(CallContext context, List<Facet> newFacets,
        SedimentLoadResult[] results, String stateId, String hash) {
        logger.debug("SedimentLoadCalculate.generateFacets");

        CallMeta meta = context.getMeta();
        boolean isUnitTA = access.getUnit().startsWith("t");

//      newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));
        for (int idx = 0; idx < results.length; idx++) {
            SedimentLoadResult res = results[idx];
            if (res.hasCoarseData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_COARSE
                    :SEDIMENT_LOAD_M3A_COARSE,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_COARSE,
                        I18N_FACET_SEDIMENTLOAD_COARSE) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSandData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SAND
                    :SEDIMENT_LOAD_M3A_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SAND,
                        I18N_FACET_SEDIMENTLOAD_SAND) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasFineMiddleData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_FINEMIDDLE
                    :SEDIMENT_LOAD_M3A_FINEMIDDLE,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE,
                        I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSandData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SAND
                    :SEDIMENT_LOAD_M3A_SUSP_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSAND,
                        I18N_FACET_SEDIMENTLOAD_SUSPSAND)
                        + " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSandBedData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SAND_BED
                    :SEDIMENT_LOAD_M3A_SUSP_SAND_BED,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSANDBED,
                        I18N_FACET_SEDIMENTLOAD_SUSPSANDBED) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSedimentData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SEDIMENT
                    :SEDIMENT_LOAD_M3A_SUSP_SEDIMENT,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT,
                        I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));

            }
            if (res.hasTotalData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_TOTAL
                    :SEDIMENT_LOAD_M3A_TOTAL,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_TOTAL,
                        I18N_FACET_SEDIMENTLOAD_TOTAL) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
        }
    }

    protected void generateEpochFacets(
        CallContext context,
        List<Facet> newFacets,
        SedimentLoadResult[] results,
        String stateId,
        String hash
    ) {
        logger.debug("SedimentLoadCalculate.generateEpochFacets");

        CallMeta meta = context.getMeta();
        boolean isUnitTA = access.getUnit().startsWith("t");

//      newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));
        for (int idx = 0; idx < results.length; idx++) {
            SedimentLoadResult res = results[idx];
            if (res.hasCoarseData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_COARSE
                    :SEDIMENT_LOAD_M3A_COARSE,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_COARSE,
                        I18N_FACET_SEDIMENTLOAD_COARSE) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSandData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SAND
                    :SEDIMENT_LOAD_M3A_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SAND,
                        I18N_FACET_SEDIMENTLOAD_SAND) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasFineMiddleData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_FINEMIDDLE
                    :SEDIMENT_LOAD_M3A_FINEMIDDLE,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE,
                        I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE) +
                        " - " + res.getStartYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSandData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SAND
                    :SEDIMENT_LOAD_M3A_SUSP_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSAND,
                        I18N_FACET_SEDIMENTLOAD_SUSPSAND)
                        + " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSandBedData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SAND_BED
                    :SEDIMENT_LOAD_M3A_SUSP_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSANDBED,
                        I18N_FACET_SEDIMENTLOAD_SUSPSANDBED) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSedimentData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SEDIMENT
                    :SEDIMENT_LOAD_M3A_SUSP_SEDIMENT,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT,
                        I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));

            }
            if (res.hasTotalLoadData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_TOTAL_LOAD
                    :SEDIMENT_LOAD_M3A_TOTAL_LOAD,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD,
                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasTotalData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_TOTAL
                    :SEDIMENT_LOAD_M3A_TOTAL,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_TOTAL,
                        I18N_FACET_SEDIMENTLOAD_TOTAL) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
        }
    }

    protected void generateOffEpochFacets(
        CallContext context,
        List<Facet> newFacets,
        SedimentLoadResult[] results,
        String stateId,
        String hash
    ) {
        logger.debug("SedimentLoadCalculate.generateOffEpochFacets");

        CallMeta meta = context.getMeta();
        boolean isUnitTA = access.getUnit().startsWith("t");

//      newFacets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));
        for (int idx = 0; idx < results.length; idx++) {
            SedimentLoadResult res = results[idx];
            if (res.hasCoarseData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_COARSE
                    :SEDIMENT_LOAD_M3A_COARSE,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_COARSE,
                        I18N_FACET_SEDIMENTLOAD_COARSE) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSandData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SAND
                    :SEDIMENT_LOAD_M3A_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SAND,
                        I18N_FACET_SEDIMENTLOAD_SAND) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasFineMiddleData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_FINEMIDDLE
                    :SEDIMENT_LOAD_M3A_FINEMIDDLE,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE,
                        I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSandData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SAND
                    :SEDIMENT_LOAD_M3A_SUSP_SAND,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSAND,
                        I18N_FACET_SEDIMENTLOAD_SUSPSAND) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSandBedData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SAND_BED
                    :SEDIMENT_LOAD_M3A_SUSP_SAND_BED,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSANDBED,
                        I18N_FACET_SEDIMENTLOAD_SUSPSANDBED) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasSuspSedimentData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_SUSP_SEDIMENT
                    :SEDIMENT_LOAD_M3A_SUSP_SEDIMENT,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT,
                        I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));

            }
            if (res.hasTotalLoadData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_TOTAL_LOAD
                    :SEDIMENT_LOAD_M3A_TOTAL_LOAD,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD,
                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
            if (res.hasTotalData()) {
                newFacets.add(new SedimentLoadFacet(
                    idx,
                    (isUnitTA)
                    ?SEDIMENT_LOAD_TA_TOTAL
                    :SEDIMENT_LOAD_M3A_TOTAL,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_TOTAL,
                        I18N_FACET_SEDIMENTLOAD_TOTAL) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org