view flys-client/src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java @ 2508:4aa70825bde1

map print: generate valid url to access service. flys-client/trunk@4363 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 May 2012 11:18:24 +0000
parents 87ac5c532523
children 256b458cc7ea
line wrap: on
line source
package de.intevation.flys.client.server;

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

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

import javax.xml.xpath.XPathConstants;

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

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

import de.intevation.artifacts.common.utils.XSLTransformer;

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.client.services.FixingsOverviewService;

import de.intevation.flys.client.shared.exceptions.ServerException;

import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
import de.intevation.flys.client.shared.model.FixingsOverviewInfo.FixEvent;
import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector;

import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;

public class FixingsOverviewServiceImpl
extends      RemoteServiceServlet
implements   FixingsOverviewService
{
    private static final Logger log =
        Logger.getLogger(FixingsOverviewServiceImpl.class);

    public static final String SERVICE_NAME = "fixings-overview";

    public static final String XSL_TRANSFORM =
        "/WEB-INF/stylesheets/fixoverview2html.xsl";

    protected static final String XPATH_RID = "/fixings/river/@rid";
    protected static final String XPATH_RIVER = "/fixings/river/@name";
    protected static final String XPATH_RFROM = "/fixings/river/@from";
    protected static final String XPATH_RTO = "/fixings/river/@to";

    protected static final String XPATH_EVENT = "/fixings/events/event";
    protected static final String XPATH_SECTORS = "sector";

    protected static final String XPATH_CID = "@cid";
    protected static final String XPATH_DATE = "@date";
    protected static final String XPATH_NAME = "@name";
    protected static final String XPATH_CLS = "@class";
    protected static final String XPATH_FROM = "@from";
    protected static final String XPATH_TO = "@to";

    @Override
    public FixingsOverviewInfo generateOverview(
        String  locale,
        String  uuid,
        String  filter,
        boolean  checkboxes,
        String   callback
    )
    throws ServerException
    {
        log.info("FixingsOverviewServiceImpl.doGet");

        if (filter == null || filter.length() == 0) {
            log.warn("Missing 'filter' parameter.");
            return null;
        }

        Document filterDoc = XMLUtils.jsonToXML(filter);

        if (filterDoc == null) {
            log.warn("Creating filter document failed.");
            return null;
        }

/*        filterDoc = XMLUtils.newDocument();
        ElementCreator ec = new ElementCreator(filterDoc, null, null);
        Element f = ec.create("fixings");
        Element r = ec.create("river");
        r.setAttribute("name", "Saar");
        f.appendChild(r);
        filterDoc.appendChild(f);
*/
        try {
            String url = getServletContext().getInitParameter("server-url");
            HttpClient client = new HttpClientImpl(url, locale);
            Document resultDoc =
                client.callService(url, SERVICE_NAME, filterDoc);
            FixingsOverviewInfo i = getInfo(resultDoc, uuid, checkboxes, callback);
            return i;
        }
        catch (ConnectionException ce) {
            log.error(ce);
        }
        return null;
    }


    protected FixingsOverviewInfo getInfo(
        Document doc,
        String uuid,
        boolean checkboxes,
        String callback
    ) {
        InputStream transform =
            getServletContext().getResourceAsStream(XSL_TRANSFORM);
        XSLTransformer xformer = new XSLTransformer();
        xformer.addParameter("project-uuid", uuid);
        xformer.addParameter(
            "render-checkboxes", checkboxes ? Boolean.TRUE : Boolean.FALSE);
        xformer.addParameter("callback", callback);
        String result = xformer.transform(doc, transform);

        try { transform.close(); }
        catch (IOException ioe) {}

        int rid = -1;
        double from = -1;
        double to = -1;
        String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null);
        String river = XMLUtils.xpathString(doc, XPATH_RIVER, null);
        String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null);
        String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null);
        try {
            rid = Integer.valueOf(rid_str).intValue();
            from = Double.valueOf(from_str).doubleValue();
            to = Double.valueOf(to_str).doubleValue();
        }
        catch(NumberFormatException nfe) {
            log.warn(nfe, nfe);
        }

        List<FixEvent> fixEvents = getFixEvents(doc);
        return new FixingsOverviewInfo(
                rid,
                river,
                from,
                to,
                fixEvents,
                result);
    }


    protected List<FixEvent> getFixEvents(Document doc) {
        NodeList events = (NodeList) XMLUtils.xpath(
            doc,
            XPATH_EVENT,
            XPathConstants.NODESET,
            null);

        if (events == null || events.getLength() == 0) {
            log.warn("No events in Overview!");
            return null;
        }

        List<FixEvent> list =
            new ArrayList<FixEvent>();
        for (int i = 0; i < events.getLength(); i++) {
            Node n = events.item(i);
            List<Sector> sectors = getSectors(n);
            String cid = XMLUtils.xpathString(n, XPATH_CID, null);
            log.debug("'" + cid + "'");
            String date = XMLUtils.xpathString(n, XPATH_DATE, null);
            String name = XMLUtils.xpathString(n, XPATH_NAME, null);
            list.add(new FixEvent(
                cid,
                date,
                name,
                sectors));
        }
        return list;
    }

    protected List<Sector> getSectors(Node event) {
        NodeList sectors = (NodeList) XMLUtils.xpath(
            event,
            XPATH_SECTORS,
            XPathConstants.NODESET,
            null);
        if (sectors == null || sectors.getLength() == 0) {
            log.warn("No Sectors in Event!");
            return null;
        }

        List<Sector> list =
            new ArrayList<Sector>();
        for (int i = 0; i < sectors.getLength(); i++) {
            Node n = sectors.item(i);
            int cls = -1;
            double from = -1;
            double to = -1;
            String cls_str = XMLUtils.xpathString(n, XPATH_CLS, null);
            String from_str = XMLUtils.xpathString(n, XPATH_FROM, null);
            String to_str = XMLUtils.xpathString(n, XPATH_TO, null);
            try {
                cls = Integer.valueOf(cls_str).intValue();
                from = Double.valueOf(from_str).doubleValue();
                to = Double.valueOf(to_str).doubleValue();
            }
            catch(NumberFormatException nfe) {
                log.warn(nfe, nfe);
            }
            list.add(new Sector(
                cls,
                from,
                to));
        }
        return list;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org