changeset 2124:a202a9e048a5

Made StaticWQKmsArtifact a fully featured Artifact. flys-artifacts/trunk@3694 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 17 Jan 2012 08:17:50 +0000
parents 907322e269e3
children 7a8f52267a5c
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/staticwqkms.xml flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWQKmsArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKmsFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticWQKmsState.java
diffstat 6 files changed, 239 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Jan 17 08:09:03 2012 +0000
+++ b/flys-artifacts/ChangeLog	Tue Jan 17 08:17:50 2012 +0000
@@ -1,3 +1,20 @@
+2012-01-17	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	"Ported" StaticWQKmsArtifact to use compute-type caching mechanism.
+
+	* doc/conf/artifacts/staticwqkms.xml: Declare trivial info.
+
+	* doc/conf/conf.xml: Register staticwqkms artifact and factory.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WQKmsFacet.java:
+	  Sublacss DataFacet.
+
+	* src/main/java/de/intevation/flys/artifacts/states/StaticWQKmsState.java:
+	  New. State.
+	
+	* src/main/java/de/intevation/flys/artifacts/StaticWQKmsArtifact.java:
+	  Refactored. Most computation and stuff now done in State and Facet.
+
 2012-01-17	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/doc/conf/artifacts/staticwqkms.xml	Tue Jan 17 08:17:50 2012 +0000
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<artifact name="staticwqkms">
+  <states>
+    <state id="state.staticwqkms.static"
+               description="state.staticwqkms.state"
+               state="de.intevation.flys.artifacts.states.StaticWQKmsState">
+      <outputmodes>
+        <outputmode name="longitudinal_section" description="output.longitudinal_section" mime-type="image/png" type="chart">
+          <facets>
+            <facet name="other.wqkms.w" description="W-type data" />
+            <facet name="other.wqkms.q" description="Q-type data" />
+          </facets>
+        </outputmode>
+      </outputmodes>
+    </state>
+  </states>
+</artifact>
--- a/flys-artifacts/doc/conf/conf.xml	Tue Jan 17 08:09:03 2012 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Tue Jan 17 08:17:50 2012 +0000
@@ -68,6 +68,9 @@
             <artifact-factory name="area" description="Factory to create an artifact to draw (wkms) area data"
                 ttl="3600000"
                 artifact="de.intevation.flys.artifacts.AreaArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
+            <artifact-factory name="staticwqkms" description="Factory to create an artifact to access 'other' WKms data"
+                ttl="3600000"
+                artifact="de.intevation.flys.artifacts.StaticWQKmsArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
             <artifact-factory name="wqinterpol" description="Factory to create an artifact to access 'other' WQ (at km) data"
                 ttl="3600000"
                 artifact="de.intevation.flys.artifacts.WQKmsInterpolArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory>
@@ -131,6 +134,7 @@
         <artifact name="wmsbackground" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/wmsbackground.xml" />
         <artifact name="new_map" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/map.xml" />
         <artifact name="new_chart" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/chart.xml" />
+        <artifact name="staticwqkms" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="${artifacts.config.dir}/artifacts/staticwqkms.xml" />
     </artifacts>
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWQKmsArtifact.java	Tue Jan 17 08:09:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWQKmsArtifact.java	Tue Jan 17 08:17:50 2012 +0000
@@ -8,24 +8,21 @@
 import org.w3c.dom.Document;
 
 import de.intevation.artifactdatabase.state.Facet;
-import de.intevation.artifactdatabase.state.DefaultOutput;
-import de.intevation.artifactdatabase.state.State;
 
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.ArtifactNamespaceContext;
 import de.intevation.artifacts.CallMeta;
 
+import de.intevation.artifacts.common.utils.XMLUtils;
+
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.WQKms;
-import de.intevation.flys.artifacts.model.WQKmsFacet;
 import de.intevation.flys.artifacts.model.WKmsFactory;
 import de.intevation.flys.artifacts.model.WQKmsFactory;
 
-import de.intevation.flys.artifacts.states.StaticState;
-import de.intevation.flys.artifacts.resources.Resources;
+import de.intevation.flys.artifacts.states.DefaultState;
 
