ingo@530: package de.intevation.flys.client.server;
ingo@530:
ingo@530: import java.util.List;
ingo@530: import java.util.Map;
ingo@804: import java.util.Set;
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@804: import de.intevation.flys.client.shared.model.AttributedTheme;
ingo@530: import de.intevation.flys.client.shared.model.Collection;
ingo@530: import de.intevation.flys.client.shared.model.OutputMode;
ingo@809: import de.intevation.flys.client.shared.model.Recommendation;
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 Ingo Weinzierl
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 tmpOuts = collection.getOutputModes();
ingo@530:
ingo@530: Element outs = createOutputElements(cr, collection, tmpOuts);
ingo@809: Element recs = createRecommendationsElements(cr, collection);
ingo@530:
ingo@530: if (outs != null) {
ingo@530: attr.appendChild(outs);
ingo@530: }
ingo@530:
ingo@809: if (recs != null) {
ingo@809: attr.appendChild(recs);
ingo@809: }
ingo@809:
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 mmodes)
ingo@530: {
ingo@530: System.out.println("CollectionHelper.createOutputElements");
ingo@530:
ingo@530: java.util.Collection 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 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@804: Element t = cr.create("facet");
ingo@530:
ingo@804: if (theme instanceof AttributedTheme) {
ingo@804: AttributedTheme at = (AttributedTheme) theme;
ingo@804: Set keys = at.getKeys();
ingo@804:
ingo@804: for (String key: keys) {
ingo@804: cr.addAttr(t, key, at.getAttr(key), true);
ingo@804: }
ingo@804: }
ingo@804: else {
ingo@804: cr.addAttr(t, "active", Integer.toString(theme.getActive()), true);
ingo@804: cr.addAttr(t, "artifact", theme.getArtifact(), true);
ingo@804: cr.addAttr(t, "facet", theme.getFacet(), true);
ingo@804: cr.addAttr(t, "pos", Integer.toString(theme.getPosition()), true);
ingo@804: cr.addAttr(t, "index", Integer.toString(theme.getIndex()), true);
ingo@804: cr.addAttr(t, "description", theme.getDescription(), true);
ingo@804: }
ingo@530:
ingo@530: return t;
ingo@530: }
ingo@809:
ingo@809:
ingo@809: /**
ingo@809: * Creates a whole block with art:loaded-recommendations nodes.
ingo@809: *
ingo@809: * @param cr The ElementCreator used to create new elements.
ingo@809: * @param c The collection.
ingo@809: *
ingo@809: * @return an element with loaded recommendations.
ingo@809: */
ingo@809: protected static Element createRecommendationsElements(
ingo@809: ElementCreator cr,
ingo@809: Collection c)
ingo@809: {
ingo@809: System.out.println("CollectionHelper.createRecommendationsElements");
ingo@809:
ingo@809: List rs = c.getRecommendations();
ingo@809:
ingo@809: if (rs == null || rs.size() == 0) {
ingo@809: System.err.println("Collection did not load recommendations: " +
ingo@809: c.identifier());
ingo@809: return null;
ingo@809: }
ingo@809:
ingo@809: Element loaded = cr.create("loaded-recommendations");
ingo@809:
ingo@809: for (Recommendation r: rs) {
ingo@809: Element recommendation = createRecommendationElement(cr, c, r);
ingo@809:
ingo@809: if (recommendation != null) {
ingo@809: loaded.appendChild(recommendation);
ingo@809: }
ingo@809: }
ingo@809:
ingo@809: return loaded;
ingo@809: }
ingo@809:
ingo@809:
ingo@809: /**
ingo@809: * Create a node art:recommended.
ingo@809: *
ingo@809: * @param cr The ElementCreator used to create new elements.
ingo@809: * @param c The collection.
ingo@809: * @param r The Recommendation.
ingo@809: *
ingo@809: * @return an element that represents an output mode with its themes.
ingo@809: */
ingo@809: protected static Element createRecommendationElement(
ingo@809: ElementCreator cr,
ingo@809: Collection c,
ingo@809: Recommendation r)
ingo@809: {
ingo@809: System.out.println("CollectionHelper.createRecommendationElement");
ingo@809:
ingo@809: Element recommendation = cr.create("recommendation");
ingo@809: cr.addAttr(recommendation, "factory", r.getFactory(), true);
sascha@836: cr.addAttr(recommendation, "ids", r.getIDs(), true);
ingo@809:
ingo@809: return recommendation;
ingo@809: }
ingo@530: }
ingo@530: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :