# HG changeset patch # User Sascha L. Teichmann # Date 1300299825 0 # Node ID 3908bfb2aa43a46045c7b8e61ecc709acac616a2 # Parent aaac9c2910671b4882045f3104c5b5e25859e16c Store imported rivers into database. Needs testing! flys-backend/trunk@1488 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r aaac9c291067 -r 3908bfb2aa43 flys-backend/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java: diff -r aaac9c291067 -r 3908bfb2aa43 flys-backend/src/main/java/de/intevation/flys/importer/Importer.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 rivers; + + protected Session session; + + public Importer() { + } + + public Importer(List rivers) { + this.rivers = rivers; + } + + public List getRivers() { + return rivers; + } + + public void setRivers(List 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 dbrivers = (List)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 : diff -r aaac9c291067 -r 3908bfb2aa43 flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java --- 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(); } + public List getRivers() { + return rivers; + } + public static final String normalize(String f) { return f.replace("\\", "/").replace("/", File.separator); }