ingo@2466: package de.intevation.flys.client.server; ingo@2466: ingo@2466: import java.util.ArrayList; ingo@2466: import java.util.List; ingo@2466: ingo@2466: import javax.xml.xpath.XPathConstants; ingo@2466: ingo@2466: import org.w3c.dom.Document; ingo@2466: import org.w3c.dom.Element; ingo@2466: import org.w3c.dom.NodeList; ingo@2466: ingo@2466: import org.apache.log4j.Logger; ingo@2466: ingo@2466: import com.google.gwt.user.server.rpc.RemoteServiceServlet; ingo@2466: ingo@2466: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@2466: import de.intevation.artifacts.common.utils.XMLUtils; ingo@2466: ingo@2466: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; ingo@2466: import de.intevation.artifacts.httpclient.http.HttpClient; ingo@2466: import de.intevation.artifacts.httpclient.http.HttpClientImpl; ingo@2466: ingo@2466: import de.intevation.flys.client.shared.exceptions.ServerException; ingo@2466: import de.intevation.flys.client.client.services.GaugeInfoService; ingo@2466: import de.intevation.flys.client.shared.model.Gauge; ingo@2466: import de.intevation.flys.client.shared.model.GaugeImpl; ingo@2466: ingo@2466: ingo@2466: /** ingo@2466: * @author Ingo Weinzierl ingo@2466: */ ingo@2466: public class GaugeInfoServiceImpl ingo@2466: extends RemoteServiceServlet ingo@2466: implements GaugeInfoService ingo@2466: { ingo@2466: private static final Logger logger = ingo@2466: Logger.getLogger(GaugeInfoServiceImpl.class); ingo@2466: ingo@2466: ingo@2466: public static final String ERROR_NO_GAUGES_FOUND = ingo@2466: "error_no_gaugeinfo_found"; ingo@2466: ingo@2466: public static final String XPATH_GAUGES = "art:service/art:gauge"; ingo@2466: ingo@2466: ingo@2466: public List getGaugeInfo(String rivername, String refnumber) ingo@2466: throws ServerException ingo@2466: { ingo@2466: logger.info("GaugeInfoServiceImpl.getGaugeInfo"); ingo@2466: ingo@2466: String url = getServletContext().getInitParameter("server-url"); ingo@2466: ingo@2466: Document doc = XMLUtils.newDocument(); ingo@2466: ingo@2466: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( ingo@2466: doc, ingo@2466: ArtifactNamespaceContext.NAMESPACE_URI, ingo@2466: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@2466: ingo@2466: Element river = ec.create("river"); ingo@2466: ec.addAttr(river, "name", rivername); ingo@2466: ingo@2466: if (refnumber != null && refnumber.length() > 0) { ingo@2466: Element filter = ec.create("filter"); ingo@2466: Element gauge = ec.create("gauge"); ingo@2466: gauge.setTextContent(refnumber); ingo@2466: ingo@2466: filter.appendChild(gauge); ingo@2466: river.appendChild(filter); ingo@2466: } ingo@2466: ingo@2466: doc.appendChild(river); ingo@2466: ingo@2466: HttpClient client = new HttpClientImpl(url); ingo@2466: ingo@2466: try { ingo@2466: Document result = client.callService(url, "gaugeinfo", doc); ingo@2466: ingo@2466: logger.debug("Extract gauge info now."); ingo@2466: List gauges = extractGauges(result); ingo@2466: ingo@2466: if (gauges != null && gauges.size() > 0) { ingo@2466: return gauges; ingo@2466: } ingo@2466: } ingo@2466: catch (ConnectionException ce) { ingo@2466: logger.error(ce, ce); ingo@2466: } ingo@2466: ingo@2466: throw new ServerException(ERROR_NO_GAUGES_FOUND); ingo@2466: } ingo@2466: ingo@2466: ingo@2466: /** ingo@2466: * Extracts all wq info objects from result document. ingo@2466: * ingo@2466: * @param result The document retrieved by the server. ingo@2466: * ingo@2466: * @return a list of WQInfoObjects. ingo@2466: */ ingo@2466: protected List extractGauges(Document result) ingo@2466: throws ServerException ingo@2466: { ingo@2466: List gauges = new ArrayList(); ingo@2466: ingo@2466: NodeList list = (NodeList) XMLUtils.xpath( ingo@2466: result, ingo@2466: XPATH_GAUGES, ingo@2466: XPathConstants.NODESET, ingo@2466: ArtifactNamespaceContext.INSTANCE); ingo@2466: ingo@2466: if (list == null || list.getLength() == 0) { ingo@2466: logger.warn("No gauges found."); ingo@2466: ingo@2466: throw new ServerException(ERROR_NO_GAUGES_FOUND); ingo@2466: } ingo@2466: ingo@2466: int num = list.getLength(); ingo@2466: logger.debug("Response contains " + num + " objects."); ingo@2466: ingo@2466: for (int i = 0; i < num; i++) { ingo@2466: Gauge obj = buildGauge((Element) list.item(i)); ingo@2466: ingo@2466: if (obj != null) { ingo@2466: gauges.add(obj); ingo@2466: } ingo@2466: } ingo@2466: ingo@2466: logger.debug("Retrieved " + gauges.size() + " gauges."); ingo@2466: ingo@2466: return gauges; ingo@2466: } ingo@2466: ingo@2466: ingo@2466: protected Gauge buildGauge(Element ele) { ingo@2466: String name = ele.getAttribute("name"); ingo@2466: String lowerStr = ele.getAttribute("lower"); ingo@2466: String upperStr = ele.getAttribute("upper"); ingo@2466: ingo@2466: if (lowerStr != null && upperStr != null) { ingo@2466: try { ingo@2466: return new GaugeImpl( ingo@2466: name, ingo@2466: Double.valueOf(lowerStr), ingo@2466: Double.valueOf(upperStr)); ingo@2466: } ingo@2466: catch (NumberFormatException nfe) { ingo@2466: logger.warn("Error while Gauge creation: " + nfe.getMessage()); ingo@2466: } ingo@2466: } ingo@2466: ingo@2466: return null; ingo@2466: } ingo@2466: } ingo@2466: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :