[Dive4elements-commits] [PATCH] Refactor Mapserver mapfile generation

Wald Commits scm-commit at wald.intevation.org
Sat Dec 8 00:19:35 CET 2012


# HG changeset patch
# User Christian Lins <christian.lins at intevation.de>
# Date 1354922369 -3600
# Node ID 861c47e0a8a0468a5bba04a930123468736ff77c
# Parent  a93699cb31eb39d09a945f8985d98a9542f7697e
Refactor Mapserver mapfile generation.

Prepares the existing mapfile generation code for
a river axis mapfile generator.
Removes unused asynchronous mapfile generation to
reduce complexity.

diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/doc/conf/conf.xml
--- a/flys-artifacts/doc/conf/conf.xml	Fri Dec 07 11:26:53 2012 +0100
+++ b/flys-artifacts/doc/conf/conf.xml	Sat Dec 08 00:19:29 2012 +0100
@@ -224,7 +224,7 @@
     <lifetime-listeners>
         <listener>de.intevation.flys.artifacts.datacage.Datacage</listener>
         <listener>de.intevation.flys.wsplgen.SchedulerSetup</listener>
-        <listener>de.intevation.flys.map.RiverMapGenerator</listener>
+        <listener>de.intevation.flys.map.RiverMapGeneratorStarter</listener>
     </lifetime-listeners>
 
     <backend-listeners>
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java	Fri Dec 07 11:26:53 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java	Sat Dec 08 00:19:29 2012 +0100
@@ -1,39 +1,29 @@
 package de.intevation.flys.artifacts;
 
-import java.io.File;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import java.util.regex.Pattern;
-import java.util.regex.Matcher;
-
-import org.w3c.dom.Document;
-
-import org.apache.log4j.Logger;
-
-import org.hibernate.impl.SessionFactoryImpl;
-
 import com.vividsolutions.jts.geom.Envelope;
 
-import de.intevation.artifacts.Artifact;
-import de.intevation.artifacts.ArtifactFactory;
-import de.intevation.artifacts.CallMeta;
-
-import de.intevation.artifacts.common.utils.FileTools;
-
 import de.intevation.artifactdatabase.data.DefaultStateData;
 import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.State;
-
-
-import de.intevation.flys.backend.SessionFactoryProvider;
-
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.common.utils.FileTools;
+import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
 import de.intevation.flys.artifacts.resources.Resources;
 import de.intevation.flys.artifacts.states.DefaultState;
-import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
+import de.intevation.flys.backend.SessionFactoryProvider;
 import de.intevation.flys.utils.FLYSUtils;
-import de.intevation.flys.utils.MapfileGenerator;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+import org.hibernate.impl.SessionFactoryImpl;
+import org.w3c.dom.Document;
 
 
 public abstract class WMSDBArtifact extends StaticFLYSArtifact {
@@ -361,7 +351,6 @@
                 logger.debug("Try to delete directory '" + dir + "'");
 
                 FileTools.deleteRecursive(dir);
-                MapfileGenerator.getInstance().update();
             }
         }
 
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/map/RiverMapfileGeneratorStarter.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/map/RiverMapfileGeneratorStarter.java	Sat Dec 08 00:19:29 2012 +0100
@@ -0,0 +1,37 @@
+package de.intevation.flys.artifacts.map;
+
+import de.intevation.artifactdatabase.LifetimeListener;
+import de.intevation.artifacts.GlobalContext;
+import de.intevation.flys.utils.RiverMapfileGenerator;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+
+/**
+ * LifetimeListener that creates a Mapfile containing river axis layers.
+ * The listener is called when the server has completed its startup.
+ * @author <a href="mailto:christian.lins at intevation.de">Christian Lins</a>
+ */
+public class RiverMapfileGeneratorStarter implements LifetimeListener {
+
+    private static Logger logger = Logger.getLogger(RiverMapfileGeneratorStarter.class);
+
+    @Override
+    public void setup(Document document) {
+
+    }
+
+    @Override
+    public void systemUp(GlobalContext globalContext) {
+        logger.debug("systemUp()");
+
+        RiverMapfileGenerator fmfg = new RiverMapfileGenerator();
+
+    }
+
+    @Override
+    public void systemDown(GlobalContext globalContext) {
+        // No, we're not cleaning up our generated mapfile
+    }
+
+}
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java	Fri Dec 07 11:26:53 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java	Sat Dec 08 00:19:29 2012 +0100
@@ -101,8 +101,6 @@
 
         copyShapeDir(orig, owner);
         modifyFacets(orig, owner, context, callMeta);
-
-        MapfileGenerator.getInstance().update();
     }
 
 
@@ -266,8 +264,6 @@
 
         Scheduler scheduler = Scheduler.getInstance();
         scheduler.cancelJob(flys.identifier());
-
-        MapfileGenerator.getInstance().update();
     }
 
 
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Fri Dec 07 11:26:53 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Sat Dec 08 00:19:29 2012 +0100
@@ -16,8 +16,8 @@
 import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
 import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet;
 import de.intevation.flys.collections.FLYSArtifactCollection;
+import de.intevation.flys.utils.ArtefactMapfileGenerator;
 import de.intevation.flys.utils.GeometryUtils;
-import de.intevation.flys.utils.MapfileGenerator;
 import de.intevation.flys.utils.ThemeUtil;
 
 import java.io.File;
@@ -133,7 +133,7 @@
     ) {
         try {
             if(wms instanceof WSPLGENLayerFacet) {
-                MapfileGenerator mfg = MapfileGenerator.getInstance();
+                ArtefactMapfileGenerator mfg = new ArtefactMapfileGenerator();
                 mfg.createUeskLayer(
                     flys,
                     (WSPLGENLayerFacet) wms,
@@ -152,7 +152,7 @@
 
 
     protected void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms) {
-        MapfileGenerator mfg = MapfileGenerator.getInstance();
+        ArtefactMapfileGenerator mfg = new ArtefactMapfileGenerator();
 
         try {
             mfg.createBarriersLayer(flys, wms);
@@ -167,7 +167,7 @@
 
 
     protected void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms) {
-        MapfileGenerator mfg = MapfileGenerator.getInstance();
+        ArtefactMapfileGenerator mfg = new ArtefactMapfileGenerator();
 
         try {
             mfg.createUserShapeLayer(flys, wms);
@@ -188,7 +188,7 @@
     ) {
         logger.debug("createDatabaseLayer for facet: " + wms.getName());
 
-        MapfileGenerator mfg = MapfileGenerator.getInstance();
+        ArtefactMapfileGenerator mfg = new ArtefactMapfileGenerator();
 
         try {
             File baseDir = mfg.getShapefileBaseDir();
@@ -224,7 +224,8 @@
     {
         logger.debug("MapGenerator.generate");
 
-        MapfileGenerator.getInstance().update();
+        ArtefactMapfileGenerator mfg = new ArtefactMapfileGenerator();
+        mfg.generate();
 
         Document response = XMLUtils.newDocument();
         ElementCreator c  = new ElementCreator(
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/utils/ArtefactMapfileGenerator.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/ArtefactMapfileGenerator.java	Sat Dec 08 00:19:29 2012 +0100
@@ -0,0 +1,312 @@
+package de.intevation.flys.utils;
+
+import de.intevation.artifacts.CallContext;
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.model.LayerInfo;
+import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
+import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
+import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet;
+import de.intevation.flys.artifacts.resources.Resources;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.apache.velocity.Template;
+import org.geotools.data.shapefile.ShpFiles;
+import org.geotools.data.shapefile.shp.ShapefileHeader;
+import org.geotools.data.shapefile.shp.ShapefileReader;
+
+public class ArtefactMapfileGenerator extends MapfileGenerator {
+
+    private static Logger logger = Logger.getLogger(ArtefactMapfileGenerator.class);
+
+    /**
+     * Method which starts searching for meta information file and mapfile
+     * generation.
+     */
+    @Override
+    public void generate() throws IOException
+    {
+        File[] userDirs = getUserDirs();
+        List<String> layers = parseLayers(userDirs);
+        logger.info("Found " + layers.size() + " layers for user mapfile.");
+
+        writeMapfile(layers);
+    }
+
+    /**
+     * Creates a layer file used for Mapserver's mapfile which represents the
+     * floodmap.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createUeskLayer(
+        FLYSArtifact  flys,
+        WSPLGENLayerFacet wms,
+        String        style,
+        CallContext context
+    ) throws FileNotFoundException, IOException
+    {
+        logger.debug("createUeskLayer");
+
+        LayerInfo layerinfo = new LayerInfo();
+        layerinfo.setName(MS_WSPLGEN_PREFIX + flys.identifier());
+        layerinfo.setType("POLYGON");
+        layerinfo.setDirectory(flys.identifier());
+        layerinfo.setData(WSPLGEN_RESULT_SHAPE);
+        layerinfo.setTitle(Resources.getMsg(Resources.getLocale(context.getMeta()),
+                                            "floodmap.uesk",
+                                            "Floodmap"));
+        layerinfo.setStyle(style);
+        layerinfo.setSrid(wms.getSrid());
+
+        String name = MS_LAYER_PREFIX + wms.getName();
+
+        Template template = getTemplateByName(WSPLGEN_LAYER_TEMPLATE);
+        if (template == null) {
+            logger.warn("Template '" + WSPLGEN_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 file used for Mapserver's mapfile which represents the
+     * user defined barriers.
+     *
+     * @param flys The FLYSArtifact that owns <i>wms</i>.
+     * @param wms The WMSLayerFacet that contains information for the layer.
+     */
+    public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms)
+    throws FileNotFoundException, IOException
+    {
+        logger.debug("createBarriersLayer");
+
+        //String uuid = flys.identifier();
+        //File   dir  = new File(getShapefileBaseDir(), uuid);
+
+        createBarriersLineLayer(flys, wms);
+        createBarriersPolygonLayer(flys, wms);
+    }
+
+
+    protected void createBarriersLineLayer(
+        FLYSArtifact  flys,
+        WMSLayerFacet wms
+    )
+    throws FileNotFoundException, IOException
+    {
+        String uuid       = flys.identifier();
+        String group      = MS_BARRIERS_PREFIX + uuid;
+        String groupTitle = "I18N_BARRIERS_TITLE";
+
+        File dir  = new File(getShapefileBaseDir(), uuid);
+        File test = new File(dir, WSPLGEN_LINES_SHAPE);
+
+        if (!test.exists() || !test.canRead()) {
+            logger.debug("No barrier line layer existing.");
+            return;
+        }
+
+        LayerInfo lineInfo = new LayerInfo();
+        lineInfo.setName(MS_LINE_PREFIX + uuid);
+        lineInfo.setType("LINE");
+        lineInfo.setDirectory(uuid);
+        lineInfo.setData(WSPLGEN_LINES_SHAPE);
+        lineInfo.setTitle("I18N_LINE_SHAPE");
+        lineInfo.setGroup(group);
+        lineInfo.setGroupTitle(groupTitle);
+        lineInfo.setSrid(wms.getSrid());
+
+        String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines";
+
+        Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
+        if (tpl == null) {
+            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
+            return;
+        }
+
+        try {
+            writeLayer(lineInfo, dir, nameLines, tpl);
+        }
+        catch (FileNotFoundException fnfe) {
+            logger.error(fnfe, fnfe);
+            logger.warn("Unable to write layer: " + nameLines);
+        }
+    }
+
+    protected void createBarriersPolygonLayer(
+            FLYSArtifact  flys,
+            WMSLayerFacet wms
+        )
+        throws FileNotFoundException, IOException
+        {
+            String uuid       = flys.identifier();
+            String group      = uuid + MS_BARRIERS_PREFIX;
+            String groupTitle = "I18N_BARRIERS_TITLE";
+
+            File dir  = new File(getShapefileBaseDir(), uuid);
+            File test = new File(dir, WSPLGEN_POLYGONS_SHAPE);
+
+            if (!test.exists() || !test.canRead()) {
+                logger.debug("No barrier line layer existing.");
+                return;
+            }
+
+            LayerInfo polygonInfo = new LayerInfo();
+            polygonInfo.setName(MS_POLYGONS_PREFIX + uuid);
+            polygonInfo.setType("POLYGON");
+            polygonInfo.setDirectory(uuid);
+            polygonInfo.setData(WSPLGEN_POLYGONS_SHAPE);
+            polygonInfo.setTitle("I18N_POLYGON_SHAPE");
+            polygonInfo.setGroup(group);
+            polygonInfo.setGroupTitle(groupTitle);
+            polygonInfo.setSrid(wms.getSrid());
+
+            String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons";
+
+            Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
+            if (tpl == null) {
+                logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
+                return;
+            }
+
+            try {
+                writeLayer(polygonInfo, dir, namePolygons, tpl);
+            }
+            catch (FileNotFoundException fnfe) {
+                logger.error(fnfe, fnfe);
+                logger.warn("Unable to write layer: " + namePolygons);
+            }
+        }
+
+
+        /**
+         * Creates a layer file used for Mapserver's mapfile which represents the
+         * shape files uploaded by the user.
+         *
+         * @param flys The FLYSArtifact that owns <i>wms</i>.
+         * @param wms The WMSLayerFacet that contains information for the layer.
+         */
+        public void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms)
+        throws FileNotFoundException, IOException
+        {
+            logger.debug("createUserShapeLayer");
+
+            String uuid = flys.identifier();
+            File   dir  = new File(getShapefileBaseDir(), uuid);
+            File   test = new File(dir, WSPLGEN_USER_SHAPE);
+
+            if (!test.exists() || !test.canRead()) {
+                logger.debug("No user layer existing.");
+                return;
+            }
+
+            File userShape = new File(dir, WSPLGEN_USER_SHAPE);
+            ShpFiles sf = new ShpFiles(userShape);
+            ShapefileReader sfr = new ShapefileReader(sf, true, false, null);
+            ShapefileHeader sfh = sfr.getHeader();
+
+            String group      = uuid + MS_USERSHAPE_PREFIX;
+            String groupTitle = "I18N_USER_SHAPE_TITLE";
+
+            LayerInfo info = new LayerInfo();
+            info.setName(MS_USERSHAPE_PREFIX + uuid);
+            if (sfh.getShapeType().isLineType()) {
+                info.setType("LINE");
+            }
+            else if (sfh.getShapeType().isPolygonType()) {
+                info.setType("POLYGON");
+            }
+            else {
+                return;
+            }
+            info.setDirectory(uuid);
+            info.setData(WSPLGEN_USER_SHAPE);
+            info.setTitle("I18N_USER_SHAPE");
+            info.setGroup(group);
+            info.setGroupTitle(groupTitle);
+            info.setSrid(wms.getSrid());
+
+            String nameUser = MS_LAYER_PREFIX + wms.getName();
+
+            Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
+            if (tpl == null) {
+                logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
+                return;
+            }
+
+            try {
+                writeLayer(info, dir, nameUser, tpl);
+            }
+            catch (FileNotFoundException fnfe) {
+                logger.error(fnfe, fnfe);
+                logger.warn("Unable to write layer: " + nameUser);
+            }
+
+        }
+
+
+        /**
+         * 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,
+            String          style
+        )
+        throws FileNotFoundException, IOException
+        {
+            logger.debug("createDatabaseLayer");
+
+            LayerInfo layerinfo = new LayerInfo();
+            layerinfo.setName(wms.getName() + "-" + flys.identifier());
+            layerinfo.setType(wms.getGeometryType());
+            layerinfo.setFilter(wms.getFilter());
+            layerinfo.setData(wms.getData());
+            layerinfo.setTitle(wms.getDescription());
+            layerinfo.setStyle(style);
+            if(wms.getExtent() != null) {
+                layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent()));
+            }
+            layerinfo.setConnection(wms.getConnection());
+            layerinfo.setConnectionType(wms.getConnectionType());
+            layerinfo.setLabelItem(wms.getLabelItem());
+            layerinfo.setSrid(wms.getSrid());
+
+            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);
+            }
+        }
+
+}
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Fri Dec 07 11:26:53 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Sat Dec 08 00:19:29 2012 +0100
@@ -1,15 +1,7 @@
 package de.intevation.flys.utils;
 
-import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.common.utils.Config;
-import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.model.LayerInfo;
-import de.intevation.flys.artifacts.model.RiverFactory;
-import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
-import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
-import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet;
-import de.intevation.flys.artifacts.resources.Resources;
-import de.intevation.flys.model.River;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -25,9 +17,6 @@
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
-import org.geotools.data.shapefile.ShpFiles;
-import org.geotools.data.shapefile.shp.ShapefileHeader;
-import org.geotools.data.shapefile.shp.ShapefileReader;
 
 /**
  * This class iterates over a bunch of directories, searches for meta
@@ -36,8 +25,7 @@
  *
  * @author <a href="mailto:ingo.weinzierl at intevation.de">Ingo Weinzierl</a>
  */
-public class MapfileGenerator
-extends Thread
+public abstract class MapfileGenerator
 {
     public static final String WSPLGEN_RESULT_SHAPE   = "wsplgen.shp";
     public static final String WSPLGEN_LINES_SHAPE    = "barrier_lines.shp";
@@ -56,98 +44,18 @@
     public static final String MS_LAYER_PREFIX     = "ms_layer-";
     public static final String MS_USERSHAPE_PREFIX = "user-";
 
-    protected static final long SLEEPTIME = 10 * 1000L; // 10 seconds
-
     private static Logger logger = Logger.getLogger(MapfileGenerator.class);
 
-    private static MapfileGenerator instance;
-
     private File shapefileDirectory;
 
     private VelocityEngine velocityEngine;
-    private final boolean lock[];
 
 
-
-    private MapfileGenerator() {
-        lock = new boolean[1];
+    protected MapfileGenerator() {
     }
 
 
     /**
-     * A main method which can be used to create a mapfile without a running
-     * artifact server.<br>
-     * <b>NOTE:</b> This method is not implemented yet!
-     *
-     * @param args Arguments.
-     */
-    public static void main(String[] args) {
-        throw new Error("MapfileGenerator.main() is CURRENTLY NOT IMPLEMENTED!");
-    }
-
-
-    /**
-     * Returns the instance of this generator.
-     *
-     * @return the instance.
-     */
-    public static synchronized MapfileGenerator getInstance() {
-        if (instance == null) {
-            instance = new MapfileGenerator();
-            instance.setDaemon(true);
-            instance.start();
-        }
-
-        return instance;
-    }
-
-
-    /**
-     * Triggers the mapfile generation process.
-     */
-    public void update() {
-        synchronized (lock) {
-            logger.debug("update");
-            lock[0] = true;
-            lock.notify();
-        }
-    }
-
-
-    /**
-     * Thread to generate a mapfile.
-     */
-    @Override
-    public void run() {
-        logger.debug("Start MapfileGenerator thread...");
-        try {
-            for (;;) {
-                synchronized (lock) {
-                    while (!lock[0]) {
-                        lock.wait(SLEEPTIME);
-                    }
-                    lock[0] = false;
-                }
-
-                logger.debug("Start sync process now...");
-                generate();
-            }
-        }
-        catch (InterruptedException ie) {
-            logger.debug("MapfileGenerator thread got an interrupt.");
-        }
-        catch (FileNotFoundException fnfe) {
-            logger.debug("Error while mapfile creation: " + fnfe.getMessage());
-        }
-        catch (IOException ioe) {
-            logger.error(ioe, ioe);
-        }
-        finally {
-            logger.debug("THREAD END");
-        }
-    }
-
-    /**
      * Method to check the existance of a template file.
      *
      * @param templateID The name of a template.
@@ -159,37 +67,7 @@
     }
 
 
-    /**
-     * Method which starts searching for meta information file and mapfile
-     * generation.
-     */
-    protected void generate()
-    throws    FileNotFoundException, IOException
-    {
-        File[]        userDirs = getUserDirs();
-
-        List<String> layers = parseLayers(userDirs);
-
-        logger.info("Found " + layers.size() + " layers for user mapfile.");
-
-        writeMapfile(layers);
-    }
-
-
-    /**
-     * Generate river axis mapfile.
-     */
-    protected void generateRiverAxisMapfile() {
-        List<River> rivers = RiverFactory.getRivers();
-
-        for (River river : rivers) {
-            createRiverAxisLayer(
-                    river.getName(),
-                    river.getId(),
-                    "41677",
-                    "100,100,100,100");
-        }
-    }
+    protected abstract void generate() throws Exception;
 
 
     /**
@@ -379,286 +257,6 @@
     }
 
 
-    /**
-     * Creates a layer file used for Mapserver's mapfile which represents the
-     * floodmap.
-     *
-     * @param flys The FLYSArtifact that owns <i>wms</i>.
-     * @param wms The WMSLayerFacet that contains information for the layer.
-     */
-    public void createUeskLayer(
-        FLYSArtifact  flys,
-        WSPLGENLayerFacet wms,
-        String        style,
-        CallContext context
-    ) throws FileNotFoundException, IOException
-    {
-        logger.debug("createUeskLayer");
-
-        LayerInfo layerinfo = new LayerInfo();
-        layerinfo.setName(MS_WSPLGEN_PREFIX + flys.identifier());
-        layerinfo.setType("POLYGON");
-        layerinfo.setDirectory(flys.identifier());
-        layerinfo.setData(WSPLGEN_RESULT_SHAPE);
-        layerinfo.setTitle(Resources.getMsg(Resources.getLocale(context.getMeta()),
-                                            "floodmap.uesk",
-                                            "Floodmap"));
-        layerinfo.setStyle(style);
-        layerinfo.setSrid(wms.getSrid());
-
-        String name = MS_LAYER_PREFIX + wms.getName();
-
-        Template template = getTemplateByName(WSPLGEN_LAYER_TEMPLATE);
-        if (template == null) {
-            logger.warn("Template '" + WSPLGEN_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 file used for Mapserver's mapfile which represents the
-     * user defined barriers.
-     *
-     * @param flys The FLYSArtifact that owns <i>wms</i>.
-     * @param wms The WMSLayerFacet that contains information for the layer.
-     */
-    public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms)
-    throws FileNotFoundException, IOException
-    {
-        logger.debug("createBarriersLayer");
-
-        //String uuid = flys.identifier();
-        //File   dir  = new File(getShapefileBaseDir(), uuid);
-
-        createBarriersLineLayer(flys, wms);
-        createBarriersPolygonLayer(flys, wms);
-    }
-
-
-    protected void createBarriersLineLayer(
-        FLYSArtifact  flys,
-        WMSLayerFacet wms
-    )
-    throws FileNotFoundException, IOException
-    {
-        String uuid       = flys.identifier();
-        String group      = MS_BARRIERS_PREFIX + uuid;
-        String groupTitle = "I18N_BARRIERS_TITLE";
-
-        File dir  = new File(getShapefileBaseDir(), uuid);
-        File test = new File(dir, WSPLGEN_LINES_SHAPE);
-
-        if (!test.exists() || !test.canRead()) {
-            logger.debug("No barrier line layer existing.");
-            return;
-        }
-
-        LayerInfo lineInfo = new LayerInfo();
-        lineInfo.setName(MS_LINE_PREFIX + uuid);
-        lineInfo.setType("LINE");
-        lineInfo.setDirectory(uuid);
-        lineInfo.setData(WSPLGEN_LINES_SHAPE);
-        lineInfo.setTitle("I18N_LINE_SHAPE");
-        lineInfo.setGroup(group);
-        lineInfo.setGroupTitle(groupTitle);
-        lineInfo.setSrid(wms.getSrid());
-
-        String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines";
-
-        Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
-        if (tpl == null) {
-            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
-            return;
-        }
-
-        try {
-            writeLayer(lineInfo, dir, nameLines, tpl);
-        }
-        catch (FileNotFoundException fnfe) {
-            logger.error(fnfe, fnfe);
-            logger.warn("Unable to write layer: " + nameLines);
-        }
-    }
-
-
-    protected void createRiverAxisLayer(String riverName, int riverID, String srid, String extend) {
-        LayerInfo layerInfo = new LayerInfo();
-        layerInfo.setName(riverName);
-        layerInfo.setSrid(srid);
-        layerInfo.setExtent(extend);
-    }
-
-
-    protected void createBarriersPolygonLayer(
-        FLYSArtifact  flys,
-        WMSLayerFacet wms
-    )
-    throws FileNotFoundException, IOException
-    {
-        String uuid       = flys.identifier();
-        String group      = uuid + MS_BARRIERS_PREFIX;
-        String groupTitle = "I18N_BARRIERS_TITLE";
-
-        File dir  = new File(getShapefileBaseDir(), uuid);
-        File test = new File(dir, WSPLGEN_POLYGONS_SHAPE);
-
-        if (!test.exists() || !test.canRead()) {
-            logger.debug("No barrier line layer existing.");
-            return;
-        }
-
-        LayerInfo polygonInfo = new LayerInfo();
-        polygonInfo.setName(MS_POLYGONS_PREFIX + uuid);
-        polygonInfo.setType("POLYGON");
-        polygonInfo.setDirectory(uuid);
-        polygonInfo.setData(WSPLGEN_POLYGONS_SHAPE);
-        polygonInfo.setTitle("I18N_POLYGON_SHAPE");
-        polygonInfo.setGroup(group);
-        polygonInfo.setGroupTitle(groupTitle);
-        polygonInfo.setSrid(wms.getSrid());
-
-        String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons";
-
-        Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
-        if (tpl == null) {
-            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
-            return;
-        }
-
-        try {
-            writeLayer(polygonInfo, dir, namePolygons, tpl);
-        }
-        catch (FileNotFoundException fnfe) {
-            logger.error(fnfe, fnfe);
-            logger.warn("Unable to write layer: " + namePolygons);
-        }
-    }
-
-
-    /**
-     * Creates a layer file used for Mapserver's mapfile which represents the
-     * shape files uploaded by the user.
-     *
-     * @param flys The FLYSArtifact that owns <i>wms</i>.
-     * @param wms The WMSLayerFacet that contains information for the layer.
-     */
-    public void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms)
-    throws FileNotFoundException, IOException
-    {
-        logger.debug("createUserShapeLayer");
-
-        String uuid = flys.identifier();
-        File   dir  = new File(getShapefileBaseDir(), uuid);
-        File   test = new File(dir, WSPLGEN_USER_SHAPE);
-
-        if (!test.exists() || !test.canRead()) {
-            logger.debug("No user layer existing.");
-            return;
-        }
-
-        File userShape = new File(dir, WSPLGEN_USER_SHAPE);
-        ShpFiles sf = new ShpFiles(userShape);
-        ShapefileReader sfr = new ShapefileReader(sf, true, false, null);
-        ShapefileHeader sfh = sfr.getHeader();
-
-        String group      = uuid + MS_USERSHAPE_PREFIX;
-        String groupTitle = "I18N_USER_SHAPE_TITLE";
-
-        LayerInfo info = new LayerInfo();
-        info.setName(MS_USERSHAPE_PREFIX + uuid);
-        if (sfh.getShapeType().isLineType()) {
-            info.setType("LINE");
-        }
-        else if (sfh.getShapeType().isPolygonType()) {
-            info.setType("POLYGON");
-        }
-        else {
-            return;
-        }
-        info.setDirectory(uuid);
-        info.setData(WSPLGEN_USER_SHAPE);
-        info.setTitle("I18N_USER_SHAPE");
-        info.setGroup(group);
-        info.setGroupTitle(groupTitle);
-        info.setSrid(wms.getSrid());
-
-        String nameUser = MS_LAYER_PREFIX + wms.getName();
-
-        Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE);
-        if (tpl == null) {
-            logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found.");
-            return;
-        }
-
-        try {
-            writeLayer(info, dir, nameUser, tpl);
-        }
-        catch (FileNotFoundException fnfe) {
-            logger.error(fnfe, fnfe);
-            logger.warn("Unable to write layer: " + nameUser);
-        }
-
-    }
-
-
-    /**
-     * 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,
-        String          style
-    )
-    throws FileNotFoundException, IOException
-    {
-        logger.debug("createDatabaseLayer");
-
-        LayerInfo layerinfo = new LayerInfo();
-        layerinfo.setName(wms.getName() + "-" + flys.identifier());
-        layerinfo.setType(wms.getGeometryType());
-        layerinfo.setFilter(wms.getFilter());
-        layerinfo.setData(wms.getData());
-        layerinfo.setTitle(wms.getDescription());
-        layerinfo.setStyle(style);
-        if(wms.getExtent() != null) {
-            layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent()));
-        }
-        layerinfo.setConnection(wms.getConnection());
-        layerinfo.setConnectionType(wms.getConnectionType());
-        layerinfo.setLabelItem(wms.getLabelItem());
-        layerinfo.setSrid(wms.getSrid());
-
-        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);
-        }
-    }
 
 
     /**
diff -r a93699cb31eb -r 861c47e0a8a0 flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java	Sat Dec 08 00:19:29 2012 +0100
@@ -0,0 +1,34 @@
+package de.intevation.flys.utils;
+
+import de.intevation.flys.artifacts.model.LayerInfo;
+import de.intevation.flys.artifacts.model.RiverFactory;
+import de.intevation.flys.model.River;
+
+import java.util.List;
+
+public class RiverMapfileGenerator extends MapfileGenerator {
+
+    /**
+     * Generate river axis mapfile.
+     */
+    @Override
+    protected void generate() {
+        List<River> rivers = RiverFactory.getRivers();
+
+        for (River river : rivers) {
+            createRiverAxisLayer(
+                    river.getName(),
+                    river.getId(),
+                    "41677",
+                    "100,100,100,100");
+        }
+    }
+
+    protected void createRiverAxisLayer(String riverName, int riverID, String srid, String extend) {
+        LayerInfo layerInfo = new LayerInfo();
+        layerInfo.setName(riverName);
+        layerInfo.setSrid(srid);
+        layerInfo.setExtent(extend);
+    }
+
+}


More information about the Dive4elements-commits mailing list