view flys-client/src/main/java/de/intevation/flys/client/server/CollectionItemAttributeServiceImpl.java @ 1302:17e7d5e437fb

Bumped OpenLayers to version 2.11 and GWT-OpenLayers to version 0.6 flys-client/trunk@2930 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 10 Oct 2011 15:29:39 +0000
parents d9cb362b8b34
children c4c957a9c092
line wrap: on
line source
package de.intevation.flys.client.server;

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

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

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

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

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

import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.CollectionItemAttribute;
import de.intevation.flys.client.shared.model.Style;
import de.intevation.flys.client.shared.model.StyleSetting;


/**
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class CollectionItemAttributeServiceImpl
extends      RemoteServiceServlet
implements   CollectionItemAttributeService
{
    public static final String XPATH_RESULT = "/art:result/text()";

    public static final String OPERATION_FAILURE = "FAILED";


    public CollectionItemAttribute getCollectionItemAttribute(
        Collection collection,
        String artifact,
        String url,
        String locale)
    throws ServerException
    {
        System.out.println(
            "CollectionItemAttributeServiceImpl.getCollectionItemAttribute");

        Document requestDoc = XMLUtils.newDocument();

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

        Element action = ec.create("action");

        Element type = ec.create("type");
        ec.addAttr(type, "name", "getitemattribute", false);

        Element art = ec.create("artifact");
        ec.addAttr(art, "uuid", artifact, false);

        type.appendChild(art);
        action.appendChild(type);
        requestDoc.appendChild (action);

        try {
            HttpClient client = new HttpClientImpl(url, locale);
            Document res = (Document) client.doCollectionAction(
                requestDoc,
                collection.identifier(),
                new DocumentResponseHandler());
            return readXML (res, artifact);
        }
        catch (ConnectionException ce) {
            System.err.println(ce.getLocalizedMessage());
        }
        throw new ServerException("");
    }


    public void setCollectionItemAttribute(
        Collection collection,
        String artifact,
        String url,
        String locale,
        CollectionItemAttribute attributes)
    throws ServerException
    {
        System.out.println(
            "CollectionItemAttributeServiceImpl.setCollectionItemAttribute");

        Document doc = writeXML(attributes, artifact);

        try {
            HttpClient client = new HttpClientImpl(url, locale);
            Document res = (Document) client.doCollectionAction(
                doc,
                collection.identifier(),
                new DocumentResponseHandler());

            return;
        }
        catch (ConnectionException ce) {
            System.err.println(ce.getLocalizedMessage());
        }
        throw new ServerException("");
    }

    protected CollectionItemAttribute readXML (Document doc, String artifact) {
        CollectionItemAttribute cia = new CollectionItemAttribute();
        cia.setArtifact(artifact);

        Element root = doc.getDocumentElement();
        NodeList themes = root.getElementsByTagName("art:themes");

        if (themes.getLength() != 1) {
            return null;
        }

        Element e = (Element) themes.item(0);
        NodeList items = e.getElementsByTagName("theme");

        for (int i = 0; i < items.getLength(); i++) {
            cia.appendStyle(getStyle ((Element) items.item(i)));
        }

        return cia;
    }


    protected Document writeXML (
        CollectionItemAttribute attributes,
        String artifact)
    {
        Document styles = XMLUtils.newDocument();

        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
                styles,
                ArtifactNamespaceContext.NAMESPACE_URI,
                ArtifactNamespaceContext.NAMESPACE_PREFIX);
        Element action = ec.create("action");
        Element type = ec.create("type");
        type.setAttribute("name", "setitemattribute");
        Element art = ec.create("artifact");
        art.setAttribute("uuid", artifact);
        Element attr = ec.create("attribute");
        Element themes = ec.create("themes");
        action.appendChild(type);
        type.appendChild(art);
        art.appendChild(attr);
        attr.appendChild(themes);

        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
                styles,
                "",
                "");

        for (int i = 0; i < attributes.getNumStyles(); i++) {
            Style s = attributes.getStyle(i);
            Element theme = creator.create("theme");
            theme.setAttribute("name", s.getName());
            theme.setAttribute("facet", s.getFacet());
            theme.setAttribute("index", String.valueOf(i));
            for (int j = 0; j < s.getNumSettings(); j++) {
                StyleSetting set = s.getSetting(j);
                Element field = creator.create("field");
                field.setAttribute("name", set.getName());
                field.setAttribute("display", set.getDisplayName());
                field.setAttribute("default", set.getDefaultValue());
                field.setAttribute("hints", set.getHints());
                field.setAttribute("type", set.getType());
                theme.appendChild(field);
            }
            themes.appendChild(theme);
        }
        styles.appendChild(action);
        return styles;
    }


    protected Style getStyle (Element element) {

        if (!element.getTagName().equals("theme")) {
            return null;
        }

        NodeList list = element.getElementsByTagName("field");
        Style style = new Style();

        style.setName (element.getAttribute("name"));
        style.setFacet (element.getAttribute("facet"));
        for(int i = 0; i < list.getLength(); i++) {
            Element e = (Element) list.item(i);
            StyleSetting set = new StyleSetting (
                e.getAttribute("name"),
                e.getAttribute("default"),
                e.getAttribute("display"),
                e.getAttribute("hints"),
                e.getAttribute("type"));
            style.appendStyleSetting(set);
        }
        return style;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org