Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/ChartOutputServiceImpl.java @ 549:e74bf6bfeeb6
Use the same code to create the attribute document for the chart creation in ChartOutputService and ChartInfoService.
flys-client/trunk@2060 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 07 Jun 2011 10:03:19 +0000 |
parents | 7c57149e8715 |
children | 51d4b51a51ed |
comparison
equal
deleted
inserted
replaced
548:aff225e07720 | 549:e74bf6bfeeb6 |
---|---|
1 package de.intevation.flys.client.server; | 1 package de.intevation.flys.client.server; |
2 | 2 |
3 import java.io.OutputStream; | 3 import java.io.OutputStream; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.util.HashMap; | |
6 import java.util.Map; | |
5 | 7 |
6 import org.w3c.dom.Document; | 8 import org.w3c.dom.Document; |
7 import org.w3c.dom.Element; | |
8 | 9 |
9 import javax.servlet.http.HttpServlet; | 10 import javax.servlet.http.HttpServlet; |
10 import javax.servlet.http.HttpServletRequest; | 11 import javax.servlet.http.HttpServletRequest; |
11 import javax.servlet.http.HttpServletResponse; | 12 import javax.servlet.http.HttpServletResponse; |
12 | 13 |
13 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.common.utils.ClientProtocolUtils; | 14 import de.intevation.artifacts.common.utils.ClientProtocolUtils; |
15 import de.intevation.artifacts.common.utils.XMLUtils; | |
16 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
17 | 15 |
18 import de.intevation.artifacts.httpclient.http.HttpClient; | 16 import de.intevation.artifacts.httpclient.http.HttpClient; |
19 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | 17 import de.intevation.artifacts.httpclient.http.HttpClientImpl; |
20 | 18 |
21 | 19 |
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 25 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
28 */ | 26 */ |
29 public class ChartOutputServiceImpl | 27 public class ChartOutputServiceImpl |
30 extends HttpServlet | 28 extends HttpServlet |
31 { | 29 { |
32 /** The default chart width if no value is specified in the request.*/ | |
33 public static final int DEFAULT_CHART_WIDTH = 600; | |
34 | |
35 /** The default chart height if no value is specified in the request.*/ | |
36 public static final int DEFAULT_CHART_HEIGHT = 400; | |
37 | |
38 | |
39 public void doGet(HttpServletRequest req, HttpServletResponse resp) { | 30 public void doGet(HttpServletRequest req, HttpServletResponse resp) { |
40 System.out.println("ChartOutputServiceImpl.doGet"); | 31 System.out.println("ChartOutputServiceImpl.doGet"); |
41 | 32 |
42 try { | 33 try { |
43 OutputStream out = resp.getOutputStream(); | 34 OutputStream out = resp.getOutputStream(); |
46 String uuid = req.getParameter("uuid"); | 37 String uuid = req.getParameter("uuid"); |
47 String type = req.getParameter("type"); | 38 String type = req.getParameter("type"); |
48 String locale = req.getParameter("locale"); | 39 String locale = req.getParameter("locale"); |
49 | 40 |
50 Document request = ClientProtocolUtils.newOutCollectionDocument( | 41 Document request = ClientProtocolUtils.newOutCollectionDocument( |
51 uuid, type, type, getChartAttributes(req)); | 42 uuid, type, type, |
43 ChartServiceHelper.getChartAttributes(prepareChartAttributes(req))); | |
52 | 44 |
53 HttpClient client = new HttpClientImpl(serverUrl, locale); | 45 HttpClient client = new HttpClientImpl(serverUrl, locale); |
54 client.collectionOut(request, uuid, "chart", out); | 46 client.collectionOut(request, uuid, "chart", out); |
55 | 47 |
56 out.close(); | 48 out.close(); |
57 out.flush(); | 49 out.flush(); |
58 } | 50 } |
59 catch (IOException ioe) { | 51 catch (IOException ioe) { |
52 ioe.printStackTrace(); | |
60 System.err.println(ioe.getMessage()); | 53 System.err.println(ioe.getMessage()); |
61 } | 54 } |
62 catch (Exception e) { | 55 catch (Exception e) { |
56 e.printStackTrace(); | |
63 System.err.println(e.getMessage()); | 57 System.err.println(e.getMessage()); |
64 } | 58 } |
65 } | 59 } |
66 | 60 |
67 | 61 |
68 /** | 62 protected Map<String, String> prepareChartAttributes(HttpServletRequest req) { |
69 * This method returns a document which might contain parameters to adjust | 63 Map<String, String> attr = new HashMap<String, String>(); |
70 * chart settings. The document is created using the information that are | |
71 * contained in the request object. | |
72 * | |
73 * @param req The request document. | |
74 * | |
75 * @return a document to adjust chart settings. | |
76 */ | |
77 protected Document getChartAttributes(HttpServletRequest req) { | |
78 Document doc = XMLUtils.newDocument(); | |
79 | 64 |
80 ElementCreator ec = new ElementCreator( | 65 Map params = req.getParameterMap(); |
81 doc, | |
82 ArtifactNamespaceContext.NAMESPACE_URI, | |
83 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
84 | 66 |
85 Element attributes = ec.create("attributes"); | 67 attr.put("width", req.getParameter("width")); |
68 attr.put("height", req.getParameter("height")); | |
69 attr.put("minx", req.getParameter("minx")); | |
70 attr.put("maxx", req.getParameter("maxx")); | |
71 attr.put("miny", req.getParameter("miny")); | |
72 attr.put("maxy", req.getParameter("maxy")); | |
86 | 73 |
87 appendChartSize(req, attributes, ec); | 74 return attr; |
88 appendXRange(req, attributes, ec); | |
89 appendYRange(req, attributes, ec); | |
90 | |
91 doc.appendChild(attributes); | |
92 | |
93 return doc; | |
94 } | |
95 | |
96 | |
97 /** | |
98 * This method extracts the size (width/height) of a chart from request | |
99 * object and append those values - if they exist - to the attribute | |
100 * document used to adjust chart settings. | |
101 * | |
102 * @param req The request object that might contain the chart size. | |
103 * @param attributes The attributes element used to adjust chart settings. | |
104 * @param ec The ElementCreator that might be used to create new Elements. | |
105 */ | |
106 protected void appendChartSize( | |
107 HttpServletRequest req, | |
108 Element attributes, | |
109 ElementCreator ec) | |
110 { | |
111 Element size = ec.create("size"); | |
112 | |
113 String width = req.getParameter("width"); | |
114 String height = req.getParameter("height"); | |
115 | |
116 if (width == null || height == null) { | |
117 width = Integer.toString(DEFAULT_CHART_WIDTH); | |
118 height = Integer.toString(DEFAULT_CHART_HEIGHT); | |
119 } | |
120 | |
121 ec.addAttr(size, "width", width, true); | |
122 ec.addAttr(size, "height", height, true); | |
123 | |
124 attributes.appendChild(size); | |
125 } | |
126 | |
127 | |
128 /** | |
129 * This method extracts the x range for the chart from request object and | |
130 * appends those range - if it exists - to the attribute document used to | |
131 * adjust the chart settings. | |
132 * | |
133 * @param req The request object that might contain the chart size. | |
134 * @param doc The attribute document used to adjust chart settings. | |
135 * @param ec The ElementCreator that might be used to create new Elements. | |
136 */ | |
137 protected void appendXRange( | |
138 HttpServletRequest req, | |
139 Element attributes, | |
140 ElementCreator ec) | |
141 { | |
142 Element range = ec.create("xrange"); | |
143 | |
144 String from = req.getParameter("minx"); | |
145 String to = req.getParameter("maxx"); | |
146 | |
147 if (from != null && to != null) { | |
148 ec.addAttr(range, "from", from, true); | |
149 ec.addAttr(range, "to", to, true); | |
150 | |
151 attributes.appendChild(range); | |
152 } | |
153 } | |
154 | |
155 | |
156 /** | |
157 * This method extracts the x range for the chart from request object and | |
158 * appends those range - if it exists - to the attribute document used to | |
159 * adjust the chart settings. | |
160 * | |
161 * @param req The request object that might contain the chart size. | |
162 * @param doc The attribute document used to adjust chart settings. | |
163 * @param ec The ElementCreator that might be used to create new Elements. | |
164 */ | |
165 protected void appendYRange( | |
166 HttpServletRequest req, | |
167 Element attributes, | |
168 ElementCreator ec) | |
169 { | |
170 Element range = ec.create("yrange"); | |
171 | |
172 String from = req.getParameter("miny"); | |
173 String to = req.getParameter("maxy"); | |
174 | |
175 if (from != null && to != null) { | |
176 ec.addAttr(range, "from", from, true); | |
177 ec.addAttr(range, "to", to, true); | |
178 | |
179 attributes.appendChild(range); | |
180 } | |
181 } | 75 } |
182 } | 76 } |
183 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 77 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |