Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/data/importer/LAFParser.java @ 337:cb47c33b119d
Imporved error/warning handling and fixed some bugs in the importer module.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Wed, 28 Aug 2013 15:11:50 +0200 |
parents | 5d11428e6a09 |
children | 3f03e954c04d |
comparison
equal
deleted
inserted
replaced
336:53417b61391c | 337:cb47c33b119d |
---|---|
1 package de.intevation.lada.data.importer; | 1 package de.intevation.lada.data.importer; |
2 | 2 |
3 import java.util.ArrayList; | 3 import java.util.ArrayList; |
4 import java.util.HashMap; | 4 import java.util.HashMap; |
5 import java.util.LinkedList; | |
6 import java.util.List; | 5 import java.util.List; |
7 import java.util.Map; | 6 import java.util.Map; |
8 | 7 |
9 import javax.inject.Inject; | 8 import javax.inject.Inject; |
10 import javax.inject.Named; | 9 import javax.inject.Named; |
11 | 10 |
12 import de.intevation.lada.model.LKommentarM; | 11 import de.intevation.lada.auth.AuthenticationResponse; |
13 import de.intevation.lada.model.LKommentarP; | |
14 import de.intevation.lada.model.LMessung; | |
15 import de.intevation.lada.model.LMesswert; | |
16 import de.intevation.lada.model.LOrt; | |
17 import de.intevation.lada.model.LProbe; | |
18 import de.intevation.lada.model.LProbe; | |
19 | 12 |
20 | 13 |
21 public class LAFParser { | 14 public class LAFParser { |
22 | 15 |
23 private static final String PROBE_NEXT = "\n%PROBE%"; | 16 private static final String PROBE_NEXT = "\n%PROBE%"; |
26 | 19 |
27 @Inject | 20 @Inject |
28 @Named("lafproducer") | 21 @Named("lafproducer") |
29 private Producer producer; | 22 private Producer producer; |
30 | 23 |
31 List<LProbe> proben; | 24 @Inject |
32 List<LMessung> messungen; | 25 @Named("lafwriter") |
33 List<LOrt> orte; | 26 private Writer writer; |
34 List<LMesswert> messwerte; | |
35 List<LKommentarP> probeKommentare; | |
36 List<LKommentarM> messungKommentare; | |
37 | 27 |
38 private Map<String, List<ReportData>> warnings; | 28 private Map<String, List<ReportData>> warnings; |
39 private Map<String, List<ReportData>> errors; | 29 private Map<String, List<ReportData>> errors; |
40 | 30 |
41 public LAFParser() { | 31 public LAFParser() { |
42 this.warnings = new HashMap<String, List<ReportData>>(); | 32 this.warnings = new HashMap<String, List<ReportData>>(); |
43 this.errors = new HashMap<String, List<ReportData>>(); | 33 this.errors = new HashMap<String, List<ReportData>>(); |
44 this.setDryRun(false); | 34 this.setDryRun(false); |
45 //this.producer = new LAFProducer(); | 35 } |
46 this.proben = new ArrayList<LProbe>(); | 36 |
47 this.messungen = new ArrayList<LMessung>(); | 37 public boolean parse(AuthenticationResponse auth, String laf) |
48 this.orte = new ArrayList<LOrt>(); | |
49 this.messwerte = new ArrayList<LMesswert>(); | |
50 this.probeKommentare = new ArrayList<LKommentarP>(); | |
51 this.messungKommentare = new ArrayList<LKommentarM>(); | |
52 } | |
53 | |
54 public boolean parse(String laf) | |
55 throws LAFParserException | 38 throws LAFParserException |
56 { | 39 { |
57 this.proben.clear(); | |
58 this.messungen.clear(); | |
59 this.orte.clear(); | |
60 this.messwerte.clear(); | |
61 this.probeKommentare.clear(); | |
62 this.messungKommentare.clear(); | |
63 | |
64 if (!laf.startsWith("%PROBE%\n")) { | 40 if (!laf.startsWith("%PROBE%\n")) { |
65 throw new LAFParserException("No %PROBE% at the begining."); | 41 throw new LAFParserException("No %PROBE% at the begining."); |
66 } | 42 } |
67 boolean parsed = false; | 43 boolean parsed = false; |
68 while (laf.startsWith("%PROBE%\n")) { | 44 while (laf.startsWith("%PROBE%\n")) { |
72 if (nextPos > 0) { | 48 if (nextPos > 0) { |
73 single = laf.substring(0, nextPos + 1); | 49 single = laf.substring(0, nextPos + 1); |
74 laf = laf.substring(nextPos + 1); | 50 laf = laf.substring(nextPos + 1); |
75 try { | 51 try { |
76 readAll(single); | 52 readAll(single); |
53 this.warnings.putAll(producer.getWarnings()); | |
54 this.errors.putAll(producer.getErrors()); | |
55 writeAll(auth); | |
56 this.producer.reset(); | |
77 } | 57 } |
78 catch (LAFParserException lpe) { | 58 catch (LAFParserException lpe) { |
79 this.errors.putAll(producer.getErrors()); | 59 Map<String, List<ReportData>> pErr = producer.getErrors(); |
60 if (pErr.isEmpty()) { | |
61 List<ReportData> err = new ArrayList<ReportData>(); | |
62 err.add(new ReportData("parser", lpe.getMessage(), 673)); | |
63 this.errors.put("parser", err); | |
64 this.warnings.put("parser", new ArrayList<ReportData>()); | |
65 } | |
66 else { | |
67 this.errors.putAll(pErr); | |
68 this.warnings.putAll(producer.getWarnings()); | |
69 } | |
80 this.producer.reset(); | 70 this.producer.reset(); |
81 continue; | 71 continue; |
82 } | 72 } |
83 } | 73 } |
84 else { | 74 else { |
85 try { | 75 try { |
86 readAll(laf); | 76 readAll(laf); |
77 this.warnings.putAll(producer.getWarnings()); | |
78 this.errors.putAll(producer.getErrors()); | |
79 writeAll(auth); | |
80 this.producer.reset(); | |
87 laf = ""; | 81 laf = ""; |
88 } | 82 } |
89 catch (LAFParserException lpe) { | 83 catch (LAFParserException lpe) { |
90 this.errors.putAll(producer.getErrors()); | 84 Map<String, List<ReportData>> pErr = producer.getErrors(); |
85 if (pErr.isEmpty()) { | |
86 List<ReportData> err = new ArrayList<ReportData>(); | |
87 err.add(new ReportData("parser", lpe.getMessage(), 673)); | |
88 this.errors.put("parser", err); | |
89 this.warnings.put("parser", new ArrayList<ReportData>()); | |
90 } | |
91 else { | |
92 this.errors.putAll(pErr); | |
93 this.warnings.putAll(producer.getWarnings()); | |
94 } | |
95 this.producer.reset(); | |
91 laf = ""; | 96 laf = ""; |
92 continue; | 97 continue; |
93 } | 98 } |
94 } | 99 } |
95 if (!this.dryRun) { | |
96 proben.add(producer.getProbe()); | |
97 messungen.addAll(producer.getMessungen()); | |
98 orte.addAll(producer.getOrte()); | |
99 messwerte.addAll(producer.getMesswerte()); | |
100 probeKommentare.addAll(producer.getProbenKommentare()); | |
101 messungKommentare.addAll(producer.getMessungsKommentare()); | |
102 producer.reset(); | |
103 } | |
104 } | 100 } |
105 return parsed; | 101 return parsed; |
102 } | |
103 | |
104 private void writeAll(AuthenticationResponse auth) { | |
105 String probeId = producer.getProbe().getProbeId(); | |
106 boolean p = writer.writeProbe(auth, producer.getProbe()); | |
107 if (!p) { | |
108 this.errors.put(probeId, writer.getErrors()); | |
109 this.warnings.put(probeId, writer.getWarnings()); | |
110 return; | |
111 } | |
112 writer.writeProbenKommentare(auth, producer.getProbenKommentare()); | |
113 boolean m = writer.writeMessungen(auth, producer.getMessungen()); | |
114 if (!m) { | |
115 return; | |
116 } | |
117 writer.writeOrte(auth, producer.getOrte()); | |
118 writer.writeMessungKommentare(auth, producer.getMessungsKommentare()); | |
119 writer.writeMesswerte(auth, producer.getMesswerte()); | |
120 List<ReportData> err = this.errors.get(probeId); | |
121 if (err == null) { | |
122 this.errors.put(probeId, writer.getErrors()); | |
123 } | |
124 else { | |
125 err.addAll(writer.getErrors()); | |
126 } | |
127 List<ReportData> warn = this.warnings.get(probeId); | |
128 if (warn == null) { | |
129 this.warnings.put(probeId, writer.getWarnings()); | |
130 } | |
131 else { | |
132 warn.addAll(writer.getWarnings()); | |
133 } | |
106 } | 134 } |
107 | 135 |
108 private void readAll(String content) | 136 private void readAll(String content) |
109 throws LAFParserException | 137 throws LAFParserException |
110 { | 138 { |
138 if (current == ' ' && !value) { | 166 if (current == ' ' && !value) { |
139 key = false; | 167 key = false; |
140 white = true; | 168 white = true; |
141 continue; | 169 continue; |
142 } | 170 } |
143 else if (current != ' ' && white) { | 171 else if (current != ' ' && |
172 current != '\n' && | |
173 current != '\r' && | |
174 white) { | |
144 value = true; | 175 value = true; |
145 white = false; | 176 white = false; |
146 } | 177 } |
147 else if (current == '%' && !header && !value) { | 178 else if (current == '%' && !header && !value) { |
148 headerString = ""; | 179 headerString = ""; |
186 } | 217 } |
187 keyString = ""; | 218 keyString = ""; |
188 valueString = ""; | 219 valueString = ""; |
189 continue; | 220 continue; |
190 } | 221 } |
191 else if ((current == '\n' || current == '\r') && key) { | 222 if ((current == '\n' || current == '\r') && (key || white)) { |
192 throw new LAFParserException("No value for key: " + keyString); | 223 throw new LAFParserException("No value for key: " + keyString); |
193 } | 224 } |
194 | 225 |
195 if (key) { | 226 if (key) { |
196 keyString += current; | 227 keyString += current; |
212 return dryRun; | 243 return dryRun; |
213 } | 244 } |
214 | 245 |
215 public void setDryRun(boolean dryRun) { | 246 public void setDryRun(boolean dryRun) { |
216 this.dryRun = dryRun; | 247 this.dryRun = dryRun; |
217 } | |
218 | |
219 /** | |
220 * @return the proben | |
221 */ | |
222 public List<LProbe> getProben() { | |
223 return proben; | |
224 } | |
225 | |
226 /** | |
227 * @return the messungen | |
228 */ | |
229 public List<LMessung> getMessungen() { | |
230 return messungen; | |
231 } | |
232 | |
233 /** | |
234 * @return the orte | |
235 */ | |
236 public List<LOrt> getOrte() { | |
237 return orte; | |
238 } | |
239 | |
240 /** | |
241 * @return the messwerte | |
242 */ | |
243 public List<LMesswert> getMesswerte() { | |
244 return messwerte; | |
245 } | |
246 | |
247 /** | |
248 * @return the probeKommentare | |
249 */ | |
250 public List<LKommentarP> getProbeKommentare() { | |
251 return probeKommentare; | |
252 } | |
253 | |
254 /** | |
255 * @return the messungKommentare | |
256 */ | |
257 public List<LKommentarM> getMessungKommentare() { | |
258 return messungKommentare; | |
259 } | 248 } |
260 | 249 |
261 /** | 250 /** |
262 * @return the warnings | 251 * @return the warnings |
263 */ | 252 */ |