changeset 4524:be9e28cff0c4

Parse and store year in sediment densities. * Year is parsed ('guessed') from description column in .csv file.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 14 Nov 2012 17:24:55 +0100
parents 504cd5801785
children f8ed8114f415
files flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensityValue.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java flys-backend/src/main/java/de/intevation/flys/model/SedimentDensityValue.java
diffstat 3 files changed, 69 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensityValue.java	Wed Nov 14 17:19:54 2012 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensityValue.java	Wed Nov 14 17:24:55 2012 +0100
@@ -25,16 +25,20 @@
 
     protected BigDecimal density;
 
+    private BigDecimal year;
+
     protected String description;
 
 
     public ImportSedimentDensityValue(
         BigDecimal station,
         BigDecimal density,
+        BigDecimal year,
         String     description
     ) {
         this.station     = station;
         this.density     = density;
+        this.year        = year;
         this.description = description;
     }
 
@@ -57,11 +61,13 @@
                 "   sedimentDensity=:sedimentDensity and " +
                 "   station=:station and " +
                 "   density=:density and " +
+                "   year=:year and " +
                 "   description=:description");
 
             query.setParameter("sedimentDensity", sedimentDensity);
             query.setParameter("station", station);
             query.setParameter("density", density);
+            query.setParameter("year", year);
             query.setParameter("description", description);
 
             List<SedimentDensityValue> values = query.list();
@@ -72,6 +78,7 @@
                     sedimentDensity,
                     station,
                     density,
+                    year,
                     description);
 
                 session.save(peer);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java	Wed Nov 14 17:19:54 2012 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java	Wed Nov 14 17:24:55 2012 +0100
@@ -5,10 +5,13 @@
 
 import java.math.BigDecimal;
 
+import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.text.ParseException;
 
 import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -19,6 +22,7 @@
 import de.intevation.flys.importer.ImportSedimentDensity;
 import de.intevation.flys.importer.ImportSedimentDensityValue;
 import de.intevation.flys.importer.ImportUnit;
+import de.intevation.flys.utils.DateGuesser;
 
 
 public class SedimentDensityParser extends LineParser {
@@ -26,6 +30,7 @@
     private static final Logger log =
         Logger.getLogger(SedimentDensityParser.class);
 
+    private int densitsyColumn = 1;
 
     public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
 
@@ -36,7 +41,6 @@
     public static final Pattern META_DEPTH =
         Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
 
-
     protected List<ImportSedimentDensity> sedimentDensities;
 
     protected ImportSedimentDensity current;
@@ -89,12 +93,27 @@
         else if (handleMetaDepth(line)) {
             return;
         }
+        else if (handleMetaColumns(line)) {
+            return;
+        }
         else {
             log.warn("Unknown meta line: '" + line + "'");
         }
     }
 
 
+    private boolean handleMetaColumns(String line) {
+        String[] columns = line.split(";");
+        for (int i = 0; i < columns.length; i++) {
+            if (columns[i].contains("Sedimentdichte")) {
+                this.densitsyColumn = i;
+                return true;
+            }
+        }
+        return false;
+    }
+
+
     protected boolean handleMetaUnit(String line) {
         Matcher m = META_UNIT.matcher(line);
 
@@ -151,19 +170,42 @@
             return;
         }
 
+        BigDecimal km;
+        BigDecimal density;
         try {
-            BigDecimal km      = new BigDecimal(nf.parse(vals[0]).doubleValue());
-            BigDecimal density = new BigDecimal(nf.parse(vals[1]).doubleValue());
+            km      = new BigDecimal(nf.parse(vals[0]).doubleValue());
+            density = new BigDecimal(nf.parse(vals[this.densitsyColumn]).doubleValue());
 
-            current.addValue(new ImportSedimentDensityValue(
-                km,
-                density,
-                vals[2])
-            );
         }
         catch (ParseException pe) {
             log.warn("Error while parsing numbers in '" + line + "'");
+            return;
         }
+
+        BigDecimal year = null;
+        try {
+            year =
+                new BigDecimal(nf.parse(vals[vals.length - 1]).doubleValue());
+        }
+        catch(ParseException pe) {
+            try {
+                Date d = DateGuesser.guessDate(vals[vals.length - 1]);
+                Calendar c = Calendar.getInstance();
+                c.setTime(d);
+                year = new BigDecimal(c.get(Calendar.YEAR));
+            }
+            catch (IllegalArgumentException iae) {
+                log.warn("Error while parsing date in '" + line + "'");
+                return;
+            }
+        }
+
+        current.addValue(new ImportSedimentDensityValue(
+            km,
+            density,
+            year,
+            vals[vals.length - 1])
+        );
     }
 
 
--- a/flys-backend/src/main/java/de/intevation/flys/model/SedimentDensityValue.java	Wed Nov 14 17:19:54 2012 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/model/SedimentDensityValue.java	Wed Nov 14 17:24:55 2012 +0100
@@ -24,6 +24,7 @@
 
     private BigDecimal station;
     private BigDecimal density;
+    private BigDecimal year;
 
     private String description;
 
@@ -36,11 +37,13 @@
         SedimentDensity sedimentDensity,
         BigDecimal      station,
         BigDecimal      density,
+        BigDecimal      year,
         String          desc
     ) {
         this.sedimentDensity = sedimentDensity;
         this.station         = station;
         this.density         = density;
+        this.year            = year;
         this.description     = desc;
     }
 
@@ -97,5 +100,14 @@
     public void setDescription(String description) {
         this.description = description;
     }
+
+    @Column(name = "year")
+    public BigDecimal getYear() {
+        return year;
+    }
+
+    public void setYear(BigDecimal year) {
+        this.year = year;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org