ingo@530: package de.intevation.flys.client.server;
ingo@530: 
ingo@530: import java.util.List;
ingo@530: import java.util.Map;
ingo@530: 
ingo@530: import org.w3c.dom.Document;
ingo@530: import org.w3c.dom.Element;
ingo@530: 
ingo@530: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@530: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@530: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@530: 
ingo@530: import de.intevation.flys.client.shared.model.Collection;
ingo@530: import de.intevation.flys.client.shared.model.OutputMode;
ingo@530: import de.intevation.flys.client.shared.model.Theme;
ingo@530: import de.intevation.flys.client.shared.model.ThemeList;
ingo@530: 
ingo@530: 
ingo@530: /**
ingo@530:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@530:  */
ingo@530: public class CollectionHelper {
ingo@530: 
ingo@530:     public static Document createAttribute(Collection collection) {
ingo@530:         System.out.println("CollectionHelper.createAttribute");
ingo@530: 
ingo@530:         Document doc = XMLUtils.newDocument();
ingo@530: 
ingo@530:         ElementCreator cr = new ElementCreator(
ingo@530:             doc,
ingo@530:             ArtifactNamespaceContext.NAMESPACE_URI,
ingo@530:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@530: 
ingo@530:         Element attr = cr.create("attribute");
ingo@530: 
ingo@530:         doc.appendChild(attr);
ingo@530: 
ingo@530:         Map<String, OutputMode> tmpOuts = collection.getOutputModes();
ingo@530: 
ingo@530:         Element outs = createOutputElements(cr, collection, tmpOuts);
ingo@530: 
ingo@530:         if (outs != null) {
ingo@530:             attr.appendChild(outs);
ingo@530:         }
ingo@530: 
ingo@530:         return doc;
ingo@530:     }
ingo@530: 
ingo@530: 
ingo@530:     /**
ingo@530:      * Creates a whole block with art:output nodes.
ingo@530:      *
ingo@530:      * @param cr The ElementCreator used to create new elements.
ingo@530:      * @param c  The collection.
ingo@530:      * @param modes The OutputModes that should be included.
ingo@530:      *
ingo@530:      * @return an element with output modes.
ingo@530:      */
ingo@530:     protected static Element createOutputElements(
ingo@530:         ElementCreator          cr,
ingo@530:         Collection              c,
ingo@530:         Map<String, OutputMode> mmodes)
ingo@530:     {
ingo@530:         System.out.println("CollectionHelper.createOutputElements");
ingo@530: 
ingo@530:         java.util.Collection<OutputMode> modes = mmodes != null
ingo@530:             ? mmodes.values()
ingo@530:             : null;
ingo@530: 
ingo@530:         if (modes == null || modes.size() == 0) {
ingo@530:             System.err.println("Collection has no modes: " + c.identifier());
ingo@530:             return null;
ingo@530:         }
ingo@530: 
ingo@530:         Element outs = cr.create("outputs");
ingo@530: 
ingo@530:         for (OutputMode mode: modes) {
ingo@530:             Element out = createOutputElement(cr, c, mode);
ingo@530: 
ingo@530:             if (out != null) {
ingo@530:                 outs.appendChild(out);
ingo@530:             }
ingo@530:         }
ingo@530: 
ingo@530:         return outs;
ingo@530:     }
ingo@530: 
ingo@530: 
ingo@530:     /**
ingo@530:      * Create a node art:output that further consist of art:theme nodes.
ingo@530:      *
ingo@530:      * @param cr The ElementCreator used to create new elements.
ingo@530:      * @param c  The collection.
ingo@530:      * @param mode The OutputMode.
ingo@530:      *
ingo@530:      * @return an element that represents an output mode with its themes.
ingo@530:      */
ingo@530:     protected static Element createOutputElement(
ingo@530:         ElementCreator cr,
ingo@530:         Collection     collection,
ingo@530:         OutputMode     mode)
ingo@530:     {
ingo@530:         System.out.println("CollectionHelper.createOutputElement");
ingo@530: 
ingo@530:         Element out = cr.create("output");
ingo@530:         cr.addAttr(out, "name", mode.getName(), false);
ingo@530: 
ingo@530:         ThemeList themeList = collection.getThemeList(mode.getName());
ingo@530:         List<Theme> themes  = themeList != null ? themeList.getThemes() : null;
ingo@530: 
ingo@530:         if (themes == null || themes.size() == 0) {
ingo@530:             System.err.println("No themes for output mode: " + mode.getName());
ingo@530:             return null;
ingo@530:         }
ingo@530: 
ingo@530:         for (Theme theme: themes) {
ingo@530:             Element t = createThemeElement(cr, collection, theme);
ingo@530: 
ingo@530:             if (t != null) {
ingo@530:                 out.appendChild(t);
ingo@530:             }
ingo@530:         }
ingo@530: 
ingo@530:         return out;
ingo@530:     }
ingo@530: 
ingo@530: 
ingo@530:     /**
ingo@530:      * Creates a theme node art:theme that represents a curve in a chart or map.
ingo@530:      *
ingo@530:      * @param cr The ElementCreator used to create new elements.
ingo@530:      * @param collection The collection.
ingo@530:      * @param theme The theme whose attributes should be written to an element.
ingo@530:      *
ingo@530:      * @return an element that contains the informtion of the given theme.
ingo@530:      */
ingo@530:     protected static Element createThemeElement(
ingo@530:         ElementCreator cr,
ingo@530:         Collection     collection,
ingo@530:         Theme          theme)
ingo@530:     {
ingo@530:         if (theme == null) {
ingo@530:             return null;
ingo@530:         }
ingo@530: 
ingo@530:         Element t = cr.create("theme");
ingo@530: 
ingo@530:         cr.addAttr(t, "active", theme.isActive() ? "1" : "0", true);
ingo@530:         cr.addAttr(t, "artifact", theme.getArtifact(), true);
ingo@530:         cr.addAttr(t, "facet", theme.getFacet(), true);
ingo@530:         cr.addAttr(t, "pos", Integer.toString(theme.getPosition()), true);
ingo@530: 
ingo@530:         return t;
ingo@530:     }
ingo@530: }
ingo@530: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :