changeset 4841:ad0990a82ab8

Insert db connection into riveraxis map files. * Added new MapUtils. * Moved connection specific strings to maputils. * Updated mapfile template.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 23 Jan 2013 17:32:30 +0100
parents 8cd2f20b1d8c
children 97a0fa4dff8e
files flys-artifacts/doc/conf/mapserver/riveraxis-layer.vm flys-artifacts/src/main/java/de/intevation/flys/artifacts/MapArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java
diffstat 5 files changed, 162 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/doc/conf/mapserver/riveraxis-layer.vm	Wed Jan 23 13:20:39 2013 +0100
+++ b/flys-artifacts/doc/conf/mapserver/riveraxis-layer.vm	Wed Jan 23 17:32:30 2013 +0100
@@ -2,7 +2,8 @@
     NAME "$LAYER.getName()"
     TYPE $LAYER.getType()
 
-    INCLUDE "conf/dbconnection.include"
+    CONNECTIONTYPE $LAYER.getConnectionType()
+    CONNECTION "$LAYER.getConnection()"
 
     DATA    "$LAYER.getData()"
     FILTER  "$LAYER.getFilter()"
@@ -51,4 +52,4 @@
             
         END
    #end
-END
\ No newline at end of file
+END
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/MapArtifact.java	Wed Jan 23 13:20:39 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/MapArtifact.java	Wed Jan 23 17:32:30 2013 +0100
@@ -21,6 +21,7 @@
 
 import de.intevation.flys.model.River;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.MapUtils;
 
 import de.intevation.flys.artifacts.RiverAxisArtifact.RiverAxisState;
 import de.intevation.flys.artifacts.states.DefaultState;
@@ -163,7 +164,6 @@
                 getUrl());
 
             String name = type + "-" + artifact.identifier();
-
             facet.addLayer(name);
             facet.setExtent(getExtent(false));
             facet.setOriginalExtent(getExtent(true));
@@ -171,8 +171,8 @@
             facet.setData(getDataString());
             facet.setFilter(getFilter());
             facet.setGeometryType(getGeometryType());
-            facet.setConnection(getConnection());
-            facet.setConnectionType(getConnectionType());
+            facet.setConnection(MapUtils.getConnection());
+            facet.setConnectionType(MapUtils.getConnectionType());
             facet.setLabelItem(getLabelItem());
 
             facets.add(facet);
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java	Wed Jan 23 13:20:39 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java	Wed Jan 23 17:32:30 2013 +0100
@@ -14,6 +14,7 @@
 import de.intevation.flys.artifacts.states.DefaultState;
 import de.intevation.flys.backend.SessionFactoryProvider;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.MapUtils;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -146,8 +147,8 @@
             facet.setData(getDataString());
             facet.setFilter(getFilter());
             facet.setGeometryType(getGeometryType());
-            facet.setConnection(getConnection());
-            facet.setConnectionType(getConnectionType());
+            facet.setConnection(MapUtils.getConnection());
+            facet.setConnectionType(MapUtils.getConnectionType());
             facet.setLabelItem(getLabelItem());
 
             facets.add(facet);
@@ -155,128 +156,6 @@
             return null;
         }
 
