changeset 8452:b9a8463ac636

(issue1732) When duplication wsplgen artificats fix shapefile layers The shapefile layers are copied but still contained the old names and paths and the wms layer refered to layers from the old artifact. This was broken. Now the old artificat id is replaced by the new artifact id in the mapfiles. Duplication most wsplgen artifacts should now work.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 28 Oct 2014 18:52:35 +0100
parents e3e5d6bb4902
children 1326fc65638c
files artifacts/pom.xml artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java
diffstat 2 files changed, 38 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/pom.xml	Tue Oct 28 16:10:42 2014 +0100
+++ b/artifacts/pom.xml	Tue Oct 28 18:52:35 2014 +0100
@@ -110,6 +110,11 @@
       <version>1.4</version>
     </dependency>
     <dependency>
+        <groupId>commons-io</groupId>
+        <artifactId>commons-io</artifactId>
+        <version>2.4</version>
+    </dependency>
+    <dependency>
       <groupId>org.geotools</groupId>
       <artifactId>gt-shapefile</artifactId>
       <version>2.7.2</version>
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java	Tue Oct 28 16:10:42 2014 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/FloodMapState.java	Tue Oct 28 18:52:35 2014 +0100
@@ -23,6 +23,7 @@
 
 import org.apache.log4j.Logger;
 import org.apache.velocity.Template;
+import org.apache.commons.io.FileUtils;
 import org.dive4elements.artifactdatabase.state.Facet;
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.CallContext;
@@ -109,6 +110,16 @@
     private static final String HWS_POINT_SHAPE = "hws-points.shp";
     private static final String HWS_POINTS = "hws-points";
 
+
+    /* List of possible map files this state handles . */
+    private static final String[] POSSIBLE_ADDITIONAL_MAPFILES = {HWS_LINES,
+                                                                  HWS_POINTS,
+                                                                  WSPLGEN_USER_RGD,
+                                                                  "barriers-lines",
+                                                                  "barriers-poly"};
+
+
+
     /**
      * @param orig
      * @param owner
@@ -124,7 +135,7 @@
     ) {
         log.info("Initialize State with Artifact: " + orig.identifier());
 
-        copyShapeDir(orig, owner);
+        copyAndFixShapeDir(orig, owner);
         modifyFacets(orig, owner, context, callMeta);
 
         ArtifactMapfileGenerator amfg = new ArtifactMapfileGenerator();
@@ -137,11 +148,28 @@
     }
 
 
-    protected void copyShapeDir(Artifact orig, Artifact owner) {
+    protected void copyAndFixShapeDir(Artifact orig, Artifact owner) {
+        /* We have a lot of generated mapfiles in here possibly.
+         * They use the orig artificact identifier. That has to
+         * be fixed.*/
         File origDir = getDirectory((D4EArtifact) orig);
         File thisDir = getDirectory((D4EArtifact) owner);
 
         FileTools.copyDirectory(origDir, thisDir);
+
+        for (String candidate: POSSIBLE_ADDITIONAL_MAPFILES) {
+            File f = new File(thisDir, MapfileGenerator.MS_LAYER_PREFIX + candidate);
+            if (f.exists()) {
+                log.debug("Fixing artifiact id's in: " + f);
+                try {
+                    String content = FileUtils.readFileToString(f);
+                    FileUtils.writeStringToFile(f,
+                            content.replaceAll(orig.identifier(), owner.identifier()));
+                } catch (IOException e) {
+                    log.debug("failed.");
+                }
+            }
+        }
     }
 
 
@@ -165,11 +193,11 @@
                 List<String> layers = wms.getLayers();
 
                 for (String layer: layers) {
-                    if (layer.startsWith(MapfileGenerator.MS_WSPLGEN_PREFIX)) {
+                    log.debug("Have layer: " + layer);
+                    if (layer.contains(orig.identifier())) {
                         wms.removeLayer(layer);
 
-                        String newLayer = MapfileGenerator.MS_WSPLGEN_PREFIX +
-                            owner.identifier();
+                        String newLayer = layer.replace(orig.identifier(), owner.identifier());
 
                         wms.addLayer(newLayer);
 

http://dive4elements.wald.intevation.org