teichmann@5835: package org.dive4elements.river.client.server; felix@1426: felix@1426: import java.util.ArrayList; felix@1426: import java.util.HashMap; felix@1426: import java.util.Map; felix@1426: felix@1426: import javax.xml.xpath.XPathConstants; felix@1426: felix@1426: import org.w3c.dom.Document; felix@1426: import org.w3c.dom.Element; felix@1426: import org.w3c.dom.NodeList; felix@1426: felix@1426: import org.apache.log4j.Logger; felix@1426: felix@1426: import com.google.gwt.user.server.rpc.RemoteServiceServlet; felix@1426: teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; felix@1426: teichmann@5835: import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; felix@1426: teichmann@5835: import org.dive4elements.river.client.shared.exceptions.ServerException; teichmann@5835: import org.dive4elements.river.client.client.services.CrossSectionKMService; felix@1426: felix@1426: /** felix@1426: * Interact with not documented service. felix@1426: */ felix@1426: public class CrossSectionKMServiceImpl felix@1426: extends RemoteServiceServlet felix@1426: implements CrossSectionKMService felix@1426: { felix@1426: private static final Logger logger = felix@1426: Logger.getLogger(CrossSectionKMServiceImpl.class); felix@1426: felix@1426: /** XPath that points to the found cross section measurements. */ felix@1426: public static final String XPATH_CROSS_SECTIONS felix@1426: = "/cross-sections/cross-section"; felix@1426: felix@1426: /** The error message key that is thrown if an error occured while getting felix@1426: * new data. */ felix@1426: public static final String ERROR_GET_CROSS_SECTION felix@1426: = "error_get_cross_section"; felix@1426: felix@1426: felix@1426: /** felix@1426: * Fetches positions (kms) at which measurements for given cross-sections felix@1426: * exists. felix@1426: * felix@1426: * @param data Map of Integer (cross-section-id) to km. felix@1426: * felix@1426: */ felix@1426: public Map getCrossSectionKMs( felix@1426: String locale, felix@1426: Map data, felix@1426: int nNeighbours) felix@1426: throws ServerException felix@1426: { felix@1426: logger.info("CrossSectionKMService.getCrossSectionKMs"); felix@1426: felix@1426: String url = getServletContext().getInitParameter("server-url"); felix@1426: felix@1426: Document doc = XMLUtils.newDocument(); felix@1426: felix@1426: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( felix@1426: doc, felix@1426: ArtifactNamespaceContext.NAMESPACE_URI, felix@1426: ArtifactNamespaceContext.NAMESPACE_PREFIX); felix@1426: Element crossSection = ec.create("cross-sections"); felix@1426: felix@1426: doc.appendChild(crossSection); felix@1426: felix@1426: for(Map.Entry oneCrossSection : data.entrySet()) { felix@1426: Element cs = ec.create("cross-section"); felix@1426: cs.setAttribute("id", oneCrossSection.getKey().toString()); felix@1426: cs.setAttribute("km", oneCrossSection.getValue().toString()); felix@1426: cs.setAttribute("n", Integer.valueOf(nNeighbours).toString()); felix@1426: crossSection.appendChild(cs); felix@1426: } felix@1426: felix@1426: HttpClient client = new HttpClientImpl(url, locale); felix@1426: logger.debug("Created httpclient"); felix@1426: felix@1426: try { felix@1426: // Document should contain: felix@1426: // crosse-sections: felix@1426: // attribute(id), attribute(km) attribute(n) felix@1426: Document response = client.callService(url, "cross-section-km", doc); felix@1426: //... felix@1426: felix@1426: NodeList nodeList = (NodeList) XMLUtils.xpath(response, felix@1426: XPATH_CROSS_SECTIONS, felix@1426: XPathConstants.NODESET); felix@1426: felix@1426: int num = nodeList.getLength(); felix@1426: felix@1426: Map result = new HashMap(); felix@1426: felix@1426: try{ felix@1426: for (int i = 0; i < num; i++) { felix@1426: Element csElement = (Element) nodeList.item(i); felix@1426: felix@1426: int idx = Integer.parseInt(csElement.getAttribute("id")); felix@1426: ArrayList kms = new ArrayList(); felix@1426: felix@1426: NodeList lineNodes = csElement.getElementsByTagName("line"); felix@1426: int numLines = lineNodes.getLength(); felix@1426: for (int k = 0; k < numLines; k++) { felix@1434: Element line = (Element) lineNodes.item(k); felix@1426: double d = Double.parseDouble(line.getAttribute("km")); felix@1426: kms.add(d); felix@1426: } felix@1426: felix@1426: Double[] doubles = new Double[kms.size()]; felix@1426: kms.toArray(doubles); felix@1426: result.put(Integer.valueOf(idx), doubles); felix@1426: } felix@1426: } felix@1426: catch(NumberFormatException nfe) { felix@1426: logger.error("Response was not parsable"); felix@1426: } felix@1426: felix@1426: return result; felix@1426: } felix@1426: catch (ConnectionException ce) { felix@1426: logger.error("ConnectionExsp", ce); felix@1426: } felix@1426: felix@1426: logger.warn("CrossSectionKMService.getCrossSectionKMS() - FAILED"); felix@1426: throw new ServerException(ERROR_GET_CROSS_SECTION); felix@1426: } felix@1426: } felix@1426: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :