felix@1426: package de.intevation.flys.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: 
felix@1426: import de.intevation.artifacts.common.ArtifactNamespaceContext;
felix@1426: import de.intevation.artifacts.common.utils.XMLUtils;
felix@1426: 
felix@1426: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
felix@1426: import de.intevation.artifacts.httpclient.http.HttpClient;
felix@1426: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
felix@1426: 
felix@1426: import de.intevation.flys.client.shared.exceptions.ServerException;
felix@1426: import de.intevation.flys.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<Integer,Double[]> getCrossSectionKMs(
felix@1426:         String               locale,
felix@1426:         Map<Integer, Double> 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<Integer, Double> 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:            //<cross-sections><cross-section id="1"><line km="19.5" line-id="189"/>...
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<Integer, Double[]> result = new HashMap<Integer, Double[]>();
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<Double> kms = new ArrayList<Double>();
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 :