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