comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/MeasurementStationsParser.java @ 4193:f63b39799d2d

Adapted DB schema (added relation measurement_station); improved importer to read files from filesystem with measurement stations.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 22 Oct 2012 09:02:26 +0200
parents
children 278d8759c92b
comparison
equal deleted inserted replaced
4192:34337e357a54 4193:f63b39799d2d
1 package de.intevation.flys.importer.parsers;
2
3 import java.math.BigDecimal;
4 import java.text.ParseException;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.List;
8
9 import org.apache.log4j.Logger;
10
11 import de.intevation.flys.importer.ImportMeasurementStation;
12 import de.intevation.flys.importer.ImportRange;
13 import de.intevation.flys.importer.ImportTimeInterval;
14
15
16 public class MeasurementStationsParser extends LineParser {
17
18 public static class MeasurementStationParserException extends Exception {
19
20 private static final long serialVersionUID = 1L;
21
22 public MeasurementStationParserException(String msg) {
23 super(msg);
24 }
25 }
26
27 public static final int MIN_COLUMNS = 10;
28
29 private static final Logger log = Logger
30 .getLogger(MeasurementStationsParser.class);
31
32 private List<ImportMeasurementStation> measurementStations;
33 private ImportMeasurementStation current;
34
35 @Override
36 protected void reset() {
37 this.measurementStations = new ArrayList<ImportMeasurementStation>();
38 }
39
40 @Override
41 protected void finish() {
42 }
43
44 @Override
45 protected void handleLine(int lineNum, String line) {
46 if (line == null || line.startsWith(START_META_CHAR)) {
47 log.info("skip meta information at line " + lineNum);
48 return;
49 }
50
51 try {
52 current = new ImportMeasurementStation();
53 handleDataLine(line);
54 measurementStations.add(current);
55 }
56 catch (MeasurementStationParserException e) {
57 log.warn("Problem in line " + lineNum + ": " + e.getMessage());
58 }
59 }
60
61 public List<ImportMeasurementStation> getMeasurementStations() {
62 return measurementStations;
63 }
64
65 protected void handleDataLine(String line)
66 throws MeasurementStationParserException {
67 String[] cols = line.split(SEPERATOR_CHAR);
68
69 if (cols == null || cols.length < MIN_COLUMNS) {
70 int num = cols != null ? cols.length : 0;
71 throw new MeasurementStationParserException("Not enough columns: "
72 + num);
73 }
74
75 current.name = getName(cols);
76 current.station = getStation(cols);
77 current.range = getRange(cols);
78 current.measurementType = getMeasurementType(cols);
79 current.riverside = getRiverside(cols);
80 current.gauge = getGauge(cols);
81 current.observationTimerange = getObservationTimerange(cols);
82 current.operator = getOperator(cols);
83 current.comment = getComment(cols);
84
85 log.debug("Found new measurement station '" + current.name + "' at km "
86 + current.station);
87 }
88
89 protected String getName(String[] cols)
90 throws MeasurementStationParserException {
91 if (cols[0] == null || cols[0].length() == 0) {
92 throw new MeasurementStationParserException("invalid name '"
93 + cols[0] + "'");
94 }
95
96 return cols[0];
97 }
98
99 protected double getStation(String[] cols)
100 throws MeasurementStationParserException {
101 if (cols[1] == null || cols[1].length() == 0) {
102 throw new MeasurementStationParserException("invalid station '"
103 + cols[1] + "'");
104 }
105
106 try {
107 return getDouble(cols[1]);
108 }
109 catch (ParseException e) {
110 throw new MeasurementStationParserException(
111 "unable to parse station: " + e.getMessage());
112 }
113 }
114
115 protected ImportRange getRange(String[] cols)
116 throws MeasurementStationParserException {
117 if (cols[4] == null || cols[4].length() == 0) {
118 throw new MeasurementStationParserException("invalid lower range '"
119 + cols[4] + "'");
120 }
121
122 if (cols[5] == null || cols[5].length() == 0) {
123 throw new MeasurementStationParserException("invalid lower range '"
124 + cols[5] + "'");
125 }
126
127 try {
128 double lower = getDouble(cols[4]);
129 double upper = getDouble(cols[5]);
130
131 return new ImportRange(new BigDecimal(lower), new BigDecimal(upper));
132 }
133 catch (ParseException e) {
134 throw new MeasurementStationParserException(
135 "unable to parse range: " + e.getMessage());
136 }
137 }
138
139 protected String getMeasurementType(String[] cols)
140 throws MeasurementStationParserException {
141 if (cols[2] == null || cols[2].length() == 0) {
142 throw new MeasurementStationParserException(
143 "invalid measurement type '" + cols[2] + "'");
144 }
145
146 return cols[2];
147 }
148
149 protected String getRiverside(String[] cols) {
150 return cols[3];
151 }
152
153 protected String getGauge(String[] cols)
154 throws MeasurementStationParserException {
155 if (cols[6] == null || cols[6].length() == 0) {
156 throw new MeasurementStationParserException("invalid gauge '"
157 + cols[6] + "'");
158 }
159
160 return cols[6];
161 }
162
163 protected ImportTimeInterval getObservationTimerange(String[] cols)
164 throws MeasurementStationParserException {
165 if (cols[8] == null || cols[8].length() == 0) {
166 throw new MeasurementStationParserException(
167 "invalid observation time '" + cols[8] + "'");
168 }
169
170 try {
171 Date date = getDate(cols[8]);
172
173 if (date != null) {
174 return new ImportTimeInterval(date);
175 }
176
177 throw new MeasurementStationParserException(
178 "invalid observation time '" + cols[8] + "'");
179 }
180 catch (ParseException pe) {
181 throw new MeasurementStationParserException(pe.getMessage());
182 }
183 }
184
185 protected String getOperator(String[] cols) {
186 return cols[9];
187 }
188
189 protected String getComment(String[] cols) {
190 return cols.length > 10 ? cols[10] : null;
191 }
192 }

http://dive4elements.wald.intevation.org