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; ingo@688: teichmann@6510: import java.util.Date; christian@3883: import java.util.List; christian@3883: christian@3883: import org.apache.log4j.Logger; christian@3883: teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.river.artifacts.ChartArtifact; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5831: import org.dive4elements.river.artifacts.WINFOArtifact; teichmann@6501: import org.dive4elements.river.artifacts.model.Calculation; teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; teichmann@5831: import org.dive4elements.river.artifacts.model.CrossSectionWaterLineFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.DataFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.EmptyFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes; teichmann@6501: import org.dive4elements.river.artifacts.model.OfficialLineFinder; teichmann@5831: import org.dive4elements.river.artifacts.model.ReportFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.WQKms; teichmann@5831: import org.dive4elements.river.artifacts.model.WaterlevelFacet; teichmann@6501: import org.dive4elements.river.artifacts.resources.Resources; teichmann@5865: import org.dive4elements.river.utils.RiverUtils; sascha@2195: felix@4412: /** State in which a waterlevel has been calculated. */ ingo@692: public class WaterlevelState ingo@692: extends DefaultState ingo@692: implements FacetTypes ingo@692: { teichmann@8202: /** The log that is used in this state. */ teichmann@8202: private static Logger log = Logger.getLogger(WaterlevelState.class); ingo@688: ingo@688: felix@1770: /** felix@1770: * From this state can only be continued trivially. felix@1770: */ ingo@688: @Override ingo@927: protected String getUIProvider() { ingo@927: return "continue"; ingo@927: } ingo@927: ingo@927: felix@2123: /** felix@2123: * Compute result or returned object from cache, create facets. felix@2123: * @param old Object that was cached. felix@2123: */ felix@1140: protected Object compute( felix@1140: WINFOArtifact winfo, ingo@1664: CallContext cc, felix@1140: String hash, felix@1140: List facets, felix@1140: Object old felix@1138: ) { felix@1138: String id = getID(); felix@1138: felix@1138: CalculationResult res = old instanceof CalculationResult felix@2123: ? (CalculationResult) old felix@1138: : winfo.getWaterlevelData(); felix@1138: felix@1138: if (facets == null) { felix@1138: return res; felix@1138: } felix@1138: teichmann@8202: boolean debug = log.isDebugEnabled(); sascha@2195: felix@2123: WQKms [] wqkms = (WQKms []) res.getData(); felix@1138: felix@1138: for (int i = 0; i < wqkms.length; i++) { felix@2123: String name = wqkms[i].getName(); felix@1138: teichmann@5865: String nameW = RiverUtils.createWspWTitle(winfo, cc, name); teichmann@5865: String nameQ = RiverUtils.createWspQTitle(winfo, cc, name); felix@1138: raimund@2618: // Hotfix for theme names. Themes with the same name cause problems raimund@2618: // aggregating chart legend items. raimund@2618: if (i > 0 && name.equals(wqkms[i - 1].getName())) { raimund@2618: nameW += "; Q=" + wqkms[i].get(0, new double[3])[1]; raimund@2618: nameQ += " = " + wqkms[i].get(0, new double[3])[1]; raimund@2618: } raimund@2618: sascha@2195: if (debug) { teichmann@8202: log.debug("Create facet: " + nameW); teichmann@8202: log.debug("Create facet: " + nameQ); sascha@2195: } felix@1138: felix@1138: Facet w = new WaterlevelFacet( felix@1138: i, LONGITUDINAL_W, nameW, ComputeType.ADVANCE, id, hash); felix@1138: Facet q = new WaterlevelFacet( felix@1138: i, LONGITUDINAL_Q, nameQ, ComputeType.ADVANCE, id, hash); felix@1138: felix@2573: facets.add(new CrossSectionWaterLineFacet(i, nameW)); felix@1802: felix@1138: facets.add(w); felix@1138: facets.add(q); felix@1138: } felix@1138: felix@1138: if (wqkms.length > 0) { felix@1138: Facet wst = new DataFacet( felix@1138: WST, "WST data", ComputeType.ADVANCE, hash, id); felix@1138: Facet csv = new DataFacet( felix@1138: CSV, "CSV data", ComputeType.ADVANCE, hash, id); raimund@2176: Facet pdf = new DataFacet( raimund@2176: PDF, "PDF data", ComputeType.ADVANCE, hash, id); felix@1138: felix@1138: facets.add(wst); felix@1138: facets.add(csv); raimund@2176: facets.add(pdf); felix@1138: } felix@1138: teichmann@6501: Calculation report = res.getReport(); teichmann@6501: teichmann@6501: List ols = teichmann@6501: OfficialLineFinder.findOfficialLines(winfo); teichmann@6501: teichmann@6501: if (!ols.isEmpty()) { teichmann@6501: for (OfficialLineFinder.ValueRange ol: ols) { teichmann@6501: report.addProblem(Resources.format( teichmann@6501: cc.getMeta(), teichmann@6501: "official.line.found", tom@8856: "Found official line for {0} from year {1,date,yyyy} " tom@8856: + "from {2}.", teichmann@6510: ol.getName(), nn(ol.getDate()), nn(ol.getSource()))); teichmann@6501: } teichmann@6501: } teichmann@6501: teichmann@6501: if (report.hasProblems()) { felix@1138: facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id)); felix@1138: } felix@1138: felix@1138: return res; felix@1138: } felix@1138: felix@6554: /** Returns empty String if argument is null, argument itself otherwise. */ teichmann@6510: private static final String nn(String s) { teichmann@6510: return s != null ? s : ""; teichmann@6510: } teichmann@6510: teichmann@6510: private static final Date nn(Date d) { teichmann@6510: return d != null ? d : new Date(); teichmann@6510: } felix@1138: felix@1140: /** felix@1140: * @param context Ignored. felix@1140: */ felix@1140: @Override felix@1140: public Object computeFeed( teichmann@5867: D4EArtifact artifact, felix@1140: String hash, felix@1140: CallContext context, felix@1140: List facets, felix@1140: Object old felix@1140: ) { raimund@2154: if (artifact instanceof ChartArtifact) { raimund@2154: facets.add(new EmptyFacet()); raimund@2154: return null; raimund@2154: } ingo@1664: return compute((WINFOArtifact) artifact, context, hash, facets, old); felix@1140: } felix@1140: felix@1140: felix@1140: /** felix@1140: * @param context Ignored. felix@1140: */ ingo@927: @Override sascha@697: public Object computeAdvance( teichmann@5867: D4EArtifact artifact, sascha@697: String hash, sascha@697: CallContext context, sascha@742: List facets, sascha@697: Object old sascha@697: ) { raimund@2154: if (artifact instanceof ChartArtifact) { raimund@2154: facets.add(new EmptyFacet()); raimund@2154: return null; raimund@2154: } ingo@1664: return compute((WINFOArtifact) artifact, context, hash, facets, old); ingo@688: } ingo@688: } ingo@688: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :