Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java @ 859:3fbabd4803d7
ISSUE252 Make it possible to export more than one Layer
gnv-artifacts/trunk@983 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Mon, 26 Apr 2010 09:09:20 +0000 |
parents | 164d102b0af5 |
children | dfd02f8d3602 |
comparison
equal
deleted
inserted
replaced
858:ec2265bb85a1 | 859:3fbabd4803d7 |
---|---|
1 package de.intevation.gnv.state.layer; | 1 package de.intevation.gnv.state.layer; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.io.OutputStream; | 5 import java.io.OutputStream; |
6 import java.util.ArrayList; | |
6 import java.util.Collection; | 7 import java.util.Collection; |
7 import java.util.Iterator; | 8 import java.util.Iterator; |
8 | 9 |
9 import org.apache.log4j.Logger; | 10 import org.apache.log4j.Logger; |
10 import org.w3c.dom.Document; | 11 import org.w3c.dom.Document; |
11 import org.w3c.dom.Element; | |
12 import org.w3c.dom.Node; | 12 import org.w3c.dom.Node; |
13 | 13 |
14 import com.vividsolutions.jts.geom.Geometry; | 14 import com.vividsolutions.jts.geom.Geometry; |
15 import com.vividsolutions.jts.io.ParseException; | 15 import com.vividsolutions.jts.io.ParseException; |
16 import com.vividsolutions.jts.io.WKTReader; | 16 import com.vividsolutions.jts.io.WKTReader; |
57 public static final String LAYER_MODEL = "layer"; | 57 public static final String LAYER_MODEL = "layer"; |
58 | 58 |
59 /** | 59 /** |
60 * The Name of the Shapefile which will be generated. | 60 * The Name of the Shapefile which will be generated. |
61 */ | 61 */ |
62 public static final String SHAPEFILE_NAME = "data.shp"; | 62 public static final String SHAPEFILE_NAME = "data"; |
63 | 63 |
64 /** | 64 /** |
65 * The ID for the Query fetching the Layer from the DB | 65 * The ID for the Query fetching the Layer from the DB |
66 */ | 66 */ |
67 private String dataQueryID = null; | 67 private String dataQueryID = null; |
96 | 96 |
97 /** | 97 /** |
98 * The Path where the Shapefile is stored. | 98 * The Path where the Shapefile is stored. |
99 */ | 99 */ |
100 private String shapeFilePath; | 100 private String shapeFilePath; |
101 | |
102 /** | |
103 * The ID of the Template which should be used to generate the Layerentry | |
104 * in the Mapfile. | |
105 */ | |
106 private String templateID = null; | |
107 | |
108 /** | |
109 * The Kind of Geometry which should be used to generate the Shapefile. | |
110 */ | |
111 private String geometryType = null; | |
112 | 101 |
113 /** | 102 /** |
114 * Constructor | 103 * Constructor |
115 */ | 104 */ |
116 public LayerOutputState() { | 105 public LayerOutputState() { |
118 } | 107 } |
119 | 108 |
120 public void out(Document format, Collection<InputData> inputData, | 109 public void out(Document format, Collection<InputData> inputData, |
121 OutputStream outputStream, String uuid, | 110 OutputStream outputStream, String uuid, |
122 CallContext callContext) throws StateException { | 111 CallContext callContext) throws StateException { |
123 | |
124 log.debug("LayerOutputState.out"); | 112 log.debug("LayerOutputState.out"); |
125 String outputMode = XMLUtils.xpathString( | 113 String outputMode = XMLUtils.xpathString( |
126 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); | 114 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); |
127 if (outputMode.equalsIgnoreCase("wms")) { | 115 if (outputMode.equalsIgnoreCase("wms")) { |
128 Collection<Result> data = this.fetchData(); | 116 |
129 if (data != null && !data.isEmpty()){ | 117 Collection<LayerMetaData> layerMetaData = |
118 this.getRequestedLayerMetadata(); | |
119 if (layerMetaData != null && !layerMetaData.isEmpty()){ | |
130 XMLUtils.toStream(this.getWMS(uuid, callContext, | 120 XMLUtils.toStream(this.getWMS(uuid, callContext, |
131 data, geometryType, | 121 layerMetaData,inputData), |
132 inputData), | |
133 outputStream); | 122 outputStream); |
134 }else{ | 123 }else{ |
135 this.writeExceptionReport2Stream(outputStream); | 124 this.writeExceptionReport2Stream(outputStream); |
136 } | 125 } |
137 }else if (outputMode.equalsIgnoreCase("zip")){ | 126 }else if (outputMode.equalsIgnoreCase("zip")){ |
138 Collection<Result> data = this.fetchData(); | 127 Collection<LayerMetaData> layerMetaData = |
139 if (data != null && !data.isEmpty()){ | 128 this.getRequestedLayerMetadata(); |
140 this.writeZip(uuid, callContext, outputStream, | 129 |
141 data,geometryType); | 130 if (layerMetaData != null && !layerMetaData.isEmpty()){ |
131 this.writeZip(uuid, callContext, | |
132 outputStream, layerMetaData); | |
142 }else{ | 133 }else{ |
143 this.writeExceptionReport2Stream(outputStream); | 134 this.writeExceptionReport2Stream(outputStream); |
144 } | 135 } |
145 | 136 |
146 } | 137 } |
156 ArtifactXMLUtilities. | 147 ArtifactXMLUtilities. |
157 createExceptionReport("No Data to Export", document); | 148 createExceptionReport("No Data to Export", document); |
158 XMLUtils.toStream(document,outputStream); | 149 XMLUtils.toStream(document,outputStream); |
159 } | 150 } |
160 | 151 |
161 | 152 /** |
162 /** | 153 * Returns the Metadata for the requested Layers for fetching the data |
163 * Fetches the Data from the Databasebackend. | 154 * of the Layer and generating the Layer and WMS. |
164 * | 155 * @return the Metadata for the requested Layers |
165 * @return the resultdata. | 156 */ |
166 */ | 157 private Collection<LayerMetaData> getRequestedLayerMetadata(){ |
167 protected Collection<Result> fetchData(){ | 158 log.debug("LayerOutputState.getRequestedLayerMetadata"); |
168 log.debug("LayerOutputState.fetchData"); | |
169 Collection<Result> result = this.getData(this.queryID); | 159 Collection<Result> result = this.getData(this.queryID); |
170 Collection<Result> data = null; | 160 Collection<LayerMetaData> returnValue = null; |
171 String geometryWKT = null; | |
172 if (result != null){ | 161 if (result != null){ |
173 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() | 162 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() |
174 .getQueryExecutor(); | 163 .getQueryExecutor(); |
175 Iterator<Result> it = result.iterator(); | 164 Iterator<Result> it = result.iterator(); |
176 String[] queryValues = null; | 165 returnValue = new ArrayList<LayerMetaData>(result.size()); |
177 if (it.hasNext()){ | 166 while (it.hasNext()){ |
178 Result resultValue = it.next(); | 167 Result resultValue = it.next(); |
179 String table = resultValue.getString(0); | 168 String table = resultValue.getString(0); |
180 | 169 String geometryType = this.getGeometryType(table, queryExecutor); |
181 this.geometryType = this.getGeometryType(table, queryExecutor); | |
182 | |
183 String where = resultValue.getString(1); | 170 String where = resultValue.getString(1); |
184 String columns = this.fetchColumns(table); | 171 String columns = this.fetchColumns(table); |
185 | 172 String templateID = resultValue.getString(2); |
186 templateID = resultValue.getString(2); | 173 String[] queryValues = null; |
174 String geometryWKT = null; | |
187 if (this.geometryID != null){ | 175 if (this.geometryID != null){ |
188 InputData geometryInputData = | 176 InputData geometryInputData = |
189 this.inputData.get(this.geometryID); | 177 this.inputData.get(this.geometryID); |
190 if (geometryInputData != null){ | 178 if (geometryInputData != null){ |
191 | |
192 try { | 179 try { |
180 | |
193 Collection<Result> geometryData = queryExecutor | 181 Collection<Result> geometryData = queryExecutor |
194 .executeQuery(this.geometryQueryID, | 182 .executeQuery(this.geometryQueryID, |
195 new String[]{geometryInputData.getValue()}); | 183 new String[]{geometryInputData.getValue()}); |
196 Iterator<Result> git = geometryData.iterator(); | 184 Iterator<Result> git = geometryData.iterator(); |
197 if (git.hasNext()){ | 185 if (git.hasNext()){ |
231 geometryWKTData.getValue()}; | 219 geometryWKTData.getValue()}; |
232 }else{ | 220 }else{ |
233 queryValues = new String[]{columns,table,where}; | 221 queryValues = new String[]{columns,table,where}; |
234 } | 222 } |
235 } | 223 } |
236 } | 224 returnValue.add(new LayerMetaData(table, geometryType, |
237 | 225 where, columns, |
238 try { | 226 templateID, queryValues, |
239 data = queryExecutor.executeQuery(dataQueryID, | 227 geometryWKT)); |
240 queryValues); | 228 } |
241 if (data != null && geometryWKT != null){ | 229 } |
242 WKTReader wktReader = new WKTReader(); | 230 return returnValue; |
243 Geometry border = wktReader.read(geometryWKT); | 231 } |
244 | 232 |
245 Iterator<Result> dataIt = data.iterator(); | 233 /** |
246 while (dataIt.hasNext()){ | 234 * Fetches the Data from the Databasebackend. |
247 // Trim the Geometries using the | 235 * |
248 // Geometry if on is available. | 236 * @return the resultdata. |
249 Result current = dataIt.next(); | 237 */ |
250 String currentWKT = current.getString(0); | 238 protected Collection<Result> fetchData(LayerMetaData layerMetaData){ |
251 Geometry currentGeometry = null; | 239 log.debug("LayerOutputState.fetchData"); |
252 try { | 240 Collection<Result> data = null; |
253 currentGeometry = wktReader.read(currentWKT); | 241 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() |
254 } catch (Exception e) { | 242 .getQueryExecutor(); |
255 log.error("Error parsing Geometry "+ currentWKT); | 243 try { |
256 log.error(e,e); | 244 data = queryExecutor.executeQuery(dataQueryID, |
257 } | 245 layerMetaData.getQueryValues()); |
258 | 246 if (data != null && layerMetaData.getGeometryWKT() != null){ |
259 if (currentGeometry != null){ | 247 WKTReader wktReader = new WKTReader(); |
260 Geometry newGeometry = currentGeometry.intersection(border); | 248 Geometry border = wktReader.read(layerMetaData.getGeometryWKT()); |
261 current.addColumnValue(0, newGeometry.toText()); | 249 Iterator<Result> dataIt = data.iterator(); |
262 } | 250 while (dataIt.hasNext()){ |
263 } | 251 // Trim the Geometries using the |
264 } | 252 // Geometry if one is available. |
265 } catch (QueryException e) { | 253 Result current = dataIt.next(); |
266 log.error(e,e); | 254 String currentWKT = current.getString(0); |
267 } catch (ParseException e){ | 255 Geometry currentGeometry = null; |
268 log.error(e,e); | 256 try { |
269 } | 257 currentGeometry = wktReader.read(currentWKT); |
258 } catch (Exception e) { | |
259 log.error("Error parsing Geometry "+ currentWKT); | |
260 log.error(e,e); | |
261 } | |
262 if (currentGeometry != null){ | |
263 Geometry newGeometry = currentGeometry.intersection(border); | |
264 current.addColumnValue(0, newGeometry.toText()); | |
265 } | |
266 } | |
267 } | |
268 } catch (QueryException e) { | |
269 log.error(e,e); | |
270 } catch (ParseException e){ | |
271 log.error(e,e); | |
270 } | 272 } |
271 return data; | 273 return data; |
272 } | 274 } |
273 | 275 |
274 | 276 |
282 private String getGeometryType(String tableName, | 284 private String getGeometryType(String tableName, |
283 QueryExecutor queryExecutor){ | 285 QueryExecutor queryExecutor){ |
284 String returnValue = null; | 286 String returnValue = null; |
285 String[] tables = tableName.toUpperCase().split(","); | 287 String[] tables = tableName.toUpperCase().split(","); |
286 String[] filter = tables[0].split("\\."); | 288 String[] filter = tables[0].split("\\."); |
287 | |
288 try { | 289 try { |
289 Collection<Result> result = | 290 Collection<Result> result = |
290 queryExecutor.executeQuery(this.geometryTypeQueryID, filter); | 291 queryExecutor.executeQuery(this.geometryTypeQueryID, filter); |
291 if (result != null && !result.isEmpty()) | 292 if (result != null && !result.isEmpty()) |
292 { | 293 { |
314 } | 315 } |
315 } | 316 } |
316 } catch (QueryException e) { | 317 } catch (QueryException e) { |
317 log.error(e,e); | 318 log.error(e,e); |
318 } | 319 } |
319 | |
320 return returnValue; | 320 return returnValue; |
321 } | 321 } |
322 | 322 |
323 | 323 |
324 /** | 324 /** |
332 try { | 332 try { |
333 String[] tables = tableName.toUpperCase().split(","); | 333 String[] tables = tableName.toUpperCase().split(","); |
334 String[] filter = tables[0].split("\\."); | 334 String[] filter = tables[0].split("\\."); |
335 // Only use the first Table the second one will be ignored. | 335 // Only use the first Table the second one will be ignored. |
336 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() | 336 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance() |
337 .getQueryExecutor(); | 337 .getQueryExecutor(); |
338 | |
339 Collection<Result> columnData = queryExecutor. | 338 Collection<Result> columnData = queryExecutor. |
340 executeQuery(this.columnQueryID, | 339 executeQuery(this.columnQueryID, |
341 filter); | 340 filter); |
342 if (columnData != null && !columnData.isEmpty()){ | 341 if (columnData != null && !columnData.isEmpty()){ |
343 StringBuffer sb = new StringBuffer(); | 342 StringBuffer sb = new StringBuffer(); |
351 } | 350 } |
352 } | 351 } |
353 } | 352 } |
354 returnValue = sb.toString(); | 353 returnValue = sb.toString(); |
355 } | 354 } |
356 | |
357 } catch (QueryException e) { | 355 } catch (QueryException e) { |
358 log.error(e,e); | 356 log.error(e,e); |
359 } | 357 } |
360 return returnValue; | 358 return returnValue; |
361 } | 359 } |
389 */ | 387 */ |
390 protected String writeToShapeFile( | 388 protected String writeToShapeFile( |
391 String uuid, | 389 String uuid, |
392 Collection<Result> data, | 390 Collection<Result> data, |
393 CallContext callContext, | 391 CallContext callContext, |
394 String geometryType | 392 String geometryType, |
393 int layerNumber | |
395 ) { | 394 ) { |
396 File baseDir = shapefileDirectory(callContext); | 395 boolean success = false; |
397 | 396 if (data != null && !data.isEmpty()){ |
397 File shapeDir = new File(shapeFilePath); | |
398 try { | |
399 File shapeFile = new File(shapeDir, createShapeFileName(layerNumber)); | |
400 if (!ShapeFileWriter.writeDataToFile(shapeFile, "data", data,geometryType)){ | |
401 log.error("writing data into shapefile failed"); | |
402 return null; | |
403 } | |
404 success = true; | |
405 callContext.afterCall(CallContext.STORE); | |
406 return shapeFilePath; | |
407 } | |
408 finally { | |
409 if (!success) { | |
410 FileUtils.deleteRecursive(shapeDir); | |
411 } | |
412 } | |
413 }else{ | |
414 return null; | |
415 } | |
416 } | |
417 | |
418 /** | |
419 * Check if the ShapeDir exists and if it exists delete all Contents | |
420 * in it. If it not exists the Director will be created. | |
421 * @param baseDir the BaseDirectory for all ShapeDirs | |
422 * @param uuid the UUID which is used to create the Directory | |
423 * @return true if the directory exists or could be created. | |
424 * false if the directory could not be created. | |
425 */ | |
426 private boolean createShapeDir(File baseDir, String uuid){ | |
398 File shapeDir = new File(baseDir, uuid); | 427 File shapeDir = new File(baseDir, uuid); |
399 boolean success = false; | |
400 boolean createdDir = false; | 428 boolean createdDir = false; |
401 | 429 synchronized (shapeFileLock) { |
402 try { | 430 if (shapeDir.exists()) { |
403 synchronized (shapeFileLock) { | 431 FileUtils.deleteContent(shapeDir); // TODO Place on getZip and getWMS |
404 if (shapeDir.exists()) { | 432 } |
405 FileUtils.deleteContent(shapeDir); | 433 else if (!shapeDir.mkdirs()) { |
406 } | 434 log.error("cannot create directory '" |
407 else if (!shapeDir.mkdirs()) { | 435 + shapeDir.getAbsolutePath() + "'"); |
408 log.error("cannot create directory '" | 436 return false; |
409 + shapeDir.getAbsolutePath() + "'"); | 437 } |
410 return null; | 438 createdDir = true; |
411 } | 439 } |
412 createdDir = true; | 440 shapeFilePath = shapeDir.getAbsolutePath(); |
413 } | 441 return createdDir; |
414 | 442 } |
415 File shapeFile = new File(shapeDir, SHAPEFILE_NAME); | |
416 if (!ShapeFileWriter.writeDataToFile(shapeFile, "data", data,geometryType)){ | |
417 log.error("writing data into shapefile failed"); | |
418 return null; | |
419 } | |
420 | |
421 shapeFilePath = shapeDir.getAbsolutePath(); | |
422 success = true; | |
423 | |
424 callContext.afterCall(CallContext.STORE); | |
425 | |
426 return shapeFilePath; | |
427 } | |
428 finally { | |
429 if (!success && createdDir) { | |
430 FileUtils.deleteRecursive(shapeDir); | |
431 } | |
432 } | |
433 } | |
434 | |
435 | 443 |
436 /** | 444 /** |
437 * Create a zip archive with the shapefiles of the given shapefiles path and | 445 * Create a zip archive with the shapefiles of the given shapefiles path and |
438 * write it to <code>output</code>. | 446 * write it to <code>output</code>. |
439 * | 447 * |
446 */ | 454 */ |
447 protected void writeZip( | 455 protected void writeZip( |
448 String uuid, | 456 String uuid, |
449 CallContext callContext, | 457 CallContext callContext, |
450 OutputStream output, | 458 OutputStream output, |
451 Collection<Result> data, | 459 Collection<LayerMetaData> layerMetaData |
452 String geometryType | |
453 ) | 460 ) |
454 throws StateException | 461 throws StateException |
455 { | 462 { |
456 try { | 463 try { |
457 String p = getShapeFilePath(); | 464 String p = getShapeFilePath(); |
460 if (dir.isDirectory()) { | 467 if (dir.isDirectory()) { |
461 FileUtils.createZipArchive(dir, output); | 468 FileUtils.createZipArchive(dir, output); |
462 } | 469 } |
463 } | 470 } |
464 else { | 471 else { |
465 | 472 File baseDir = shapefileDirectory(callContext); |
466 if ((p = writeToShapeFile(uuid, data, callContext,geometryType)) != null) { | 473 if (!this.createShapeDir(baseDir, uuid)){ |
474 return; | |
475 } | |
476 Iterator<LayerMetaData> it = layerMetaData.iterator(); | |
477 int i = 1; | |
478 while(it.hasNext()){ | |
479 LayerMetaData lmd = it.next(); | |
480 Collection<Result> data = this.fetchData(lmd); | |
481 p = writeToShapeFile(uuid, data, callContext,lmd.getGeometryType(),i++); | |
482 } | |
483 if (p != null) { | |
467 FileUtils.createZipArchive(new File(p), output); | 484 FileUtils.createZipArchive(new File(p), output); |
468 } | 485 } |
469 } | 486 } |
470 } | 487 } |
471 catch (IOException ioe) { | 488 catch (IOException ioe) { |
482 synchronized (shapeFileLock) { | 499 synchronized (shapeFileLock) { |
483 return shapeFilePath; | 500 return shapeFilePath; |
484 } | 501 } |
485 } | 502 } |
486 | 503 |
487 | 504 /** |
505 * Returns the basic-directory where the Shapefiles should be placed in. | |
506 * @param callContext the Context of this Call | |
507 * @return the Directory where the Shapefiles could be placed in. | |
508 * (Please create an own directory in this dir and not put the | |
509 * Files directly in it) | |
510 */ | |
488 private static File shapefileDirectory(CallContext callContext) { | 511 private static File shapefileDirectory(CallContext callContext) { |
489 // TODO: Refactoring nessessary it should be used only one Shapefilepath | 512 // Code was taken from HorizontalCrossSectionMeshOutputState |
490 // for alle Modes. Code was taken from HorizontalCrossSectionMeshOutputState | |
491 GNVArtifactContext context = | 513 GNVArtifactContext context = |
492 (GNVArtifactContext)callContext.globalContext(); | 514 (GNVArtifactContext)callContext.globalContext(); |
493 File dir = (File)context.get( | 515 File dir = (File)context.get( |
494 GNVArtifactContext.HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH_KEY); | 516 GNVArtifactContext.HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH_KEY); |
495 return dir != null | 517 return dir != null |
499 | 521 |
500 | 522 |
501 @Override | 523 @Override |
502 public void endOfLife(Object globalContext) { | 524 public void endOfLife(Object globalContext) { |
503 super.endOfLife(globalContext); | 525 super.endOfLife(globalContext); |
504 | |
505 // do it in background | 526 // do it in background |
506 new Thread() { | 527 new Thread() { |
507 @Override | 528 @Override |
508 public void run() { | 529 public void run() { |
509 String path = resetShapeFilePath(); | 530 String path = resetShapeFilePath(); |
510 | |
511 if (path == null) { | 531 if (path == null) { |
512 return; | 532 return; |
513 } | 533 } |
514 | |
515 File dir = new File(path); | 534 File dir = new File(path); |
516 | |
517 for (int i = 0; i < 10; ++i) { | 535 for (int i = 0; i < 10; ++i) { |
518 if (!dir.exists() || FileUtils.deleteRecursive(dir)) { | 536 if (!dir.exists() || FileUtils.deleteRecursive(dir)) { |
519 MapfileGenerator.getInstance().update(); | 537 MapfileGenerator.getInstance().update(); |
520 return; | 538 return; |
521 } | 539 } |
522 | |
523 try { | 540 try { |
524 Thread.sleep(10000L); | 541 Thread.sleep(10000L); |
525 } | 542 } |
526 catch (InterruptedException ie) { | 543 catch (InterruptedException ie) { |
527 } | 544 } |
538 */ | 555 */ |
539 private String resetShapeFilePath() { | 556 private String resetShapeFilePath() { |
540 synchronized (shapeFileLock) { | 557 synchronized (shapeFileLock) { |
541 String path = shapeFilePath; | 558 String path = shapeFilePath; |
542 shapeFilePath = null; | 559 shapeFilePath = null; |
543 templateID = null; | |
544 return path; | 560 return path; |
545 } | 561 } |
546 } | 562 } |
547 | 563 |
548 | 564 |
551 * these shapefiles. The map service can be queried for displaying | 567 * these shapefiles. The map service can be queried for displaying |
552 * corresponding layers as WMS. | 568 * corresponding layers as WMS. |
553 * | 569 * |
554 * @param uuid The UUID of the current artifact. | 570 * @param uuid The UUID of the current artifact. |
555 * @param callContext The CallContext object. | 571 * @param callContext The CallContext object. |
556 * @param data A collection with some input data. | 572 * @param layerMetaData The Metadata which is required to create the |
557 * @param geometryType The geometry type. | 573 * different Layers. |
558 * @param inputData the Parameters which are send by the out-Call. | 574 * @param inputData the Parameters which are send by the out-Call. |
559 * @return a document with some meta information (shapefile path, geometry | 575 * @return a document with some meta information (shapefile path, geometry |
560 * type, time to live of the current artifact, etc). | 576 * type, time to live of the current artifact, etc). |
561 * @throws StateException if an error occured while shapefile writing. | 577 * @throws StateException if an error occured while shapefile writing. |
562 */ | 578 */ |
563 protected Document getWMS(String uuid, | 579 protected Document getWMS(String uuid, |
564 CallContext callContext, | 580 CallContext callContext, |
565 Collection<Result> data, | 581 Collection<LayerMetaData> layerMetaData, |
566 String geometryType, | |
567 Collection<InputData> inputData) | 582 Collection<InputData> inputData) |
568 throws StateException | 583 throws StateException |
569 { | 584 { |
585 String path = getShapeFilePath(); | |
586 if (path != null && new File(path).isDirectory()){ | |
587 return this.refreshMetaFile(layerMetaData, inputData, | |
588 uuid, callContext); | |
589 }else{ | |
590 Document document = XMLUtils.newDocument(); | |
591 if (this.shapeFilePath == null){ | |
592 File baseDir = shapefileDirectory(callContext); | |
593 if (!this.createShapeDir(baseDir, uuid)){ | |
594 // TODO Insert Error Report | |
595 return document; | |
596 } | |
597 } | |
598 path = getShapeFilePath(); | |
599 Iterator<LayerMetaData> it = layerMetaData.iterator(); | |
600 Node meta = null; | |
601 int layerNumber = 0; | |
602 while (it.hasNext()){ | |
603 LayerMetaData lmd = it.next(); | |
604 layerNumber ++; | |
605 String geometryType = lmd.getGeometryType(); | |
606 String templateId = lmd.getTemplateID(); | |
607 ExclusiveExec.UniqueKey key = ExclusiveExec.INSTANCE.acquire(uuid); | |
608 try{ | |
609 Collection<Result> data = this.fetchData(lmd); | |
610 if (data != null && | |
611 (this.writeToShapeFile(uuid, data, callContext, | |
612 geometryType,layerNumber)) != null) { | |
613 String paramType = findParameterTitle(geometryType,templateId); | |
614 String title = getLayerTitle(inputData); | |
615 if (title == null) { | |
616 title = uuid+"_"+layerNumber; | |
617 }else{ | |
618 title = title+"_"+layerNumber; | |
619 } | |
620 if (meta == null){ | |
621 meta = MetaWriter.writeLayerMeta(callContext,document); | |
622 } | |
623 MetaWriter.writeLayerMeta(callContext, document, | |
624 meta, uuid, paramType, | |
625 this.determineGeometryType(geometryType), | |
626 createShapeFileName(layerNumber), | |
627 title); | |
628 } | |
629 if (meta != null && !it.hasNext()) { | |
630 MetaWriter.writeMetaFile(path,document); | |
631 MapfileGenerator.getInstance().update(); | |
632 return document; | |
633 } | |
634 }finally{ | |
635 ExclusiveExec.INSTANCE.release(key); | |
636 } | |
637 } | |
638 return document; | |
639 } | |
640 } | |
641 | |
642 /** | |
643 * Creates the name of the Shapefile | |
644 * @param layerNumber the Number of the Layer | |
645 * @return the create name of the Shapefile. | |
646 */ | |
647 private String createShapeFileName(int layerNumber) { | |
648 return SHAPEFILE_NAME+"_"+layerNumber+".shp"; | |
649 } | |
650 | |
651 /** | |
652 * Method that refreshes the Metadatafile for publishing the WMS | |
653 * Without generating the Data ones again. | |
654 * @param layerMetaData the Metadata which is required to create the Layers | |
655 * @param inputData the Inputdata which was sent by the Client | |
656 * @param uuid the uuid of the Artifact | |
657 * @param callContext the context of this Call | |
658 * @return a refreshed Metadata-Document | |
659 */ | |
660 private Document refreshMetaFile(Collection<LayerMetaData> layerMetaData, | |
661 Collection<InputData> inputData, | |
662 String uuid, | |
663 CallContext callContext){ | |
570 Document document = XMLUtils.newDocument(); | 664 Document document = XMLUtils.newDocument(); |
571 | 665 Node meta = null; |
572 Element pathElement = document.createElement("path"); | 666 int layerNumber = 0; |
573 document.appendChild(pathElement); | 667 Iterator<LayerMetaData> it = layerMetaData.iterator(); |
574 | 668 while (it.hasNext()){ |
575 String path = getShapeFilePath(); | 669 LayerMetaData lmd = it.next(); |
576 | 670 layerNumber ++; |
577 if (path != null && new File(path).isDirectory()) { | 671 String geometryType = lmd.getGeometryType(); |
672 String templateId = lmd.getTemplateID(); | |
578 String title = getLayerTitle(inputData); | 673 String title = getLayerTitle(inputData); |
579 if (title == null) { | 674 if (title == null) { |
580 title = uuid; | 675 title = uuid+"_"+layerNumber; |
581 } | 676 }else{ |
582 | 677 title = title+"_"+layerNumber; |
678 } | |
583 callContext.putContextValue( | 679 callContext.putContextValue( |
584 MetaWriter.CONTEXT_LAYER_TITLE, title); | 680 MetaWriter.CONTEXT_LAYER_TITLE, title); |
585 | 681 String paramType = findParameterTitle(geometryType,templateId); |
586 String paramType = findParameterTitle(geometryType); | |
587 | |
588 if (log.isDebugEnabled()) { | 682 if (log.isDebugEnabled()) { |
589 log.debug("Layer title: " + title); | 683 log.debug("Layer title: " + title); |
590 log.debug("Layer type: " + paramType); | 684 log.debug("Layer type: " + paramType); |
591 } | 685 } |
592 | 686 if (meta == null){ |
593 Document meta = MetaWriter.writeLayerMeta(callContext, uuid, | 687 meta = MetaWriter.writeLayerMeta(callContext,document); |
594 path, paramType, | 688 } |
595 this.determineGeometryType(geometryType), | 689 MetaWriter.writeLayerMeta(callContext, document, |
596 SHAPEFILE_NAME, | 690 meta, uuid, paramType, |
597 title); | 691 this.determineGeometryType(geometryType), |
598 if (meta != null) { | 692 createShapeFileName(layerNumber), |
693 title); | |
694 if (meta != null && !it.hasNext()) { | |
695 MetaWriter.writeMetaFile(getShapeFilePath(),document); | |
599 MapfileGenerator.getInstance().update(); | 696 MapfileGenerator.getInstance().update(); |
600 return meta; | 697 return document; |
601 } | 698 } |
602 | 699 } |
603 pathElement.setTextContent(path); | |
604 } | |
605 else { | |
606 ExclusiveExec.UniqueKey key = ExclusiveExec.INSTANCE.acquire(uuid); | |
607 try{ | |
608 if (data != null && | |
609 (path = writeToShapeFile(uuid, data, callContext,geometryType)) != null) { | |
610 String paramType = findParameterTitle(geometryType); | |
611 String title = getLayerTitle(inputData); | |
612 if (title == null) { | |
613 title = uuid; | |
614 } | |
615 Document meta = MetaWriter.writeLayerMeta(callContext, uuid, | |
616 path, paramType, | |
617 this.determineGeometryType(geometryType), | |
618 SHAPEFILE_NAME, | |
619 title); | |
620 if (meta != null) { | |
621 MapfileGenerator.getInstance().update(); | |
622 return meta; | |
623 } | |
624 pathElement.setTextContent(path); | |
625 } | |
626 }finally{ | |
627 ExclusiveExec.INSTANCE.release(key); | |
628 } | |
629 } | |
630 | |
631 return document; | 700 return document; |
632 } | 701 } |
633 | 702 |
634 /** | 703 /** |
635 * Returns the parameterType for the Layer. | 704 * Returns the parameterType for the Layer. |
636 * @param geometryType | 705 * @param geometryType |
637 * @return | 706 * @return |
638 */ | 707 */ |
639 private String findParameterTitle(String geometryType) { | 708 private String findParameterTitle(String geometryType, String templateID) { |
640 String paramType = LAYER_MODEL+"_"+templateID; | 709 String paramType = LAYER_MODEL+"_"+templateID; |
641 | |
642 if (!MapfileGenerator.getInstance().templateExists(paramType)){ | 710 if (!MapfileGenerator.getInstance().templateExists(paramType)){ |
643 // If the template doesn't exist the Defaulttemplates will be used. | 711 // If the template doesn't exist the Defaulttemplates will be used. |
644 paramType = LAYER_MODEL+"_"+ | 712 paramType = LAYER_MODEL+"_"+ |
645 this.determineDefaultTemplateName(geometryType); | 713 this.determineDefaultTemplateName(geometryType); |
646 } | 714 } |
670 * | 738 * |
671 * @param geometryType The original geometry type. | 739 * @param geometryType The original geometry type. |
672 * @return a valid geometry type fpr the template mechanism. | 740 * @return a valid geometry type fpr the template mechanism. |
673 */ | 741 */ |
674 private String determineGeometryType(String geometryType){ | 742 private String determineGeometryType(String geometryType){ |
675 | |
676 String returnValue = geometryType.toLowerCase(); | 743 String returnValue = geometryType.toLowerCase(); |
677 | |
678 if (returnValue.equalsIgnoreCase("linestring")){ | 744 if (returnValue.equalsIgnoreCase("linestring")){ |
679 returnValue = "Line"; | 745 returnValue = "Line"; |
680 }else if (returnValue.equalsIgnoreCase("multilinestring")){ | 746 }else if (returnValue.equalsIgnoreCase("multilinestring")){ |
681 returnValue ="Line"; | 747 returnValue ="Line"; |
682 }else if (returnValue.equalsIgnoreCase("multipolygon")){ | 748 }else if (returnValue.equalsIgnoreCase("multipolygon")){ |
692 * | 758 * |
693 * @param geometryType The geometry type. | 759 * @param geometryType The geometry type. |
694 * @return a default geometry fitting to the original geometry type. | 760 * @return a default geometry fitting to the original geometry type. |
695 */ | 761 */ |
696 private String determineDefaultTemplateName(String geometryType){ | 762 private String determineDefaultTemplateName(String geometryType){ |
697 | |
698 String returnValue = geometryType.toLowerCase(); | 763 String returnValue = geometryType.toLowerCase(); |
699 | |
700 if (returnValue.equalsIgnoreCase("multilinestring")){ | 764 if (returnValue.equalsIgnoreCase("multilinestring")){ |
701 returnValue ="linestring"; | 765 returnValue ="linestring"; |
702 }else if (returnValue.equalsIgnoreCase("multipolygon")){ | 766 }else if (returnValue.equalsIgnoreCase("multipolygon")){ |
703 returnValue = "polygon"; | 767 returnValue = "polygon"; |
704 }else if (returnValue.equalsIgnoreCase("multipoint")){ | 768 }else if (returnValue.equalsIgnoreCase("multipoint")){ |