-        /**
-         * This method returns a connection string for databases used by
-         * Mapserver's Mapfile.
-         *
-         * @return A connection string for Mapserver.
-         */
-        protected String getConnection() {
-            SessionFactoryImpl sf = (SessionFactoryImpl)
-                SessionFactoryProvider.getSessionFactory();
-
-            String user = SessionFactoryProvider.getUser(sf);
-            String pass = SessionFactoryProvider.getPass(sf);
-            String url  = SessionFactoryProvider.getURL(sf);
-
-            logger.debug("Parse connection url: " + url);
-
-            Matcher m = DB_URL_PATTERN.matcher(url);
-            if (!m.matches()) {
-                logger.warn("Could not parse Connection string." +
-                	"Try to parse PostgreSQL string.");
-                // maybe this is a PostgreSQL connection...
-                return getPostgreSQLConnection();
-            }
-
-            logger.debug("Groups for connection string: " + m.groupCount());
-            int groups = m.groupCount();
-
-            for (int i = 0; i <= groups; i++) {
-                logger.debug("Group " + i + ": " + m.group(i));
-            }
-
-            String connection = null;
-
-            if (FLYSUtils.isUsingOracle()) {
-                if (groups < 3) {
-                    logger.warn("Could only partially parse connection string.");
-                    return null;
-                }
-
-                String host = m.group(2);
-                String port = m.group(3);
-
-                connection = user + "/" + pass + "@" + host;
-            }
-            else {
-                if (groups < 4) {
-                    logger.warn("Could only partially parse connection string.");
-                    return null;
-                }
-
-                String host = m.group(2);
-                String port = m.group(3);
-                String db   = m.group(4);
-
-                StringBuilder sb = new StringBuilder();
-                sb.append("dbname=" + db);
-                sb.append("host='" + host + "'");
-                sb.append("port=" + port);
-                sb.append("password='" + pass + "'");
-                sb.append("sslmode=disable");
-
-                connection = sb.toString();
-            }
-
-            logger.debug("Created connection: '" + connection + "'");
-
-            return connection;
-        }
-
-        protected String getPostgreSQLConnection() {
-            SessionFactoryImpl sf = (SessionFactoryImpl)
-                SessionFactoryProvider.getSessionFactory();
-
-            String user = SessionFactoryProvider.getUser(sf);
-            String pass = SessionFactoryProvider.getPass(sf);
-            String url  = SessionFactoryProvider.getURL(sf);
-
-            Matcher m = DB_PSQL_URL_PATTERN.matcher(url);
-            if (!m.matches()) {
-                logger.warn("Could not parse PostgreSQL Connection string.");
-                return null;
-            }
-
-            int groups = m.groupCount();
-            logger.debug("Groups for PostgreSQL connection string: " + groups);
-
-            if (logger.isDebugEnabled()) {
-                for (int i = 0; i <= groups; i++) {
-                    logger.debug("Group " + i + ": " + m.group(i));
-                }
-            }
-
-            String connection = null;
-
-            if (groups < 4) {
-                logger.warn("Could only partially parse connection string.");
-                return null;
-            }
-
-            String host = m.group(2);
-            String port = m.group(3);
-            String db   = m.group(4);
-
-            StringBuilder sb = new StringBuilder();
-            sb.append("dbname=" + db);
-            sb.append(" host='" + host + "'");
-            sb.append(" port=" + port);
-            sb.append(" user=" + user);
-            sb.append(" password='" + pass + "'");
-            sb.append(" sslmode=disable");
-
-            connection = sb.toString();
-
-            logger.debug("Created connection: '" + connection + "'");
-
-            return connection;
-        }
-
-        protected String getConnectionType() {
-            return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
-        }
-
         protected String getLabelItem() {
             return null;
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java	Wed Jan 23 17:32:30 2013 +0100
@@ -0,0 +1,141 @@
+package de.intevation.flys.utils;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+import org.hibernate.impl.SessionFactoryImpl;
+
+import de.intevation.flys.backend.SessionFactoryProvider;
+
+
+public class MapUtils
+{
+    private static final Logger logger = Logger.getLogger(MapUtils.class);
+
+    public static final Pattern DB_URL_PATTERN =
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
+
+    public static final Pattern DB_PSQL_URL_PATTERN =
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)");
+
+    /**
+     * This method returns a connection string for databases used by
+     * Mapserver's Mapfile.
+     *
+     * @return A connection string for Mapserver.
+     */
+    public static String getConnection() {
+        SessionFactoryImpl sf = (SessionFactoryImpl)
+        SessionFactoryProvider.getSessionFactory();
+
+        String user = SessionFactoryProvider.getUser(sf);
+        String pass = SessionFactoryProvider.getPass(sf);
+        String url  = SessionFactoryProvider.getURL(sf);
+
+        logger.debug("Parse connection url: " + url);
+
+        Matcher m = DB_URL_PATTERN.matcher(url);
+        if (!m.matches()) {
+            logger.warn("Could not parse Connection string." +
+                "Try to parse PostgreSQL string.");
+            // maybe this is a PostgreSQL connection...
+            return getPostgreSQLConnection();
+        }
+
+        logger.debug("Groups for connection string: " + m.groupCount());
+        int groups = m.groupCount();
+
+        for (int i = 0; i <= groups; i++) {
+            logger.debug("Group " + i + ": " + m.group(i));
+        }
+
+        String connection = null;
+
+        if (FLYSUtils.isUsingOracle()) {
+            if (groups < 3) {
+                logger.warn("Could only partially parse connection string.");
+                return null;
+            }
+
+            String host = m.group(2);
+            String port = m.group(3);
+
+            connection = user + "/" + pass + "@" + host;
+        }
+        else {
+            if (groups < 4) {
+                logger.warn("Could only partially parse connection string.");
+                return null;
+            }
+
+            String host = m.group(2);
+            String port = m.group(3);
+            String db   = m.group(4);
+
+            StringBuilder sb = new StringBuilder();
+            sb.append("dbname=" + db);
+            sb.append("host='" + host + "'");
+            sb.append("port=" + port);
+            sb.append("password='" + pass + "'");
+            sb.append("sslmode=disable");
+
+            connection = sb.toString();
+        }
+
+        return connection;
+    }
+
+    protected static String getPostgreSQLConnection() {
+        SessionFactoryImpl sf = (SessionFactoryImpl)
+        SessionFactoryProvider.getSessionFactory();
+
+        String user = SessionFactoryProvider.getUser(sf);
+        String pass = SessionFactoryProvider.getPass(sf);
+        String url  = SessionFactoryProvider.getURL(sf);
+
+        Matcher m = DB_PSQL_URL_PATTERN.matcher(url);
+        if (!m.matches()) {
+            logger.warn("Could not parse PostgreSQL Connection string.");
+            return null;
+        }
+
+        int groups = m.groupCount();
+        logger.debug("Groups for PostgreSQL connection string: " + groups);
+
+        if (logger.isDebugEnabled()) {
+            for (int i = 0; i <= groups; i++) {
+                logger.debug("Group " + i + ": " + m.group(i));
+            }
+        }
+
+        String connection = null;
+
+        if (groups < 4) {
+            logger.warn("Could only partially parse connection string.");
+            return null;
+        }
+
+        String host = m.group(2);
+        String port = m.group(3);
+        String db   = m.group(4);
+
+        StringBuilder sb = new StringBuilder();
+        sb.append("dbname=" + db);
+        sb.append(" host='" + host + "'");
+        sb.append(" port=" + port);
+        sb.append(" user=" + user);
+        sb.append(" password='" + pass + "'");
+        sb.append(" sslmode=disable");
+
+        connection = sb.toString();
+
+        logger.debug("Created connection: '" + connection + "'");
+
+        return connection;
+    }
+
+    public static String getConnectionType() {
+        return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
+    }
+}
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java	Wed Jan 23 13:20:39 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java	Wed Jan 23 17:32:30 2013 +0100
@@ -5,6 +5,7 @@
 
 import de.intevation.flys.artifacts.model.LayerInfo;
 import de.intevation.flys.artifacts.model.RiverFactory;
