view flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java @ 2917:be99bf1aa59b

Create simplified column filter. flys-client/trunk@4739 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jun 2012 11:55:37 +0000
parents bc06a671ef60
children e70ff0a600a3
line wrap: on
line source
package de.intevation.flys.client.server;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.apache.log4j.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.intevation.artifacts.common.ArtifactNamespaceContext;
import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
import de.intevation.artifacts.httpclient.http.HttpClient;
import de.intevation.artifacts.httpclient.http.HttpClientImpl;

import de.intevation.flys.client.shared.exceptions.ServerException;
import de.intevation.flys.client.client.services.WQInfoService;
import de.intevation.flys.client.shared.model.WQInfoObject;
import de.intevation.flys.client.shared.model.WQInfoObjectImpl;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class WQInfoServiceImpl
extends      RemoteServiceServlet
implements   WQInfoService
{
    private static final Logger logger =
        Logger.getLogger(WQInfoServiceImpl.class);


    public static final String ERROR_NO_WQINFO_FOUND =
        "error_no_wqinfo_found";

    public static final String XPATH_WQS = "art:service/art:mainvalues/art:mainvalue";


    public WQInfoObject[] getWQInfo(
        String locale,
        String river,
        double from,
        double to)
    throws ServerException
    {
        logger.info("WQInfoServiceImpl.getWQInfo");

        String url = getServletContext().getInitParameter("server-url");

        Document doc = XMLUtils.newDocument();

        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element mainvalues = ec.create("mainvalues");
        Element riverEl = ec.create("river");
        Element startEl = ec.create("start");
        Element endEl   = ec.create("end");

        riverEl.setTextContent(river);
        startEl.setTextContent(Double.valueOf(from).toString());
        endEl.setTextContent(Double.valueOf(to).toString());

        mainvalues.appendChild(riverEl);
        mainvalues.appendChild(startEl);
        mainvalues.appendChild(endEl);

        doc.appendChild(mainvalues);

        HttpClient client = new HttpClientImpl(url, locale);

        try {
            Document result = client.callService(url, "mainvalues", doc);

            logger.debug("Extract wq info objects now.");
            WQInfoObject[] objects = extractWQInfoObjects(result);

            if (objects != null && objects.length > 0) {
                return objects;
            }
        }
        catch (ConnectionException ce) {
            logger.error(ce, ce);
        }

        throw new ServerException(ERROR_NO_WQINFO_FOUND);
    }


    /**
     * Extracts all wq info objects from <i>result</i> document.
     *
     * @param result The document retrieved by the server.
     *
     * @return a list of WQInfoObjects.
     */
    protected WQInfoObject[] extractWQInfoObjects(Document result)
    throws    ServerException
    {
        NodeList list = (NodeList) XMLUtils.xpath(
            result,
            XPATH_WQS,
            XPathConstants.NODESET,
            ArtifactNamespaceContext.INSTANCE);

        if (list == null || list.getLength() == 0) {
            logger.warn("No wq info found.");

            throw new ServerException(ERROR_NO_WQINFO_FOUND);
        }

        int num = list.getLength();
        logger.debug("Response contains " + num + " objects.");

        List<WQInfoObject> objects =
            new ArrayList<WQInfoObject>(num);

        for (int i = 0; i < num; i++) {
            WQInfoObject obj = buildWQInfoObject(list.item(i));

            if (obj != null) {
                objects.add(obj);
            }
        }

        logger.debug("Retrieved " + objects.size() + " wq values");

        return (WQInfoObject[])
            objects.toArray(new WQInfoObject[num]);
    }


    /**
     * Extracts information for a single wq info object and intializes an
     * WQInfoObject with them.
     *
     * @param node The node that contains the information.
     *
     * @return a valid WQInfoObject.
     */
    protected WQInfoObject buildWQInfoObject(Node node) {
        String name = XMLUtils.xpathString(
            node, "@name", ArtifactNamespaceContext.INSTANCE);

        String type = XMLUtils.xpathString(
            node, "@type", ArtifactNamespaceContext.INSTANCE);

        String value = XMLUtils.xpathString(
            node, "@value", ArtifactNamespaceContext.INSTANCE);


        if (name != null && type != null) {
            try {
                return new WQInfoObjectImpl(
                    name,
                    type,
                    new Double(value));
            }
            catch (NumberFormatException nfe) {
                logger.warn(nfe.getLocalizedMessage());
            }
        }

        logger.warn("Invalid wq info object found.");

        return null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org