teichmann@8085: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@8085: * Software engineering by Intevation GmbH teichmann@8085: * teichmann@8085: * This file is Free Software under the GNU AGPL (>=v3) teichmann@8085: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@8085: * documentation coming with Dive4Elements River for details. teichmann@8085: */ teichmann@8085: teichmann@8085: package org.dive4elements.river.artifacts.states.minfo; teichmann@8085: teichmann@8085: import java.util.List; teichmann@8085: teichmann@8085: import org.apache.log4j.Logger; teichmann@8085: import org.dive4elements.artifactdatabase.state.Facet; teichmann@8085: import org.dive4elements.artifactdatabase.state.FacetActivity; teichmann@8085: import org.dive4elements.artifacts.Artifact; teichmann@8085: import org.dive4elements.artifacts.CallContext; teichmann@8085: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@8085: import org.dive4elements.river.artifacts.access.SedimentLoadAccess; teichmann@8085: import org.dive4elements.river.artifacts.model.CalculationResult; andre@8126: import org.dive4elements.river.artifacts.model.Calculation; teichmann@8085: import org.dive4elements.river.artifacts.model.DataFacet; teichmann@8085: import org.dive4elements.river.artifacts.model.FacetTypes; teichmann@8085: import org.dive4elements.river.artifacts.model.ReportFacet; teichmann@8085: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataCalculation; andre@8125: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult; andre@8125: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult.Fraction; andre@8125: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataFacet; teichmann@8085: import org.dive4elements.river.artifacts.states.DefaultState; teichmann@8085: teichmann@8085: /** State in which Sediment Load(s) are calculated/retrieved. */ teichmann@8085: public class SedimentLoadDataCalculate teichmann@8085: extends DefaultState teichmann@8085: implements FacetTypes teichmann@8085: { teichmann@8085: teichmann@8085: private static final long serialVersionUID = 1L; teichmann@8085: teichmann@8085: private static final Logger logger = Logger andre@8125: .getLogger(SedimentLoadDataCalculate.class); teichmann@8085: teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_COARSE = "facet.sedimentload.coarse"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_SAND = "facet.sedimentload.sand"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_FINE_MIDDLE = "facet.sedimentload.fine_middle"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_SUSPSAND = "facet.sedimentload.susp_sand"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_SUSPSANDBED = "facet.sediemntload.susp_sand_bed"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT = "facet.sedimentload.susp_sediment"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD = "facet.sedimentload.total_load"; teichmann@8085: public static final String I18N_FACET_SEDIMENTLOAD_TOTAL = "facet.sedimentload.total"; teichmann@8085: teichmann@8085: static { teichmann@8085: // Active/deactivate facets. teichmann@8085: FacetActivity.Registry.getInstance().register( teichmann@8085: "minfo", teichmann@8085: new FacetActivity() { teichmann@8085: @Override teichmann@8085: public Boolean isInitialActive( teichmann@8085: Artifact artifact, teichmann@8085: Facet facet, teichmann@8085: String output teichmann@8085: ) { tom@8173: String name = facet.getName(); tom@8173: if (name.endsWith("total") || name.endsWith("bed_load") tom@8173: || name.endsWith("bed_load_susp_sand")) { tom@8173: return Boolean.TRUE; tom@8173: } tom@8173: return Boolean.FALSE; teichmann@8085: } teichmann@8085: }); teichmann@8085: } teichmann@8085: teichmann@8085: public SedimentLoadDataCalculate() { teichmann@8085: } teichmann@8085: teichmann@8085: @Override teichmann@8085: public Object computeAdvance(D4EArtifact artifact, String hash, teichmann@8085: CallContext context, List facets, Object old) { teichmann@8085: logger.debug("SedimentLoadDataCalculate.computeAdvance"); teichmann@8085: teichmann@8085: SedimentLoadAccess access = new SedimentLoadAccess(artifact); teichmann@8085: teichmann@8085: CalculationResult res = old instanceof CalculationResult teichmann@8085: ? (CalculationResult) old teichmann@8085: : new SedimentLoadDataCalculation().calculate(access); teichmann@8085: andre@8125: andre@8125: if (res == null) { andre@8125: logger.error ("No calculation result."); andre@8125: } andre@8125: andre@8133: if (facets == null) { andre@8133: /* No need to create facets as they already exist in this case. */ andre@8133: return res; andre@8133: } andre@8133: andre@8133: Calculation report = res.getReport(); andre@8133: andre@8133: if (report != null && report.hasProblems()) { andre@8133: facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id)); andre@8133: logger.warn ("Problems : " + report.problemsToString()); andre@8133: } andre@8133: andre@8125: Object raw = res.getData(); andre@8125: if (raw == null) { andre@8125: logger.warn("No result data."); teichmann@8085: return res; teichmann@8085: } teichmann@8085: andre@8126: SedimentLoadDataResult sdRes; andre@8125: if (raw instanceof SedimentLoadDataResult) { andre@8125: sdRes = (SedimentLoadDataResult) raw; andre@8125: } else { andre@8125: logger.error ("Unknown result"); andre@8125: return null; andre@8125: } andre@8133: String unit = access.getUnit().replace("_per_","/"); andre@8125: andre@8125: int i = 0; andre@8125: for (Fraction fract: sdRes.getFractions()) { tom@8146: logger.debug("Adding facet for fraction '" + fract.getName() + tom@8146: "' and period '" + fract.getPeriod() + "'"); tom@8174: facets.add(new SedimentLoadDataFacet(i, fract.getName(), tom@8146: unit, fract.getPeriod(), tom@8146: ComputeType.ADVANCE, id, hash, context)); tom@8174: i++; andre@8125: } andre@8125: andre@8133: facets.add( andre@8125: new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id)); andre@8125: teichmann@8085: return res; teichmann@8085: } teichmann@8085: teichmann@8085: } teichmann@8085: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :