diff flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java @ 5429:13596605e81f

Added new columns to sq relation importer to import all values from csv and use measurement station instead of km.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 26 Mar 2013 14:02:58 +0100
parents cc8fc6b29649
children e667c127b600
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java	Tue Mar 26 14:00:48 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java	Tue Mar 26 14:02:58 2013 +0100
@@ -1,5 +1,6 @@
 package de.intevation.flys.importer;
 
+import java.math.BigDecimal;
 import java.sql.SQLException;
 import java.util.List;
 
@@ -9,6 +10,7 @@
 import org.hibernate.Session;
 import org.hibernate.exception.ConstraintViolationException;
 
+import de.intevation.flys.model.MeasurementStation;
 import de.intevation.flys.model.SQRelation;
 import de.intevation.flys.model.SQRelationValue;
 
@@ -22,27 +24,42 @@
 
     private String parameter;
     private String fraction;
-    private String function;
 
-    private double km;
-    private double a;
-    private double b;
+    private Double km;
+    private Double a;
+    private Double b;
+    private Double qMax;
+    private Double rSQ;
+    private Integer nTot;
+    private Integer nOutlier;
+    private Double cFerguson;
+    private Double cDuan;
 
 
     public ImportSQRelationValue(
         String parameter,
         String fraction,
-        String function,
-        double km,
-        double a,
-        double b
+        Double km,
+        Double a,
+        Double b,
+        Double qMax,
+        Double rSQ,
+        Integer nTot,
+        Integer nOutlier,
+        Double cFerguson,
+        Double cDuan
     ) {
         this.parameter = parameter;
         this.fraction  = fraction;
-        this.function  = function;
         this.km        = km;
         this.a         = a;
         this.b         = b;
+        this.qMax      = qMax;
+        this.rSQ       = rSQ;
+        this.nTot      = nTot;
+        this.nOutlier  = nOutlier;
+        this.cFerguson = cFerguson;
+        this.cDuan     = cDuan;
     }
 
 
@@ -58,30 +75,58 @@
             Session session = ImporterSession.getInstance().getDatabaseSession();
 
             Query query = session.createQuery(
+                "from MeasurementStation " +
+                "   where station between :kml and :kmh");
+            query.setDouble("kml", km - 1e-4);
+            query.setDouble("kmh", km + 1e-4);
+
+            List<MeasurementStation> result = query.list();
+
+            if (result.isEmpty()) {
+                log.error("No measurement stations found.");
+                return null;
+            }
+
+            Query query2 = session.createQuery(
                 "from SQRelationValue " +
                 "   where sqRelation=:owner " +
                 "   and parameter=:parameter" +
                 "   and fraction=:fraction" +
-                "   and function=:function" +
-                "   and km=:km");
+                "   and measurementStation=:measurementStation" +
+                "   and a=:a" +
+                "   and b=:b" +
+                "   and qMax=:qMax" +
+                "   and rSQ=:rSQ" +
+                "   and cFerguson=:cFerguson" +
+                "   and cDuan=:cDuan");
 
-            query.setParameter("owner", owner);
-            query.setString("parameter", parameter);
-            query.setString("fraction", fraction);
-            query.setString("function", function);
-            query.setDouble("km", km);
+            query2.setParameter("owner", owner);
+            query2.setString("parameter", parameter);
+            query2.setString("fraction", fraction);
+            query2.setParameter("measurementStation", result.get(0));
+            query2.setBigDecimal("a", toBigDecimal(a));
+            query2.setBigDecimal("b", toBigDecimal(b));
+            query2.setBigDecimal("qMax", toBigDecimal(qMax));
+            query2.setBigDecimal("rSQ", toBigDecimal(rSQ));
+            query2.setBigDecimal("cFerguson", toBigDecimal(cFerguson));
+            query2.setBigDecimal("cDuan", toBigDecimal(cDuan));
 
-            List<SQRelationValue> values = query.list();
+            List<SQRelationValue> values = query2.list();
 
             if (values.isEmpty()) {
                 peer = new SQRelationValue(
                     owner,
                     parameter,
                     fraction,
-                    function,
-                    km,
+                    result.get(0),
                     a,
-                    b
+                    b,
+                    qMax,
+                    rSQ,
+                    nTot,
+                    nOutlier,
+                    cFerguson,
+                    cDuan
                 );
 
                 session.save(peer);
@@ -90,8 +135,12 @@
                 peer = values.get(0);
             }
         }
+        return peer;
+    }
 
-        return peer;
+    private static final BigDecimal toBigDecimal(Double x) {
+        if (x == null) return null;
+        return new BigDecimal(x);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org