Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/server/FixingsOverviewServiceImpl.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java@653dd9d7f5d5 |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
4 | |
5 import de.intevation.artifacts.common.utils.XMLUtils; | |
6 import de.intevation.artifacts.common.utils.XSLTransformer; | |
7 | |
8 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
9 | |
10 import de.intevation.artifacts.httpclient.http.HttpClient; | |
11 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
12 | |
13 import de.intevation.flys.client.client.services.FixingsOverviewService; | |
14 | |
15 import de.intevation.flys.client.shared.exceptions.ServerException; | |
16 | |
17 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.FixEvent; | |
18 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector; | |
19 | |
20 import de.intevation.flys.client.shared.model.FixingsOverviewInfo; | |
21 | |
22 import java.io.IOException; | |
23 import java.io.InputStream; | |
24 | |
25 import java.util.ArrayList; | |
26 import java.util.List; | |
27 | |
28 import javax.xml.xpath.XPathConstants; | |
29 | |
30 import org.apache.log4j.Logger; | |
31 | |
32 import org.w3c.dom.Document; | |
33 import org.w3c.dom.Element; | |
34 import org.w3c.dom.NodeList; | |
35 | |
36 public class FixingsOverviewServiceImpl | |
37 extends RemoteServiceServlet | |
38 implements FixingsOverviewService | |
39 { | |
40 private static final Logger log = | |
41 Logger.getLogger(FixingsOverviewServiceImpl.class); | |
42 | |
43 public static final String SERVICE_NAME = "fixings-overview"; | |
44 | |
45 public static final String XSL_TRANSFORM = | |
46 "/WEB-INF/stylesheets/fixoverview2html.xsl"; | |
47 | |
48 protected static final String XPATH_RID = "/fixings/river/@rid"; | |
49 protected static final String XPATH_RIVER = "/fixings/river/@name"; | |
50 protected static final String XPATH_RFROM = "/fixings/river/@from"; | |
51 protected static final String XPATH_RTO = "/fixings/river/@to"; | |
52 | |
53 protected static final String XPATH_EVENT = "/fixings/events/event"; | |
54 | |
55 | |
56 @Override | |
57 public FixingsOverviewInfo generateOverview( | |
58 String locale, | |
59 String uuid, | |
60 String filter, | |
61 boolean checkboxes, | |
62 String callback | |
63 ) | |
64 throws ServerException | |
65 { | |
66 log.info("FixingsOverviewServiceImpl.doGet"); | |
67 | |
68 if (filter == null || filter.length() == 0) { | |
69 log.warn("Missing 'filter' parameter."); | |
70 return null; | |
71 } | |
72 | |
73 boolean debug = log.isDebugEnabled(); | |
74 | |
75 if (debug) { | |
76 log.debug("JSON filter: ------------------"); | |
77 log.debug(filter); | |
78 } | |
79 | |
80 Document filterDoc = XMLUtils.jsonToXML(filter); | |
81 | |
82 if (filterDoc == null) { | |
83 log.warn("Creating filter document failed."); | |
84 return null; | |
85 } | |
86 | |
87 if (debug) { | |
88 log.debug("XML filter: ------------------"); | |
89 log.debug(XMLUtils.toString(filterDoc)); | |
90 } | |
91 | |
92 try { | |
93 String url = getServletContext().getInitParameter("server-url"); | |
94 HttpClient client = new HttpClientImpl(url, locale); | |
95 Document resultDoc = | |
96 client.callService(url, SERVICE_NAME, filterDoc); | |
97 | |
98 if (debug) { | |
99 log.debug("Result XML: -----------"); | |
100 log.debug(XMLUtils.toString(resultDoc)); | |
101 } | |
102 | |
103 FixingsOverviewInfo i = getInfo( | |
104 locale, resultDoc, uuid, checkboxes, callback); | |
105 return i; | |
106 } | |
107 catch (ConnectionException ce) { | |
108 log.error(ce); | |
109 } | |
110 return null; | |
111 } | |
112 | |
113 | |
114 protected FixingsOverviewInfo getInfo( | |
115 String locale, | |
116 Document doc, | |
117 String uuid, | |
118 boolean checkboxes, | |
119 String callback | |
120 ) { | |
121 // TODO: Find a more general solution. | |
122 locale = locale == null || locale.toLowerCase().startsWith("de") | |
123 ? "de" | |
124 : "en"; | |
125 | |
126 InputStream transform = getServletContext() | |
127 .getResourceAsStream(XSL_TRANSFORM); | |
128 | |
129 if (transform == null) { | |
130 log.warn("transform not found"); | |
131 return null; | |
132 } | |
133 | |
134 String result = null; | |
135 try { | |
136 XSLTransformer xformer = new XSLTransformer(); | |
137 xformer.addParameter("locale", locale); | |
138 xformer.addParameter("project-uuid", uuid); | |
139 xformer.addParameter( | |
140 "render-checkboxes", | |
141 checkboxes ? Boolean.TRUE : Boolean.FALSE); | |
142 xformer.addParameter("callback", callback); | |
143 result = xformer.transform(doc, transform); | |
144 } | |
145 finally { | |
146 try { transform.close(); } | |
147 catch (IOException ioe) {} | |
148 } | |
149 | |
150 if (log.isDebugEnabled()) { | |
151 log.debug("--------------------------------------"); | |
152 log.debug(result); | |
153 log.debug("--------------------------------------"); | |
154 } | |
155 | |
156 int rid = -1; | |
157 double from = -1; | |
158 double to = -1; | |
159 | |
160 String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null); | |
161 String river = XMLUtils.xpathString(doc, XPATH_RIVER, null); | |
162 String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null); | |
163 String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null); | |
164 | |
165 try { | |
166 rid = Integer.parseInt(rid_str); | |
167 from = Double.parseDouble(from_str); | |
168 to = Double.parseDouble(to_str); | |
169 } | |
170 catch(NumberFormatException nfe) { | |
171 log.warn(nfe, nfe); | |
172 } | |
173 | |
174 List<FixEvent> fixEvents = getFixEvents(doc); | |
175 return new FixingsOverviewInfo( | |
176 rid, | |
177 river, | |
178 from, | |
179 to, | |
180 fixEvents, | |
181 result); | |
182 } | |
183 | |
184 | |
185 protected List<FixEvent> getFixEvents(Document doc) { | |
186 List<FixEvent> list = new ArrayList<FixEvent>(); | |
187 | |
188 NodeList events = (NodeList) XMLUtils.xpath( | |
189 doc, | |
190 XPATH_EVENT, | |
191 XPathConstants.NODESET, | |
192 null); | |
193 | |
194 if (events == null || events.getLength() == 0) { | |
195 log.warn("No events in Overview!"); | |
196 return list; | |
197 } | |
198 | |
199 for (int i = 0, E = events.getLength(); i < E; i++) { | |
200 Element n = (Element)events.item(i); | |
201 List<Sector> sectors = getSectors(n); | |
202 String cid = n.getAttribute("cid"); | |
203 String date = n.getAttribute("date");; | |
204 String name = n.getAttribute("description"); | |
205 list.add(new FixEvent( cid, date, name, sectors)); | |
206 } | |
207 return list; | |
208 } | |
209 | |
210 protected List<Sector> getSectors(Element event) { | |
211 NodeList sectors = event.getElementsByTagName("sector"); | |
212 | |
213 if (sectors.getLength() == 0) { | |
214 log.warn("No Sectors in Event!"); | |
215 return null; | |
216 } | |
217 | |
218 List<Sector> list = new ArrayList<Sector>(); | |
219 for (int i = 0, S = sectors.getLength(); i < S; i++) { | |
220 Element n = (Element)sectors.item(i); | |
221 int cls = -1; | |
222 double from = -1; | |
223 double to = -1; | |
224 String cls_str = n.getAttribute("class"); | |
225 String from_str = n.getAttribute("from"); | |
226 String to_str = n.getAttribute("to"); | |
227 try { | |
228 cls = Integer.parseInt(cls_str); | |
229 from = Double.parseDouble(from_str); | |
230 to = Double.parseDouble(to_str); | |
231 } | |
232 catch(NumberFormatException nfe) { | |
233 log.warn(nfe, nfe); | |
234 } | |
235 list.add(new Sector(cls, from, to)); | |
236 } | |
237 return list; | |
238 } | |
239 } | |
240 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |