teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.server; ingo@549: ingo@549: import java.util.Map; ingo@549: ingo@1367: import org.apache.log4j.Logger; teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; gernotbelger@9567: import org.w3c.dom.Document; gernotbelger@9567: import org.w3c.dom.Element; ingo@549: ingo@549: /** ingo@549: * @author Ingo Weinzierl ingo@549: */ ingo@549: public class ChartServiceHelper { ingo@549: gernotbelger@9567: private static final Logger log = Logger.getLogger(ChartServiceHelper.class); ingo@1367: gernotbelger@9567: /** The default chart width if no value is specified in the request. */ gernotbelger@9567: public static final int DEFAULT_CHART_WIDTH = 600; ingo@549: gernotbelger@9567: /** The default chart height if no value is specified in the request. */ ingo@549: public static final int DEFAULT_CHART_HEIGHT = 400; ingo@549: ingo@549: private ChartServiceHelper() { ingo@549: } ingo@549: ingo@549: /** ingo@549: * This method returns a document which might contain parameters to adjust ingo@549: * chart settings. The document is created using the information that are ingo@549: * contained in the request object. ingo@549: * gernotbelger@9567: * @param req gernotbelger@9567: * The request document. ingo@549: * ingo@549: * @return a document to adjust chart settings. ingo@549: */ gernotbelger@9567: public static Document getChartAttributes(final Map req) { teichmann@8203: log.debug("ChartServiceHelper.getChartAttributes"); ingo@549: gernotbelger@9567: final Document doc = XMLUtils.newDocument(); ingo@549: gernotbelger@9567: final ElementCreator ec = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@549: gernotbelger@9567: final Element attributes = ec.create("attributes"); ingo@549: ingo@549: appendChartSize(req, attributes, ec); ingo@1351: appendFormat(req, attributes, ec); gernotbelger@9104: appendExport(req, attributes, ec); ingo@549: appendXRange(req, attributes, ec); ingo@549: appendYRange(req, attributes, ec); raimund@2906: appendCurrentKm(req, attributes, ec); ingo@549: ingo@549: doc.appendChild(attributes); ingo@549: ingo@549: return doc; ingo@549: } ingo@549: ingo@549: /** ingo@549: * This method extracts the size (width/height) of a chart from request ingo@549: * object and append those values - if they exist - to the attribute ingo@549: * document used to adjust chart settings. ingo@549: * gernotbelger@9567: * @param req gernotbelger@9567: * The request object that might contain the chart size. gernotbelger@9567: * @param attributes gernotbelger@9567: * The attributes element used to adjust chart settings. gernotbelger@9567: * @param ec gernotbelger@9567: * The ElementCreator that might be used to create new Elements. ingo@549: */ gernotbelger@9567: protected static void appendChartSize(final Map req, final Element attributes, final ElementCreator ec) { teichmann@8203: log.debug("ChartServiceHelper.appendChartSize"); ingo@549: gernotbelger@9567: final Element size = ec.create("size"); ingo@549: gernotbelger@9567: String width = req.get("width"); ingo@549: String height = req.get("height"); ingo@549: ingo@549: if (width == null || height == null) { gernotbelger@9567: width = String.valueOf(DEFAULT_CHART_WIDTH); ingo@549: height = String.valueOf(DEFAULT_CHART_HEIGHT); ingo@549: } ingo@549: ingo@549: ec.addAttr(size, "width", width, true); ingo@549: ec.addAttr(size, "height", height, true); ingo@549: ingo@549: attributes.appendChild(size); ingo@549: } ingo@549: ingo@549: /** ingo@549: * This method extracts the x range for the chart from request object and ingo@549: * appends those range - if it exists - to the attribute document used to ingo@549: * adjust the chart settings. ingo@549: * gernotbelger@9567: * @param req gernotbelger@9567: * The request object that might contain the chart size. gernotbelger@9567: * @param doc gernotbelger@9567: * The attribute document used to adjust chart settings. gernotbelger@9567: * @param ec gernotbelger@9567: * The ElementCreator that might be used to create new Elements. ingo@549: */ gernotbelger@9567: protected static void appendXRange(final Map req, final Element attributes, final ElementCreator ec) { teichmann@8203: log.debug("ChartServiceHelper.appendXRange"); ingo@549: gernotbelger@9567: final Element range = ec.create("xrange"); ingo@549: gernotbelger@9567: final String from = req.get("minx"); gernotbelger@9567: final String to = req.get("maxx"); ingo@549: ingo@549: if (from != null && to != null) { ingo@549: ec.addAttr(range, "from", from, true); ingo@549: ec.addAttr(range, "to", to, true); ingo@549: ingo@549: attributes.appendChild(range); ingo@549: } ingo@549: } ingo@549: ingo@549: /** ingo@549: * This method extracts the x range for the chart from request object and ingo@549: * appends those range - if it exists - to the attribute document used to ingo@549: * adjust the chart settings. ingo@549: * gernotbelger@9567: * @param req gernotbelger@9567: * The request object that might contain the chart size. gernotbelger@9567: * @param doc gernotbelger@9567: * The attribute document used to adjust chart settings. gernotbelger@9567: * @param ec gernotbelger@9567: * The ElementCreator that might be used to create new Elements. ingo@549: */ gernotbelger@9567: protected static void appendYRange(final Map req, final Element attributes, final ElementCreator ec) { teichmann@8203: log.debug("ChartServiceHelper.appendYRange"); ingo@549: gernotbelger@9567: final Element range = ec.create("yrange"); ingo@549: gernotbelger@9567: final String from = req.get("miny"); gernotbelger@9567: final String to = req.get("maxy"); ingo@549: ingo@549: if (from != null && to != null) { ingo@549: ec.addAttr(range, "from", from, true); ingo@549: ec.addAttr(range, "to", to, true); ingo@549: ingo@549: attributes.appendChild(range); ingo@549: } ingo@549: } ingo@1351: ingo@1351: /** ingo@1351: * This method extracts the format string from request object and appends ingo@1351: * those format - if existing - to the attribute document used to adjust ingo@1351: * the chart settings. ingo@1351: * gernotbelger@9567: * @param req gernotbelger@9567: * The request object that might contain the chart format. gernotbelger@9567: * @param doc gernotbelger@9567: * The attribute document used to adjust chart settings. gernotbelger@9567: * @param ec gernotbelger@9567: * The ElementCreator that might be used to create new Elements. ingo@1351: */ gernotbelger@9567: private static void appendFormat(final Map req, final Element attributes, final ElementCreator ec ingo@1351: ingo@1351: ) { teichmann@8203: log.debug("ChartServiceHelper.appendFormat"); ingo@1351: gernotbelger@9567: final String formatStr = req.get("format"); ingo@1351: if (formatStr == null || formatStr.length() == 0) { ingo@1351: return; ingo@1351: } ingo@1351: gernotbelger@9567: final Element format = ec.create("format"); ingo@1351: ec.addAttr(format, "value", formatStr, true); ingo@1351: ingo@1351: attributes.appendChild(format); ingo@1351: } raimund@2906: gernotbelger@9104: /** gernotbelger@9104: * This method extracts the export string from request object and appends gernotbelger@9104: * those format - if existing - to the attribute document used to adjust gernotbelger@9104: * the chart settings. gernotbelger@9104: * gernotbelger@9567: * @param req gernotbelger@9567: * The request object that might contain the chart export flag. gernotbelger@9567: * @param doc gernotbelger@9567: * The attribute document used to adjust chart settings. gernotbelger@9567: * @param ec gernotbelger@9567: * The ElementCreator that might be used to create new Elements. gernotbelger@9104: */ gernotbelger@9567: private static void appendExport(final Map req, final Element attributes, final ElementCreator ec) { gernotbelger@9567: gernotbelger@9104: final String exportStr = req.get("export"); gernotbelger@9567: if (exportStr == null || exportStr.isEmpty()) gernotbelger@9104: return; gernotbelger@9567: gernotbelger@9104: final Element format = ec.create("export"); gernotbelger@9104: ec.addAttr(format, "value", exportStr, true); gernotbelger@9567: gernotbelger@9104: attributes.appendChild(format); gernotbelger@9104: } raimund@2906: raimund@2906: /** raimund@2906: * This method extracts the current km for the chart from request object and raimund@2906: * appends this km - if it exists - to the attribute document used to raimund@2906: * adjust the chart settings. raimund@2906: * gernotbelger@9567: * @param req gernotbelger@9567: * The request object that might contain the chart size. gernotbelger@9567: * @param doc gernotbelger@9567: * The attribute document used to adjust chart settings. gernotbelger@9567: * @param ec gernotbelger@9567: * The ElementCreator that might be used to create new Elements. raimund@2906: */ gernotbelger@9567: protected static void appendCurrentKm(final Map req, final Element attributes, final ElementCreator ec) { teichmann@8203: log.debug("ChartServiceHelper.appendCurrentKm"); raimund@2906: gernotbelger@9567: final Element currentKm = ec.create("currentKm"); gernotbelger@9567: gernotbelger@9567: final String km = req.get("km"); raimund@2906: raimund@2906: if (km != null) { raimund@2906: ec.addAttr(currentKm, "km", km, true); raimund@2906: raimund@2906: attributes.appendChild(currentKm); raimund@2906: } raimund@2906: } raimund@2906: ingo@549: } ingo@549: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :