diff flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 1792:49ad801076e4

Enabled the MapGenerator to create WMS layers with database datastore - riveraxis is the first layer that is no longer fetched from static WMS service but from user specific one. flys-artifacts/trunk@3115 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 13:48:02 +0000
parents ef2300b450bf
children 1636686070f7
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Oct 28 13:39:37 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Oct 28 13:48:02 2011 +0000
@@ -5,7 +5,6 @@
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.StringWriter;
 import java.io.Writer;
 
 import java.util.ArrayList;
@@ -21,8 +20,10 @@
 import de.intevation.artifacts.common.utils.Config;
 
 import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.model.DBLayerInfo;
 import de.intevation.flys.artifacts.model.LayerInfo;
 import de.intevation.flys.artifacts.model.WMSLayerFacet;
+import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
 
 /**
  * This class iterates over a bunch of directories, searches for meta
@@ -39,6 +40,7 @@
     public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp";
 
     public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm";
+    public static final String DB_LAYER_TEMPLATE  = "db_layer.vm";
 
     public static final String MS_WSPLGEN_POSTFIX  = "-wsplgen";
     public static final String MS_BARRIERS_POSTFIX = "-barriers";
@@ -220,6 +222,28 @@
     }
 
 
+    protected VelocityContext getVelocityContext() {
+        VelocityContext context = new VelocityContext();
+
+        try {
+            context.put("MAPSERVERURL",
+                FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL));
+            context.put("SHAPEFILEPATH",
+                getShapefileBaseDir().getCanonicalPath());
+            context.put("CONFIGDIR",
+                Config.getConfigDirectory().getCanonicalPath());
+        }
+        catch (FileNotFoundException fnfe) {
+            // this is bad
+        }
+        catch (IOException ioe) {
+            // this is also bad
+        }
+
+        return context;
+    }
+
+
     /**
      * Returns a template specified by <i>model</i>.
      *
@@ -272,7 +296,7 @@
      * @throws FileNotFoundException if no shapefile path is found or
      * configured.
      */
-    protected File getShapefileBaseDir()
+    public File getShapefileBaseDir()
     throws    FileNotFoundException, IOException
     {
         if (shapefileDirectory == null) {
@@ -436,6 +460,44 @@
 
 
     /**
+     * Creates a layer file used for Mapserver's mapfile which represents
+     * geometries from database.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createDatabaseLayer(FLYSArtifact flys, WMSDBLayerFacet wms)
+    throws FileNotFoundException, IOException
+    {
+        logger.debug("createDatabaseLayer");
+
+        LayerInfo layerinfo = new DBLayerInfo(
+            flys.identifier() + "-" + wms.getName(),
+            "LINE",
+            wms.getFilter(),
+            wms.getData(),
+            wms.getDescription());
+
+        String name = MS_LAYER_PREFIX + wms.getName();
+
+        Template template = getTemplateByName(DB_LAYER_TEMPLATE);
+        if (template == null) {
+            logger.warn("Template '" + DB_LAYER_TEMPLATE + "' found.");
+            return;
+        }
+
+        try {
+            File dir = new File(getShapefileBaseDir(), flys.identifier());
+            writeLayer(layerinfo, dir, name, template);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+            logger.warn("Unable to write layer: " + name);
+        }
+    }
+
+
+    /**
      * Creates a layer snippet which might be included in the mapfile.
      *
      * @param layerinfo A LayerInfo object that contains all necessary
@@ -465,7 +527,7 @@
         try {
             writer = new FileWriter(layer);
 
-            VelocityContext context = new VelocityContext();
+            VelocityContext context = getVelocityContext();
             context.put("LAYER", layerinfo);
 
             tpl.merge(context, writer);
@@ -512,13 +574,7 @@
 
             writer = new FileWriter(tmp);
 
-            VelocityContext context = new VelocityContext();
-            context.put("MAPSERVERURL",
-                FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL));
-            context.put("SHAPEFILEPATH",
-                getShapefileBaseDir().getCanonicalPath());
-            context.put("CONFIGDIR",
-                Config.getConfigDirectory().getCanonicalPath());
+            VelocityContext context = getVelocityContext();
             context.put("LAYERS", layers);
 
             Template mapTemplate = getMapfileTemplate();

http://dive4elements.wald.intevation.org