teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; felix@2723: felix@2723: import java.util.List; felix@2723: aheinecke@6851: import java.text.DateFormat; aheinecke@6851: felix@2723: import org.apache.log4j.Logger; felix@2723: teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5831: import org.dive4elements.artifacts.CallContext; felix@2723: teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; felix@2723: teichmann@5831: import org.dive4elements.river.artifacts.ChartArtifact; teichmann@5831: import org.dive4elements.river.artifacts.GaugeDischargeArtifact; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; felix@2723: teichmann@5831: import org.dive4elements.river.artifacts.model.GaugeDischargeFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes; teichmann@5831: import org.dive4elements.river.artifacts.model.WQKms; teichmann@5831: import org.dive4elements.river.artifacts.model.ReportFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.EmptyFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; teichmann@5831: teichmann@5831: import org.dive4elements.river.artifacts.resources.Resources; teichmann@5831: teichmann@5831: import org.dive4elements.river.model.Gauge; aheinecke@6851: import org.dive4elements.river.model.DischargeTable; aheinecke@6851: import org.dive4elements.river.model.TimeInterval; felix@3051: aheinecke@6851: import org.dive4elements.river.utils.Formatter; felix@2723: felix@2723: /** felix@2723: * The only state for an GaugeDischargeState (River and km known). felix@2723: */ felix@2723: public class GaugeDischargeState felix@2723: extends DefaultState felix@2723: implements FacetTypes felix@2723: { felix@2723: /** Developer-centric description of facet. */ felix@2723: public static final String I18N_DESCRIPTION = "facet.discharge.curve"; felix@2723: teichmann@8202: /** The log that is used in this state. */ teichmann@8202: private static final Logger log = felix@3051: Logger.getLogger(GaugeDischargeState.class); felix@3051: felix@3051: felix@3051: /** felix@3051: * Create i18ned name for gaugedischargeFacet. felix@3051: * @param artifact The artifact which has information about the gauge. felix@3051: * @param meta used for i18n. felix@3051: * @return localized name for gaugedischargefacet. felix@3051: */ felix@3051: protected String createFacetName(GaugeDischargeArtifact artifact, felix@3051: CallMeta meta) { felix@3051: aheinecke@6693: if (artifact.getFacetWishName() != null) { aheinecke@6693: /* We let the Artifact overwrite our name as this allows aheinecke@6693: * injecting the facet name from the Datacage */ aheinecke@6693: return artifact.getFacetWishName(); aheinecke@6693: } aheinecke@6693: felix@3051: Gauge gauge = artifact.getGauge(); aheinecke@6851: DischargeTable mdt = gauge.fetchMasterDischargeTable(); aheinecke@6851: TimeInterval validity = mdt.getTimeInterval(); aheinecke@6851: DateFormat df = Formatter.getDateFormatter(meta, "dd.MM.yyyy"); aheinecke@6851: String start = validity.getStartTime() != null ? aheinecke@6851: df.format(validity.getStartTime()) : "Unknown"; aheinecke@6851: String stop = validity.getStopTime() != null ? aheinecke@6851: df.format(validity.getStopTime()) : ""; aheinecke@6851: felix@3051: Object[] args = new Object[] { felix@3051: gauge.getName(), aheinecke@6851: start, aheinecke@6851: stop felix@3051: }; felix@3051: felix@3051: String name = Resources.getMsg( felix@3051: meta, felix@3051: "chart.computed.discharge.curve.gauge", felix@3051: "", felix@3051: args); felix@3051: felix@3051: return name; felix@3051: } felix@2723: felix@2723: felix@2723: /** felix@2723: * Add an GaugeDischargeFacet to list of Facets. felix@2723: * felix@2723: * @param artifact Ignored. felix@2723: * @param hash Ignored. felix@2723: * @param context Ignored. felix@2723: * @param meta CallMeta to be used for internationalization. felix@2723: * @param facets List to add AnnotationFacet to. felix@2723: * felix@2723: * @return null. felix@2723: */ felix@2723: @Override felix@2723: public Object computeInit( teichmann@5867: D4EArtifact artifact, felix@2723: String hash, felix@2723: Object context, felix@2723: CallMeta meta, felix@2723: List facets felix@2723: ) { teichmann@8202: log.debug("GaugeDischargeState.computeInit()"); felix@2723: felix@2723: GaugeDischargeFacet facet = new GaugeDischargeFacet( felix@2723: 0, felix@2723: DISCHARGE_CURVE, felix@3051: createFacetName((GaugeDischargeArtifact) artifact, meta)); felix@3051: felix@2723: facets.add(facet); felix@2723: felix@2723: return null; felix@2723: } felix@2723: felix@2734: felix@3051: /** felix@3051: * 'Calculate' Discharge at Gauge. felix@3051: */ felix@2734: @Override felix@2734: public Object computeAdvance( teichmann@5867: D4EArtifact artifact, felix@2734: String hash, felix@2734: CallContext context, felix@2734: List facets, felix@2734: Object old felix@2734: ) { felix@2734: if (artifact instanceof GaugeDischargeArtifact) { teichmann@8202: log.debug("GaugeDischargeState.computeAdvance()"); felix@2734: GaugeDischargeArtifact dischargeArtifact = (GaugeDischargeArtifact) artifact; felix@2734: felix@2734: CalculationResult res; felix@2734: felix@3053: felix@3053: if (old instanceof CalculationResult) { felix@2734: res = (CalculationResult) old; felix@2734: } felix@3053: else { felix@2734: res = dischargeArtifact.getDischargeCurveData(); felix@3053: } felix@2734: felix@2734: WQKms[] wqkms = (WQKms[]) res.getData(); felix@2734: felix@2734: if (wqkms != null && facets != null) { teichmann@8202: log.debug("GaugeDischargeState.computeAdvance(): create facets"); felix@2734: felix@2734: GaugeDischargeFacet facet = new GaugeDischargeFacet( felix@2734: 0, felix@3051: DISCHARGE_CURVE, felix@3051: createFacetName(dischargeArtifact, context.getMeta())); felix@3051: felix@2734: facets.add(facet); felix@2734: felix@2734: //facets.add(new DataFacet(CSV, "CSV data")); felix@2734: //facets.add(new DataFacet(PDF, "PDF data")); felix@2734: felix@2734: if (res.getReport().hasProblems()) { felix@2734: facets.add(new ReportFacet()); felix@2734: } felix@2734: } felix@2734: else { felix@2734: if (wqkms == null) teichmann@8202: log.debug("GaugeDischargeState.computeAdvance(): wqkms 0"); felix@2734: else teichmann@8202: log.debug("GaugeDischargeState.computeAdvance(): facets 0"); felix@2734: } felix@2734: felix@2734: return res; felix@2734: } felix@2734: else if (artifact instanceof ChartArtifact) { felix@2734: ChartArtifact chart = (ChartArtifact)artifact; felix@2734: facets.add(new EmptyFacet()); felix@2734: return null; felix@2734: } felix@2734: return null; felix@2734: } felix@2723: } felix@2723: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :