Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/ChartInfoServiceImpl.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/ChartInfoServiceImpl.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.server; | |
2 | |
3 import java.io.InputStream; | |
4 import java.io.IOException; | |
5 import java.util.ArrayList; | |
6 import java.util.Date; | |
7 import java.util.List; | |
8 import java.util.Map; | |
9 | |
10 import javax.xml.xpath.XPathConstants; | |
11 | |
12 import org.w3c.dom.Document; | |
13 import org.w3c.dom.Element; | |
14 import org.w3c.dom.NodeList; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
19 | |
20 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
21 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; | |
22 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
23 | |
24 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
25 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
26 | |
27 import org.dive4elements.river.client.shared.Transform2D; | |
28 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
29 import org.dive4elements.river.client.shared.model.Axis; | |
30 import org.dive4elements.river.client.shared.model.DateAxis; | |
31 import org.dive4elements.river.client.shared.model.NumberAxis; | |
32 import org.dive4elements.river.client.shared.model.ChartInfo; | |
33 import org.dive4elements.river.client.shared.model.Collection; | |
34 | |
35 import org.dive4elements.river.client.client.services.ChartInfoService; | |
36 | |
37 | |
38 /** | |
39 * This service fetches a document that contains meta information for a specific | |
40 * chart. | |
41 * | |
42 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
43 */ | |
44 public class ChartInfoServiceImpl | |
45 extends RemoteServiceServlet | |
46 implements ChartInfoService | |
47 { | |
48 private static final Logger logger = | |
49 Logger.getLogger(ChartInfoServiceImpl.class); | |
50 | |
51 public static final String XPATH_TRANSFORM_MATRIX = | |
52 "/art:chartinfo/art:transformation-matrix/art:matrix"; | |
53 | |
54 public static final String XPATH_X_AXES = | |
55 "/art:chartinfo/art:axes/art:domain"; | |
56 | |
57 public static final String XPATH_Y_AXES = | |
58 "/art:chartinfo/art:axes/art:range"; | |
59 | |
60 public static final String EXCEPTION_STRING = "error_chart_info_service"; | |
61 | |
62 | |
63 public ChartInfo getChartInfo( | |
64 Collection collection, | |
65 String locale, | |
66 String type, | |
67 Map<String, String> attr) | |
68 throws ServerException | |
69 { | |
70 logger.info("ChartInfoServiceImpl.getChartInfo"); | |
71 | |
72 String url = getServletContext().getInitParameter("server-url"); | |
73 | |
74 Document request = ClientProtocolUtils.newOutCollectionDocument( | |
75 collection.identifier(), | |
76 type, | |
77 type, | |
78 ChartServiceHelper.getChartAttributes(attr)); | |
79 | |
80 try { | |
81 HttpClient client = new HttpClientImpl(url, locale); | |
82 InputStream in = client.collectionOut( | |
83 request, | |
84 collection.identifier(), | |
85 type + "_chartinfo"); | |
86 | |
87 Document info = XMLUtils.parseDocument(in); | |
88 | |
89 return parseInfoDocument(info); | |
90 } | |
91 catch (IOException ioe) { | |
92 ioe.printStackTrace(); | |
93 } | |
94 catch (Exception e) { | |
95 e.printStackTrace(); | |
96 } | |
97 | |
98 logger.debug("Error while fetching chart info."); | |
99 | |
100 throw new ServerException(EXCEPTION_STRING); | |
101 } | |
102 | |
103 | |
104 /** | |
105 * Parse ChartInfo-Part of document, create Transforms and axes | |
106 * from it. | |
107 */ | |
108 protected ChartInfo parseInfoDocument(Document doc) { | |
109 Transform2D[] transformer = parseTransformationMatrix(doc); | |
110 Axis[] xAxes = parseXAxes(doc); | |
111 Axis[] yAxes = parseYAxes(doc); | |
112 | |
113 return new ChartInfo(xAxes, yAxes, transformer); | |
114 } | |
115 | |
116 | |
117 protected Axis[] parseXAxes(Document doc) { | |
118 logger.debug("ChartInfoServiceImpl.parseXAxes"); | |
119 | |
120 NodeList axes = (NodeList) XMLUtils.xpath( | |
121 doc, | |
122 XPATH_X_AXES, | |
123 XPathConstants.NODESET, | |
124 ArtifactNamespaceContext.INSTANCE); | |
125 | |
126 return parseAxes(axes); | |
127 } | |
128 | |
129 | |
130 protected Axis[] parseYAxes(Document doc) { | |
131 logger.debug("ChartInfoServiceImpl.parseYAxes"); | |
132 | |
133 NodeList axes = (NodeList) XMLUtils.xpath( | |
134 doc, | |
135 XPATH_Y_AXES, | |
136 XPathConstants.NODESET, | |
137 ArtifactNamespaceContext.INSTANCE); | |
138 | |
139 return parseAxes(axes); | |
140 } | |
141 | |
142 | |
143 protected Axis[] parseAxes(NodeList axes) { | |
144 logger.debug("ChartInfoServiceImpl.parseAxes"); | |
145 | |
146 int count = axes != null ? axes.getLength() : 0; | |
147 | |
148 logger.debug("Chart has " + count + " axes."); | |
149 | |
150 if (count == 0) { | |
151 return null; | |
152 } | |
153 | |
154 Axis[] result = new Axis[count]; | |
155 | |
156 String ns = ArtifactNamespaceContext.NAMESPACE_URI; | |
157 | |
158 for (int i = 0; i < count; i++) { | |
159 Element node = (Element) axes.item(i); | |
160 | |
161 String posStr = node.getAttributeNS(ns, "pos"); | |
162 String fromStr = node.getAttributeNS(ns, "from"); | |
163 String toStr = node.getAttributeNS(ns, "to"); | |
164 String minStr = node.getAttributeNS(ns, "min"); | |
165 String maxStr = node.getAttributeNS(ns, "max"); | |
166 String axisType = node.getAttributeNS(ns, "axistype"); | |
167 | |
168 try { | |
169 int pos = Integer.parseInt(posStr); | |
170 | |
171 if (pos >= result.length) { | |
172 // this should never happen | |
173 logger.debug("The axis is out of valid range: " + pos); | |
174 continue; | |
175 } | |
176 | |
177 if (axisType != null && axisType.equals(DateAxis.TYPE)) { | |
178 long from = Long.parseLong(fromStr); | |
179 long to = Long.parseLong(toStr); | |
180 long min = Long.parseLong(minStr); | |
181 long max = Long.parseLong(maxStr); | |
182 | |
183 if (logger.isDebugEnabled()) { | |
184 logger.debug("date axis from: " + new Date(from)); | |
185 logger.debug("date axis to : " + new Date(to)); | |
186 logger.debug("date axis min : " + new Date(min)); | |
187 logger.debug("date axis max : " + new Date(max)); | |
188 } | |
189 | |
190 result[pos] = new DateAxis(pos, from, to, min, max); | |
191 } | |
192 else { | |
193 double from = Double.parseDouble(fromStr); | |
194 double to = Double.parseDouble(toStr); | |
195 double min = Double.parseDouble(minStr); | |
196 double max = Double.parseDouble(maxStr); | |
197 | |
198 result[pos] = new NumberAxis(pos, from, to, min, max); | |
199 } | |
200 } | |
201 catch (NumberFormatException nfe) { | |
202 nfe.printStackTrace(); | |
203 } | |
204 } | |
205 | |
206 logger.debug("Parsed " + result.length + " axes"); | |
207 | |
208 return result; | |
209 } | |
210 | |
211 | |
212 /** | |
213 * Parses the chart info document and extract the Transform2D values. | |
214 * | |
215 * @param doc The chart info document. | |
216 * | |
217 * @return a Transform2D object to transfrom pixel coordinates into chart | |
218 * coordinates. | |
219 */ | |
220 protected Transform2D[] parseTransformationMatrix(Document doc) { | |
221 logger.debug("ChartInfoServiceImpl.parseTransformationMatrix"); | |
222 | |
223 NodeList matrix = (NodeList) XMLUtils.xpath( | |
224 doc, | |
225 XPATH_TRANSFORM_MATRIX, | |
226 XPathConstants.NODESET, | |
227 ArtifactNamespaceContext.INSTANCE); | |
228 | |
229 int num = matrix != null ? matrix.getLength() : 0; | |
230 | |
231 List<Transform2D> transformer = new ArrayList<Transform2D>(num); | |
232 | |
233 for (int i = 0; i < num; i++) { | |
234 Transform2D t = createTransformer((Element) matrix.item(i)); | |
235 | |
236 if (t == null) { | |
237 logger.warn("Broken transformation matrix at pos: " + i); | |
238 continue; | |
239 } | |
240 | |
241 transformer.add(t); | |
242 } | |
243 | |
244 return transformer.toArray(new Transform2D[num]); | |
245 } | |
246 | |
247 | |
248 protected Transform2D createTransformer(Element matrix) { | |
249 String ns = ArtifactNamespaceContext.NAMESPACE_URI; | |
250 | |
251 String sx = matrix.getAttributeNS(ns, "sx"); | |
252 String sy = matrix.getAttributeNS(ns, "sy"); | |
253 String tx = matrix.getAttributeNS(ns, "tx"); | |
254 String ty = matrix.getAttributeNS(ns, "ty"); | |
255 String xType = matrix.getAttributeNS(ns, "xtype"); | |
256 String yType = matrix.getAttributeNS(ns, "ytype"); | |
257 | |
258 xType = xType == null || xType.length() == 0 ? "number" : xType; | |
259 yType = yType == null || yType.length() == 0 ? "number" : yType; | |
260 | |
261 if (sx != null && sy != null && tx != null && ty != null) { | |
262 try { | |
263 logger.debug("Create new Transform2D with x format: " + xType); | |
264 logger.debug("Create new Transform2D with y format: " + yType); | |
265 | |
266 return new Transform2D( | |
267 Double.parseDouble(sx), | |
268 Double.parseDouble(sy), | |
269 Double.parseDouble(tx), | |
270 Double.parseDouble(ty), | |
271 xType, yType); | |
272 } | |
273 catch (NumberFormatException nfe) { | |
274 logger.warn("Error while parsing matrix values."); | |
275 } | |
276 } | |
277 | |
278 logger.warn("No matrix values found."); | |
279 | |
280 return new Transform2D(1d, 1d, 0d, 0d); | |
281 } | |
282 } | |
283 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |