changeset 180:3908bfb2aa43

Store imported rivers into database. Needs testing! flys-backend/trunk@1488 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 16 Mar 2011 18:23:45 +0000 (2011-03-16)
parents aaac9c291067
children 4d9984411b3f
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/Importer.java flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java
diffstat 3 files changed, 84 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Wed Mar 16 17:28:16 2011 +0000
+++ b/flys-backend/ChangeLog	Wed Mar 16 18:23:45 2011 +0000
@@ -1,3 +1,11 @@
+2011-03-16	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/InfoGewParser.java:
+	  Expose imported rivers.
+
+	* src/main/java/de/intevation/flys/importer/InfoGewParser.java:
+	  Store imported rivers into database. Needs testing!
+
 2011-03-16	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
--- a/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Wed Mar 16 17:28:16 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Wed Mar 16 18:23:45 2011 +0000
@@ -3,12 +3,82 @@
 import java.io.File;
 import java.io.IOException;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
+import org.hibernate.SessionFactory;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.Query;
+
+import de.intevation.flys.backend.SessionFactoryProvider;
+
+import de.intevation.flys.model.River;
+
 public class Importer
 {
     private static Logger log = Logger.getLogger(Importer.class);
 
+    protected List<ImportRiver> rivers;
+
+    protected Session session;
+
+    public Importer() {
+    }
+
+    public Importer(List<ImportRiver> rivers) {
+        this.rivers = rivers;
+    }
+
+    public List<ImportRiver> getRivers() {
+        return rivers;
+    }
+
+    public void setRivers(List<ImportRiver> rivers) {
+        this.rivers = rivers;
+    }
+
+    public void writeRivers() {
+        Query query = session.createQuery("select River where name=:name");
+        for (ImportRiver iriver: rivers) {
+            query.setString("name", iriver.getName());
+            List<River> dbrivers = (List<River>)query.list();
+            if (dbrivers.isEmpty()) {
+                River dbriver = new River(iriver.getName());
+                session.save(dbriver);
+            }
+        }
+    }
+
+    public void writeToDatabase() {
+        SessionFactory sessionFactory =
+            SessionFactoryProvider.createSessionFactory();
+
+        session = sessionFactory.openSession();
+        Transaction tx = null;
+
+        try {
+            tx = session.beginTransaction();
+
+            writeRivers();
+
+            tx.commit();
+        }
+        catch (RuntimeException re) {
+            if (tx != null) {
+                tx.rollback();
+            }
+            throw re;
+        }
+        finally {
+            if (session != null) {
+                session.close();
+                session = null;
+            }
+        }
+    }
+
     public static void main(String [] args) {
 
         InfoGewParser infoGewParser = new InfoGewParser();
@@ -21,8 +91,9 @@
             catch (IOException ioe) {
                 log.error("cannot while parsing: " + gew);
             }
-            
         }
+
+        new Importer(infoGewParser.getRivers()).writeToDatabase();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java	Wed Mar 16 17:28:16 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java	Wed Mar 16 18:23:45 2011 +0000
@@ -38,6 +38,10 @@
         rivers = new ArrayList<ImportRiver>();
     }
 
+    public List<ImportRiver> getRivers() {
+        return rivers;
+    }
+
     public static final String normalize(String f) {
         return f.replace("\\", "/").replace("/", File.separator);
     }

http://dive4elements.wald.intevation.org