Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java @ 778:9a828e5a2390
Removed trailing whitespace
gnv-artifacts/trunk@851 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 29 Mar 2010 07:58:51 +0000 |
parents | 1614b27dcbfa |
children | b1f5f2a8840f |
comparison
equal
deleted
inserted
replaced
777:8009961db1cb | 778:9a828e5a2390 |
---|---|
44 | 44 |
45 /** | 45 /** |
46 * the logger, used to log exceptions and additonaly information | 46 * the logger, used to log exceptions and additonaly information |
47 */ | 47 */ |
48 private static Logger log = Logger.getLogger(LayerOutputState.class); | 48 private static Logger log = Logger.getLogger(LayerOutputState.class); |
49 | 49 |
50 /** | 50 /** |
51 * The UID of this Class. | 51 * The UID of this Class. |
52 */ | 52 */ |
53 private static final long serialVersionUID = 9180957321704424049L; | 53 private static final long serialVersionUID = 9180957321704424049L; |
54 | 54 |
55 // TODO: Replace | 55 // TODO: Replace |
56 public static final String LAYER_MODEL = "layer"; | 56 public static final String LAYER_MODEL = "layer"; |
57 | 57 |
58 /** | 58 /** |
59 * The ID for the Query fetching the Layer from the DB | 59 * The ID for the Query fetching the Layer from the DB |
60 */ | 60 */ |
61 private String dataQueryID = null; | 61 private String dataQueryID = null; |
62 | 62 |
63 /** | 63 /** |
64 * The ID for the Query fetching the Geometry from the DB | 64 * The ID for the Query fetching the Geometry from the DB |
65 * which should be used to Clip the Layerdata | 65 * which should be used to Clip the Layerdata |
66 */ | 66 */ |
67 private String geometryQueryID = null; | 67 private String geometryQueryID = null; |
68 | 68 |
69 private String columnQueryID = null; | 69 private String columnQueryID = null; |
70 | 70 |
71 /** | 71 /** |
72 * The ID for the Value which will hold the Geometrie-Value | 72 * The ID for the Value which will hold the Geometrie-Value |
73 */ | 73 */ |
74 private String geometryID = null; | 74 private String geometryID = null; |
75 | 75 |
76 private Boolean shapeFileLock = new Boolean(true); | 76 private Boolean shapeFileLock = new Boolean(true); |
77 | 77 |
78 private String shapeFilePath; | 78 private String shapeFilePath; |
79 | 79 |
80 private String geometryType = null; | 80 private String geometryType = null; |
81 | 81 |
82 private String templateID = null; | 82 private String templateID = null; |
83 | 83 |
84 public static final String SHAPEFILE_NAME = "data.shp"; | 84 public static final String SHAPEFILE_NAME = "data.shp"; |
85 | 85 |
86 /** | 86 /** |
87 * Constructor | 87 * Constructor |
88 */ | 88 */ |
89 public LayerOutputState() { | 89 public LayerOutputState() { |
90 super(); | 90 super(); |
91 } | 91 } |
92 | 92 |
93 /** | 93 /** |
94 * @see de.intevation.gnv.state.OutputState#out(org.w3c.dom.Document, | 94 * @see de.intevation.gnv.state.OutputState#out(org.w3c.dom.Document, |
95 * java.util.Collection, java.io.OutputStream, | 95 * java.util.Collection, java.io.OutputStream, |
96 * java.lang.String, de.intevation.artifacts.CallContext) | 96 * java.lang.String, de.intevation.artifacts.CallContext) |
97 */ | 97 */ |
98 public void out(Document format, Collection<InputData> inputData, | 98 public void out(Document format, Collection<InputData> inputData, |
99 OutputStream outputStream, String uuid, | 99 OutputStream outputStream, String uuid, |
100 CallContext callContext) throws StateException { | 100 CallContext callContext) throws StateException { |
101 | 101 |
102 log.debug("LayerOutputState.out"); | 102 log.debug("LayerOutputState.out"); |
103 String outputMode = XMLUtils.xpathString( | 103 String outputMode = XMLUtils.xpathString( |
104 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); | 104 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); |
105 if (outputMode.equalsIgnoreCase("wms")) { | 105 if (outputMode.equalsIgnoreCase("wms")) { |
106 Collection<Result> data = this.fetchData(); | 106 Collection<Result> data = this.fetchData(); |
115 if (data != null && !data.isEmpty()){ | 115 if (data != null && !data.isEmpty()){ |
116 this.writeZip(uuid, callContext, outputStream, data); | 116 this.writeZip(uuid, callContext, outputStream, data); |
117 }else{ | 117 }else{ |
118 this.writeExceptionReport2Stream(outputStream); | 118 this.writeExceptionReport2Stream(outputStream); |
119 } | 119 } |
120 | 120 |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 /** | 124 /** |
125 * @param outputStream | 125 * @param outputStream |
128 Document document = XMLUtils.newDocument(); | 128 Document document = XMLUtils.newDocument(); |
129 new ArtifactXMLUtilities(). | 129 new ArtifactXMLUtilities(). |
130 createExceptionReport("No Data to Export", document); | 130 createExceptionReport("No Data to Export", document); |
131 XMLUtils.toStream(document,outputStream); | 131 XMLUtils.toStream(document,outputStream); |
132 } | 132 } |
133 | 133 |
134 | 134 |
135 /** | 135 /** |
136 * Fetches the Data from the Databasebackend | 136 * Fetches the Data from the Databasebackend |
137 * @return | 137 * @return |
138 */ | 138 */ |
139 protected Collection<Result> fetchData(){ | 139 protected Collection<Result> fetchData(){ |
150 if (it.hasNext()){ | 150 if (it.hasNext()){ |
151 Result resultValue = it.next(); | 151 Result resultValue = it.next(); |
152 String table = resultValue.getString(0); | 152 String table = resultValue.getString(0); |
153 String where = resultValue.getString(1); | 153 String where = resultValue.getString(1); |
154 String columns = this.fetchColumns(table); | 154 String columns = this.fetchColumns(table); |
155 | 155 |
156 templateID = resultValue.getString(2); | 156 templateID = resultValue.getString(2); |
157 if (this.geometryID != null){ | 157 if (this.geometryID != null){ |
158 InputData geometryInputData = | 158 InputData geometryInputData = |
159 this.inputData.get(this.geometryID); | 159 this.inputData.get(this.geometryID); |
160 if (geometryInputData != null){ | 160 if (geometryInputData != null){ |
161 | 161 |
162 try { | 162 try { |
163 Collection<Result> geometryData = queryExecutor | 163 Collection<Result> geometryData = queryExecutor |
164 .executeQuery(this.geometryQueryID, | 164 .executeQuery(this.geometryQueryID, |
165 new String[]{geometryInputData.getValue()}); | 165 new String[]{geometryInputData.getValue()}); |
166 Iterator<Result> git = geometryData.iterator(); | 166 Iterator<Result> git = geometryData.iterator(); |
176 table, | 176 table, |
177 where, | 177 where, |
178 geometryWKT}; | 178 geometryWKT}; |
179 }else{ | 179 }else{ |
180 //Look into the presetting for an WKT | 180 //Look into the presetting for an WKT |
181 InputData geometryWKTData = this.preSettings != null ? | 181 InputData geometryWKTData = this.preSettings != null ? |
182 this.preSettings.get("geometry") : | 182 this.preSettings.get("geometry") : |
183 null ; | 183 null ; |
184 if (geometryWKTData != null){ | 184 if (geometryWKTData != null){ |
185 queryValues = new String[]{columns, | 185 queryValues = new String[]{columns, |
186 table, | 186 table, |
190 queryValues = new String[]{columns,table,where}; | 190 queryValues = new String[]{columns,table,where}; |
191 } | 191 } |
192 } | 192 } |
193 }else{ | 193 }else{ |
194 //Look into the presetting for an WKT | 194 //Look into the presetting for an WKT |
195 InputData geometryWKTData = this.preSettings != null ? | 195 InputData geometryWKTData = this.preSettings != null ? |
196 this.preSettings.get("geometry") : | 196 this.preSettings.get("geometry") : |
197 null ; | 197 null ; |
198 if (geometryWKTData != null){ | 198 if (geometryWKTData != null){ |
199 queryValues = new String[]{columns, | 199 queryValues = new String[]{columns, |
200 table, | 200 table, |
203 }else{ | 203 }else{ |
204 queryValues = new String[]{columns,table,where}; | 204 queryValues = new String[]{columns,table,where}; |
205 } | 205 } |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 try { | 209 try { |
210 data = queryExecutor.executeQuery(dataQueryID, | 210 data = queryExecutor.executeQuery(dataQueryID, |
211 queryValues); | 211 queryValues); |
212 if (data != null && geometryWKT != null){ | 212 if (data != null && geometryWKT != null){ |
213 WKTReader wktReader = new WKTReader(); | 213 WKTReader wktReader = new WKTReader(); |
214 Geometry border = wktReader.read(geometryWKT); | 214 Geometry border = wktReader.read(geometryWKT); |
215 | 215 |
216 Iterator<Result> dataIt = data.iterator(); | 216 Iterator<Result> dataIt = data.iterator(); |
217 while (dataIt.hasNext()){ | 217 while (dataIt.hasNext()){ |
218 // Trim the Geometries using the | 218 // Trim the Geometries using the |
219 // Geometry if on is available. | 219 // Geometry if on is available. |
220 Result current = dataIt.next(); | 220 Result current = dataIt.next(); |
224 currentGeometry = wktReader.read(currentWKT); | 224 currentGeometry = wktReader.read(currentWKT); |
225 } catch (Exception e) { | 225 } catch (Exception e) { |
226 log.error("Error parsing Geometry "+ currentWKT); | 226 log.error("Error parsing Geometry "+ currentWKT); |
227 log.error(e,e); | 227 log.error(e,e); |
228 } | 228 } |
229 | 229 |
230 if (currentGeometry != null){ | 230 if (currentGeometry != null){ |
231 Geometry newGeometry = currentGeometry.intersection(border); | 231 Geometry newGeometry = currentGeometry.intersection(border); |
232 current.addColumnValue(0, newGeometry.toText()); | 232 current.addColumnValue(0, newGeometry.toText()); |
233 } | 233 } |
234 } | 234 } |
248 String[] tables = tableName.toUpperCase().split(","); | 248 String[] tables = tableName.toUpperCase().split(","); |
249 String[] filter = tables[0].split("\\."); | 249 String[] filter = tables[0].split("\\."); |
250 // Only use the first Table the second one will be ignored. | 250 // Only use the first Table the second one will be ignored. |
251 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() | 251 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() |
252 .getQueryExecutor(); | 252 .getQueryExecutor(); |
253 | 253 |
254 Collection<Result> columnData = queryExecutor. | 254 Collection<Result> columnData = queryExecutor. |
255 executeQuery(this.columnQueryID, | 255 executeQuery(this.columnQueryID, |
256 filter); | 256 filter); |
257 if (columnData != null && !columnData.isEmpty()){ | 257 if (columnData != null && !columnData.isEmpty()){ |
258 StringBuffer sb = new StringBuffer(); | 258 StringBuffer sb = new StringBuffer(); |
259 synchronized (sb) { | 259 synchronized (sb) { |
260 Iterator<Result> it = columnData.iterator(); | 260 Iterator<Result> it = columnData.iterator(); |
266 } | 266 } |
267 } | 267 } |
268 } | 268 } |
269 returnValue = sb.toString(); | 269 returnValue = sb.toString(); |
270 } | 270 } |
271 | 271 |
272 } catch (QueryException e) { | 272 } catch (QueryException e) { |
273 log.error(e,e); | 273 log.error(e,e); |
274 } | 274 } |
275 return returnValue; | 275 return returnValue; |
276 } | 276 } |
282 "queryID-layerdata"); | 282 "queryID-layerdata"); |
283 this.geometryID = Config.getStringXPath(configuration, | 283 this.geometryID = Config.getStringXPath(configuration, |
284 "inputvalue-geometry"); | 284 "inputvalue-geometry"); |
285 this.geometryQueryID = Config.getStringXPath(configuration, | 285 this.geometryQueryID = Config.getStringXPath(configuration, |
286 "queryID-geometry"); | 286 "queryID-geometry"); |
287 | 287 |
288 this.columnQueryID = "layer_colums"; //Config.getStringXPath(configuration, | 288 this.columnQueryID = "layer_colums"; //Config.getStringXPath(configuration, |
289 // "queryID-columns"); | 289 // "queryID-columns"); |
290 } | 290 } |
291 | 291 |
292 protected String writeToShapeFile( | 292 protected String writeToShapeFile( |
293 String uuid, | 293 String uuid, |
294 Collection<Result> data, | 294 Collection<Result> data, |
295 CallContext callContext | 295 CallContext callContext |
296 ) { | 296 ) { |
297 File baseDir = shapefileDirectory(callContext); | 297 File baseDir = shapefileDirectory(callContext); |
298 | 298 |
299 File shapeDir = new File(baseDir, uuid); | 299 File shapeDir = new File(baseDir, uuid); |
300 boolean success = false; | 300 boolean success = false; |
301 boolean createdDir = false; | 301 boolean createdDir = false; |
302 | 302 |
303 try { | 303 try { |
307 shapeDir = new File(baseDir, uuid + "-" + count); | 307 shapeDir = new File(baseDir, uuid + "-" + count); |
308 ++count; | 308 ++count; |
309 } | 309 } |
310 | 310 |
311 if (!shapeDir.mkdirs()) { | 311 if (!shapeDir.mkdirs()) { |
312 log.error("cannot create directory '" | 312 log.error("cannot create directory '" |
313 + shapeDir.getAbsolutePath() + "'"); | 313 + shapeDir.getAbsolutePath() + "'"); |
314 return null; | 314 return null; |
315 } | 315 } |
316 createdDir = true; | 316 createdDir = true; |
317 } | 317 } |
319 File shapeFile = new File(shapeDir, SHAPEFILE_NAME); | 319 File shapeFile = new File(shapeDir, SHAPEFILE_NAME); |
320 if ((geometryType = ShapeFileWriter.writeDataToFile(shapeFile, "data", data)) == null){ | 320 if ((geometryType = ShapeFileWriter.writeDataToFile(shapeFile, "data", data)) == null){ |
321 log.error("writing data into shapefile failed"); | 321 log.error("writing data into shapefile failed"); |
322 return null; | 322 return null; |
323 } | 323 } |
324 | 324 |
325 shapeFilePath = shapeDir.getAbsolutePath(); | 325 shapeFilePath = shapeDir.getAbsolutePath(); |
326 success = true; | 326 success = true; |
327 | 327 |
328 callContext.afterCall(CallContext.STORE); | 328 callContext.afterCall(CallContext.STORE); |
329 | 329 |
333 if (!success && createdDir) { | 333 if (!success && createdDir) { |
334 FileUtils.deleteRecursive(shapeDir); | 334 FileUtils.deleteRecursive(shapeDir); |
335 } | 335 } |
336 } | 336 } |
337 } | 337 } |
338 | 338 |
339 protected void writeZip( | 339 protected void writeZip( |
340 String uuid, | 340 String uuid, |
341 CallContext callContext, | 341 CallContext callContext, |
342 OutputStream output, | 342 OutputStream output, |
343 Collection<Result> data | 343 Collection<Result> data |
344 ) | 344 ) |
345 throws StateException | 345 throws StateException |
346 { | 346 { |
347 try { | 347 try { |
348 String p = getShapeFilePath(); | 348 String p = getShapeFilePath(); |
349 if (p != null) { | 349 if (p != null) { |
351 if (dir.isDirectory()) { | 351 if (dir.isDirectory()) { |
352 FileUtils.createZipArchive(dir, output); | 352 FileUtils.createZipArchive(dir, output); |
353 } | 353 } |
354 } | 354 } |
355 else { | 355 else { |
356 | 356 |
357 if ((p = writeToShapeFile(uuid, data, callContext)) != null) { | 357 if ((p = writeToShapeFile(uuid, data, callContext)) != null) { |
358 FileUtils.createZipArchive(new File(p), output); | 358 FileUtils.createZipArchive(new File(p), output); |
359 } | 359 } |
360 } | 360 } |
361 } | 361 } |
362 catch (IOException ioe) { | 362 catch (IOException ioe) { |
363 log.error(ioe.getLocalizedMessage(), ioe); | 363 log.error(ioe.getLocalizedMessage(), ioe); |
364 } | 364 } |
365 } | 365 } |
366 | 366 |
367 public String getShapeFilePath() { | 367 public String getShapeFilePath() { |
368 synchronized (shapeFileLock) { | 368 synchronized (shapeFileLock) { |
369 return shapeFilePath; | 369 return shapeFilePath; |
370 } | 370 } |
371 } | 371 } |
372 | 372 |
373 private static File shapefileDirectory(CallContext callContext) { | 373 private static File shapefileDirectory(CallContext callContext) { |
374 // TODO: Refactoring nessessary it should be used only one Shapefilepath | 374 // TODO: Refactoring nessessary it should be used only one Shapefilepath |
375 // for alle Modes. Code was taken from HorizontalCrossSectionMeshOutputState | 375 // for alle Modes. Code was taken from HorizontalCrossSectionMeshOutputState |
376 GNVArtifactContext context = | 376 GNVArtifactContext context = |
377 (GNVArtifactContext)callContext.globalContext(); | 377 (GNVArtifactContext)callContext.globalContext(); |
379 GNVArtifactContext.HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH_KEY); | 379 GNVArtifactContext.HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH_KEY); |
380 return dir != null | 380 return dir != null |
381 ? dir | 381 ? dir |
382 : GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_PROFILE_SHAPEFILE_PATH; | 382 : GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_PROFILE_SHAPEFILE_PATH; |
383 } | 383 } |
384 | 384 |
385 /** | 385 /** |
386 * @see de.intevation.gnv.state.StateBase#endOfLife(java.lang.Object) | 386 * @see de.intevation.gnv.state.StateBase#endOfLife(java.lang.Object) |
387 */ | 387 */ |
388 @Override | 388 @Override |
389 public void endOfLife(Object globalContext) { | 389 public void endOfLife(Object globalContext) { |
416 | 416 |
417 log.error("failed to remove directory '" + path + "'"); | 417 log.error("failed to remove directory '" + path + "'"); |
418 } // run | 418 } // run |
419 }.start(); | 419 }.start(); |
420 } | 420 } |
421 | 421 |
422 public String resetShapeFilePath() { | 422 public String resetShapeFilePath() { |
423 synchronized (shapeFileLock) { | 423 synchronized (shapeFileLock) { |
424 String path = shapeFilePath; | 424 String path = shapeFilePath; |
425 shapeFilePath = null; | 425 shapeFilePath = null; |
426 geometryType = null; | 426 geometryType = null; |
427 templateID = null; | 427 templateID = null; |
428 return path; | 428 return path; |
429 } | 429 } |
430 } | 430 } |
431 protected Document getWMS(String uuid, | 431 protected Document getWMS(String uuid, |
432 CallContext callContext, | 432 CallContext callContext, |
433 Collection<Result> data) | 433 Collection<Result> data) |
434 throws StateException | 434 throws StateException |
435 { | 435 { |
436 // TODO: Do the real WMS publishing here! | 436 // TODO: Do the real WMS publishing here! |
437 Document document = XMLUtils.newDocument(); | 437 Document document = XMLUtils.newDocument(); |
438 | 438 |
443 | 443 |
444 if (path != null && new File(path).isDirectory()) { | 444 if (path != null && new File(path).isDirectory()) { |
445 pathElement.setTextContent(path); | 445 pathElement.setTextContent(path); |
446 } | 446 } |
447 else { | 447 else { |
448 | 448 |
449 if (data != null && | 449 if (data != null && |
450 (path = writeToShapeFile(uuid, data, callContext)) != null) { | 450 (path = writeToShapeFile(uuid, data, callContext)) != null) { |
451 | 451 |
452 String paramType = LAYER_MODEL+"_"+templateID; | 452 String paramType = LAYER_MODEL+"_"+templateID; |
453 | 453 |
454 if (!MapfileGenerator.getInstance().templateExists(paramType)){ | 454 if (!MapfileGenerator.getInstance().templateExists(paramType)){ |
455 // If the template doesn't exist the Defaulttemplates will be used. | 455 // If the template doesn't exist the Defaulttemplates will be used. |
456 paramType = LAYER_MODEL+"_"+this.geometryType.toLowerCase(); | 456 paramType = LAYER_MODEL+"_"+this.geometryType.toLowerCase(); |
457 } | 457 } |
458 Document meta = MetaWriter.writeLayerMeta(callContext, uuid, | 458 Document meta = MetaWriter.writeLayerMeta(callContext, uuid, |
459 path, paramType, | 459 path, paramType, |
460 this.determineGeometryType()); | 460 this.determineGeometryType()); |
461 if (meta != null) { | 461 if (meta != null) { |
462 MapfileGenerator.getInstance().update(); | 462 MapfileGenerator.getInstance().update(); |
463 return meta; | 463 return meta; |
464 } | 464 } |
467 } | 467 } |
468 } | 468 } |
469 | 469 |
470 return document; | 470 return document; |
471 } | 471 } |
472 | 472 |
473 private String determineGeometryType(){ | 473 private String determineGeometryType(){ |
474 | 474 |
475 String returnValue = this.geometryType.toLowerCase(); | 475 String returnValue = this.geometryType.toLowerCase(); |
476 | 476 |
477 if (returnValue.equalsIgnoreCase("linestring")){ | 477 if (returnValue.equalsIgnoreCase("linestring")){ |
478 returnValue = "Line"; | 478 returnValue = "Line"; |
479 } | 479 } |
480 return returnValue; | 480 return returnValue; |
481 } | 481 } |