comparison backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 8412:17db08570637

SCHEMA CHANGE: removed superfluous columns station and river_id from measurement_stations and adapted other components accordingly.
author Tom Gottfried <tom@intevation.de>
date Wed, 15 Oct 2014 19:20:26 +0200
parents 4c3ccf2b0304
children 9db1f48bfea9
comparison
equal deleted inserted replaced
8411:b8c6cb36607e 8412:17db08570637
30 public MeasurementStationParserException(String msg) { 30 public MeasurementStationParserException(String msg) {
31 super(msg); 31 super(msg);
32 } 32 }
33 } 33 }
34 34
35 public static final int MIN_COLUMNS = 10; 35 public static final int MIN_COLUMNS = 9;
36
37 public static final int MAX_COMMENT_LENGTH = 512;
36 38
37 private static final Logger log = Logger 39 private static final Logger log = Logger
38 .getLogger(MeasurementStationsParser.class); 40 .getLogger(MeasurementStationsParser.class);
39 41
40 private List<ImportMeasurementStation> measurementStations; 42 private List<ImportMeasurementStation> measurementStations;
56 return; 58 return;
57 } 59 }
58 60
59 try { 61 try {
60 current = new ImportMeasurementStation(); 62 current = new ImportMeasurementStation();
61 handleDataLine(line); 63 handleDataLine(lineNum, line);
62 measurementStations.add(current); 64 measurementStations.add(current);
63 } 65 }
64 catch (MeasurementStationParserException e) { 66 catch (MeasurementStationParserException e) {
65 log.warn("Problem in line " + lineNum + ": " + e.getMessage()); 67 log.warn("Problem in line " + lineNum + ": " + e.getMessage());
66 } 68 }
68 70
69 public List<ImportMeasurementStation> getMeasurementStations() { 71 public List<ImportMeasurementStation> getMeasurementStations() {
70 return measurementStations; 72 return measurementStations;
71 } 73 }
72 74
73 protected void handleDataLine(String line) 75 protected void handleDataLine(int lineNum, String line)
74 throws MeasurementStationParserException { 76 throws MeasurementStationParserException {
75 String[] cols = line.split(SEPERATOR_CHAR); 77 String[] cols = line.split(SEPERATOR_CHAR);
76 78
77 if (cols == null || cols.length < MIN_COLUMNS) { 79 if (cols == null || cols.length < MIN_COLUMNS) {
78 int num = cols != null ? cols.length : 0; 80 int num = cols != null ? cols.length : 0;
79 throw new MeasurementStationParserException("Not enough columns: " 81 throw new MeasurementStationParserException("Not enough columns: "
80 + num); 82 + num);
81 } 83 }
82 84
83 current.name = getName(cols); 85 current.name = getName(cols, lineNum);
84 current.station = getStation(cols); 86 current.range = getRange(cols, lineNum);
85 current.range = getRange(cols); 87 current.measurementType = getMeasurementType(cols, lineNum);
86 current.measurementType = getMeasurementType(cols); 88 current.riverside = getRiverside(cols, lineNum);
87 current.riverside = getRiverside(cols); 89 current.gauge = getGauge(cols, lineNum);
88 current.gauge = getGauge(cols); 90 current.observationTimerange = getObservationTimerange(cols, lineNum);
89 current.observationTimerange = getObservationTimerange(cols); 91 current.operator = getOperator(cols, lineNum);
90 current.operator = getOperator(cols); 92 current.comment = getComment(cols, lineNum);
91 current.description = getDescription(cols); 93 }
92 94
93 log.debug("Found new measurement station '" + current.name + "' at km " 95 protected String getName(String[] cols, int lineNum)
94 + current.station);
95 }
96
97 protected String getName(String[] cols)
98 throws MeasurementStationParserException { 96 throws MeasurementStationParserException {
99 if (cols[0] == null || cols[0].length() == 0) { 97 if (cols[0] == null || cols[0].length() == 0) {
100 throw new MeasurementStationParserException("invalid name '" 98 throw new MeasurementStationParserException("invalid name in line "
101 + cols[0] + "'"); 99 + lineNum);
102 } 100 }
103 101
104 return cols[0]; 102 return cols[0];
105 } 103 }
106 104
107 protected double getStation(String[] cols) 105 protected ImportRange getRange(String[] cols, int lineNum) {
108 throws MeasurementStationParserException { 106 String from = cols[1];
109 if (cols[1] == null || cols[1].length() == 0) { 107 String to = cols[4];
110 throw new MeasurementStationParserException("invalid station '" 108 if (from == null || from.length() == 0) {
111 + cols[1] + "'"); 109 log.error("No station found in line" + lineNum);
110 return null;
112 } 111 }
113 112
114 try { 113 try {
115 return getDouble(cols[1]); 114 double lower = getDouble(from);
115
116 if (to == null || to.length() == 0) {
117 log.warn("No end km found in line " + lineNum);
118 return new ImportRange(new BigDecimal(lower));
119 }
120
121 try {
122 double upper = getDouble(to);
123
124 return new ImportRange(new BigDecimal(lower),
125 new BigDecimal(upper));
126 }
127 catch (ParseException e) {
128 log.warn("Unparseable end km in line " + lineNum +
129 ". Error: " + e.getMessage());
130 return new ImportRange(new BigDecimal(lower));
131 }
132
116 } 133 }
117 catch (ParseException e) { 134 catch (ParseException e) {
118 throw new MeasurementStationParserException( 135 log.error("Unparseable station in line " + lineNum +
119 "unable to parse station: " + e.getMessage()); 136 ". Error: " + e.getMessage());
120 }
121 }
122
123 protected ImportRange getRange(String[] cols) {
124 if (cols[4] == null || cols[4].length() == 0) {
125 log.warn("No upper value for range found in '" + cols[4] + "'");
126 return null; 137 return null;
127 } 138 }
128 139 }
129 if (cols[5] == null || cols[5].length() == 0) { 140
130 log.warn("No upper value for range found in '" + cols[5] + "'"); 141 protected String getMeasurementType(String[] cols, int lineNum)
131 return null;
132 }
133
134 try {
135 double lower = getDouble(cols[4]);
136 double upper = getDouble(cols[5]);
137
138 return new ImportRange(new BigDecimal(lower), new BigDecimal(upper));
139 }
140 catch (ParseException e) {
141 log.warn("unable to parse range: " + e.getMessage());
142 return null;
143 }
144 }
145
146 protected String getMeasurementType(String[] cols)
147 throws MeasurementStationParserException { 142 throws MeasurementStationParserException {
148 if (cols[2] == null || cols[2].length() == 0) { 143 if (cols[2] == null || cols[2].length() == 0) {
149 throw new MeasurementStationParserException( 144 throw new MeasurementStationParserException(
150 "invalid measurement type '" + cols[2] + "'"); 145 "invalid measurement type in line " + lineNum);
151 } 146 }
152 147
153 return cols[2]; 148 return cols[2];
154 } 149 }
155 150
156 protected String getRiverside(String[] cols) { 151 protected String getRiverside(String[] cols, int lineNum) {
157 return cols[3]; 152 String col = cols[3];
158 } 153 if (col == null || col.length() == 0) {
159 154 log.warn("No river side given in line " + lineNum);
160 protected String getGauge(String[] cols) { 155 }
161 if (cols[6] == null || cols[6].length() == 0) { 156 return col;
162 log.warn("invalid gauge found: '" + cols[6] + "'"); 157 }
163 } 158
164 159 protected String getGauge(String[] cols, int lineNum) {
165 return cols[6]; 160 String col = cols[5];
166 } 161 if (col == null || col.length() == 0) {
167 162 log.warn("Invalid gauge found in line " + lineNum);
168 protected ImportTimeInterval getObservationTimerange(String[] cols) { 163 }
169 if (cols[8] == null || cols[8].length() == 0) { 164 return col;
170 log.warn("Found invalid observation time '" + cols[8] + "'"); 165 }
166
167 protected ImportTimeInterval getObservationTimerange(
168 String[] cols,
169 int lineNum
170 ) {
171 String col = cols[7];
172 if (col == null || col.length() == 0) {
173 log.warn("Observation time invalid in line " + lineNum);
174 return null;
171 } 175 }
172 176
173 try { 177 try {
174 Date date = getDate(cols[8]); 178 Date date = getDate(col);
175 179
176 if (date != null) { 180 if (date != null) {
177 return new ImportTimeInterval(date); 181 return new ImportTimeInterval(date);
178 } 182 }
179 log.warn("Observation time date invalid: '" + cols[8] + "'"); 183 log.warn("Observation time invalid in line " + lineNum);
180 } 184 }
181 catch (ParseException pe) { 185 catch (ParseException pe) {
182 log.warn("Observation time date not parseable: '" + cols[8] + "'"); 186 log.warn("Unparseable observation time '" + col +
183 return null; 187 "' in line " + lineNum);
184 } 188 }
185 return null; 189 return null;
186 } 190 }
187 191
188 protected String getOperator(String[] cols) { 192 protected String getOperator(String[] cols, int lineNum) {
189 return cols[9]; 193 String col = cols[8];
190 } 194 if (col == null || col.length() == 0) {
191 195 log.warn("No operator given in line " + lineNum);
192 protected String getDescription(String[] cols) { 196 }
193 return cols.length > 10 ? cols[10] : null; 197 return col;
198 }
199
200 protected String getComment(String[] cols, int lineNum) {
201 if (cols.length > MIN_COLUMNS) {
202 String col = cols[9];
203 if (col.length() > MAX_COMMENT_LENGTH) {
204 log.warn("Comment in line " + lineNum +
205 " longer than allowed " + MAX_COMMENT_LENGTH +
206 " characters. Truncated.");
207 return col.substring(0, MAX_COMMENT_LENGTH);
208 }
209 return col;
210 }
211 return null;
194 } 212 }
195 } 213 }

http://dive4elements.wald.intevation.org