diff flys-backend/src/main/java/org/dive4elements/river/importer/ImportSQRelationValue.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java@bc140668db82
children 18619c1e7c2a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/org/dive4elements/river/importer/ImportSQRelationValue.java	Thu Apr 25 11:53:11 2013 +0200
@@ -0,0 +1,140 @@
+package de.intevation.flys.importer;
+
+import java.math.BigDecimal;
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Query;
+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;
+
+
+public class ImportSQRelationValue {
+
+    private static Logger log = Logger.getLogger(ImportSQRelationValue.class);
+
+
+    private SQRelationValue peer;
+
+    private String parameter;
+
+    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,
+        Double km,
+        Double a,
+        Double b,
+        Double qMax,
+        Double rSQ,
+        Integer nTot,
+        Integer nOutlier,
+        Double cFerguson,
+        Double cDuan
+    ) {
+        this.parameter = parameter;
+        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;
+    }
+
+
+    public void storeDependencies(SQRelation owner)
+    throws SQLException, ConstraintViolationException
+    {
+        getPeer(owner);
+    }
+
+
+    public SQRelationValue getPeer(SQRelation owner) {
+        if (peer == null) {
+            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 at km " + km);
+                return null;
+            }
+
+            Query query2 = session.createQuery(
+                "from SQRelationValue " +
+                "   where sqRelation=:owner " +
+                "   and parameter=:parameter" +
+                "   and measurementStation=:measurementStation" +
+                "   and a=:a" +
+                "   and b=:b" +
+                "   and qMax=:qMax" +
+                "   and rSQ=:rSQ" +
+                "   and cFerguson=:cFerguson" +
+                "   and cDuan=:cDuan");
+
+            query2.setParameter("owner", owner);
+            query2.setString("parameter", parameter);
+            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 = query2.list();
+
+            if (values.isEmpty()) {
+                peer = new SQRelationValue(
+                    owner,
+                    parameter,
+                    result.get(0),
+                    a,
+                    b,
+                    qMax,
+                    rSQ,
+                    nTot,
+                    nOutlier,
+                    cFerguson,
+                    cDuan
+                );
+
+                session.save(peer);
+            }
+            else {
+                peer = values.get(0);
+            }
+        }
+        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