raimund@245: package de.intevation.flys.client.server;
raimund@245: 
raimund@245: import java.util.ArrayList;
raimund@245: import java.util.List;
raimund@245: 
raimund@245: import javax.xml.xpath.XPathConstants;
raimund@245: 
raimund@245: import org.w3c.dom.Document;
raimund@245: import org.w3c.dom.Element;
raimund@245: import org.w3c.dom.Node;
raimund@245: import org.w3c.dom.NodeList;
raimund@245: 
ingo@1367: import org.apache.log4j.Logger;
ingo@1367: 
raimund@245: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
raimund@245: 
raimund@245: import de.intevation.artifacts.common.ArtifactNamespaceContext;
raimund@245: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@245: 
raimund@245: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
raimund@245: import de.intevation.artifacts.httpclient.http.HttpClient;
raimund@245: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
raimund@245: 
raimund@245: import de.intevation.flys.client.shared.exceptions.ServerException;
raimund@245: import de.intevation.flys.client.client.services.WQInfoService;
raimund@245: import de.intevation.flys.client.shared.model.WQInfoObject;
raimund@245: import de.intevation.flys.client.shared.model.WQInfoObjectImpl;
raimund@245: 
raimund@245: 
raimund@245: /**
raimund@245:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
raimund@245:  */
raimund@245: public class WQInfoServiceImpl
raimund@245: extends      RemoteServiceServlet
raimund@245: implements   WQInfoService
raimund@245: {
ingo@1367:     private static final Logger logger =
ingo@1367:         Logger.getLogger(WQInfoServiceImpl.class);
ingo@1367: 
raimund@245:     public static final String ERROR_NO_WQINFO_FOUND =
raimund@245:         "error_no_wqinfo_found";
raimund@245: 
raimund@245:     public static final String XPATH_WQS = "art:service/art:mainvalues/art:mainvalue";
raimund@245: 
raimund@245: 
raimund@245:     public WQInfoObject[] getWQInfo(
raimund@245:         String locale,
raimund@245:         String river,
raimund@245:         double from,
raimund@245:         double to)
raimund@245:     throws ServerException
raimund@245:     {
ingo@1367:         logger.info("WQInfoServiceImpl.getWQInfo");
raimund@245: 
raimund@1425:         String url = getServletContext().getInitParameter("server-url");
raimund@1425: 
raimund@245:         Document doc = XMLUtils.newDocument();
raimund@245: 
raimund@245:         XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
raimund@245:             doc,
raimund@245:             ArtifactNamespaceContext.NAMESPACE_URI,
raimund@245:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
raimund@245: 
raimund@245:         Element mainvalues = ec.create("mainvalues");
raimund@245:         Element riverEl = ec.create("river");
raimund@245:         Element startEl = ec.create("start");
raimund@245:         Element endEl   = ec.create("end");
raimund@245: 
raimund@245:         riverEl.setTextContent(river);
raimund@245:         startEl.setTextContent(Double.valueOf(from).toString());
raimund@245:         endEl.setTextContent(Double.valueOf(to).toString());
raimund@245: 
raimund@245:         mainvalues.appendChild(riverEl);
raimund@245:         mainvalues.appendChild(startEl);
raimund@245:         mainvalues.appendChild(endEl);
raimund@245: 
raimund@245:         doc.appendChild(mainvalues);
raimund@245: 
raimund@245:         HttpClient client = new HttpClientImpl(url, locale);
raimund@245: 
raimund@245:         try {
raimund@245:             Document result = client.callService(url, "mainvalues", doc);
raimund@245: 
ingo@1367:             logger.debug("Extract wq info objects now.");
raimund@245:             WQInfoObject[] objects = extractWQInfoObjects(result);
raimund@245: 
raimund@245:             if (objects != null && objects.length > 0) {
raimund@245:                 return objects;
raimund@245:             }
raimund@245:         }
raimund@245:         catch (ConnectionException ce) {
ingo@1367:             logger.error(ce, ce);
raimund@245:         }
raimund@245: 
raimund@245:         throw new ServerException(ERROR_NO_WQINFO_FOUND);
raimund@245:     }
raimund@245: 
raimund@245: 
raimund@245:     /**
raimund@245:      * Extracts all wq info objects from <i>result</i> document.
raimund@245:      *
raimund@245:      * @param result The document retrieved by the server.
raimund@245:      *
raimund@245:      * @return a list of WQInfoObjects.
raimund@245:      */
raimund@245:     protected WQInfoObject[] extractWQInfoObjects(Document result)
raimund@245:     throws    ServerException
raimund@245:     {
raimund@245:         NodeList list = (NodeList) XMLUtils.xpath(
raimund@245:             result,
raimund@245:             XPATH_WQS,
raimund@245:             XPathConstants.NODESET,
raimund@245:             ArtifactNamespaceContext.INSTANCE);
raimund@245: 
raimund@245:         if (list == null || list.getLength() == 0) {
ingo@1367:             logger.warn("No wq info found.");
raimund@245: 
raimund@245:             throw new ServerException(ERROR_NO_WQINFO_FOUND);
raimund@245:         }
raimund@245: 
raimund@245:         int num = list.getLength();
ingo@1367:         logger.debug("Response contains " + num + " objects.");
raimund@245: 
raimund@245:         List<WQInfoObject> objects =
raimund@245:             new ArrayList<WQInfoObject>(num);
raimund@245: 
raimund@245:         for (int i = 0; i < num; i++) {
raimund@245:             WQInfoObject obj = buildWQInfoObject(list.item(i));
raimund@245: 
raimund@245:             if (obj != null) {
raimund@245:                 objects.add(obj);
raimund@245:             }
raimund@245:         }
raimund@245: 
ingo@1367:         logger.debug("Retrieved " + objects.size() + " wq values");
raimund@245: 
raimund@245:         return (WQInfoObject[])
raimund@245:             objects.toArray(new WQInfoObject[num]);
raimund@245:     }
raimund@245: 
raimund@245: 
raimund@245:     /**
raimund@245:      * Extracts information for a single wq info object and intializes an
raimund@245:      * WQInfoObject with them.
raimund@245:      *
raimund@245:      * @param node The node that contains the information.
raimund@245:      *
raimund@245:      * @return a valid WQInfoObject.
raimund@245:      */
raimund@245:     protected WQInfoObject buildWQInfoObject(Node node) {
raimund@245:         String name = XMLUtils.xpathString(
raimund@245:             node, "@name", ArtifactNamespaceContext.INSTANCE);
raimund@245: 
raimund@245:         String type = XMLUtils.xpathString(
raimund@245:             node, "@type", ArtifactNamespaceContext.INSTANCE);
raimund@245: 
raimund@245:         String value = XMLUtils.xpathString(
raimund@245:             node, "@value", ArtifactNamespaceContext.INSTANCE);
raimund@245: 
raimund@245: 
raimund@245:         if (name != null && type != null) {
raimund@245:             try {
raimund@245:                 return new WQInfoObjectImpl(
raimund@245:                     name,
raimund@245:                     type,
raimund@245:                     new Double(value));
raimund@245:             }
raimund@245:             catch (NumberFormatException nfe) {
ingo@1367:                 logger.warn(nfe.getLocalizedMessage());
raimund@245:             }
raimund@245:         }
raimund@245: 
ingo@1367:         logger.warn("Invalid wq info object found.");
raimund@245: 
raimund@245:         return null;
raimund@245:     }
raimund@245: }
raimund@245: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :