changeset 940:f4439e015278

Append artifact recommendations to the artifact's describe document. flys-artifacts/trunk@2334 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 14 Jul 2011 15:30:55 +0000
parents 32d0a543e3e8
children 1b509e4cf99b
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java
diffstat 3 files changed, 81 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jul 14 14:25:36 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jul 14 15:30:55 2011 +0000
@@ -1,3 +1,17 @@
+2011-07-14  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java:
+	  Removed the code to generate new artifacts. Instead of creating new
+	  artifacts automatically, we decided to suggest creating new artifacts
+	  from specific types. Therefore, the DESCRIBE document of the artifacts
+	  is extended with a node that contains recommended artifact types.
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java:
+	  FLYSArtifacts might be setup with the identifier of an other
+	  FLYSArtifact. Subclasses are able to override a method called
+	  initialize(Artifact, GlobalContext). This might be helpful to extract
+	  required values or clone artifacts.
+
 2011-07-14  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java	Thu Jul 14 14:25:36 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java	Thu Jul 14 15:30:55 2011 +0000
@@ -16,42 +16,22 @@
 import org.w3c.dom.NodeList;
 
 import de.intevation.artifacts.Artifact;
-import de.intevation.artifacts.ArtifactDatabase;
-import de.intevation.artifacts.ArtifactDatabaseException;
 import de.intevation.artifacts.ArtifactNamespaceContext;
 import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.Hook;
 
