bjoern@3713: package de.intevation.flys.client.server; bjoern@3713: bjoern@3713: import java.util.ArrayList; bjoern@3713: import javax.xml.xpath.XPathConstants; bjoern@3713: bjoern@3713: import org.apache.log4j.Logger; bjoern@3713: bjoern@3713: import org.w3c.dom.Document; bjoern@3713: import org.w3c.dom.Element; bjoern@3713: import org.w3c.dom.NodeList; bjoern@3713: bjoern@3713: import de.intevation.artifacts.common.ArtifactNamespaceContext; bjoern@3713: import de.intevation.artifacts.common.utils.XMLUtils; bjoern@3713: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; bjoern@3713: import de.intevation.artifacts.httpclient.http.HttpClient; bjoern@3713: import de.intevation.artifacts.httpclient.http.HttpClientImpl; bjoern@3713: bjoern@3713: import de.intevation.flys.client.client.services.GaugeOverviewInfoService; bjoern@3713: import de.intevation.flys.client.shared.exceptions.ServerException; bjoern@3713: import de.intevation.flys.client.shared.model.DefaultGaugeInfo; bjoern@3713: import de.intevation.flys.client.shared.model.DefaultRiverInfo; bjoern@3713: import de.intevation.flys.client.shared.model.GaugeInfo; bjoern@3713: import de.intevation.flys.client.shared.model.RiverInfo; bjoern@3713: bjoern@3713: /** bjoern@3713: * @author Björn Ricks bjoern@3713: */ bjoern@3713: public class GaugeOverviewInfoServiceImpl bjoern@3713: extends RemoteServiceServlet bjoern@3713: implements GaugeOverviewInfoService bjoern@3713: { bjoern@3713: private static final Logger logger = bjoern@3713: Logger.getLogger(GaugeOverviewInfoServiceImpl.class); bjoern@3713: bjoern@3713: public static final String ERROR_NO_RIVERINFO_FOUND = bjoern@3713: "error_no_gaugeoverviewinfo_found"; bjoern@3713: bjoern@3713: private static final String XPATH_RIVER = "/art:gauge-info/art:river"; bjoern@3713: bjoern@3713: private static final String XPATH_GAUGES = "/art:gauge-info/art:gauges/art:gauge"; bjoern@3713: bjoern@3713: public RiverInfo getRiverInfo(String river) throws ServerException { bjoern@3713: logger.info("RiverInfoServiceImpl.getRiverInfo"); bjoern@3713: bjoern@3713: String url = getServletContext().getInitParameter("server-url"); bjoern@3713: bjoern@3713: Document doc = XMLUtils.newDocument(); bjoern@3713: bjoern@3713: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( bjoern@3713: doc, bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, bjoern@3713: ArtifactNamespaceContext.NAMESPACE_PREFIX); bjoern@3713: bjoern@3713: Element riverele = ec.create("river"); bjoern@3713: riverele.setTextContent(river); bjoern@3713: bjoern@3713: doc.appendChild(riverele); bjoern@3713: bjoern@3713: HttpClient client = new HttpClientImpl(url); bjoern@3713: bjoern@3713: try { bjoern@3713: Document result = client.callService(url, "gaugeoverviewinfo", doc); bjoern@3713: bjoern@3713: Element riverresp = (Element) XMLUtils.xpath( bjoern@3713: result, bjoern@3713: XPATH_RIVER, bjoern@3713: XPathConstants.NODE, bjoern@3713: ArtifactNamespaceContext.INSTANCE); bjoern@3713: bjoern@3713: String rname = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "name"); bjoern@3713: String rkmup = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "kmup"); bjoern@3713: String rstart = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "start"); bjoern@3713: String rend = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "end"); bjoern@3713: String rwstunit = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "wstunit"); bjoern@3713: String rminq = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "minq"); bjoern@3713: String rmaxq = riverresp.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "maxq"); bjoern@3847: String rofficial = riverresp.getAttributeNS( bjoern@3847: ArtifactNamespaceContext.NAMESPACE_URI, "official"); bjoern@3713: bjoern@3713: logger.debug("River is " + rname); bjoern@3713: bjoern@3713: boolean kmup = rkmup.equalsIgnoreCase("true"); bjoern@3713: bjoern@3713: NodeList gaugenodes = (NodeList) XMLUtils.xpath( bjoern@3713: result, bjoern@3713: XPATH_GAUGES, bjoern@3713: XPathConstants.NODESET, bjoern@3713: ArtifactNamespaceContext.INSTANCE); bjoern@3713: bjoern@3713: int num = gaugenodes == null ? 0 : gaugenodes.getLength(); bjoern@3713: bjoern@3713: ArrayList gauges = new ArrayList(num); bjoern@3713: bjoern@3713: if (num == 0) { bjoern@3713: logger.warn("No gauge info found."); bjoern@3713: } bjoern@3713: else { bjoern@3713: logger.debug("Found " + num + " gauges."); bjoern@3713: bjoern@3713: for (int i = 0; i < num; i++) { bjoern@3713: Element gaugeele = (Element)gaugenodes.item(i); bjoern@3713: bjoern@3713: String gname = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "name"); bjoern@3713: String gstart = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "start"); bjoern@3713: String gend = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "end"); bjoern@3713: String gdatum = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "datum"); bjoern@3713: String gaeo = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "aeo"); bjoern@3713: String gminq = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "minq"); bjoern@3713: String gminw = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "minw"); bjoern@3713: String gmaxq = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "maxq"); bjoern@3713: String gmaxw = gaugeele.getAttributeNS( bjoern@3713: ArtifactNamespaceContext.NAMESPACE_URI, "maxw"); bjoern@3826: String gstation = gaugeele.getAttributeNS( bjoern@3826: ArtifactNamespaceContext.NAMESPACE_URI, "station"); bjoern@3847: String gofficial = gaugeele.getAttributeNS( bjoern@3843: ArtifactNamespaceContext.NAMESPACE_URI, "official"); bjoern@3713: bjoern@3713: logger.debug("Found gauge with name " + gname); bjoern@3713: bjoern@3713: GaugeInfo gaugeinfo = new DefaultGaugeInfo( bjoern@3864: rname, bjoern@3713: gname, bjoern@3713: kmup, bjoern@3826: parseDouble(gstation), bjoern@3713: parseDouble(gstart), bjoern@3713: parseDouble(gend), bjoern@3713: parseDouble(gdatum), bjoern@3713: parseDouble(gaeo), bjoern@3713: parseDouble(gminq), bjoern@3713: parseDouble(gmaxq), bjoern@3713: parseDouble(gminw), bjoern@3831: parseDouble(gmaxw), bjoern@3838: rwstunit, bjoern@3843: parseLong(gofficial) bjoern@3713: ); bjoern@3713: bjoern@3713: gauges.add(gaugeinfo); bjoern@3713: } bjoern@3713: } bjoern@3713: bjoern@3713: RiverInfo riverinfo = new DefaultRiverInfo( bjoern@3713: rname, bjoern@3713: kmup, bjoern@3713: parseDouble(rstart), bjoern@3713: parseDouble(rend), bjoern@3713: rwstunit, bjoern@3713: parseDouble(rminq), bjoern@3713: parseDouble(rmaxq), bjoern@3847: parseLong(rofficial), bjoern@3713: gauges); bjoern@3713: bjoern@3713: logger.debug("Finished RiverInfoService."); bjoern@3713: bjoern@3713: return riverinfo; bjoern@3713: } bjoern@3713: catch (ConnectionException ce) { bjoern@3713: logger.error(ce, ce); bjoern@3713: } bjoern@3713: bjoern@3713: logger.warn("No gauge found"); bjoern@3713: throw new ServerException(ERROR_NO_RIVERINFO_FOUND); bjoern@3713: } bjoern@3713: bjoern@3713: /** bjoern@3713: * Avoids NullPointerException when parsing double value bjoern@3713: */ bjoern@3713: private Double parseDouble(String value) { bjoern@3713: if (value == null || value.isEmpty()) { bjoern@3713: return null; bjoern@3713: } bjoern@3713: try { bjoern@3713: return Double.valueOf(value); bjoern@3713: } bjoern@3713: catch(NumberFormatException e) { bjoern@3713: logger.error(e, e); bjoern@3713: return null; bjoern@3713: } bjoern@3713: } bjoern@3843: bjoern@3843: private Long parseLong(String value) { bjoern@3843: if (value == null || value.isEmpty()) { bjoern@3843: return null; bjoern@3843: } bjoern@3843: try { bjoern@3843: return Long.valueOf(value); bjoern@3843: } bjoern@3843: catch(NumberFormatException e) { bjoern@3843: logger.error(e, e); bjoern@3843: return null; bjoern@3843: } bjoern@3843: } bjoern@3713: }