changeset 803:653ae84533e7

Read/create recommended Artifacts and add them to the current Collection. flys-client/trunk@2350 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 18 Jul 2011 09:52:16 +0000
parents d45ad7fd3027
children 374712890b94
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/FLYS.java flys-client/src/main/java/de/intevation/flys/client/client/services/ArtifactService.java flys-client/src/main/java/de/intevation/flys/client/client/services/ArtifactServiceAsync.java flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterList.java flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java flys-client/src/main/java/de/intevation/flys/client/server/ArtifactServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/shared/model/ArtifactDescription.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultArtifactDescription.java
diffstat 10 files changed, 163 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/ChangeLog	Mon Jul 18 09:52:16 2011 +0000
@@ -1,3 +1,25 @@
+2011-07-18  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/services/ArtifactServiceAsync.java,
+	  src/main/java/de/intevation/flys/client/client/services/ArtifactService.java,
+	  src/main/java/de/intevation/flys/client/server/ArtifactServiceImpl.java:
+	  An artifact can be created using a UUID as model artifact.
+
+	* src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java:
+	  Read artifact recommendations from artifact description.
+
+	* src/main/java/de/intevation/flys/client/shared/model/ArtifactDescription.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DefaultArtifactDescription.java:
+	  Added method to retrieve recommendations.
+
+	* src/main/java/de/intevation/flys/client/client/FLYS.java,
+	  src/main/java/de/intevation/flys/client/client/ui/ParameterList.java:
+	  Adapted the call of the ArtifactService.
+
+	* src/main/java/de/intevation/flys/client/client/ui/CollectionView.java:
+	  If the Artifact of a collection changes, the recommended Artifacts - if
+	  existing - are created and added to the current Collection.
+
 2011-07-12  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/ui/map/MeasureControl.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/client/FLYS.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/FLYS.java	Mon Jul 18 09:52:16 2011 +0000
@@ -397,7 +397,7 @@
         String url    = config.getServerUrl();
         String locale = config.getLocale();
 
-        artifactService.create(url, locale, factory,
+        artifactService.create(url, locale, factory, null,
             new AsyncCallback<Artifact>() {
                 public void onFailure(Throwable caught) {
                     GWT.log("Could not create the new artifact.");
--- a/flys-client/src/main/java/de/intevation/flys/client/client/services/ArtifactService.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/ArtifactService.java	Mon Jul 18 09:52:16 2011 +0000
@@ -24,7 +24,11 @@
      *
      * @return the new artifact.
      */
-    public Artifact create(String serverUrl, String locale, String factory)
-    throws ServerException;
+    public Artifact create(
+        String serverUrl,
+        String locale,
+        String factory,
+        String uuid
+    ) throws ServerException;
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/client/services/ArtifactServiceAsync.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/ArtifactServiceAsync.java	Mon Jul 18 09:52:16 2011 +0000
@@ -17,6 +17,7 @@
         String serverUrl,
         String locale,
         String factory,
+        String uuid,
         AsyncCallback<Artifact> callback);
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java	Mon Jul 18 09:52:16 2011 +0000
@@ -36,6 +36,8 @@
 import de.intevation.flys.client.client.event.OutputModesChangeHandler;
 import de.intevation.flys.client.client.event.ParameterChangeEvent;
 import de.intevation.flys.client.client.event.ParameterChangeHandler;
+import de.intevation.flys.client.client.services.ArtifactService;
+import de.intevation.flys.client.client.services.ArtifactServiceAsync;
 import de.intevation.flys.client.client.services.AddArtifactService;
 import de.intevation.flys.client.client.services.AddArtifactServiceAsync;
 import de.intevation.flys.client.client.services.CreateCollectionService;
@@ -57,6 +59,10 @@
     protected CreateCollectionServiceAsync createCollectionService =
         GWT.create(CreateCollectionService.class);
 
+    /** The ArtifactService used to communicate with the Artifact server. */
+    protected ArtifactServiceAsync createArtifactService =
+        GWT.create(ArtifactService.class);
+
     /** The AddArtifactService used to add an artifact to a collection. */
     protected AddArtifactServiceAsync addArtifactService =
         GWT.create(AddArtifactService.class);
@@ -200,21 +206,10 @@
                 public void onSuccess(Collection collection) {
                     GWT.log("Successfully created a new collection.");
 
-                    Artifact artifact = getArtifact();
-                    addArtifactService.add(collection, artifact, url, locale,
-                        new AsyncCallback<Collection>() {
+                    setCollection(collection);
 
-                            public void onFailure(Throwable caught) {
-                                GWT.log("An error occured while adding artifact.");
-                                SC.warn(messages.getString(caught.getMessage()));
-                            }
-
-                            public void onSuccess(Collection newCollection) {
-                                GWT.log("Successfully added artifact.");
-                                setCollection(newCollection);
-                            }
-                        }
-                    );
+                    Artifact artifact = getArtifact();
+                    addArtifactToCollection(artifact);
                 }
             });
     }