-import de.intevation.artifacts.common.utils.ClientProtocolUtils;
 import de.intevation.artifacts.common.utils.Config;
 import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
 
 
 public class CollectionMonitor implements Hook {
 
-    public static final String XPATH_UUID = "/art:result/art:uuid/@art:value";
-    public static final String XPATH_HASH = "/art:result/art:hash/@art:value";
+    public static final String XPATH_STATES = "output-defaults/state";
+    public static final String XPATH_RESULT = "/art:result";
 
 
-    public class LoadArtifact {
-        public String factory;
-        public List<String> parameters;
-
-        public LoadArtifact(String factory) {
-            this.factory    = factory;
-            this.parameters = new ArrayList<String>();
-        }
-
-        public void addParameter(String parameter) {
-            parameters.add(parameter);
-        }
-    } // end of class LoadArtifact
-
-
-    public static final String XPATH_STATES = "output-defaults/state";
-
-
-    protected Map<String, List<LoadArtifact>> states;
+    protected Map<String, List<String>> states;
 
 
     private static final Logger logger =
@@ -80,160 +60,68 @@
 
         int len = states != null ? states.getLength() : 0;
 
-        this.states = new HashMap<String, List<LoadArtifact>>(len);
+        this.states = new HashMap<String, List<String>>(len);
 
         for (int i = 0; i < len; i++) {
             Element state = (Element) states.item(i);
 
-            String             stateId   = state.getAttribute("id");
-            List<LoadArtifact> artifacts = parseLoadArtifacts(state);
+            String stateId = state.getAttribute("id");
+            String factory = state.getAttribute("artifact-factory");
 
-            if (artifacts != null) {
-                this.states.put(stateId, artifacts);
+            if (stateId != null && factory != null) {
+                List<String> factories = this.states.get(stateId);
+
+                if (factories == null) {
+                    factories = new ArrayList<String>();
+                    this.states.put(stateId, factories);
+                }
+
+                factories.add(factory);
             }
         }
     }
 
 
-    protected List<LoadArtifact> parseLoadArtifacts(Element state) {
-        NodeList artifacts = (NodeList) XMLUtils.xpath(
-            state, "artifact", XPathConstants.NODESET);
-
-        int len = artifacts != null ? artifacts.getLength() : 0;
-
-        List<LoadArtifact> loadArtifacts = new ArrayList<LoadArtifact>(len);
-
-        for (int i = 0; i < len; i++) {
-            LoadArtifact la = parseLoadArtifact((Element) artifacts.item(i));
-
-            if (la != null) {
-                loadArtifacts.add(la);
-            }
-        }
-
-        return loadArtifacts;
-    }
-
-
-    protected LoadArtifact parseLoadArtifact(Element art) {
-        String factory = art.getAttribute("factory");
-
-        LoadArtifact artifact = new LoadArtifact(factory);
-
-        NodeList parameters = (NodeList) XMLUtils.xpath(
-            art, "parameter", XPathConstants.NODESET);
-
-        int len = parameters != null ? parameters.getLength() : 0;
-
-        for (int i = 0; i < len; i++) {
-            Element parameter = (Element) parameters.item(i);
-            artifact.addParameter(parameter.getAttribute("name"));
-        }
-
-        return artifact;
-    }
-
-
     @Override
-    public void execute(Artifact artifact, CallContext context) {
+    public void execute(Artifact artifact, CallContext context, Document doc) {
         FLYSArtifact flys = (FLYSArtifact) artifact;
 
         String stateId = flys.getCurrentStateId();
 
-        List<LoadArtifact> loadArtifacts = states.get(stateId);
+        List<String> factories = states.get(stateId);
 
-        if (loadArtifacts == null || loadArtifacts.isEmpty()) {
+        if (factories == null || factories.isEmpty()) {
             return;
         }
 
-        ArtifactDatabase db = context.getDatabase();
-        List<String> uuids  = new ArrayList<String>();
-
-        for (LoadArtifact rawArtifact: loadArtifacts) {
-            String[] art = createArtifact(db, context, rawArtifact);
+        logger.info("Found " + factories.size() + " recommended artifacts.");
 
-            if (art != null && art.length >= 2) {
-                Document feed   = prepareFeed(
-                    artifact, rawArtifact, art[0], art[1]);
-
-                boolean success = initializeArtifact(db, context, feed, art[0]);
-
-                if (success) {
-                    uuids.add(art[0]);
-                }
-            }
-        }
-
-        // TODO ADD UUIDS TO DESCRIBE
+        appendRecommendations(doc, factories);
     }
 
 
-    protected String[] createArtifact(
-        ArtifactDatabase db,
-        CallContext      cc,
-        LoadArtifact     raw)
-    {
-        Document create = ClientProtocolUtils.newCreateDocument(raw.factory);
+    protected void appendRecommendations(Document doc, List<String> factories) {
+        Element result = (Element) XMLUtils.xpath(
+            doc,
+            XPATH_RESULT,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
 
-        try {
-            Document result = db.createArtifactWithFactory(
-                raw.factory, cc.getMeta(), create);
+        ElementCreator creator = new ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
-            String uuid = XMLUtils.xpathString(
-                result, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
-            String hash = XMLUtils.xpathString(
-                result, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);
+        Element recommended = creator.create("recommended-artifacts");
 
-            return new String[] {uuid, hash};
-        }
-        catch (ArtifactDatabaseException ade) {
-            logger.error(ade, ade);
+        for (String factory: factories) {
+            Element fac = creator.create("artifact-factory");
+            creator.addAttr(fac, "name", factory);
+
+            recommended.appendChild(fac);
         }
 
-        return null;
-    }
-
-
-    protected Document prepareFeed(
-        Artifact     artifact,
-        LoadArtifact rawArtifact,
-        String       uuid,
-        String       hash)
-    {
-        FLYSArtifact flys = (FLYSArtifact) artifact;
-
-        String[][] data = new String[rawArtifact.parameters.size()][2];
-
-        for (int i = 0, len = rawArtifact.parameters.size(); i < len; i++) {
-            String param = rawArtifact.parameters.get(i);
-            String value = flys.getDataAsString(param);
-
-            if (value != null) {
-                data[i][0] = param;
-                data[i][1] = value;
-            }
-        }
-
-        return ClientProtocolUtils.newFeedDocument(uuid, hash, data);
-    }
-
-
-    protected boolean initializeArtifact(
-        ArtifactDatabase db,
-        CallContext      cc,
-        Document         feed,
-        String           uuid)
-    {
-        try {
-            db.feed(uuid, feed, cc.getMeta());
-
-            return true;
-        }
-        catch (ArtifactDatabaseException adbe) {
-            logger.error(adbe, adbe);
-        }
-
-        return false;
+        result.appendChild(recommended);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Thu Jul 14 14:25:36 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Thu Jul 14 15:30:55 2011 +0000
@@ -23,12 +23,16 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactDatabase;
+import de.intevation.artifacts.ArtifactDatabaseException;
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.CallContext;
 
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.artifactdatabase.ArtifactDatabaseImpl;
 import de.intevation.artifactdatabase.DefaultArtifact;
 import de.intevation.artifactdatabase.data.DefaultStateData;
 import de.intevation.artifactdatabase.data.StateData;
@@ -78,6 +82,9 @@
     public static final String XPATH_ADVANCE_TARGET =
         "/art:action/art:target/@art:name";
 
+    public static final String XPATH_MODEL_ARTIFACT =
+        "/art:action/art:template/@art:uuid";
+
     /** The constant string that shows that an operation was successful.*/
     public static final String OPERATION_SUCCESSFUL = "SUCCESS";
 
@@ -170,6 +177,28 @@
         List<State> states = engine.getStates(name);
 
         setCurrentState(states.get(0));
+
+        String model = XMLUtils.xpathString(
+            data,
+            XPATH_MODEL_ARTIFACT,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (model != null && model.length() > 0) {
+            ArtifactDatabase db = (ArtifactDatabase) flysContext.get(
+                ArtifactDatabaseImpl.GLOBAL_CONTEXT_KEY);
+
+            try {
+                initialize(db.getRawArtifact(model), context);
+            }
+            catch (ArtifactDatabaseException adbe) {
+                logger.error(adbe, adbe);
+            }
+        }
+    }
+
+
+    protected void initialize(Artifact artifact, Object context) {
+        // do nothing here
     }
 
 

http://dive4elements.wald.intevation.org