Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/Importer.java @ 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 |
parents | 31895d24387e |
children | 1c0afb01bc93 |
comparison
equal
deleted
inserted
replaced
179:aaac9c291067 | 180:3908bfb2aa43 |
---|---|
1 package de.intevation.flys.importer; | 1 package de.intevation.flys.importer; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 | 5 |
6 import java.util.List; | |
7 | |
6 import org.apache.log4j.Logger; | 8 import org.apache.log4j.Logger; |
9 | |
10 import org.hibernate.SessionFactory; | |
11 import org.hibernate.Session; | |
12 import org.hibernate.Transaction; | |
13 import org.hibernate.Query; | |
14 | |
15 import de.intevation.flys.backend.SessionFactoryProvider; | |
16 | |
17 import de.intevation.flys.model.River; | |
7 | 18 |
8 public class Importer | 19 public class Importer |
9 { | 20 { |
10 private static Logger log = Logger.getLogger(Importer.class); | 21 private static Logger log = Logger.getLogger(Importer.class); |
22 | |
23 protected List<ImportRiver> rivers; | |
24 | |
25 protected Session session; | |
26 | |
27 public Importer() { | |
28 } | |
29 | |
30 public Importer(List<ImportRiver> rivers) { | |
31 this.rivers = rivers; | |
32 } | |
33 | |
34 public List<ImportRiver> getRivers() { | |
35 return rivers; | |
36 } | |
37 | |
38 public void setRivers(List<ImportRiver> rivers) { | |
39 this.rivers = rivers; | |
40 } | |
41 | |
42 public void writeRivers() { | |
43 Query query = session.createQuery("select River where name=:name"); | |
44 for (ImportRiver iriver: rivers) { | |
45 query.setString("name", iriver.getName()); | |
46 List<River> dbrivers = (List<River>)query.list(); | |
47 if (dbrivers.isEmpty()) { | |
48 River dbriver = new River(iriver.getName()); | |
49 session.save(dbriver); | |
50 } | |
51 } | |
52 } | |
53 | |
54 public void writeToDatabase() { | |
55 SessionFactory sessionFactory = | |
56 SessionFactoryProvider.createSessionFactory(); | |
57 | |
58 session = sessionFactory.openSession(); | |
59 Transaction tx = null; | |
60 | |
61 try { | |
62 tx = session.beginTransaction(); | |
63 | |
64 writeRivers(); | |
65 | |
66 tx.commit(); | |
67 } | |
68 catch (RuntimeException re) { | |
69 if (tx != null) { | |
70 tx.rollback(); | |
71 } | |
72 throw re; | |
73 } | |
74 finally { | |
75 if (session != null) { | |
76 session.close(); | |
77 session = null; | |
78 } | |
79 } | |
80 } | |
11 | 81 |
12 public static void main(String [] args) { | 82 public static void main(String [] args) { |
13 | 83 |
14 InfoGewParser infoGewParser = new InfoGewParser(); | 84 InfoGewParser infoGewParser = new InfoGewParser(); |
15 | 85 |
19 infoGewParser.parse(new File(gew)); | 89 infoGewParser.parse(new File(gew)); |
20 } | 90 } |
21 catch (IOException ioe) { | 91 catch (IOException ioe) { |
22 log.error("cannot while parsing: " + gew); | 92 log.error("cannot while parsing: " + gew); |
23 } | 93 } |
24 | |
25 } | 94 } |
95 | |
96 new Importer(infoGewParser.getRivers()).writeToDatabase(); | |
26 } | 97 } |
27 } | 98 } |
28 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 99 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |