bjoern@3739: package de.intevation.flys.artifacts.services; bjoern@3739: bjoern@3739: import java.math.BigDecimal; bjoern@3739: import java.util.List; bjoern@3739: bjoern@3739: import org.apache.log4j.Logger; bjoern@3739: bjoern@3739: import org.w3c.dom.Document; bjoern@3739: import org.w3c.dom.Element; bjoern@3739: bjoern@3739: import de.intevation.artifacts.CallMeta; bjoern@3739: import de.intevation.artifacts.GlobalContext; bjoern@3739: import de.intevation.artifacts.common.ArtifactNamespaceContext; bjoern@3739: import de.intevation.artifacts.common.utils.XMLUtils; bjoern@3739: bjoern@3739: import de.intevation.flys.artifacts.model.RiverFactory; bjoern@3739: import de.intevation.flys.model.Gauge; bjoern@3739: import de.intevation.flys.model.MinMaxWQ; bjoern@3739: import de.intevation.flys.model.Range; bjoern@3739: import de.intevation.flys.model.River; bjoern@3739: bjoern@3739: /** bjoern@3739: * @author Björn Ricks bjoern@3739: */ bjoern@3739: public class GaugeOverviewInfoService extends FLYSService { bjoern@3739: bjoern@3739: private static final Logger logger = Logger.getLogger( bjoern@3739: GaugeOverviewInfoService.class); bjoern@3739: bjoern@3739: public static final String RIVER_XPATH = "/art:river/text()"; bjoern@3739: bjoern@3739: @Override bjoern@3739: public Document doProcess( bjoern@3739: Document data, bjoern@3739: GlobalContext globalContext, bjoern@3739: CallMeta callMeta bjoern@3739: ) { bjoern@3739: logger.debug("GaugeOverviewInfoService.process"); bjoern@3739: bjoern@3739: String riverstr = XMLUtils.xpathString( bjoern@3739: data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE); bjoern@3739: bjoern@3739: River river = RiverFactory.getRiver(riverstr); bjoern@3739: bjoern@3739: Document result = XMLUtils.newDocument(); bjoern@3739: bjoern@3739: if (river == null) { bjoern@3739: logger.warn("No river with name " + riverstr + " found."); bjoern@3739: return result; bjoern@3739: } bjoern@3739: bjoern@3739: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( bjoern@3739: result, bjoern@3739: ArtifactNamespaceContext.NAMESPACE_URI, bjoern@3739: ArtifactNamespaceContext.NAMESPACE_PREFIX); bjoern@3739: bjoern@3739: Element go = ec.create("gauge-info"); bjoern@3739: bjoern@3753: double[] minmax = river.determineMinMaxDistance(); bjoern@3753: double[] minmaxq = river.determineMinMaxQ(); bjoern@3739: bjoern@3739: Element r = ec.create("river"); bjoern@3739: ec.addAttr(r, "name", river.getName(), true); bjoern@3739: ec.addAttr(r, "start", Double.toString(minmax[0]), true); bjoern@3739: ec.addAttr(r, "end", Double.toString(minmax[1]), true); bjoern@3739: ec.addAttr(r, "wstunit", river.getWstUnit().getName(), true); bjoern@3739: ec.addAttr(r, "kmup", Boolean.toString(river.getKmUp()), true); bjoern@3753: ec.addAttr(r, "minq", Double.toString(minmaxq[0]), true); bjoern@3753: ec.addAttr(r, "maxq", Double.toString(minmaxq[1]), true); bjoern@3739: bjoern@3739: Element egs = ec.create("gauges"); bjoern@3739: bjoern@3739: List gauges = river.getGauges(); sascha@3748: sascha@3748: if (logger.isDebugEnabled()) { sascha@3748: logger.debug("Loaded gauges: " + gauges); sascha@3748: } sascha@3748: bjoern@3739: for (Gauge gauge: river.getGauges()) { bjoern@3739: Element eg = ec.create("gauge"); sascha@3748: bjoern@3739: String name = gauge.getName(); bjoern@3739: if (name != null) { bjoern@3739: ec.addAttr(eg, "name", gauge.getName(), true); bjoern@3739: } bjoern@3739: sascha@3748: String aeo = getGaugeValue(gauge.getAeo()); sascha@3748: if (aeo != null) { sascha@3748: ec.addAttr(eg, "aeo", aeo, true); sascha@3748: } sascha@3748: sascha@3748: String datum = getGaugeValue(gauge.getDatum()); sascha@3748: if (datum != null) { sascha@3748: ec.addAttr(eg, "datum", datum, true); sascha@3748: } sascha@3748: bjoern@3739: Range range = gauge.getRange(); bjoern@3739: if (range != null) { bjoern@3753: BigDecimal a = range.getA(); bjoern@3753: if (a != null) { bjoern@3753: double min = a.doubleValue(); bjoern@3753: ec.addAttr(eg, "start", Double.toString(min), true); bjoern@3753: } bjoern@3739: bjoern@3739: BigDecimal b = range.getB(); bjoern@3739: if (b != null) { bjoern@3739: double max = range.getB().doubleValue(); bjoern@3739: ec.addAttr(eg, "end", Double.toString(max), true); bjoern@3739: } bjoern@3739: } bjoern@3739: MinMaxWQ minmaxwq = gauge.fetchMaxMinWQ(); bjoern@3739: String minw = getGaugeValue(minmaxwq.getMinW()); bjoern@3739: String maxw = getGaugeValue(minmaxwq.getMaxW()); bjoern@3739: String minq = getGaugeValue(minmaxwq.getMinQ()); bjoern@3739: String maxq = getGaugeValue(minmaxwq.getMaxQ()); bjoern@3739: bjoern@3739: if (minw != null) { bjoern@3739: ec.addAttr(eg, "minw", minw, true); bjoern@3739: } bjoern@3739: if (maxw != null) { bjoern@3739: ec.addAttr(eg, "maxw", maxw, true); bjoern@3739: } bjoern@3739: if (minq != null) { bjoern@3739: ec.addAttr(eg, "minq", minq, true); bjoern@3739: } bjoern@3739: if (maxq != null) { bjoern@3739: ec.addAttr(eg, "maxq", maxq, true); bjoern@3739: } bjoern@3739: bjoern@3739: egs.appendChild(eg); bjoern@3739: } bjoern@3739: bjoern@3739: go.appendChild(r); bjoern@3739: go.appendChild(egs); bjoern@3739: result.appendChild(go); bjoern@3739: bjoern@3739: return result; bjoern@3739: } bjoern@3739: bjoern@3747: /** bjoern@3747: * Returns a Double from a BigDecimal value or null if value is null bjoern@3747: */ sascha@3748: private static String getGaugeValue(BigDecimal value) { sascha@3748: return value != null sascha@3748: ? Double.toString(value.doubleValue()) sascha@3748: : ""; bjoern@3739: } bjoern@3739: }