-import de.intevation.artifacts.common.utils.XMLUtils;
 
 /**
  * Artifact to access additional "waterlevel/discharge"-type of data, like
@@ -49,9 +46,6 @@
     public static final String STATIC_STATE_NAME =
         "state.additional_wqkms.static";
 
-    /** One and only state to be in. */
-    protected transient State state = null;
-
 
     /**
      * Trivial Constructor.
@@ -74,15 +68,12 @@
     {
         logger.debug("StaticWQKmsArtifact.setup");
 
-        state = new StaticState(STATIC_STATE_NAME);
+        // Store the 'ids' (from datacage).
+        logger.debug("StaticWQKmsArtiact.setup" + XMLUtils.toString(data));
 
-        List<Facet> fs = new ArrayList<Facet>();
-        logger.debug(XMLUtils.toString(data));
         String code = XMLUtils.xpathString(
             data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE);
-
-        // TODO Go for JSON, one day.
-        //ex.: flood_protection-wstv-114-12
+        addStringData("ids", code);
         if (code != null) {
             String [] parts = code.split("-");
 
@@ -92,54 +83,15 @@
 
                 addStringData("col_pos", parts[2]);
                 addStringData("wst_id",  parts[3]);
-
-                String wkmsName = WKmsFactory.getWKmsName(col, wst);
-
-                String name;
-                if (parts[0].equals(HEIGHTMARKS_POINTS)) {
-                    name = HEIGHTMARKS_POINTS;
-                }
-                else {
-                    name = STATIC_WQKMS;
-                }
-
-                Facet facet = new WQKmsFacet(
-                    name,
-                    Resources.getMsg(
-                        callMeta,
-                        wkmsName,
-                        wkmsName));
-                fs.add(facet);
-                facets.put(state.getID(), fs);
             }
         }
 
-        spawnState();
+        // Do this AFTER we have set the col_pos etc.
         super.setup(identifier, factory, context, callMeta, data);
     }
 
 
     /**
-     * Initialize the static state with output.
-     * @return static state
-     * @TODO merge with StaticWKmsArtifact implementation.
-     */
-    protected State spawnState() {
-        state = new StaticState(STATIC_STATE_NAME);
-        List<Facet> fs = facets.get(STATIC_STATE_NAME);
-        DefaultOutput output = new DefaultOutput(
-            "general",
-            "general",
-            "image/png",
-            fs,
-            "chart");
-
-        state.getOutputs().add(output);
-        return state;
-    }
-
-
-    /**
      * Called via setup.
      *
      * @param artifact The master-artifact.
@@ -152,69 +104,42 @@
     {
         logger.debug("StaticWQKmsArtifact.initialize");
         WINFOArtifact winfo = (WINFOArtifact) artifact;
-        // TODO: The river is of no interest, so far.
+        // TODO: The river is of no interest, so far., also use importData
         addData("river", winfo.getData("river"));
-    }
-
-
-    /**
-     * Get a list containing the one and only State.
-     * @param  context ignored.
-     * @return list with one and only state.
-     */
-    @Override
-    protected List<State> getStates(Object context) {
-        ArrayList<State> states = new ArrayList<State>();
-        states.add(getState());
-        return states;
-    }
-
 
-    /**
-     * Get the "current" state (there is but one).
-     * @param cc ignored.
-     * @return the "current" (only possible) state.
-     */
-    @Override
-    public State getCurrentState(Object cc) {
-        return getState();
-    }
-
+        List<Facet> fs = new ArrayList<Facet>();
 
-    /**
-     * Get the only possible state.
-     * @return the state.
-     */
-    protected State getState() {
-        return getState(null, null);
-    }
-
-
-    /**
-     * Get the state.
-     * @param context ignored.
-     * @param stateID ignored.
-     * @return the state.
-     */
-    @Override
-    protected State getState(Object context, String stateID) {
-        return (state != null)
-            ? state
-            : spawnState();
+        DefaultState state = (DefaultState) getCurrentState(context);
+        state.computeInit(this, hash(), context, meta, fs);
+        if (!fs.isEmpty()) { 
+            logger.debug("Facets to add in StaticWQKmsArtifact.initialize ."); 
+            facets.put(getCurrentStateId(), fs); 
+        } 
+        else { 
+            logger.debug("No facets to add in StaticWQKmsArtifact.initialize ("
+                + state.getID() + ").");
+        }
     }
 
 
     /**
      * Get WQKms from factory.
-     * @param TODO idx param is not needed
      * @return WQKms according to parameterization (can be null);
      */
