view flys-client/src/main/java/de/intevation/flys/client/server/CollectionHelper.java @ 884:ace7e9cfbb7f

Fixed some bugs that avoided datacage usage. flys-client/trunk@2718 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 13 Sep 2011 11:31:53 +0000
parents 0fe456332785
children 478a571f1f94
line wrap: on
line source
package de.intevation.flys.client.server;

import java.util.List;
import java.util.Map;
import java.util.Set;

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

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

import de.intevation.flys.client.shared.model.AttributedTheme;
import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.OutputMode;
import de.intevation.flys.client.shared.model.Recommendation;
import de.intevation.flys.client.shared.model.Theme;
import de.intevation.flys.client.shared.model.ThemeList;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class CollectionHelper {

    public static Document createAttribute(Collection collection) {
        System.out.println("CollectionHelper.createAttribute");

        Document doc = XMLUtils.newDocument();

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

        Element attr = cr.create("attribute");

        doc.appendChild(attr);

        Map<String, OutputMode> tmpOuts = collection.getOutputModes();

        Element outs = createOutputElements(cr, collection, tmpOuts);
        Element recs = createRecommendationsElements(cr, collection);

        if (outs != null) {
            attr.appendChild(outs);
        }

        if (recs != null) {
            attr.appendChild(recs);
        }

        return doc;
    }


    /**
     * Creates a whole block with art:output nodes.
     *
     * @param cr The ElementCreator used to create new elements.
     * @param c  The collection.
     * @param modes The OutputModes that should be included.
     *
     * @return an element with output modes.
     */
    protected static Element createOutputElements(
        ElementCreator          cr,
        Collection              c,
        Map<String, OutputMode> mmodes)
    {
        System.out.println("CollectionHelper.createOutputElements");

        java.util.Collection<OutputMode> modes = mmodes != null
            ? mmodes.values()
            : null;

        if (modes == null || modes.size() == 0) {
            System.err.println("Collection has no modes: " + c.identifier());
            return null;
        }

        Element outs = cr.create("outputs");

        for (OutputMode mode: modes) {
            Element out = createOutputElement(cr, c, mode);

            if (out != null) {
                outs.appendChild(out);
            }
        }

        return outs;
    }


    /**
     * Create a node art:output that further consist of art:theme nodes.
     *
     * @param cr The ElementCreator used to create new elements.
     * @param c  The collection.
     * @param mode The OutputMode.
     *
     * @return an element that represents an output mode with its themes.
     */
    protected static Element createOutputElement(
        ElementCreator cr,
        Collection     collection,
        OutputMode     mode)
    {
        System.out.println("CollectionHelper.createOutputElement");

        Element out = cr.create("output");
        cr.addAttr(out, "name", mode.getName(), false);

        ThemeList themeList = collection.getThemeList(mode.getName());
        List<Theme> themes  = themeList != null ? themeList.getThemes() : null;

        if (themes == null || themes.size() == 0) {
            System.err.println("No themes for output mode: " + mode.getName());
            return null;
        }

        for (Theme theme: themes) {
            Element t = createThemeElement(cr, collection, theme);

            if (t != null) {
                out.appendChild(t);
            }
        }

        return out;
    }


    /**
     * Creates a theme node art:theme that represents a curve in a chart or map.
     *
     * @param cr The ElementCreator used to create new elements.
     * @param collection The collection.
     * @param theme The theme whose attributes should be written to an element.
     *
     * @return an element that contains the informtion of the given theme.
     */
    protected static Element createThemeElement(
        ElementCreator cr,
        Collection     collection,
        Theme          theme)
    {
        if (theme == null) {
            return null;
        }

        Element t = cr.create("facet");

        if (theme instanceof AttributedTheme) {
            AttributedTheme at = (AttributedTheme) theme;
            Set<String>   keys = at.getKeys();

            for (String key: keys) {
                cr.addAttr(t, key, at.getAttr(key), true);
            }
        }
        else {
            cr.addAttr(t, "active", Integer.toString(theme.getActive()), true);
            cr.addAttr(t, "artifact", theme.getArtifact(), true);
            cr.addAttr(t, "facet", theme.getFacet(), true);
            cr.addAttr(t, "pos", Integer.toString(theme.getPosition()), true);
            cr.addAttr(t, "index", Integer.toString(theme.getIndex()), true);
            cr.addAttr(t, "description", theme.getDescription(), true);
        }

        return t;
    }


    /**
     * Creates a whole block with art:loaded-recommendations nodes.
     *
     * @param cr The ElementCreator used to create new elements.
     * @param c  The collection.
     *
     * @return an element with loaded recommendations.
     */
    protected static Element createRecommendationsElements(
        ElementCreator cr,
        Collection     c)
    {
        System.out.println("CollectionHelper.createRecommendationsElements");

        List<Recommendation> rs = c.getRecommendations();

        if (rs == null || rs.size() == 0) {
            System.err.println("Collection did not load recommendations: " +
                c.identifier());
            return null;
        }

        Element loaded = cr.create("loaded-recommendations");

        for (Recommendation r: rs) {
            Element recommendation = createRecommendationElement(cr, c, r);

            if (recommendation != null) {
                loaded.appendChild(recommendation);
            }
        }

        return loaded;
    }


    /**
     * Create a node art:recommended.
     *
     * @param cr The ElementCreator used to create new elements.
     * @param c  The collection.
     * @param r  The Recommendation.
     *
     * @return an element that represents an output mode with its themes.
     */
    protected static Element createRecommendationElement(
        ElementCreator cr,
        Collection     c,
        Recommendation r)
    {
        System.out.println("CollectionHelper.createRecommendationElement");

        Element recommendation = cr.create("recommendation");
        cr.addAttr(recommendation, "factory", r.getFactory(), true);
        cr.addAttr(recommendation, "ids", r.getIDs(), true);

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

http://dive4elements.wald.intevation.org