comparison flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java @ 245:1e73d5a4859c

Added new WQ info service and data structures. flys-client/trunk@1825 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 04 May 2011 14:54:38 +0000
parents
children ab8eb2f544f2
comparison
equal deleted inserted replaced
244:1bc787eeb556 245:1e73d5a4859c
1 package de.intevation.flys.client.server;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.xml.xpath.XPathConstants;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList;
12
13 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
14
15 import de.intevation.artifacts.common.ArtifactNamespaceContext;
16 import de.intevation.artifacts.common.utils.XMLUtils;
17
18 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
19 import de.intevation.artifacts.httpclient.http.HttpClient;
20 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
21
22 import de.intevation.flys.client.shared.exceptions.ServerException;
23 import de.intevation.flys.client.client.services.WQInfoService;
24 import de.intevation.flys.client.shared.model.WQInfoObject;
25 import de.intevation.flys.client.shared.model.WQInfoObjectImpl;
26
27
28 /**
29 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
30 */
31 public class WQInfoServiceImpl
32 extends RemoteServiceServlet
33 implements WQInfoService
34 {
35 public static final String ERROR_NO_WQINFO_FOUND =
36 "error_no_wqinfo_found";
37
38 public static final String XPATH_WQS = "art:service/art:mainvalues/art:mainvalue";
39
40
41 public WQInfoObject[] getWQInfo(
42 String url,
43 String locale,
44 String river,
45 double from,
46 double to)
47 throws ServerException
48 {
49 System.out.println("WQInfoServiceImpl.getWQInfo");
50
51 Document doc = XMLUtils.newDocument();
52
53 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
54 doc,
55 ArtifactNamespaceContext.NAMESPACE_URI,
56 ArtifactNamespaceContext.NAMESPACE_PREFIX);
57
58 Element mainvalues = ec.create("mainvalues");
59 Element riverEl = ec.create("river");
60 Element startEl = ec.create("start");
61 Element endEl = ec.create("end");
62
63 riverEl.setTextContent(river);
64 startEl.setTextContent(Double.valueOf(from).toString());
65 endEl.setTextContent(Double.valueOf(to).toString());
66
67 mainvalues.appendChild(riverEl);
68 mainvalues.appendChild(startEl);
69 mainvalues.appendChild(endEl);
70
71 doc.appendChild(mainvalues);
72
73 HttpClient client = new HttpClientImpl(url, locale);
74
75 try {
76 Document result = client.callService(url, "mainvalues", doc);
77
78 System.out.println("Extract wq info objects now.");
79 WQInfoObject[] objects = extractWQInfoObjects(result);
80
81 if (objects != null && objects.length > 0) {
82 return objects;
83 }
84 }
85 catch (ConnectionException ce) {
86 System.err.println(ce.getLocalizedMessage());
87 }
88
89 throw new ServerException(ERROR_NO_WQINFO_FOUND);
90 }
91
92
93 /**
94 * Extracts all wq info objects from <i>result</i> document.
95 *
96 * @param result The document retrieved by the server.
97 *
98 * @return a list of WQInfoObjects.
99 */
100 protected WQInfoObject[] extractWQInfoObjects(Document result)
101 throws ServerException
102 {
103 NodeList list = (NodeList) XMLUtils.xpath(
104 result,
105 XPATH_WQS,
106 XPathConstants.NODESET,
107 ArtifactNamespaceContext.INSTANCE);
108
109 if (list == null || list.getLength() == 0) {
110 System.err.println("No wq info found.");
111
112 throw new ServerException(ERROR_NO_WQINFO_FOUND);
113 }
114
115 int num = list.getLength();
116 System.out.println("Response contains " + num + " objects.");
117
118 List<WQInfoObject> objects =
119 new ArrayList<WQInfoObject>(num);
120
121 for (int i = 0; i < num; i++) {
122 WQInfoObject obj = buildWQInfoObject(list.item(i));
123
124 if (obj != null) {
125 objects.add(obj);
126 }
127 }
128
129 System.out.println("Retrieved " + objects.size() + " wq values");
130
131 return (WQInfoObject[])
132 objects.toArray(new WQInfoObject[num]);
133 }
134
135
136 /**
137 * Extracts information for a single wq info object and intializes an
138 * WQInfoObject with them.
139 *
140 * @param node The node that contains the information.
141 *
142 * @return a valid WQInfoObject.
143 */
144 protected WQInfoObject buildWQInfoObject(Node node) {
145 String name = XMLUtils.xpathString(
146 node, "@name", ArtifactNamespaceContext.INSTANCE);
147
148 String type = XMLUtils.xpathString(
149 node, "@type", ArtifactNamespaceContext.INSTANCE);
150
151 String value = XMLUtils.xpathString(
152 node, "@value", ArtifactNamespaceContext.INSTANCE);
153
154
155 if (name != null && type != null) {
156 try {
157 return new WQInfoObjectImpl(
158 name,
159 type,
160 new Double(value));
161 }
162 catch (NumberFormatException nfe) {
163 System.err.println(nfe.getLocalizedMessage());
164 }
165 }
166
167 System.err.println("Invalid wq info object found.");
168
169 return null;
170 }
171 }
172 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org