Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/Importer.java @ 5027:25c180fa6162
Use BWASTRParser to set official numbers on rivers.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Tue, 19 Feb 2013 16:41:00 +0100 |
parents | 255898799ed9 |
children | b87e504f916e |
comparison
equal
deleted
inserted
replaced
5021:bf22086747bc | 5027:25c180fa6162 |
---|---|
1 package de.intevation.flys.importer; | 1 package de.intevation.flys.importer; |
2 | 2 |
3 import de.intevation.artifacts.common.utils.XMLUtils; | 3 import de.intevation.artifacts.common.utils.XMLUtils; |
4 | 4 |
5 import de.intevation.flys.importer.parsers.AnnotationClassifier; | |
6 import de.intevation.flys.importer.parsers.BundesWasserStrassenParser; | |
5 import de.intevation.flys.importer.parsers.InfoGewParser; | 7 import de.intevation.flys.importer.parsers.InfoGewParser; |
6 import de.intevation.flys.importer.parsers.AnnotationClassifier; | |
7 | 8 |
8 import java.io.File; | 9 import java.io.File; |
9 import java.io.IOException; | 10 import java.io.IOException; |
10 | 11 |
12 import java.util.HashMap; | |
11 import java.util.List; | 13 import java.util.List; |
14 import java.util.Map; | |
12 | 15 |
13 import java.sql.SQLException; | 16 import java.sql.SQLException; |
14 | 17 |
15 import org.apache.log4j.Logger; | 18 import org.apache.log4j.Logger; |
16 | 19 |
17 import org.hibernate.Transaction; | 20 import org.hibernate.Transaction; |
18 import org.hibernate.HibernateException; | 21 import org.hibernate.HibernateException; |
19 | 22 |
20 import org.w3c.dom.Document; | 23 import org.w3c.dom.Document; |
21 | 24 |
25 import de.intevation.flys.utils.StringUtil; | |
22 | 26 |
23 /** Data Importer. Further processing happens per-river. */ | 27 /** Data Importer. Further processing happens per-river. */ |
24 public class Importer | 28 public class Importer |
25 { | 29 { |
26 /** Private logger. */ | 30 /** Private logger. */ |
27 private static Logger log = Logger.getLogger(Importer.class); | 31 private static Logger log = Logger.getLogger(Importer.class); |
32 | |
33 private static String BWASTR_ID_CSV_FILE = "BWASTR_ID.csv"; | |
28 | 34 |
29 protected List<ImportRiver> rivers; | 35 protected List<ImportRiver> rivers; |
30 | 36 |
31 public Importer() { | 37 public Importer() { |
32 } | 38 } |
41 | 47 |
42 public void setRivers(List<ImportRiver> rivers) { | 48 public void setRivers(List<ImportRiver> rivers) { |
43 this.rivers = rivers; | 49 this.rivers = rivers; |
44 } | 50 } |
45 | 51 |
52 /** Write rivers and their dependencies/dependants to db. */ | |
46 public void writeRivers() { | 53 public void writeRivers() { |
47 log.debug("write rivers started"); | 54 log.debug("write rivers started"); |
48 | 55 |
49 for (ImportRiver river: rivers) { | 56 for (ImportRiver river: rivers) { |
50 log.debug("writing river '" + river.getName() + "'"); | 57 log.debug("writing river '" + river.getName() + "'"); |
120 InfoGewParser infoGewParser = new InfoGewParser( | 127 InfoGewParser infoGewParser = new InfoGewParser( |
121 getAnnotationClassifier()); | 128 getAnnotationClassifier()); |
122 | 129 |
123 log.info("Start parsing rivers..."); | 130 log.info("Start parsing rivers..."); |
124 | 131 |
132 File bwastrFile = null; | |
133 | |
125 for (String gew: args) { | 134 for (String gew: args) { |
126 log.info("parsing info gew file: " + gew); | 135 log.info("parsing info gew file: " + gew); |
136 File gewFile = new File(gew); | |
137 if (bwastrFile == null) { | |
138 bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE); | |
139 } | |
127 try { | 140 try { |
128 infoGewParser.parse(new File(gew)); | 141 infoGewParser.parse(gewFile); |
129 } | 142 } |
130 catch (IOException ioe) { | 143 catch (IOException ioe) { |
131 log.error("error while parsing gew: " + gew); | 144 log.error("error while parsing gew: " + gew); |
132 } | 145 } |
133 } | 146 } |
134 | 147 |
135 String gew = Config.INSTANCE.getInfoGewFile(); | 148 String gew = Config.INSTANCE.getInfoGewFile(); |
136 if (gew != null && gew.length() > 0) { | 149 if (gew != null && gew.length() > 0) { |
137 log.info("parsing info gew file: " + gew); | 150 log.info("parsing info gew file: " + gew); |
151 File gewFile = new File(gew); | |
152 if (bwastrFile == null) { | |
153 bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE); | |
154 } | |
138 try { | 155 try { |
139 infoGewParser.parse(new File(gew)); | 156 infoGewParser.parse(gewFile); |
140 } | 157 } |
141 catch (IOException ioe) { | 158 catch (IOException ioe) { |
142 log.error("error while parsing gew: " + gew); | 159 log.error("error while parsing gew: " + gew); |
143 } | 160 } |
161 } | |
162 | |
163 // Look for official numbers. | |
164 BundesWasserStrassenParser bwastrIdParser = | |
165 new BundesWasserStrassenParser(); | |
166 | |
167 // Read bwastFile (river-dir + BWASTR_ID_CSV_FILE). | |
168 if (!Config.INSTANCE.skipBWASTR()) { | |
169 try{ | |
170 bwastrIdParser.parse(bwastrFile); | |
171 HashMap<String,Long> map = bwastrIdParser.getMap(); | |
172 | |
173 // Now link rivers with official numbers. | |
174 for(ImportRiver river: infoGewParser.getRivers()) { | |
175 for(Map.Entry<String, Long> entry: map.entrySet()) { | |
176 if (StringUtil.containsIgnoreCase(entry.getKey(), river.getName())) { | |
177 river.setOfficialNumber(entry.getValue()); | |
178 river.storeOfficialNumber(); | |
179 log.debug(river.getName() + " is mapped to bwastr " + entry.getValue()); | |
180 } | |
181 } | |
182 } | |
183 } catch (IOException ioe) { | |
184 log.warn("BWASTR-file could not be loaded."); | |
185 } | |
186 } | |
187 else { | |
188 log.debug("skip reading BWASTR_ID.csv"); | |
144 } | 189 } |
145 | 190 |
146 if (!Config.INSTANCE.dryRun()) { | 191 if (!Config.INSTANCE.dryRun()) { |
147 new Importer(infoGewParser.getRivers()).writeToDatabase(); | 192 new Importer(infoGewParser.getRivers()).writeToDatabase(); |
148 } | 193 } |