comparison gwt-client/src/main/java/org/dive4elements/river/client/server/ChartServiceHelper.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-client/src/main/java/org/dive4elements/river/client/server/ChartServiceHelper.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.server;
2
3 import java.util.Map;
4
5 import org.w3c.dom.Document;
6 import org.w3c.dom.Element;
7
8 import org.apache.log4j.Logger;
9
10 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
11 import org.dive4elements.artifacts.common.utils.XMLUtils;
12 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
13
14
15 /**
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
17 */
18 public class ChartServiceHelper {
19
20 private static final Logger logger =
21 Logger.getLogger(ChartServiceHelper.class);
22
23
24 /** The default chart width if no value is specified in the request.*/
25 public static final int DEFAULT_CHART_WIDTH = 600;
26
27 /** The default chart height if no value is specified in the request.*/
28 public static final int DEFAULT_CHART_HEIGHT = 400;
29
30
31 private ChartServiceHelper() {
32 }
33
34 /**
35 * This method returns a document which might contain parameters to adjust
36 * chart settings. The document is created using the information that are
37 * contained in the request object.
38 *
39 * @param req The request document.
40 *
41 * @return a document to adjust chart settings.
42 */
43 protected static Document getChartAttributes(Map<String, String> req) {
44 logger.debug("ChartServiceHelper.getChartAttributes");
45
46 Document doc = XMLUtils.newDocument();
47
48 ElementCreator ec = new ElementCreator(
49 doc,
50 ArtifactNamespaceContext.NAMESPACE_URI,
51 ArtifactNamespaceContext.NAMESPACE_PREFIX);
52
53 Element attributes = ec.create("attributes");
54
55 appendChartSize(req, attributes, ec);
56 appendFormat(req, attributes, ec);
57 appendXRange(req, attributes, ec);
58 appendYRange(req, attributes, ec);
59 appendCurrentKm(req, attributes, ec);
60
61 doc.appendChild(attributes);
62
63 return doc;
64 }
65
66
67 /**
68 * This method extracts the size (width/height) of a chart from request
69 * object and append those values - if they exist - to the attribute
70 * document used to adjust chart settings.
71 *
72 * @param req The request object that might contain the chart size.
73 * @param attributes The attributes element used to adjust chart settings.
74 * @param ec The ElementCreator that might be used to create new Elements.
75 */
76 protected static void appendChartSize(
77 Map<String, String> req,
78 Element attributes,
79 ElementCreator ec)
80 {
81 logger.debug("ChartServiceHelper.appendChartSize");
82
83 Element size = ec.create("size");
84
85 String width = req.get("width");
86 String height = req.get("height");
87
88 if (width == null || height == null) {
89 width = String.valueOf(DEFAULT_CHART_WIDTH);
90 height = String.valueOf(DEFAULT_CHART_HEIGHT);
91 }
92
93 ec.addAttr(size, "width", width, true);
94 ec.addAttr(size, "height", height, true);
95
96 attributes.appendChild(size);
97 }
98
99
100 /**
101 * This method extracts the x range for the chart from request object and
102 * appends those range - if it exists - to the attribute document used to
103 * adjust the chart settings.
104 *
105 * @param req The request object that might contain the chart size.
106 * @param doc The attribute document used to adjust chart settings.
107 * @param ec The ElementCreator that might be used to create new Elements.
108 */
109 protected static void appendXRange(
110 Map<String, String> req,
111 Element attributes,
112 ElementCreator ec)
113 {
114 logger.debug("ChartServiceHelper.appendXRange");
115
116 Element range = ec.create("xrange");
117
118 String from = req.get("minx");
119 String to = req.get("maxx");
120
121 if (from != null && to != null) {
122 ec.addAttr(range, "from", from, true);
123 ec.addAttr(range, "to", to, true);
124
125 attributes.appendChild(range);
126 }
127 }
128
129
130 /**
131 * This method extracts the x range for the chart from request object and
132 * appends those range - if it exists - to the attribute document used to
133 * adjust the chart settings.
134 *
135 * @param req The request object that might contain the chart size.
136 * @param doc The attribute document used to adjust chart settings.
137 * @param ec The ElementCreator that might be used to create new Elements.
138 */
139 protected static void appendYRange(
140 Map<String, String> req,
141 Element attributes,
142 ElementCreator ec)
143 {
144 logger.debug("ChartServiceHelper.appendYRange");
145
146 Element range = ec.create("yrange");
147
148 String from = req.get("miny");
149 String to = req.get("maxy");
150
151 if (from != null && to != null) {
152 ec.addAttr(range, "from", from, true);
153 ec.addAttr(range, "to", to, true);
154
155 attributes.appendChild(range);
156 }
157 }
158
159
160 /**
161 * This method extracts the format string from request object and appends
162 * those format - if existing - to the attribute document used to adjust
163 * the chart settings.
164 *
165 * @param req The request object that might contain the chart format.
166 * @param doc The attribute document used to adjust chart settings.
167 * @param ec The ElementCreator that might be used to create new Elements.
168 */
169 protected static void appendFormat(
170 Map<String, String> req,
171 Element attributes,
172 ElementCreator ec
173
174 ) {
175 logger.debug("ChartServiceHelper.appendFormat");
176
177 String formatStr = req.get("format");
178 if (formatStr == null || formatStr.length() == 0) {
179 return;
180 }
181
182 Element format = ec.create("format");
183 ec.addAttr(format, "value", formatStr, true);
184
185 attributes.appendChild(format);
186 }
187
188
189 /**
190 * This method extracts the current km for the chart from request object and
191 * appends this km - if it exists - to the attribute document used to
192 * adjust the chart settings.
193 *
194 * @param req The request object that might contain the chart size.
195 * @param doc The attribute document used to adjust chart settings.
196 * @param ec The ElementCreator that might be used to create new Elements.
197 */
198 protected static void appendCurrentKm(
199 Map<String, String> req,
200 Element attributes,
201 ElementCreator ec)
202 {
203 logger.debug("ChartServiceHelper.appendCurrentKm");
204
205 Element currentKm = ec.create("currentKm");
206
207 String km = req.get("km");
208
209 if (km != null) {
210 ec.addAttr(currentKm, "km", km, true);
211
212 attributes.appendChild(currentKm);
213 }
214 }
215
216 }
217 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org