1 | /* WRS 2.0 |
---|
2 | * Copyright (C) 2009 Anton Patrushev |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | |
---|
18 | package handler; |
---|
19 | |
---|
20 | import geometry.Point; |
---|
21 | |
---|
22 | import java.io.UnsupportedEncodingException; |
---|
23 | import java.sql.Connection; |
---|
24 | import java.sql.ResultSet; |
---|
25 | import java.sql.SQLException; |
---|
26 | import java.sql.Statement; |
---|
27 | import java.util.ArrayList; |
---|
28 | import java.util.Arrays; |
---|
29 | import java.util.HashMap; |
---|
30 | import java.util.Hashtable; |
---|
31 | import java.util.Iterator; |
---|
32 | |
---|
33 | import model.Resource; |
---|
34 | import model.Service; |
---|
35 | import util.Log; |
---|
36 | import util.ObjectPool; |
---|
37 | import util.Parameter; |
---|
38 | import util.ServiceRequest; |
---|
39 | |
---|
40 | import org.antlr.stringtemplate.StringTemplate; |
---|
41 | import org.opengis.geometry.MismatchedDimensionException; |
---|
42 | import org.opengis.referencing.FactoryException; |
---|
43 | import org.opengis.referencing.NoSuchAuthorityCodeException; |
---|
44 | import org.opengis.referencing.operation.TransformException; |
---|
45 | |
---|
46 | import com.vividsolutions.jts.io.ParseException; |
---|
47 | |
---|
48 | public class DatabaseHandler extends ResourceHandler |
---|
49 | { |
---|
50 | private Resource resource; |
---|
51 | private Hashtable<String, String> functions; |
---|
52 | |
---|
53 | public static final String SRID = "srid"; |
---|
54 | public static final String UNITS = "units"; |
---|
55 | |
---|
56 | public static final String FUNCTION = "function"; |
---|
57 | public static final String INPUT = "input"; |
---|
58 | public static final String OUTPUT = "output"; |
---|
59 | |
---|
60 | // Arrays in Services enum's elements specify an order of parameters |
---|
61 | private enum Services |
---|
62 | { |
---|
63 | ROUTE |
---|
64 | { |
---|
65 | ArrayList<String> getParameters() |
---|
66 | { |
---|
67 | String[] p = { "table", "x1", "y1", "x2", "y2", "box", |
---|
68 | "cost_value", "reverse_cost_value", "directed", "hasrc" }; |
---|
69 | return new ArrayList<String>(Arrays.asList(p)); |
---|
70 | } |
---|
71 | }, |
---|
72 | CATCH |
---|
73 | { |
---|
74 | ArrayList<String> getParameters() |
---|
75 | { |
---|
76 | String[] p = { "table", "x1", "y1", "distance", "rbox", |
---|
77 | "cost_value", "reverse_cost_value", "directed", "hasrc" }; |
---|
78 | return new ArrayList<String>(Arrays.asList(p)); |
---|
79 | } |
---|
80 | }, |
---|
81 | GEOCODE |
---|
82 | { |
---|
83 | ArrayList<String> getParameters() |
---|
84 | { |
---|
85 | String[] p = { "address" }; |
---|
86 | return new ArrayList<String>(Arrays.asList(p)); |
---|
87 | } |
---|
88 | }; |
---|
89 | |
---|
90 | abstract ArrayList<String> getParameters(); |
---|
91 | } |
---|
92 | |
---|
93 | public DatabaseHandler() |
---|
94 | { |
---|
95 | // TODO implement reading functions mapping from a config file |
---|
96 | this.functions = new Hashtable<String, String>(); |
---|
97 | this.functions.put("route", "shootingstar_sp_smart"); |
---|
98 | this.functions.put("travel", "tsp_astar_directed_smart"); |
---|
99 | this.functions.put("catch", "driving_distance"); |
---|
100 | this.functions.put("geocode", "geocode"); |
---|
101 | } |
---|
102 | |
---|
103 | @SuppressWarnings("unchecked") |
---|
104 | @Override |
---|
105 | public ArrayList<HashMap<String, Parameter>> handleRequest( |
---|
106 | ServiceRequest request, Log log) |
---|
107 | { |
---|
108 | ArrayList<HashMap<String, Parameter>> out = new ArrayList<HashMap<String, Parameter>>(); |
---|
109 | |
---|
110 | if (this.resource != null) |
---|
111 | { |
---|
112 | |
---|
113 | String query = fillQueryTemplate(this.resource.getQuery(), request |
---|
114 | .getService(), request.getParameters()); |
---|
115 | |
---|
116 | System.out.println("Query:" + query); |
---|
117 | |
---|
118 | ObjectPool<Connection> pool = this.resource.getPool(); |
---|
119 | Connection con = pool.checkOut(); |
---|
120 | |
---|
121 | if (con != null) |
---|
122 | { |
---|
123 | |
---|
124 | // TODO send query and get a result |
---|
125 | try |
---|
126 | { |
---|
127 | Statement stmt = con.createStatement(); |
---|
128 | ResultSet result = stmt.executeQuery(query); |
---|
129 | |
---|
130 | while (result.next()) |
---|
131 | { |
---|
132 | Iterator<String> keys = request.getService().getOut() |
---|
133 | .keySet().iterator(); |
---|
134 | HashMap<String, Parameter> paraMap = new HashMap<String, Parameter>(); |
---|
135 | while (keys.hasNext()) |
---|
136 | { |
---|
137 | String key = keys.next(); |
---|
138 | Parameter op = new Parameter(); |
---|
139 | op.setName(key); |
---|
140 | op.setType(request.getService().getOut().get(key) |
---|
141 | .getType()); |
---|
142 | //op.setValue(result.getString(key)); |
---|
143 | byte[] bytes = result.getBytes(key); |
---|
144 | |
---|
145 | //TODO Ugly! Ugly! Better way of handling null values is needed |
---|
146 | if(bytes != null) |
---|
147 | op.setValue(new String(result.getBytes(key), "UTF-8")); |
---|
148 | else |
---|
149 | op.setValue("0"); |
---|
150 | |
---|
151 | paraMap.put(key, op); |
---|
152 | } |
---|
153 | out.add(paraMap); |
---|
154 | } |
---|
155 | stmt.close(); |
---|
156 | } |
---|
157 | catch (SQLException e) |
---|
158 | { |
---|
159 | // TODO Auto-generated catch block |
---|
160 | e.printStackTrace(); |
---|
161 | } |
---|
162 | catch (UnsupportedEncodingException e) |
---|
163 | { |
---|
164 | // TODO Auto-generated catch block |
---|
165 | e.printStackTrace(); |
---|
166 | } |
---|
167 | |
---|
168 | pool.checkIn(con); |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | // Connection pool is full |
---|
173 | log.logger.severe("Connection to " + resource.getUrl() |
---|
174 | + " failed."); |
---|
175 | } |
---|
176 | } |
---|
177 | return out; |
---|
178 | } |
---|
179 | |
---|
180 | @Override |
---|
181 | public void setResource(Resource resource) |
---|
182 | { |
---|
183 | this.resource = resource; |
---|
184 | } |
---|
185 | |
---|
186 | @Override |
---|
187 | public String fillQueryTemplate(String template, Service service, |
---|
188 | HashMap<String, Parameter> parameters) |
---|
189 | { |
---|
190 | StringTemplate query = new StringTemplate(template); |
---|
191 | String function = this.functions.get(service.getName()); |
---|
192 | query.setAttribute(FUNCTION, function); |
---|
193 | |
---|
194 | // fill output |
---|
195 | StringBuffer output = new StringBuffer(""); |
---|
196 | Iterator<String> opit = service.getOut().keySet().iterator(); |
---|
197 | while (opit.hasNext()) |
---|
198 | { |
---|
199 | try |
---|
200 | { |
---|
201 | String key = opit.next(); |
---|
202 | Parameter p = this.resource.getParameters().get(key); |
---|
203 | output.append(p.getKey()).append(" as ").append(p.getName()); |
---|
204 | if (opit.hasNext()) |
---|
205 | output.append(","); |
---|
206 | } |
---|
207 | catch (NullPointerException e) |
---|
208 | { |
---|
209 | // Required parameter is missing! |
---|
210 | // TODO do something smart here |
---|
211 | } |
---|
212 | } |
---|
213 | query.setAttribute(OUTPUT, output.toString()); |
---|
214 | |
---|
215 | StringBuffer params = new StringBuffer(""); |
---|
216 | |
---|
217 | // fill parameters |
---|
218 | // Iterator<String> keys = service.getIn().keySet().iterator(); |
---|
219 | Iterator<String> keys = Services.valueOf( |
---|
220 | service.getName().toUpperCase()).getParameters().iterator(); |
---|
221 | while (keys.hasNext()) |
---|
222 | { |
---|
223 | try |
---|
224 | { |
---|
225 | String key = keys.next(); |
---|
226 | |
---|
227 | Parameter p = parameters.get(key); |
---|
228 | String value = p.getValue(); |
---|
229 | |
---|
230 | // Check if we need to re-project coordinates |
---|
231 | if (p.getType().toUpperCase().equals(DataTypes.DOUBLE.name()) |
---|
232 | && key.toLowerCase().startsWith("x")) |
---|
233 | { |
---|
234 | try |
---|
235 | { |
---|
236 | String srid_in = parameters.get("srid_in").getValue(); |
---|
237 | String srid_out = resource.getParameters().get("srid").getValue(); |
---|
238 | |
---|
239 | if (srid_in != srid_out) |
---|
240 | { |
---|
241 | // try to find Y coordinate |
---|
242 | String yKey = key.toLowerCase().replaceFirst("x", |
---|
243 | "y"); |
---|
244 | Parameter yP = parameters.get(yKey); |
---|
245 | String yValue = yP.getValue(); |
---|
246 | String[] transformed = transformPoint(value, yValue, |
---|
247 | srid_in, srid_out); |
---|
248 | p.setValue(transformed[0]); |
---|
249 | yP.setValue(transformed[1]); |
---|
250 | value = transformed[0]; |
---|
251 | } |
---|
252 | } |
---|
253 | catch (NullPointerException e) |
---|
254 | { |
---|
255 | //Can't find srid_in or srid_out |
---|
256 | //Do nothing |
---|
257 | } |
---|
258 | catch (MismatchedDimensionException e) |
---|
259 | { |
---|
260 | // TODO Auto-generated catch block |
---|
261 | e.printStackTrace(); |
---|
262 | } |
---|
263 | catch (NoSuchAuthorityCodeException e) |
---|
264 | { |
---|
265 | // TODO Auto-generated catch block |
---|
266 | e.printStackTrace(); |
---|
267 | } |
---|
268 | catch (FactoryException e) |
---|
269 | { |
---|
270 | // TODO Auto-generated catch block |
---|
271 | e.printStackTrace(); |
---|
272 | } |
---|
273 | catch (ParseException e) |
---|
274 | { |
---|
275 | // TODO Auto-generated catch block |
---|
276 | e.printStackTrace(); |
---|
277 | } |
---|
278 | catch (TransformException e) |
---|
279 | { |
---|
280 | // TODO Auto-generated catch block |
---|
281 | e.printStackTrace(); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | if (p.getType().toUpperCase().equals(DataTypes.STRING.name())) |
---|
286 | { |
---|
287 | value = "'" + value + "'"; |
---|
288 | } |
---|
289 | params.append(value); |
---|
290 | |
---|
291 | if (keys.hasNext()) |
---|
292 | params.append(","); |
---|
293 | |
---|
294 | } |
---|
295 | catch (NullPointerException e) |
---|
296 | { |
---|
297 | // Required parameter is missing! |
---|
298 | // TODO do something smart here |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | query.setAttribute(INPUT, params.toString()); |
---|
303 | |
---|
304 | return query.toString(); |
---|
305 | } |
---|
306 | |
---|
307 | @Override |
---|
308 | public boolean checkService(Service service) |
---|
309 | { |
---|
310 | return this.resource.getServices().containsKey(service.getName()); |
---|
311 | } |
---|
312 | |
---|
313 | @Override |
---|
314 | public ArrayList<Point> parseGeometry(String geom) |
---|
315 | { |
---|
316 | ArrayList<Point> points = new ArrayList<Point>(); |
---|
317 | String[] wkts = parseWKT(geom); |
---|
318 | |
---|
319 | for (int i = 0; i < wkts.length; i++) |
---|
320 | { |
---|
321 | String wkt2[] = wkts[i].split(","); |
---|
322 | for (int j = 0; j < wkt2.length; j++) |
---|
323 | { |
---|
324 | Point point = new Point(); |
---|
325 | point.setX(Double.parseDouble(wkt2[j].split(" ")[0])); |
---|
326 | point.setY(Double.parseDouble(wkt2[j].split(" ")[1])); |
---|
327 | points.add(point); |
---|
328 | } |
---|
329 | |
---|
330 | } |
---|
331 | return points; |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | * Parses a WKT geometry string to an array |
---|
336 | * |
---|
337 | * @param resultSet |
---|
338 | * @return |
---|
339 | * @throws SQLException |
---|
340 | */ |
---|
341 | private String[] parseWKT(String wkt) |
---|
342 | { |
---|
343 | String wkts[] = null; |
---|
344 | |
---|
345 | if (wkt.contains("MULTILINESTRING")) |
---|
346 | { |
---|
347 | wkt = wkt.split("MULTILINESTRING\\(\\(")[1].split("\\)\\)")[0]; |
---|
348 | wkts = wkt.split("\\)\\("); |
---|
349 | } |
---|
350 | else if (wkt.contains("POINT")) |
---|
351 | { |
---|
352 | wkts = wkt.split("POINT\\(")[1].split("\\)"); |
---|
353 | } |
---|
354 | else if (wkt.contains("LINESTRING")) |
---|
355 | { |
---|
356 | wkts = wkt.split("LINESTRING\\(")[1].split("\\)"); |
---|
357 | } |
---|
358 | else if (wkt.contains("POLYGON")) |
---|
359 | { |
---|
360 | wkts = wkt.split("POLYGON\\(\\(")[1].split("\\)\\)"); |
---|
361 | } |
---|
362 | |
---|
363 | return wkts; |
---|
364 | } |
---|
365 | |
---|
366 | } |
---|