changeset 1075:feaf778d510e

Changed the way of collecting the data for the RiverAxisArtifact - use the database instead of a Master-Artifact. flys-artifacts/trunk@2568 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 24 Aug 2011 14:35:38 +0000
parents 21b9eb24bd47
children 6ada2b84ddf8
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java
diffstat 3 files changed, 91 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Aug 24 14:30:27 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Aug 24 14:35:38 2011 +0000
@@ -1,3 +1,13 @@
+2011-08-24  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java:
+	  Added a method that returns a River object based on its database id.
+
+	* src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java: The
+	  data that is required for this artifact is fetched from database instead
+	  from a Master-Artifact. The creation of static artifacts should use
+	  database ids instead of cloning a Master-Artifact.
+
 2011-08-24  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java	Wed Aug 24 14:30:27 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java	Wed Aug 24 14:35:38 2011 +0000
@@ -3,13 +3,23 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.w3c.dom.Document;
+
 import org.apache.log4j.Logger;
 
 import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.CallMeta;
 
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+import de.intevation.artifactdatabase.data.DefaultStateData;
 import de.intevation.artifactdatabase.state.Facet;
 
+import de.intevation.flys.model.River;
+
+import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.artifacts.states.DefaultState;
 
 
@@ -17,6 +27,8 @@
 
     public static final String NAME = "riveraxis";
 
+    public static final String XPATH_IDS = "/art:action/art:ids/@value";
+
 
     private static final Logger logger =
         Logger.getLogger(RiverAxisArtifact.class);
@@ -29,21 +41,60 @@
 
 
     @Override
-    protected void initialize(Artifact artifact, Object context, CallMeta meta) {
-        logger.debug("Initialize internal state with: "+ artifact.identifier());
-
-        FLYSArtifact flys = (FLYSArtifact) artifact;
-        addData("river", flys.getData("river"));
+    public void setup(
+        String          identifier,
+        ArtifactFactory factory,
+        Object          context,
+        CallMeta        callMeta,
+        Document        data)
+    {
+        logger.debug("RiverAxisArtifact.setup");
 
-        List<Facet> fs = new ArrayList<Facet>();
+        super.setup(identifier, factory, context, callMeta, data);
 
-        // TODO Add CallMeta
-        DefaultState state = (DefaultState) getCurrentState(context);
-        state.computeInit(this, hash(), context, meta, fs);
+        String ids = XMLUtils.xpathString(
+            data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE);
 
-        if (!fs.isEmpty()) {
-            facets.put(getCurrentStateId(), fs);
+        String[] splitted = ids != null ? ids.split(" ") : null;
+
+        if (splitted != null && splitted.length > 0) {
+            try {
+                int   river_id = Integer.parseInt(splitted[0]);
+                River river    = RiverFactory.getRiver(river_id);
+
+                if (river == null) {
+                    throw new IllegalArgumentException(
+                        "No river found for id: " + river_id);
+                }
+
+                String name = river.getName();
+
+                addData("river", new DefaultStateData("river",null,null,name));
+
+                List<Facet> fs = new ArrayList<Facet>();
+
+                DefaultState state = (DefaultState) getCurrentState(context);
+                state.computeInit(this, hash(), context, callMeta, fs);
+
+                if (!fs.isEmpty()) {
+                    facets.put(getCurrentStateId(), fs);
+                }
+            }
+            catch (NumberFormatException nfe) {
+                logger.error("Could not create Artifact: " + nfe.getMessage());
+                throw new IllegalArgumentException("No river id given.");
+            }
         }
     }
+
+
+    @Override
+    protected void initialize(
+        Artifact artifact,
+        Object   context,
+        CallMeta callMeta)
+    {
+        // do nothing
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Wed Aug 24 14:30:27 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Wed Aug 24 14:35:38 2011 +0000
@@ -31,6 +31,25 @@
 
 
     /**
+     * Returns a River object fetched from database based on its id.
+     *
+     * @param river_id The id of the desired river.
+     *
+     * @return the river.
+     */
+    public static River getRiver(int river_id) {
+        Session session = SessionHolder.HOLDER.get();
+
+        Query query = session.createQuery("from River where id=:river_id");
+        query.setParameter("river_id", river_id);
+
+        List<River> rivers = query.list();
+
+        return (rivers != null && rivers.size() > 0) ? rivers.get(0) : null;
+    }
+
+
+    /**
      * Returns a River object fetched from database based on its name.
      *
      * @param river The name of a river.

http://dive4elements.wald.intevation.org