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