view artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadCalculate.java @ 6665:b7945db8a43b

issue1413: Only show unknown sediment loads of selected unit type. Therefore, adjusted the factory to take the units name. Unfortunately, names in db do not match values of data items. Thus do manual replacing. In Facet and Calculate, take the chosen unit via access and to the string replacement. In Facet, do not transform data (we assume it comes in unit as labeled in the db), and removed the possibility of m3/a-data of unknown yields in a t/a diagram and vice versa.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 25 Jul 2013 15:08:13 +0200
parents 113c0fc71280
children 368db84f1241
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.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;


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";

    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_COARSE) ||
                        name.equals(SEDIMENT_LOAD_FINEMIDDLE) ||
                        name.equals(SEDIMENT_LOAD_SAND) ||
                        name.equals(SEDIMENT_LOAD_SUSP_SAND) ||
                        name.equals(SEDIMENT_LOAD_SUSP_SEDIMENT) ||
                        name.equals(SEDIMENT_LOAD_SUSP_SAND_BED)){
                        return Boolean.FALSE;
                    }
                    else if (name.equals(SEDIMENT_LOAD_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>();

        SedimentLoadAccess 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 river = access.getRiver();
        SedimentLoad[] unknown =
            SedimentLoadFactory.getSedimentLoadUnknown(river, access.getUnit().replace("_per_","/"));

        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.");
        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,
                SEDIMENT_LOAD_UNKNOWN,
                unknown[i].getDescription(),
                ComputeType.ADVANCE,
                getID(),
                hash));
        }
        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();

//      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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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();

//      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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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();

//      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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_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,
                    SEDIMENT_LOAD_TOTAL,
                    Resources.getMsg(
                        meta,
                        I18N_FACET_SEDIMENTLOAD_TOTAL,
                        I18N_FACET_SEDIMENTLOAD_TOTAL) +
                        " - " + res.getStartYear() + "-" + res.getEndYear(),
                    ComputeType.ADVANCE,
                    stateId,
                    hash));
            }
        }
    }
}

http://dive4elements.wald.intevation.org