view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/minfo/SedimentLoadCalculate.java @ 4546:af1938d4e957

New field in sediment load object and calculation. * Factory reads total load value. * New field in sediment load object. * State adds new facet to generate output for this type of data.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 15 Nov 2012 17:44:07 +0100
parents 3e1810e72199
children b195fede1c3b
line wrap: on
line source
package de.intevation.flys.artifacts.states.minfo;

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

import org.apache.log4j.Logger;

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CallMeta;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.access.SedimentLoadAccess;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.ReportFacet;
import de.intevation.flys.artifacts.model.minfo.SedimentLoadCalculation;
import de.intevation.flys.artifacts.model.minfo.SedimentLoadFacet;
import de.intevation.flys.artifacts.model.minfo.SedimentLoadResult;
import de.intevation.flys.artifacts.resources.Resources;
import de.intevation.flys.artifacts.states.DefaultState;


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

    @Override
    public Object computeAdvance(FLYSArtifact 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 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));
        }
        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