comparison flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java @ 1206:c7370734b872

Prevent parsing and storing PRF duplicates. flys-backend/trunk@2315 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 Jul 2011 09:31:52 +0000
parents 22858e7cca79
children f08fe480092c
comparison
equal deleted inserted replaced
1205:5f1506fc7636 1206:c7370734b872
2 2
3 import java.math.BigDecimal; 3 import java.math.BigDecimal;
4 4
5 import java.util.List; 5 import java.util.List;
6 import java.util.Map; 6 import java.util.Map;
7 import java.util.Set;
8 import java.util.HashSet;
7 import java.util.ArrayList; 9 import java.util.ArrayList;
8 import java.util.Date; 10 import java.util.Date;
9 import java.util.Calendar; 11 import java.util.Calendar;
10 12
11 import java.io.File; 13 import java.io.File;
14 import org.apache.log4j.Logger; 16 import org.apache.log4j.Logger;
15 17
16 import de.intevation.flys.model.River; 18 import de.intevation.flys.model.River;
17 19
18 import de.intevation.flys.utils.FileTools; 20 import de.intevation.flys.utils.FileTools;
21 import de.intevation.flys.utils.FileTools.HashedFile;
19 22
20 import org.hibernate.Session; 23 import org.hibernate.Session;
21 import org.hibernate.Query; 24 import org.hibernate.Query;
22 25
23 public class ImportRiver 26 public class ImportRiver
24 implements PRFParser.Callback
25 { 27 {
26 private static Logger log = Logger.getLogger(ImportRiver.class); 28 private static Logger log = Logger.getLogger(ImportRiver.class);
27 29
28 public static final String PEGEL_GLT = "PEGEL.GLT"; 30 public static final String PEGEL_GLT = "PEGEL.GLT";
29 31
360 PRFParser parser = new PRFParser(); 362 PRFParser parser = new PRFParser();
361 File riverDir = wstFile 363 File riverDir = wstFile
362 .getParentFile() // Basisdaten 364 .getParentFile() // Basisdaten
363 .getParentFile() // Hydrologie 365 .getParentFile() // Hydrologie
364 .getParentFile(); // <river> 366 .getParentFile(); // <river>
365 parser.parsePRFs(riverDir, this); 367
368 parser.parsePRFs(riverDir, new PRFParser.Callback() {
369
370 Set<HashedFile> prfs = new HashSet<HashedFile>();
371
372 @Override
373 public boolean prfAccept(File file) {
374 HashedFile hf = new HashedFile(file);
375 boolean success = prfs.add(hf);
376 if (!success) {
377 log.warn("PRF file '" + file + "' seems to be a duplicate.");
378 }
379 return success;
380 }
381
382 @Override
383 public void prfParsed(PRFParser parser) {
384 log.debug("callback from PRF parser");
385
386 String description = parser.getDescription();
387 Integer year = parser.getYear();
388 ImportTimeInterval ti = year != null
389 ? new ImportTimeInterval(yearToDate(year))
390 : null;
391
392 List<ImportCrossSectionLine> lines =
393 new ArrayList<ImportCrossSectionLine>();
394
395 for (Map.Entry<Double, List<XY>> entry: parser.getData().entrySet()) {
396 BigDecimal km = new BigDecimal(entry.getKey());
397 List<XY> points = entry.getValue();
398 lines.add(new ImportCrossSectionLine(km, points));
399 }
400
401 crossSections.add(new ImportCrossSection(
402 ImportRiver.this, description, ti, lines));
403 }
404 });
366 } 405 }
367 406
368 public static Date yearToDate(int year) { 407 public static Date yearToDate(int year) {
369 Calendar cal = Calendar.getInstance(); 408 Calendar cal = Calendar.getInstance();
370 cal.set(year, 5, 15, 12, 0, 0); 409 cal.set(year, 5, 15, 12, 0, 0);
371 long ms = cal.getTimeInMillis(); 410 long ms = cal.getTimeInMillis();
372 cal.setTimeInMillis(ms - ms%1000); 411 cal.setTimeInMillis(ms - ms%1000);
373 return cal.getTime(); 412 return cal.getTime();
374 }
375
376 @Override
377 public void prfParsed(PRFParser parser) {
378 log.debug("callback from PRF parser");
379
380 String description = parser.getDescription();
381 Integer year = parser.getYear();
382 ImportTimeInterval ti = year != null
383 ? new ImportTimeInterval(yearToDate(year))
384 : null;
385
386 List<ImportCrossSectionLine> lines =
387 new ArrayList<ImportCrossSectionLine>();
388
389 for (Map.Entry<Double, List<XY>> entry: parser.getData().entrySet()) {
390 BigDecimal km = new BigDecimal(entry.getKey());
391 List<XY> points = entry.getValue();
392 lines.add(new ImportCrossSectionLine(km, points));
393 }
394
395 crossSections.add(new ImportCrossSection(
396 this, description, ti, lines));
397 } 413 }
398 414
399 public void storeDependencies() { 415 public void storeDependencies() {
400 storeAnnotations(); 416 storeAnnotations();
401 storeCrossSections(); 417 storeCrossSections();

http://dive4elements.wald.intevation.org