ingo@549: package de.intevation.flys.client.server;
ingo@549: 
ingo@549: import java.util.Map;
ingo@549: 
ingo@549: import org.w3c.dom.Document;
ingo@549: import org.w3c.dom.Element;
ingo@549: 
ingo@1367: import org.apache.log4j.Logger;
ingo@1367: 
ingo@549: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@549: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@549: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@549: 
ingo@549: 
ingo@549: /**
ingo@549:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@549:  */
ingo@549: public class ChartServiceHelper {
ingo@549: 
ingo@1367:     private static final Logger logger =
ingo@1367:         Logger.getLogger(ChartServiceHelper.class);
ingo@1367: 
ingo@1367: 
ingo@549:     /** The default chart width if no value is specified in the request.*/
ingo@549:     public static final int DEFAULT_CHART_WIDTH  = 600;
ingo@549: 
ingo@549:     /** 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: 
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:      *
ingo@549:      * @param req The request document.
ingo@549:      *
ingo@549:      * @return a document to adjust chart settings.
ingo@549:      */
ingo@549:     protected static Document getChartAttributes(Map<String, String> req) {
ingo@1367:         logger.debug("ChartServiceHelper.getChartAttributes");
ingo@549: 
ingo@549:         Document doc = XMLUtils.newDocument();
ingo@549: 
ingo@549:         ElementCreator ec = new ElementCreator(
ingo@549:             doc,
ingo@549:             ArtifactNamespaceContext.NAMESPACE_URI,
ingo@549:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@549: 
ingo@549:         Element attributes = ec.create("attributes");
ingo@549: 
ingo@549:         appendChartSize(req, attributes, ec);
ingo@1351:         appendFormat(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:     /**
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:      *
ingo@549:      * @param req The request object that might contain the chart size.
ingo@549:      * @param attributes The attributes element used to adjust chart settings.
ingo@549:      * @param ec The ElementCreator that might be used to create new Elements.
ingo@549:      */
ingo@549:     protected static void appendChartSize(
ingo@549:         Map<String, String> req,
ingo@549:         Element             attributes,
ingo@549:         ElementCreator      ec)
ingo@549:     {
ingo@1367:         logger.debug("ChartServiceHelper.appendChartSize");
ingo@549: 
ingo@549:         Element size = ec.create("size");
ingo@549: 
ingo@549:         String width  = req.get("width");
ingo@549:         String height = req.get("height");
ingo@549: 
ingo@549:         if (width == null || height == null) {
ingo@549:             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:     /**
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:      *
ingo@549:      * @param req The request object that might contain the chart size.
ingo@549:      * @param doc The attribute document used to adjust chart settings.
ingo@549:      * @param ec The ElementCreator that might be used to create new Elements.
ingo@549:      */
ingo@549:     protected static void appendXRange(
ingo@549:         Map<String, String> req,
ingo@549:         Element             attributes,
ingo@549:         ElementCreator      ec)
ingo@549:     {
ingo@1367:         logger.debug("ChartServiceHelper.appendXRange");
ingo@549: 
ingo@549:         Element range = ec.create("xrange");
ingo@549: 
ingo@549:         String from = req.get("minx");
ingo@549:         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:     /**
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:      *
ingo@549:      * @param req The request object that might contain the chart size.
ingo@549:      * @param doc The attribute document used to adjust chart settings.
ingo@549:      * @param ec The ElementCreator that might be used to create new Elements.
ingo@549:      */
ingo@549:     protected static void appendYRange(
ingo@549:         Map<String, String> req,
ingo@549:         Element             attributes,
ingo@549:         ElementCreator      ec)
ingo@549:     {
ingo@1367:         logger.debug("ChartServiceHelper.appendYRange");
ingo@549: 
ingo@549:         Element range = ec.create("yrange");
ingo@549: 
ingo@549:         String from = req.get("miny");
ingo@549:         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:     /**
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:      *
ingo@1351:      * @param req The request object that might contain the chart format.
ingo@1351:      * @param doc The attribute document used to adjust chart settings.
ingo@1351:      * @param ec The ElementCreator that might be used to create new Elements.
ingo@1351:      */
ingo@1351:     protected static void appendFormat(
ingo@1351:         Map<String, String> req,
ingo@1351:         Element             attributes,
ingo@1351:         ElementCreator      ec
ingo@1351: 
ingo@1351:     ) {
ingo@1367:         logger.debug("ChartServiceHelper.appendFormat");
ingo@1351: 
ingo@1351:         String formatStr = req.get("format");
ingo@1351:         if (formatStr == null || formatStr.length() == 0) {
ingo@1351:             return;
ingo@1351:         }
ingo@1351: 
ingo@1351:         Element format = ec.create("format");
ingo@1351:         ec.addAttr(format, "value", formatStr, true);
ingo@1351: 
ingo@1351:         attributes.appendChild(format);
ingo@1351:     }
raimund@2906: 
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:      *
raimund@2906:      * @param req The request object that might contain the chart size.
raimund@2906:      * @param doc The attribute document used to adjust chart settings.
raimund@2906:      * @param ec The ElementCreator that might be used to create new Elements.
raimund@2906:      */
raimund@2906:     protected static void appendCurrentKm(
raimund@2906:         Map<String, String> req,
raimund@2906:         Element             attributes,
raimund@2906:         ElementCreator      ec)
raimund@2906:     {
raimund@2906:         logger.debug("ChartServiceHelper.appendCurrentKm");
raimund@2906: 
raimund@2906:         Element currentKm = ec.create("currentKm");
raimund@2906: 
raimund@2906:         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 :