changeset 2191:f28844a57fed

Added states (work is not finished yet) for the 'historical discharge curve' calculation. flys-artifacts/trunk@3801 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 27 Jan 2012 13:26:04 +0000
parents 2792d2617be9
children 922c672ec6c1
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/GaugeTimerangeState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ReferenceGaugeState.java
diffstat 5 files changed, 309 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jan 27 13:23:56 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jan 27 13:26:04 2012 +0000
@@ -1,3 +1,11 @@
+2012-01-27  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/states/ReferenceGaugeState.java,
+	  src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java,
+	  src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeState.java,
+	  src/main/java/de/intevation/flys/artifacts/states/GaugeTimerangeState.java:
+	  New. States used in the 'historical discharge curve' calculation.
+
 2012-01-27  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/MinMaxState.java: New
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/GaugeTimerangeState.java	Fri Jan 27 13:26:04 2012 +0000
@@ -0,0 +1,81 @@
+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;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java	Fri Jan 27 13:26:04 2012 +0000
@@ -0,0 +1,34 @@
+package de.intevation.flys.artifacts.states;
+
+
+import org.w3c.dom.Element;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+
+
+
+
+
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class HistoricalDischargeComputeState extends DefaultState {
+
+    @Override
+    protected void appendItems(
+        Artifact       artifact,
+        ElementCreator creator,
+        String         name,
+        CallContext    context,
+        Element        select
+    ) {
+        // TODO IMPLEMENT ME
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeState.java	Fri Jan 27 13:26:04 2012 +0000
@@ -0,0 +1,126 @@
+package de.intevation.flys.artifacts.states;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Element;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import de.intevation.flys.artifacts.resources.Resources;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class HistoricalDischargeState extends DefaultState {
+
+    private static final Logger logger =
+        Logger.getLogger(HistoricalDischargeState.class);
+
+
+    public static final String DATA_MODE   = "historical_mode";
+    public static final String DATA_VALUES = "historical_values";
+    public static final int    DATA_MODE_W = 0;
+    public static final int    DATA_MODE_Q = 1;
+
+
+    @Override
+    protected String getUIProvider() {
+        return "wq_simple_array";
+    }
+
+    protected void appendItems(
+        Artifact       artifact,
+        ElementCreator creator,
+        String         name,
+        CallContext    context,
+        Element        select
+    ) {
+        if (name != null && name.equals(DATA_VALUES)) {
+            select.setAttributeNS(
+                ArtifactNamespaceContext.NAMESPACE_URI,
+                "art:type",
+                "intarray");
+        }
+        else if (name != null && name.equals(DATA_MODE)) {
+            select.setAttributeNS(
+                ArtifactNamespaceContext.NAMESPACE_URI,
+                "art:type",
+                "intoptions");
+        }
+
+        super.appendItems(artifact, creator, name, context, select);
+    }
+
+
+    @Override
+    protected Element[] createItems(
+        ElementCreator creator,
+        Artifact       artifact,
+        String         name,
+        CallContext    context
+    ) {
+        logger.debug("createItems()");
+
+        if (name != null && name.equals(DATA_MODE)) {
+            return createModeItem(creator, artifact, name, context);
+        }
+        else if (name != null && name.equals(DATA_VALUES)) {
+            return createValuesItem(creator, artifact, name, context);
+        }
+
+        logger.warn("Tried to create item for invalid data: " + name);
+
+        return new Element[0];
+    }
+
+
+    protected Element[] createModeItem(
+        ElementCreator creator,
+        Artifact       artifact,
+        String         name,
+        CallContext    context
+    ) {
+        logger.debug("createModeItem()");
+
+        CallMeta meta = context.getMeta();
+
+        Element modeW = createItem(
+            creator,
+            new String[] {
+                Resources.getMsg(meta, "historical.mode.w","historical.mode.w"),
+                String.valueOf(DATA_MODE_W) } );
+
+        Element modeQ = createItem(
+            creator,
+            new String[] {
+                Resources.getMsg(meta, "historical.mode.q","historical.mode.q"),
+                String.valueOf(DATA_MODE_Q) } );
+
+        return new Element[] { modeW, modeQ };
+    }
+
+
+    protected Element[] createValuesItem(
+        ElementCreator creator,
+        Artifact       artifact,
+        String         name,
+        CallContext    context
+    ) {
+        logger.debug("createValuesItem()");
+
+        Element valuesW = createItem(
+            creator, new String[] { "ws", "" } );
+
+        Element valuesQ = createItem(
+            creator, new String[] { "qs", "" } );
+
+        return new Element[] { valuesW, valuesQ };
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ReferenceGaugeState.java	Fri Jan 27 13:26:04 2012 +0000
@@ -0,0 +1,60 @@
+package de.intevation.flys.artifacts.states;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Element;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+import de.intevation.flys.model.Gauge;
+import de.intevation.flys.model.River;
+
+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 ReferenceGaugeState extends DefaultState {
+
+    private static final Logger logger =
+        Logger.getLogger(ReferenceGaugeState.class);
+
+
+    @Override
+    protected Element[] createItems(
+        XMLUtils.ElementCreator cr,
+        Artifact    artifact,
+        String      name,
+        CallContext context)
+    {
+        CallMeta  meta = context.getMeta();
+
+        River       river   = FLYSUtils.getRiver((FLYSArtifact) artifact);
+        List<Gauge> gauges  = river.getGauges();
+
+        int num = gauges != null ? gauges.size() : 0;
+
+        Element[] opts = new Element[num];
+
+        for (int i = 0; i < num; i++ ) {
+            Gauge g = gauges.get(i);
+
+            String gaugeName      = g.getName();
+            long   officialNumber = g.getOfficialNumber();
+
+            opts[i] = createItem(
+                cr, new String[] { gaugeName, String.valueOf(officialNumber) });
+        }
+
+        return opts;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org