+import de.intevation.flys.backend.SessionFactoryProvider;
 import de.intevation.flys.model.River;
 import de.intevation.flys.model.RiverAxis;
 
@@ -12,9 +13,12 @@
 import java.io.FileNotFoundException;
 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.apache.velocity.Template;
+import org.hibernate.impl.SessionFactoryImpl;
 
 public class RiverMapfileGenerator extends MapfileGenerator {
 
@@ -39,6 +43,12 @@
     public static final String XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH =
             "/artifact-database/rivermap/mapserver/templates/@path";
 
+    public static final Pattern DB_URL_PATTERN =
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
+
+    public static final Pattern DB_PSQL_URL_PATTERN =
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)");
+
     private static Logger logger = Logger.getLogger(RiverMapfileGenerator.class);
 
     /**
@@ -80,6 +90,8 @@
     protected void createRiverAxisLayer(String riverName, int riverID, String srid, String extend) {
         LayerInfo layerInfo = new LayerInfo();
         layerInfo.setName(riverName);
+        layerInfo.setConnection(MapUtils.getConnection());
+        layerInfo.setConnectionType(MapUtils.getConnectionType());
         layerInfo.setSrid(srid);
         layerInfo.setExtent(extend);
         layerInfo.setType("line");

http://dive4elements.wald.intevation.org