changeset 1515:298a4ce64c2e

Issue451. Load and add all recommendations together in one async request using the LoadArtifactService. flys-client/trunk@3666 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 12 Jan 2012 16:06:55 +0000
parents b6af10d5f3da
children 102e472bd2a1
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java
diffstat 2 files changed, 43 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Thu Jan 12 12:33:25 2012 +0000
+++ b/flys-client/ChangeLog	Thu Jan 12 16:06:55 2012 +0000
@@ -1,3 +1,12 @@
+2012-01-12  Raimund Renkert <raimund.renkert@intevation.de>
+
+	Issue451.
+
+	* src/main/java/de/intevation/flys/client/client/ui/CollectionView.java:
+	  Load and add all recommendations together in one async request using
+	  the LoadArtifactService. This is a better way to avoid loading
+	  multiple map tabs and reduces the amount of async requests.
+
 2012-01-12  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	Partial flys/issue441 (Fläche über HSQ (zweite Achse) verkehrt). 
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java	Thu Jan 12 12:33:25 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java	Thu Jan 12 16:06:55 2012 +0000
@@ -49,6 +49,8 @@
 import de.intevation.flys.client.client.services.CreateCollectionServiceAsync;
 import de.intevation.flys.client.client.services.DescribeCollectionService;
 import de.intevation.flys.client.client.services.DescribeCollectionServiceAsync;
+import de.intevation.flys.client.client.services.LoadArtifactService;
+import de.intevation.flys.client.client.services.LoadArtifactServiceAsync;
 
 
 /**
@@ -79,6 +81,10 @@
     protected CollectionAttributeServiceAsync updater =
         GWT.create(CollectionAttributeService.class);
 
+    /** The LoadArtifactService used to load recommendations*/
+    protected LoadArtifactServiceAsync loadArtifactService =
+        GWT.create(LoadArtifactService.class);
+
     /** The message class that provides i18n strings. */
     protected FLYSConstants messages = GWT.create(FLYSConstants.class);
 
@@ -361,14 +367,10 @@
     protected void artifactChanged() {
         ArtifactDescription desc = getArtifact().getArtifactDescription();
         OutputMode[] outs        = desc.getOutputModes();
-        Recommendation[] recom   = desc.getRecommendations();
+        final Recommendation[] recom   = desc.getRecommendations();
 
         Collection c = getCollection();
 
-        if (recom != null && collection != null) {
-            loadRecommendedArtifacts(recom);
-        }
-
         if (c != null) {
             Config config = Config.getInstance();
             String locale = config.getLocale();
@@ -383,13 +385,14 @@
 
                     public void onSuccess(Collection newCollection) {
                         GWT.log("Successfully DESCRIBED collection.");
-                        boolean loaded = false;
-                        List<Recommendation> r = newCollection.getRecommendations();
-                        for (Recommendation rec: r) {
-                            loaded = newCollection.loadedRecommendation(rec);
+                        boolean loaded = true;
+                        for (Recommendation r: recom) {
+                            if(!newCollection.loadedRecommendation(r)) {
+                                loaded = false;
+                            }
                         }
-                        if (loaded) {
-                           setCollection(newCollection);
+                        if  (!loaded) {
+                            loadRecommendedArtifacts(recom);
                         }
                     }
                 }
@@ -575,29 +578,22 @@
     }
 
 
-    public void addArtifactToCollection(final Artifact artifact) {
+    public void addArtifactToCollection(Artifact artifact) {
         Config config               = Config.getInstance();
         final String locale         = config.getLocale();
         final Collection collection = getCollection();
 
-        artifactsQueue++;
-
         addArtifactService.add(
             collection, artifact, locale,
             new AsyncCallback<Collection>() {
                 public void onFailure(Throwable caught) {
                     GWT.log("An error occured while adding artifact.");
-                    artifactsQueue--;
                     SC.warn(messages.getString(caught.getMessage()));
                 }
 
                 public void onSuccess(Collection newCollection) {
-                    GWT.log("Successfully added artifact.");
+                    GWT.log("Successfully added artifacts.");
                     setCollection(newCollection);
-
-                    artifactsQueue--;
-                    addRecommendationsToCollection();
-                    recommendationQueue++;
                 }
             }
         );
@@ -624,10 +620,7 @@
                 public void onSuccess(Collection newCol) {
                     GWT.log("Successfully saved recommendations.");
                     newRecommendations.removeAllElements();
-                    recommendationQueue--;
-                    if(recommendationQueue == 0) {
-                        setCollection(newCol);
-                    }
+                    setCollection(newCol);
                 }
             }
         );
@@ -639,10 +632,8 @@
         final String locale         = config.getLocale();
         final Collection collection = getCollection();
 
-
         Artifact masterArtifact = getArtifact();
 
-
         if (recommendations == null) {
             GWT.log("WARNING: Currently no recommendations.");
             return;
@@ -652,6 +643,7 @@
             if (collection.loadedRecommendation(recommendation)) {
                 continue;
             }
+            newRecommendations.push(recommendation);
 
             // XXX: UGLY! If no reference artifact given use uuid of
             //      current artifact as reference.      
@@ -659,25 +651,24 @@
                 recommendation.setMasterArtifact(masterArtifact.getUuid());
             }
 
-            final String factory = recommendation.getFactory();
-
-            GWT.log("Load recommended artifact with factory: " + factory);
+        }
 
-            createArtifactService.create(
-                locale, factory, recommendation,
-                new AsyncCallback<Artifact>() {
-                    public void onFailure(Throwable caught) {
-                        GWT.log("Error loading recommendations: " +
-                            caught.getMessage());
-                    }
+        loadArtifactService.loadMany(
+            collection,
+            recommendations,
+            null,
+            locale,
+            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);
-                        newRecommendations.push(recommendation);
-                    }
-            });
-        }
+                public void onSuccess(Artifact[] artifacts) {
+                    GWT.log("Loaded artifacts: " + artifacts.length);
+                    addRecommendationsToCollection();
+                }
+        });
     }
 
 

http://dive4elements.wald.intevation.org