changeset 7236:a9bd4a23a852

(issue1474) Add datacage loading for SQ Relations
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 02 Oct 2013 17:04:23 +0200
parents 03201eaf594f
children 6e87dd4738df
files artifacts/doc/conf/meta-data.xml artifacts/src/main/java/org/dive4elements/river/artifacts/SQRelationArtifact.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java artifacts/src/main/resources/messages.properties artifacts/src/main/resources/messages_de.properties artifacts/src/main/resources/messages_de_DE.properties artifacts/src/main/resources/messages_en.properties
diffstat 8 files changed, 174 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/doc/conf/meta-data.xml	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/doc/conf/meta-data.xml	Wed Oct 02 17:04:23 2013 +0200
@@ -296,6 +296,9 @@
                       <dc:call-macro name="basedata_5_flood-protections"/>
                     </dc:if>
                   </dc:when>
+                  <dc:when test="starts-with($out, 'sq_relation')">
+                    <dc:call-macro name="sqrelations"/>
+                  </dc:when>
                 </dc:choose>
               </dc:iterate>
             </dc:otherwise>
@@ -1618,6 +1621,40 @@
       </dc:context>
     </dc:macro>
 
+    <dc:macro name="sqrelations">
+      <dc:context>
+        <dc:statement>
+          SELECT sq.description  AS description,
+            ti.start_time        AS start_time,
+            ti.stop_time         AS stop_time,
+            ms.name              AS station_name,
+            lower(sqv.parameter) AS parameter,
+            sqv.id               AS sqvid
+          FROM sq_relation sq
+          JOIN time_intervals ti ON ti.id   = sq.time_interval_id
+          JOIN rivers r ON r.id = sq.river_id
+          JOIN sq_relation_value sqv ON sqv.sq_relation_id = sq.id
+          JOIN measurement_station ms ON sqv.measurement_station_id = ms.id
+          WHERE r.id = ${river_id}
+        </dc:statement>
+        <dc:if test="dc:has-result()">
+          <sq_relations>
+            <dc:filter expr="$out = concat('sq_relation_', $parameter)">
+              <dc:group expr="$station_name">
+                <station description="{dc:group-key()}">
+                  <dc:for-each>
+                    <dc:variable name="combined_desc" expr="concat(dc:date-format('yyyy', $start_time), ' bis ', dc:date-format('yyyy', $stop_time))"/>
+                    <sqvalue factory="staticsqrelation" target_out="{$out}"
+                      ids="{$sqvid};{$station_name}: {$combined_desc}" description="{$combined_desc}"/>
+                  </dc:for-each>
+                </station>
+              </dc:group>
+            </dc:filter>
+          </sq_relations>
+        </dc:if>
+      </dc:context>
+    </dc:macro>
+
     <dc:macro name="basedata_0_macro">
       <dc:filter expr="$kind = 0">
         <dc:if test="dc:has-result()">
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/SQRelationArtifact.java	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/SQRelationArtifact.java	Wed Oct 02 17:04:23 2013 +0200
@@ -17,6 +17,7 @@
 import org.dive4elements.artifactdatabase.data.DefaultStateData;
 import org.dive4elements.artifactdatabase.state.DefaultOutput;
 import org.dive4elements.artifactdatabase.state.Facet;
+import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.ArtifactFactory;
 import org.dive4elements.artifacts.ArtifactNamespaceContext;
 import org.dive4elements.artifacts.CallMeta;
@@ -28,7 +29,7 @@
 
 
 public class SQRelationArtifact
-extends AbstractStaticStateArtifact
+extends StaticD4EArtifact
 {
     private static final Logger logger =
         Logger.getLogger(SQRelationArtifact.class);
@@ -57,6 +58,17 @@
     ) {
         logger.debug("SQRelationArtifact.setup()");
 
+        String code = getDatacageIDValue(data);
+
+        logger.debug("SQRelationDCArtifact.setup Id: " + code);
+
+        if (code != null && !code.isEmpty()) {
+            /* Case that we were instantiated from the datacage */
+            addStringData("ids", code);
+            super.setup(identifier, factory, context, callmeta, data);
+            return;
+        }
+
         String river = XMLUtils.xpathString(
             data,
             XPATH_RIVER,
@@ -81,14 +93,26 @@
                 "String",
                 station));
         super.setup(identifier, factory, context, callmeta, data);