-    public WQKms getWQKms(int idx) {
+    public WQKms getWQKms() {
         logger.debug("StaticWQKmsArtifact.getWQKms");
 
-        return WQKmsFactory.getWQKms(
+        int col = Integer.valueOf(getDataAsString("col_pos"));
+        int wst = Integer.valueOf(getDataAsString("wst_id"));
+
+        /** TODO do not run twice against db to do this. */
+        String wkmsName = WKmsFactory.getWKmsName(col, wst);
+
+        WQKms res = WQKmsFactory.getWQKms(
             Integer.valueOf(getDataAsString("col_pos")),
             Integer.valueOf(getDataAsString("wst_id")));
+        res.setName(wkmsName);
+        return res;
     }
 
 
@@ -236,5 +161,13 @@
     {
         return 0;
     }
+
+
+    /** Return specific name. */
+    public String getName() {
+        return "staticwqkms";
+    }
+
+    // TODO implement deepCopy.
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKmsFacet.java	Tue Jan 17 08:09:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKmsFacet.java	Tue Jan 17 08:17:50 2012 +0000
@@ -1,29 +1,42 @@
 package de.intevation.flys.artifacts.model;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
-import de.intevation.artifactdatabase.state.DefaultFacet;
-
 import de.intevation.flys.artifacts.StaticWQKmsArtifact;
 import de.intevation.flys.artifacts.model.FacetTypes;
 
+import de.intevation.flys.artifacts.states.DefaultState;
+import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
+
+
 /**
  * Facet to show W|Q|km Values.
+ * We have following 'Types' (from FacetTypes):
+ *   String STATIC_WQKMS = "other.wqkms";
+ *   String STATIC_WQMS_W = "other.wqkms.w";
+ *   String STATIC_WQKMS_Q = "other.wqkms.q";
  */
 public class WQKmsFacet
-extends      DefaultFacet
+extends      DataFacet
 implements   FacetTypes {
 
+    /** House logger. */
+    private static Logger logger = Logger.getLogger(WQKmsFacet.class);
+
     /** Trivial Constructor. */
     public WQKmsFacet(String description) {
         this(STATIC_WQKMS, description);
     }
 
+
+    /**
+     * @param name Name of this facet (we have at least two flavors (w and q).
+     */
     public WQKmsFacet(String name, String description) {
-        this.name        = name;
-        this.description = description;
-        this.index       = 0;
+        super(0, name, description, ComputeType.FEED, null, null);
     }
 
 
@@ -37,9 +50,13 @@
      */
     @Override
     public Object getData(Artifact artifact, CallContext context) {
+        logger.debug("WQKmsFacet.getData");
+
         StaticWQKmsArtifact staticData =
             (StaticWQKmsArtifact) artifact;
-        return staticData.getWQKms(0);
+        Object res = staticData.compute(context, hash, stateId, type, false);
+
+        return res;
     }
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticWQKmsState.java	Tue Jan 17 08:17:50 2012 +0000
@@ -0,0 +1,140 @@
+package de.intevation.flys.artifacts.states;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.StaticWQKmsArtifact;
+
+import de.intevation.flys.artifacts.model.DataFacet;
+import de.intevation.flys.artifacts.model.FacetTypes;
+import de.intevation.flys.artifacts.model.WQKms;
+import de.intevation.flys.artifacts.model.WQKmsFacet;
+
+import de.intevation.flys.artifacts.resources.Resources;
+
+
+/**
+ * Only state of WQKmsArtifact.
+ */
+public class StaticWQKmsState
+extends      DefaultState
+implements   FacetTypes
+{
+    /** The logger that is used in this state. */
+    private static Logger logger = Logger.getLogger(StaticWQKmsState.class);
+
+
+    /**
+     * From this state can not be continued.
+     */
+    @Override
+    protected String getUIProvider() {
+        return "noinput";
+    }
+
+
+    /**
+     * Compute, create Facets, do the same stuff as all the other states do.
+     */
+    protected Object compute(
+        StaticWQKmsArtifact winfo,
+        CallMeta      metaLocale,
+        String        hash,
+        List<Facet>   facets,
+        Object        old
+    ) {
+        String id = getID();
+
+        WQKms res = old instanceof WQKms
+            ? (WQKms)old
+            : winfo.getWQKms();
+
+        WQKms wqkms = res;
+
+        if (facets == null) {
+            return res;
+        }
+
+        /*
+         * TODO: re-enable HEIGHTMARKS_POINTS-thing
+
+           String name;
+           if (parts[0].equals(HEIGHTMARKS_POINTS)) {
+               name = HEIGHTMARKS_POINTS;
+           }
+           else {
+               name = STATIC_WQKMS;
+           }
+        */
+
+        String wkmsName = wqkms.getName();
+        Facet qfacet = new WQKmsFacet(
+            STATIC_WQKMS_Q,
+            wkmsName
+            // TODO re-enable translations.
+            /*
+            Resources.getMsg(
+                metaLocale,
+                wkmsName,
+                wkmsName)*/);
+        facets.add(qfacet);
+
+        wkmsName = "W (" + wkmsName + ")";
+
+        Facet wfacet = new WQKmsFacet(
+            STATIC_WQKMS_W,
+            wkmsName
+            /*
+            // TODO re-enable translations.
+            Resources.getMsg(
+                metaLocale,
+                wkmsName,
+                wkmsName)*/);
+        facets.add(wfacet);
+
+        return res;
+    }
+
+
+    /**
+     * Get data, create the facets.
+     *
+     * @param context Ignored.
+     */
+    @Override
+    public Object computeFeed(
+        FLYSArtifact artifact,
+        String       hash,
+        CallContext  context,
+        List<Facet>  facets,
+        Object       old
+    ) {
+        return compute((StaticWQKmsArtifact) artifact, context.getMeta(),
+            hash, facets, old);
+    }
+
+
+    /**
+     * Create the facets.
+     * @param context Ignored.
+     */
+    @Override
+    public Object computeInit(
+        FLYSArtifact artifact,
+        String       hash,
+        Object       context,
+        CallMeta     meta,
+        List<Facet>  facets
+    ) {
+        return compute((StaticWQKmsArtifact) artifact, meta, hash, facets,
+            null);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org