diff flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java @ 887:6a27949c7022

Issue234/266: Added feature for duplicating projects. flys-client/trunk@2721 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 13 Sep 2011 16:51:26 +0000
parents eeea6a02d62c
children 2c9c3448e499
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java	Tue Sep 13 12:47:54 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java	Tue Sep 13 16:51:26 2011 +0000
@@ -12,8 +12,6 @@
 import com.google.gwt.user.client.rpc.AsyncCallback;
 
 import com.smartgwt.client.data.Criteria;
-import de.intevation.flys.client.client.event.FilterHandler;
-import de.intevation.flys.client.client.event.StringFilterEvent;
 import com.smartgwt.client.types.Alignment;
 import com.smartgwt.client.types.ListGridEditEvent;
 import com.smartgwt.client.types.ListGridFieldType;
@@ -48,12 +46,19 @@
 import de.intevation.flys.client.shared.model.Collection;
 import de.intevation.flys.client.shared.model.CollectionRecord;
 import de.intevation.flys.client.shared.model.User;
+import de.intevation.flys.client.shared.model.Artifact;
+import de.intevation.flys.client.shared.model.Recommendation;
+
+import de.intevation.flys.client.client.event.FilterHandler;
+import de.intevation.flys.client.client.event.StringFilterEvent;
 
 import de.intevation.flys.client.client.Config;
 import de.intevation.flys.client.client.FLYS;
 import de.intevation.flys.client.client.FLYSConstants;
 import de.intevation.flys.client.client.event.CollectionChangeEvent;
 import de.intevation.flys.client.client.event.CollectionChangeHandler;
+import de.intevation.flys.client.client.services.ArtifactService;
+import de.intevation.flys.client.client.services.ArtifactServiceAsync;
 import de.intevation.flys.client.client.services.DeleteCollectionService;
 import de.intevation.flys.client.client.services.DeleteCollectionServiceAsync;
 import de.intevation.flys.client.client.services.SetCollectionNameService;
@@ -62,6 +67,12 @@
 import de.intevation.flys.client.client.services.SetCollectionTTLServiceAsync;
 import de.intevation.flys.client.client.services.UserCollectionsService;
 import de.intevation.flys.client.client.services.UserCollectionsServiceAsync;
+import de.intevation.flys.client.client.services.DescribeCollectionService;
+import de.intevation.flys.client.client.services.DescribeCollectionServiceAsync;
+import de.intevation.flys.client.client.services.AddArtifactService;
+import de.intevation.flys.client.client.services.AddArtifactServiceAsync;
+import de.intevation.flys.client.client.services.CreateCollectionService;
+import de.intevation.flys.client.client.services.CreateCollectionServiceAsync;
 
 
 /**
@@ -110,6 +121,22 @@
     protected DeleteCollectionServiceAsync deleteService =
         GWT.create(DeleteCollectionService.class);
 
+    /** The DescribeCollectionService used to update the existing collection. */
+    protected DescribeCollectionServiceAsync describeCollectionService =
+        GWT.create(DescribeCollectionService.class);
+
+    /** The ArtifactService used to communicate with the Artifact server. */
+    protected ArtifactServiceAsync createArtifactService =
+        GWT.create(ArtifactService.class);
+
+    /** The ArtifactService used to communicate with the Artifact server. */
+    protected CreateCollectionServiceAsync createCollectionService =
+        GWT.create(CreateCollectionService.class);
+
+    /** The AddArtifactService used to add an artifact to a collection. */
+    protected AddArtifactServiceAsync addArtifactService =
+        GWT.create(AddArtifactService.class);
+
     /** A pointer to the FLYS instance.*/
     protected FLYS flys;
 
@@ -125,6 +152,9 @@
     /** The collections visible in the grid.*/
     protected List<Collection> filteredCollections;
 
+    /** The collection to clone*/
+    protected Collection cloneCollection;
+
     /**
      * The default constructor that creates a new ProjectList for a specific
      * user.
@@ -277,8 +307,16 @@
             }
         });
 
+        MenuItem clone = new MenuItem(messages.clone_project());
+        clone.addClickHandler(new ClickHandler() {
+            public void onClick(MenuItemClickEvent evt) {
+                cloneProject(record.getCollection());
+            }
+        });
+
         menu.addItem(open);
         menu.addItem(rename);
+        menu.addItem(clone);
         menu.addItem(new MenuItemSeparator());
         menu.addItem(del);
 
@@ -680,5 +718,122 @@
 
         return fav;
     }
+
+
+    protected void cloneProject(Collection c) {
+        Config config = Config.getInstance();
+        String url    = config.getServerUrl();
+        String locale = config.getLocale();
+
+        cloneCollection = c;
+
+        describeCollectionService.describe(c.identifier(), url, locale,
+            new AsyncCallback<Collection>() {
+                public void onFailure(Throwable caught) {
+                    GWT.log("Could not DESCRIBE collection.");
+                    SC.warn(messages.getString(caught.getMessage()));
+                }
+
+
+                public void onSuccess(Collection newCollection) {
+                    GWT.log("Successfully DESCRIBED collection.");
+                    String uuid = getMasterArtifact(newCollection);
+                    cloneArtifact(uuid);
+                }
+            }
+        );
+    }
+
+
+    protected String getMasterArtifact(Collection newCollection) {
+        String uuid = newCollection.getItem(0).identifier();
+        // The master artifact uuid.
+        return uuid;
+    }
+
+
+    protected void cloneArtifact(String uuid) {
+        Config config               = Config.getInstance();
+        final String url            = config.getServerUrl();
+        final String locale         = config.getLocale();
+
+        Recommendation recommendation = new Recommendation(
+            "winfo",
+            null,
+            uuid,
+            null);
+
+        String factory = recommendation.getFactory();
+        createArtifactService.create(
+            url, locale, factory, recommendation,
+            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());
+                    createCollection(artifact);
+                }
+            }
+        );
+    }
+
+
+    protected void createCollection(final Artifact artifact) {
+        Config config        = Config.getInstance();
+        final String url     = config.getServerUrl();
+        final String locale  = config.getLocale();
+        final String ownerid = user.identifier();
+
+        createCollectionService.create(
+            url, locale, ownerid,
+            new AsyncCallback<Collection>() {
+                public void onFailure(Throwable caught) {
+                    GWT.log("Could not create the new collection.");
+                    SC.warn(messages.getString(caught.getMessage()));
+                }
+
+                public void onSuccess(Collection collection) {
+                    GWT.log("Successfully created a new collection.");
+                    addArtifactToCollection(artifact, collection);
+                }
+            }
+        );
+    }
+
+
+    protected void addArtifactToCollection(Artifact a, Collection c) {
+        Config config               = Config.getInstance();
+        final String url            = config.getServerUrl();
+        final String locale         = config.getLocale();
+
+        addArtifactService.add(
+            c, a, 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 newColl) {
+                    String name = cloneCollection.getName();
+                    if(name == null || name.equals("")) {
+                        name = cloneCollection.identifier();
+                    }
+
+                    newColl.setName("(" + messages.copy_of() + ")" + name);
+
+                    updateCollectionName(newColl);
+                    if(cloneCollection.getTTL() == 0) {
+                        newColl.setTTL(0);
+                        updateCollectionTTL(newColl);
+                    }
+                    updateUserCollections();
+                }
+            }
+        );
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org