bjoern@4247: package de.intevation.flys.artifacts.services; bjoern@4247: bjoern@4247: import java.math.BigDecimal; bjoern@4247: bjoern@4247: import org.apache.log4j.Logger; bjoern@4247: bjoern@4247: import org.w3c.dom.Document; bjoern@4247: import org.w3c.dom.Element; bjoern@4247: bjoern@4247: import de.intevation.artifacts.CallMeta; bjoern@4247: import de.intevation.artifacts.GlobalContext; bjoern@4247: import de.intevation.artifacts.common.ArtifactNamespaceContext; bjoern@4247: import de.intevation.artifacts.common.utils.XMLUtils; bjoern@4247: bjoern@4247: import de.intevation.flys.artifacts.model.RiverFactory; bjoern@4247: import de.intevation.flys.model.River; bjoern@4247: bjoern@4247: /** bjoern@4247: * @author Björn Ricks bjoern@4247: */ bjoern@4247: public class RiverInfoService extends FLYSService { bjoern@4247: bjoern@4247: private static final Logger logger = Logger.getLogger( bjoern@4247: RiverInfoService.class); bjoern@4247: bjoern@4247: protected static final String RIVER_XPATH = "/art:river/text()"; bjoern@4247: bjoern@4247: protected XMLUtils.ElementCreator ec; bjoern@4247: protected River river; bjoern@4247: protected Element riverele; bjoern@4247: bjoern@4247: protected Document doProcess( bjoern@4247: Document data, bjoern@4247: GlobalContext globalContext, bjoern@4247: CallMeta callMeta bjoern@4247: ) { bjoern@4247: String rivername = XMLUtils.xpathString( bjoern@4247: data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE); bjoern@4247: bjoern@4247: river = RiverFactory.getRiver(rivername); bjoern@4247: bjoern@4247: Document result = XMLUtils.newDocument(); bjoern@4247: bjoern@4247: if (river == null) { bjoern@4247: logger.warn("No river with name " + rivername + " found."); bjoern@4247: return null; bjoern@4247: } bjoern@4247: bjoern@4247: ec = new XMLUtils.ElementCreator( bjoern@4247: result, bjoern@4247: ArtifactNamespaceContext.NAMESPACE_URI, bjoern@4247: ArtifactNamespaceContext.NAMESPACE_PREFIX); bjoern@4247: bjoern@4247: riverele = ec.create("river-info"); bjoern@4247: bjoern@4247: double[] minmax = river.determineMinMaxDistance(); bjoern@4247: double[] minmaxq = river.determineMinMaxQ(); bjoern@4247: bjoern@4247: Element r = ec.create("river"); bjoern@4247: ec.addAttr(r, "name", river.getName(), true); bjoern@4247: ec.addAttr(r, "start", Double.toString(minmax[0]), true); bjoern@4247: ec.addAttr(r, "end", Double.toString(minmax[1]), true); bjoern@4247: ec.addAttr(r, "wstunit", river.getWstUnit().getName(), true); bjoern@4247: ec.addAttr(r, "kmup", Boolean.toString(river.getKmUp()), true); bjoern@4247: ec.addAttr(r, "minq", Double.toString(minmaxq[0]), true); bjoern@4247: ec.addAttr(r, "maxq", Double.toString(minmaxq[1]), true); bjoern@4247: ec.addAttr(r, "official", Long.toString(river.getOfficialNumber()), bjoern@4247: true); bjoern@4247: bjoern@4247: riverele.appendChild(r); bjoern@4247: result.appendChild(riverele); bjoern@4247: bjoern@4247: return result; bjoern@4247: } bjoern@4247: bjoern@4247: /** bjoern@4247: * Returns a Double as String from a BigDecimal value. bjoern@4247: * bjoern@4247: * If value is null an empty String is returned. bjoern@4247: */ bjoern@4247: protected static String getStringValue(BigDecimal value) { bjoern@4247: return value != null bjoern@4247: ? Double.toString(value.doubleValue()) : ""; bjoern@4247: } bjoern@4247: }