@@ -318,6 +313,11 @@
         Artifact art             = event.getNewValue();
         ArtifactDescription desc = art.getArtifactDescription();
         OutputMode[] outs        = desc.getOutputModes();
+        String[] recommended     = desc.getRecommendedArtifacts();
+
+        if (recommended != null) {
+            loadRecommendedArtifacts(recommended);
+        }
 
         setArtifact(art);
 
@@ -511,5 +511,55 @@
             destroy();
         }
     }
+
+
+    protected void addArtifactToCollection(final Artifact artifact) {
+        Config config               = Config.getInstance();
+        final String url            = config.getServerUrl();
+        final String locale         = config.getLocale();
+        final Collection collection = getCollection();
+
+        addArtifactService.add(
+            collection, artifact, url, locale,
+            new AsyncCallback<Collection>() {
+                public void onFailure(Throwable caught) {
+                    GWT.log("An error occured while adding artifact.");
+                    SC.warn(messages.getString(caught.getMessage()));
+                }
+
+                public void onSuccess(Collection newCollection) {
+                    GWT.log("Successfully added artifact.");
+                    setCollection(newCollection);
+                }
+            }
+        );
+    }
+
+
+    protected void loadRecommendedArtifacts(String[] factories) {
+        Config config               = Config.getInstance();
+        final String url            = config.getServerUrl();
+        final String locale         = config.getLocale();
+        final Collection collection = getCollection();
+        final Artifact master       = getArtifact();
+
+        for (String factory: factories) {
+            GWT.log("Load recommended artifact: " + factory);
+
+            createArtifactService.create(url, locale, factory, master.getUuid(),
+                new AsyncCallback<Artifact>() {
+
+                    public void onFailure(Throwable caught) {
+                        GWT.log("Error loading recommendations: " +
+                            caught.getMessage());
+                    }
+
+                    public void onSuccess(Artifact artifact) {
+                        GWT.log("Created new artifact: " + artifact.getUuid());
+                        addArtifactToCollection(artifact);
+                    }
+            });
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterList.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/ParameterList.java	Mon Jul 18 09:52:16 2011 +0000
@@ -351,7 +351,7 @@
                 final Data[] feedData  = new Data[] { data[1] };
 
                 artifactService.create(
-                    url, locale, module.toLowerCase(),
+                    url, locale, module.toLowerCase(), null,
                     new AsyncCallback<Artifact>() {
                         public void onFailure(Throwable caught) {
                             GWT.log("Could not create the new artifact.");
--- a/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java	Mon Jul 18 09:52:16 2011 +0000
@@ -6,6 +6,7 @@
 import javax.xml.xpath.XPathConstants;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
@@ -46,6 +47,9 @@
 
     public static final String XPATH_STATIC_ITEM_NODE = "art:item";
 
+    public static final String XPATH_RECOMMENDED_ARTIFACTS =
+        "/art:result/art:recommended-artifacts/art:artifact-factory";
+
     /**
      * This method creates the {@link ArtifactDescription} of the DESCRIBE
      * document <i>doc</i>.
@@ -73,14 +77,16 @@
         DataList currentData = extractCurrentData(dynamicNode, state);
         DataList[] old       = extractOldData(staticNode);
         String[] states      = extractReachableStates(reachable);
-        OutputMode[] outs        = extractOutputModes(outputs);
+        OutputMode[] outs    = extractOutputModes(outputs);
+        String[] recommended = extractRecommendedArtifacts(doc);
 
         return new DefaultArtifactDescription(
             old,
             currentData,
             state,
             states,
-            outs);
+            outs,
+            recommended);
     }
 
 
@@ -464,5 +470,31 @@
 
         return (OutputMode[]) outs.toArray(new OutputMode[size]);
     }
+
+
+    protected static String[] extractRecommendedArtifacts(Document doc) {
+        System.out.println("ArtifactDescriptionFactory - extract recommended.");
+
+        NodeList list = (NodeList) XMLUtils.xpath(
+            doc,
+            XPATH_RECOMMENDED_ARTIFACTS,
+            XPathConstants.NODESET,
+            ArtifactNamespaceContext.INSTANCE);
+
+        int num = list != null ? list.getLength() : 0;
+
+        String[] factories = new String[num];
+
+        for (int i = 0; i < num; i++) {
+            Element e = (Element) list.item(i);
+            String  f = e.getAttribute("name");
+
+            if (f != null && f.length() > 0) {
+                factories[i] = f;
+            }
+        }
+
+        return factories;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactServiceImpl.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactServiceImpl.java	Mon Jul 18 09:52:16 2011 +0000
@@ -30,12 +30,24 @@
     public static final String ERROR_CREATE_ARTIFACT = "error_create_artifact";
 
 
-    public Artifact create(String serverUrl, String locale, String factory)
+    public Artifact create(
+        String    serverUrl,
+        String    locale,
+        String    factory,
+        String    uuid)
     throws ServerException
     {
         System.out.println("ArtifactServiceImpl.create");
 
-        Document create   = ClientProtocolUtils.newCreateDocument(factory);
+        Document create = null;
+
+        if (uuid != null && uuid.length() > 0) {
+            create = ClientProtocolUtils.newCreateDocument(factory, uuid);
+        }
+        else {
+            create = ClientProtocolUtils.newCreateDocument(factory);
+        }
+
         HttpClient client = new HttpClientImpl(serverUrl, locale);
 
         try {
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/ArtifactDescription.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/ArtifactDescription.java	Mon Jul 18 09:52:16 2011 +0000
@@ -52,5 +52,13 @@
      * @return the available output modes.
      */
     public OutputMode[] getOutputModes();
+
+
+    /**
+     * Returns the recommended artifacts suggested by the server.
+     *
+     * @return the recommended artifacts.
+     */
+    public String[] getRecommendedArtifacts();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultArtifactDescription.java	Tue Jul 12 15:04:50 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultArtifactDescription.java	Mon Jul 18 09:52:16 2011 +0000
@@ -25,6 +25,9 @@
     /** The output modes of this state.*/
     protected OutputMode[] outputModes;
 
+    /** A list of artifact factories suggested by the server.*/
+    protected String[] recommendedArtifacts;
+
 
     public DefaultArtifactDescription() {
     }
@@ -65,10 +68,12 @@
         DataList     current,
         String       state,
         String[]     reachableStates,
-        OutputMode[] outputModes)
+        OutputMode[] outputModes,
+        String[]     recommendedArtifacts)
     {
         this(old, current, state, reachableStates);
-        this.outputModes = outputModes;
+        this.outputModes          = outputModes;
+        this.recommendedArtifacts = recommendedArtifacts;
     }
 
 
@@ -95,5 +100,10 @@
     public OutputMode[] getOutputModes() {
         return outputModes;
     }
+
+
+    public String[] getRecommendedArtifacts() {
+        return recommendedArtifacts;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org