diff flys-client/src/main/java/de/intevation/flys/client/client/ui/CollectionView.java @ 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 cd8603aaa730
children 6f65e70fa11d
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org