+        // When we are in this case we did not come from the datacage
+        // e.g. had an ID string set. So we also did not have a template
+        // set and initialize is not called. So we have to do this ourself.
+        initialize(this, context, callmeta);
     }
 
     @Override
-    protected void initStaticState() {
+    protected void initialize(
+        Artifact artifact,
+        Object   context,
+        CallMeta callMeta
+    ) {
         StaticState state = new SQStaticState(STATIC_STATE_NAME);
 
         List<Facet> fs = new ArrayList<Facet>();
-        state.staticCompute(fs, this);
+        state.computeInit(this, hash(), context, callMeta, fs);
+        logger.debug("Init static state computed facets");
+        for (Facet face: fs) {
+            logger.debug("Got a facet with name: " + face.getName());
+        }
 
         if (hasParameter(StaticSQRelation.Parameter.A, fs)) {
             DefaultOutput outputA = new DefaultOutput(
@@ -145,8 +169,6 @@
             state.addOutput(outputF);
         }
         addFacets(STATIC_STATE_NAME, fs);
-        state.setUIProvider(UIPROVIDER);
-        setStaticState(state);
     }
 
     @Override
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java	Wed Oct 02 17:04:23 2013 +0200
@@ -44,11 +44,16 @@
             "JOIN time_intervals ti ON ti.id   = sq.time_interval_id " +
             "JOIN rivers r ON r.id = sq.river_id " +
             "JOIN sq_relation_value sqv ON sqv.sq_relation_id = sq.id " +
-            "JOIN measurement_station ms ON sqv.measurement_station_id = ms.id " +
+            "JOIN measurement_station ms ON sqv.measurement_station_id = ms.id ";
+
+    public static final String STATION_CLAUSE =
         "WHERE " +
             "r.name = :river " +
             "AND ms.id = :ms_id ";
 
+    public static final String ID_CLAUSE =
+        "WHERE " +
+            "sqv.id = :dis_id ";
 
     private StaticSQFactory() {
     }
@@ -83,13 +88,56 @@
         return values;
     }
 
+    public static StaticSQContainer getDistinctRelation(int id) {
+        Session session = SessionHolder.HOLDER.get();
+
+        Query query = session.createSQLQuery(SQL_SQ + ID_CLAUSE)
+            .addScalar("description")
+            .addScalar("start_time")
+            .addScalar("stop_time")
+            .addScalar("station_name")
+            .addScalar("station_km")
+            .addScalar("measurement_type")
+            .addScalar("parameter")
+            .addScalar("a")
+            .addScalar("b")
+            .addScalar("qmax");
+
+        query.setParameter("dis_id", id);
+
+        /* This could be done nicer with hibernate */
+        List<Object []> list = query.list();
+        if (list.isEmpty()) {
+            log.debug("Query returened nothing");
+            return null;
+        }
+        Object [] row = list.get(0);
+
+        StaticSQContainer sq = new StaticSQContainer();
+        sq.setDescription((String)list.get(0)[0]);
+        sq.setStationName((String)list.get(0)[3]);
+        sq.setKm(((BigDecimal)list.get(0)[4]).doubleValue());
+
+        StaticSQRelation relation = new StaticSQRelation();
+        relation.setStartTime((Date)row[1]);
+        relation.setStopTime((Date)row[2]);
+        relation.setType((String)row[5]);
+        relation.setParameter((String)row[6]);
+        relation.setA(((BigDecimal)row[7]).doubleValue());
+        relation.setB(((BigDecimal)row[8]).doubleValue());
+        relation.setQmax(((BigDecimal)row[9]).doubleValue());
+        sq.addSQRelation(relation);
+
+        return sq;
+    }
+
     private static StaticSQContainer getUncached(
         String river,
         int measurementStation
     ) {
         Session session = SessionHolder.HOLDER.get();
 
-        Query query = session.createSQLQuery(SQL_SQ)
+        Query query = session.createSQLQuery(SQL_SQ + STATION_CLAUSE)
             .addScalar("description")
             .addScalar("start_time")
             .addScalar("stop_time")
@@ -107,6 +155,7 @@
         List<Object []> list = query.list();
 
         if (list.isEmpty()) {
+            log.debug("Query returened empty");
             return new StaticSQContainer();
         }
 
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/SQStaticState.java	Wed Oct 02 17:04:23 2013 +0200
@@ -10,11 +10,12 @@
 
 import java.text.DateFormat;
 import java.util.List;
-
+import java.text.SimpleDateFormat;
 
 import org.apache.log4j.Logger;
 
 import org.dive4elements.artifactdatabase.state.Facet;
+import org.dive4elements.artifacts.CallMeta;
 import org.dive4elements.river.artifacts.D4EArtifact;
 import org.dive4elements.river.artifacts.access.StaticSQRelationAccess;
 import org.dive4elements.river.artifacts.model.sq.StaticSQContainer;
@@ -22,13 +23,18 @@
 import org.dive4elements.river.artifacts.model.sq.StaticSQRelation;
 import org.dive4elements.river.artifacts.states.StaticState;
 
+import org.dive4elements.river.artifacts.resources.Resources;
 
 public class SQStaticState
 extends StaticState
 {
+
     private static final Logger log =
         Logger.getLogger(SQStaticState.class);
 
+    private static final String FACET_DESCRIPTION =
+        "facet.sq_relation.static_data";
+
     public SQStaticState() {
         super();
     }
@@ -38,26 +44,51 @@
     }
 
     @Override
-    public Object staticCompute(
-        List<Facet> facets,
-        D4EArtifact artifact
+    public Object computeInit(
+        D4EArtifact artifact,
+        String       hash,
+        Object       context,
+        CallMeta     meta,
+        List<Facet>  facets
     ) {
-        StaticSQRelationAccess access = new StaticSQRelationAccess(artifact);
+        StaticSQContainer sqRelations;
 
-        String river = access.getRiver();
-        String measurementStation = access.getMeasurementStation();
+        String id_string = artifact.getDataAsString("ids");
 
-        int ms = -1;
-        try {
-            ms = Integer.parseInt(measurementStation);
-        }
-        catch (NumberFormatException nfe) {
-            log.error("Unparseable measurement station: " + measurementStation);
-            return null;
+        int static_id = -1;
+        String static_desc = null;
+
+        if (id_string != null && !id_string.isEmpty()) {
+            String[] id_parts = id_string.split(";");
+            static_id = Integer.parseInt(id_parts[0]);
+            if (id_parts.length > 1) {
+                static_desc = id_parts[1];
+            }
         }
 
-        StaticSQContainer sqRelations =
-            StaticSQFactory.getSQRelations(river, ms);
+        if (static_id != -1) {
+            // If the next line fails a traceback is the best debug output anyhow
+            sqRelations = StaticSQFactory.getDistinctRelation(static_id);
+            log.debug("Got a distinct relation" + sqRelations);
+        } else {
+            StaticSQRelationAccess access = new StaticSQRelationAccess(artifact);
+            String river = access.getRiver();
+            String measurementStation = access.getMeasurementStation();
+
+            int ms = -1;
+            try {
+                ms = Integer.parseInt(measurementStation);
+            }
+            catch (NumberFormatException nfe) {
+                log.error("Unparseable measurement station: " + measurementStation);
+                return null;
+            }
+            log.debug("Parsed measurement station: " + ms);
+
+            sqRelations = StaticSQFactory.getSQRelations(river, ms);
+        }
+
+        DateFormat df = new SimpleDateFormat("yyyy");
 
         for (StaticSQRelation.Parameter p: StaticSQRelation.Parameter.values()) {
 
@@ -69,11 +100,14 @@
 
                 for (StaticSQRelation relation : relations) {
                     String name = "sq_" + p.toString().toLowerCase() + "_curve";
-                    DateFormat df =
-                        DateFormat.getDateInstance(DateFormat.SHORT);
-                    String desc = p.toString() + ": " +
-                        df.format(relation.getStartTime()) + " - " +
-                        df.format(relation.getStopTime());
+                    String desc = static_desc == null ?
+                        Resources.getMsg(meta,
+                            FACET_DESCRIPTION,
+                            FACET_DESCRIPTION,
+                            new Object[] {
+                                df.format(relation.getStartTime()),
+                                df.format(relation.getStopTime())}) :
+                            static_desc;
                     facets.add(new StaticSQRelationFacet(
                         count,
                         name,
--- a/artifacts/src/main/resources/messages.properties	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/resources/messages.properties	Wed Oct 02 17:04:23 2013 +0200
@@ -234,6 +234,7 @@
 chart.sq_relation_e.title = Grobkornanteil (> Mittelkies)
 chart.sq_relation_f.title = Geschiebetransport gesamt
 facet.sq_relation.curve = Potenziell (Geschiebedaten)
+facet.sq_relation.static_data = {0} to {1}
 facet.sq_relation.measurements = Geschiebedaten
 facet.sq_relation.outliers = Ausrei\u00dfer Durchgang {0}
 facet.sq_relation.outlier.curve = Potenziell Durchgang {0}
--- a/artifacts/src/main/resources/messages_de.properties	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/resources/messages_de.properties	Wed Oct 02 17:04:23 2013 +0200
@@ -235,6 +235,7 @@
 chart.sq_relation_f.title = Geschiebetransport gesamt
 facet.sq_relation.curve = Potenziell (Geschiebedaten)
 facet.sq_relation.measurements = Geschiebedaten
+facet.sq_relation.static_data = {0} bis {1}
 facet.sq_relation.outliers = Ausrei\u00dfer Durchgang {0}
 facet.sq_relation.outlier.curve = Potenziell Durchgang {0}
 facet.sq_relation.outlier.measurement = Geschiebedaten Durchgang {0}
--- a/artifacts/src/main/resources/messages_de_DE.properties	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/resources/messages_de_DE.properties	Wed Oct 02 17:04:23 2013 +0200
@@ -233,6 +233,7 @@
 chart.sq_relation_f.title = Geschiebetransport gesamt
 facet.sq_relation.curve = Potenziell (Geschiebedaten)
 facet.sq_relation.measurements = Geschiebedaten
+facet.sq_relation.static_data = {0} bis {1}
 facet.sq_relation.outliers = Ausrei\u00dfer Durchgang {0}
 facet.sq_relation.outlier.curve = Potenziell Durchgang {0}
 facet.sq_relation.outlier.measurement = Geschiebedaten Durchgang {0}
--- a/artifacts/src/main/resources/messages_en.properties	Tue Oct 01 17:10:55 2013 +0200
+++ b/artifacts/src/main/resources/messages_en.properties	Wed Oct 02 17:04:23 2013 +0200
@@ -238,6 +238,7 @@
 chart.sq_relation_f.title = Geschiebetransport gesamt
 facet.sq_relation.curve = Potenziell (Geschiebedaten)
 facet.sq_relation.measurements = Geschiebedaten
+facet.sq_relation.static_data = {0} to {1}
 facet.sq_relation.outliers = Ausrei\u00dfer Durchgang {0}
 facet.sq_relation.outlier.curve = Potenziell Durchgang {0}
 facet.sq_relation.outlier.measurement = Geschiebedaten Durchgang {0}

http://dive4elements.wald.intevation.org