Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/Importer.java @ 7730:e1b831fe435a slt-simplify-cross-sections
Merged default into slt-simplify-cross-sections branch and updated package and class names.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Mon, 20 Jan 2014 14:04:20 +0100 |
parents | 8ca5d0e3c438 |
children | 3bb1c62ad732 |
comparison
equal
deleted
inserted
replaced
5084:ca45dd039b54 | 7730:e1b831fe435a |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.importer; | |
10 | |
11 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
12 | |
13 import org.dive4elements.river.importer.parsers.AnnotationClassifier; | |
14 import org.dive4elements.river.importer.parsers.BundesWasserStrassenParser; | |
15 import org.dive4elements.river.importer.parsers.InfoGewParser; | |
16 | |
17 import java.io.File; | |
18 import java.io.IOException; | |
19 | |
20 import java.util.HashMap; | |
21 import java.util.List; | |
22 import java.util.Map; | |
23 | |
24 import java.sql.SQLException; | |
25 | |
26 import org.apache.log4j.Logger; | |
27 | |
28 import org.hibernate.Transaction; | |
29 import org.hibernate.HibernateException; | |
30 | |
31 import org.w3c.dom.Document; | |
32 | |
33 import org.dive4elements.river.utils.StringUtil; | |
34 | |
35 /** Data Importer. Further processing happens per-river. */ | |
36 public class Importer | |
37 { | |
38 /** Private logger. */ | |
39 private static Logger log = Logger.getLogger(Importer.class); | |
40 | |
41 private static String BWASTR_ID_CSV_FILE = "BWASTR_ID.csv"; | |
42 | |
43 protected List<ImportRiver> rivers; | |
44 | |
45 public Importer() { | |
46 } | |
47 | |
48 public Importer(List<ImportRiver> rivers) { | |
49 this.rivers = rivers; | |
50 } | |
51 | |
52 public List<ImportRiver> getRivers() { | |
53 return rivers; | |
54 } | |
55 | |
56 public void setRivers(List<ImportRiver> rivers) { | |
57 this.rivers = rivers; | |
58 } | |
59 | |
60 /** Write rivers and their dependencies/dependants to db. */ | |
61 public void writeRivers() { | |
62 log.debug("write rivers started"); | |
63 | |
64 for (ImportRiver river: rivers) { | |
65 log.debug("writing river '" + river.getName() + "'"); | |
66 river.storeDependencies(); | |
67 ImporterSession.getInstance().getDatabaseSession().flush(); | |
68 } | |
69 | |
70 log.debug("write rivers finished"); | |
71 } | |
72 | |
73 public void writeToDatabase() { | |
74 | |
75 Transaction tx = null; | |
76 | |
77 try { | |
78 tx = ImporterSession.getInstance() | |
79 .getDatabaseSession().beginTransaction(); | |
80 | |
81 try { | |
82 writeRivers(); | |
83 } | |
84 catch (HibernateException he) { | |
85 Throwable t = he.getCause(); | |
86 while (t instanceof SQLException) { | |
87 SQLException sqle = (SQLException) t; | |
88 log.error("SQL exeception chain:", sqle); | |
89 t = sqle.getNextException(); | |
90 } | |
91 throw he; | |
92 } | |
93 | |
94 tx.commit(); | |
95 } | |
96 catch (RuntimeException re) { | |
97 if (tx != null) { | |
98 tx.rollback(); | |
99 } | |
100 throw re; | |
101 } | |
102 } | |
103 | |
104 public static AnnotationClassifier getAnnotationClassifier() { | |
105 String annotationTypes = Config.INSTANCE.getAnnotationTypes(); | |
106 | |
107 if (annotationTypes == null) { | |
108 log.info("no annotation types file configured."); | |
109 return null; | |
110 } | |
111 | |
112 File file = new File(annotationTypes); | |
113 | |
114 log.info("use annotation types file '" + file + "'"); | |
115 | |
116 if (!(file.isFile() && file.canRead())) { | |
117 log.warn("annotation type file '" + file + "' is not readable."); | |
118 return null; | |
119 } | |
120 | |
121 Document rules = XMLUtils.parseDocument(file); | |
122 | |
123 if (rules == null) { | |
124 log.warn("cannot parse annotation types file."); | |
125 return null; | |
126 } | |
127 | |
128 return new AnnotationClassifier(rules); | |
129 } | |
130 | |
131 | |
132 /** Starting point for importing river data. */ | |
133 public static void main(String [] args) { | |
134 | |
135 InfoGewParser infoGewParser = new InfoGewParser( | |
136 getAnnotationClassifier()); | |
137 | |
138 log.info("Start parsing rivers..."); | |
139 | |
140 File bwastrFile = null; | |
141 | |
142 for (String gew: args) { | |
143 log.info("parsing info gew file: " + gew); | |
144 File gewFile = new File(gew); | |
145 if (bwastrFile == null) { | |
146 bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE); | |
147 } | |
148 try { | |
149 infoGewParser.parse(gewFile); | |
150 } | |
151 catch (IOException ioe) { | |
152 log.error("error while parsing gew: " + gew, ioe); | |
153 System.exit(1); | |
154 } | |
155 } | |
156 | |
157 String gew = Config.INSTANCE.getInfoGewFile(); | |
158 if (gew != null && gew.length() > 0) { | |
159 log.info("parsing info gew file: " + gew); | |
160 File gewFile = new File(gew); | |
161 if (bwastrFile == null) { | |
162 bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE); | |
163 } | |
164 try { | |
165 infoGewParser.parse(gewFile); | |
166 } | |
167 catch (IOException ioe) { | |
168 log.error("error while parsing gew: " + gew, ioe); | |
169 System.exit(1); | |
170 } | |
171 } | |
172 | |
173 // Look for official numbers. | |
174 BundesWasserStrassenParser bwastrIdParser = | |
175 new BundesWasserStrassenParser(); | |
176 | |
177 // Read bwastFile (river-dir + BWASTR_ID_CSV_FILE). | |
178 if (!Config.INSTANCE.skipBWASTR()) { | |
179 try{ | |
180 bwastrIdParser.parse(bwastrFile); | |
181 HashMap<String,Long> map = bwastrIdParser.getMap(); | |
182 | |
183 // Now link rivers with official numbers. | |
184 for(ImportRiver river: infoGewParser.getRivers()) { | |
185 for(Map.Entry<String, Long> entry: map.entrySet()) { | |
186 if (StringUtil.containsIgnoreCase(river.getName(), entry.getKey())) { | |
187 river.setOfficialNumber(entry.getValue()); | |
188 log.debug(river.getName() + " is mapped to bwastr " + entry.getValue()); | |
189 } | |
190 } | |
191 } | |
192 } catch (IOException ioe) { | |
193 log.warn("BWASTR-file could not be loaded."); | |
194 } | |
195 } | |
196 else { | |
197 log.debug("skip reading BWASTR_ID.csv"); | |
198 } | |
199 | |
200 if (!Config.INSTANCE.dryRun()) { | |
201 new Importer(infoGewParser.getRivers()).writeToDatabase(); | |
202 } | |
203 else { | |
204 log.info("Dry run, not writing to database."); | |
205 } | |
206 } | |
207 } | |
208 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |