comparison gwt-client/src/main/java/org/dive4elements/river/client/server/ChartServiceHelper.java @ 9567:86e522bc7f36

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

http://dive4elements.wald.intevation.org