diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/GaugeTimerangeState.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 2966787b5188
children e86cd5176678
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/GaugeTimerangeState.java	Fri Sep 28 12:14:35 2012 +0200
@@ -0,0 +1,87 @@
+package de.intevation.flys.artifacts.states;
+
+import java.util.Calendar;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.SQLQuery;
+import org.hibernate.type.StandardBasicTypes;
+
+import de.intevation.flys.model.Gauge;
+import de.intevation.flys.backend.SessionHolder;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.utils.FLYSUtils;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class GaugeTimerangeState extends IntRangeState {
+
+    private static final Logger logger =
+        Logger.getLogger(GaugeTimerangeState.class);
+
+
+    protected int[] getLowerUpper(FLYSArtifact flys) {
+        Gauge gauge = FLYSUtils.getReferenceGauge(flys);
+
+        if (gauge == null) {
+            logger.warn("No reference gauge specified!");
+            return new int[] { 0, 0 };
+        }
+
+        Session session = SessionHolder.HOLDER.get();
+
+        SQLQuery query = session.createSQLQuery(
+            "SELECT min(start_time) as min, max(stop_time) as max " +
+            "FROM time_intervals WHERE id in " +
+            "(SELECT time_interval_id FROM discharge_tables " +
+            "WHERE gauge_id =:gid)");
+
+        query.addScalar("min", StandardBasicTypes.CALENDAR);
+        query.addScalar("max", StandardBasicTypes.CALENDAR);
+
+        query.setInteger("gid", gauge.getId());
+
+        List results = query.list();
+
+        if (results != null) {
+            Object[] res = (Object[]) results.get(0);
+
+            Calendar lo = (Calendar) res[0];
+            Calendar up = (Calendar) res[1];
+
+            return new int[] { lo.get(Calendar.YEAR), up.get(Calendar.YEAR) };
+        }
+
+        logger.warn("Could not determine time range for gauge: " + gauge);
+
+        return null;
+    }
+
+
+    @Override
+    protected Object getLower(FLYSArtifact flys) {
+        int[] lowerUpper = getLowerUpper(flys);
+
+        return lowerUpper != null ? lowerUpper[0] : 0;
+    }
+
+
+    @Override
+    protected Object getUpper(FLYSArtifact flys) {
+        int[] lowerUpper = getLowerUpper(flys);
+
+        return lowerUpper != null ? lowerUpper[1] : 0;
+    }
+
+
+    @Override
+    protected String getUIProvider() {
+        return "gaugetimerange";
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org