Implemented move file(s) to trash
authorChristos Stathis <chstath@ebs.gr>
Tue, 12 Jul 2011 14:17:59 +0000 (17:17 +0300)
committerChristos Stathis <chstath@ebs.gr>
Tue, 12 Jul 2011 14:17:59 +0000 (17:17 +0300)
web_client/src/gr/grnet/pithos/web/client/EditMenu.java
web_client/src/gr/grnet/pithos/web/client/FileContextMenu.java
web_client/src/gr/grnet/pithos/web/client/FolderContextMenu.java
web_client/src/gr/grnet/pithos/web/client/commands/ToTrashCommand.java
web_client/src/gr/grnet/pithos/web/client/rest/PostRequest.java [new file with mode: 0644]

index b688099..5869ccd 100644 (file)
@@ -216,7 +216,7 @@ public class EditMenu extends PopupPanel implements ClickHandler {
                pasteItem.getElement().setId("topMenu.edit.paste");
                if (GSS.get().getClipboard().getItem() != null)
                contextMenu.addItem(pasteItem);
-               MenuItem moveToTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
+               MenuItem moveToTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(GSS.get(), this, null));
                moveToTrashItem.getElement().setId("topMenu.edit.moveToTrash");
                contextMenu     .addItem(moveToTrashItem)
                                        .setVisible(cutcopyVisible);
index d54f5bf..1358073 100644 (file)
@@ -159,7 +159,7 @@ public class FileContextMenu extends PopupPanel {
                        copyItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new CopyCommand(GSS.get(), this, selectedFiles));
             contextMenu.addItem(copyItem);
 
-                       trashItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
+                       trashItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(GSS.get(), this, selectedFiles));
             contextMenu.addItem(trashItem);
 
                        deleteItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, selectedFiles, images));
index 93b5184..c24493b 100644 (file)
@@ -109,7 +109,7 @@ public class FolderContextMenu extends PopupPanel {
         contextMenu.addItem(pasteItem);
 
         // do not show delete options for the user's root folder
-        MenuItem moveToTrash = new MenuItem("<span id = 'folderContextMenu.moveToTrash'>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
+        MenuItem moveToTrash = new MenuItem("<span id = 'folderContextMenu.moveToTrash'>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(GSS.get(), this, folder));
         contextMenu.addItem(moveToTrash);
 
         MenuItem delete = new MenuItem("<span id = 'folderContextMenu.delete'>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, folder, newImages));
index e3261f0..8460038 100644 (file)
  */
 package gr.grnet.pithos.web.client.commands;
 
+import com.google.gwt.core.client.Scheduler;
 import gr.grnet.pithos.web.client.GSS;
+import gr.grnet.pithos.web.client.foldertree.File;
+import gr.grnet.pithos.web.client.foldertree.Folder;
+import gr.grnet.pithos.web.client.foldertree.Resource;
 import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
 import gr.grnet.pithos.web.client.rest.PostCommand;
+import gr.grnet.pithos.web.client.rest.PostRequest;
 import gr.grnet.pithos.web.client.rest.RestException;
 import gr.grnet.pithos.web.client.rest.resource.FileResource;
 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 
@@ -59,116 +65,53 @@ import com.google.gwt.user.client.ui.PopupPanel;
  */
 public class ToTrashCommand implements Command{
        private PopupPanel containerPanel;
+    private GSS app;
+    private Object resource;
 
-       public ToTrashCommand(PopupPanel _containerPanel){
+       public ToTrashCommand(GSS _app, PopupPanel _containerPanel, Object _resource){
                containerPanel = _containerPanel;
+        app = _app;
+        resource = _resource;
        }
 
        @Override
        public void execute() {
                containerPanel.hide();
-               Object selection = GSS.get().getCurrentSelection();
-               if (selection == null)
-                       return;
-               GWT.log("selection: " + selection.toString(), null);
-               if (selection instanceof RestResourceWrapper) {
-                       FolderResource fdto = ((RestResourceWrapper) selection).getResource();
-                       PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
+        if (resource instanceof List) {
+            Iterator<File> iter = ((List<File>) resource).iterator();
+            deleteFiles(iter);
+        }
+        else if (resource instanceof Folder) {
 
-                               @Override
-                               public void onComplete() {
-                                       //TODO:CELLTREE
-                                       /*
-                                       TreeItem folder = GSS.get().getFolders().getCurrent();
-                                       if(folder.getParentItem() != null){
-                                               GSS.get().getFolders().select(folder.getParentItem());
-                                               GSS.get().getFolders().updateFolder((DnDTreeItem) folder.getParentItem());
-                                       }
-                                       GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
-                                       */
-                                       FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
-                                       GSS.get().getTreeView().updateNodeChildrenForRemove(fres.getParentURI());
-                                       GSS.get().getTreeView().clearSelection();
-                                       //GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getTrash());
-                                       GSS.get().getTreeView().updateTrashNode();
-                                       GSS.get().showFileList(true);
-                               }
-
-                               @Override
-                               public void onError(Throwable t) {
-                                       GWT.log("", t);
-                                       if(t instanceof RestException){
-                                               int statusCode = ((RestException)t).getHttpStatusCode();
-                                               if(statusCode == 405)
-                                                       GSS.get().displayError("You don't have the necessary permissions");
-                                               else if(statusCode == 404)
-                                                       GSS.get().displayError("Folder does not exist");
-                                               else
-                                                       GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
-                                       }
-                                       else
-                                               GSS.get().displayError("System error trashing folder:"+t.getMessage());
-                               }
-                       };
-                       DeferredCommand.addCommand(tot);
-               } else if (selection instanceof FileResource) {
-                       FileResource fdto = (FileResource) selection;
-                       PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
-
-                               @Override
-                               public void onComplete() {
-                                       GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
-                               }
-
-                               @Override
-                               public void onError(Throwable t) {
-                                       GWT.log("", t);
-                                       if(t instanceof RestException){
-                                               int statusCode = ((RestException)t).getHttpStatusCode();
-                                               if(statusCode == 405)
-                                                       GSS.get().displayError("You don't have the necessary permissions");
-                                               else if(statusCode == 404)
-                                                       GSS.get().displayError("File does not exist");
-                                               else
-                                                       GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
-                                       }
-                                       else
-                                               GSS.get().displayError("System error trashing file:"+t.getMessage());
-                               }
-                       };
-                       DeferredCommand.addCommand(tot);
-
-               }
-               else if (selection instanceof List) {
-                       List<FileResource> fdtos = (List<FileResource>) selection;
-                       final List<String> fileIds = new ArrayList<String>();
-                       for(FileResource f : fdtos)
-                               fileIds.add(f.getUri()+"?trash=");
-                       MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
-
-                               @Override
-                               public void onComplete() {
-                                       GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
-                               }
-
-                               @Override
-                               public void onError(String p, Throwable t) {
-                                       GWT.log("", t);
-                                       if(t instanceof RestException){
-                                               int statusCode = ((RestException)t).getHttpStatusCode();
-                                               if(statusCode == 405)
-                                                       GSS.get().displayError("You don't have the necessary permissions");
-                                               else if(statusCode == 404)
-                                                       GSS.get().displayError("File does not exist");
-                                               else
-                                                       GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
-                                       }
-                                       else
-                                               GSS.get().displayError("System error trashing file:"+t.getMessage());
-                               }
-                       };
-                       DeferredCommand.addCommand(tot);
-               }
+        }
        }
 
+    private void deleteFiles(final Iterator<File> iter) {
+        if (iter.hasNext()) {
+            File file = iter.next();
+            String path = app.getApiPath() + app.getUsername() + file.getUri() + "?update=";
+            PostRequest trashFile = new PostRequest(path) {
+                @Override
+                public void onSuccess(Resource result) {
+                    deleteFiles(iter);
+                }
+
+                @Override
+                public void onError(Throwable t) {
+                    GWT.log("", t);
+                    if (t instanceof RestException) {
+                        GSS.get().displayError("Unable to move file to trash: " + ((RestException) t).getHttpStatusText());
+                    }
+                    else
+                        GSS.get().displayError("System error unable to move file to trash: "+t.getMessage());
+                }
+            };
+            trashFile.setHeader("X-Auth-Token", app.getToken());
+            trashFile.setHeader("X-Object-Meta-Trash", "true");
+            Scheduler.get().scheduleDeferred(trashFile);
+        }
+        else {
+            app.get().updateFolder(((List<File>) resource).get(0).getParent());
+        }
+    }
 }
diff --git a/web_client/src/gr/grnet/pithos/web/client/rest/PostRequest.java b/web_client/src/gr/grnet/pithos/web/client/rest/PostRequest.java
new file mode 100644 (file)
index 0000000..28ac9ad
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.pithos.web.client.rest;
+
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
+import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.http.client.RequestException;
+import com.google.gwt.http.client.Response;
+import gr.grnet.pithos.web.client.foldertree.Resource;
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class PostRequest implements ScheduledCommand {
+
+    private String path;
+
+    private Map<String, String> headers = new HashMap<String, String>();
+
+    public abstract void onSuccess(Resource result);
+
+    public abstract void onError(Throwable t);
+
+    public PostRequest(String path) {
+        this.path = path;
+    }
+
+    @Override
+    public void execute() {
+        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
+        for (String header : headers.keySet()) {
+            builder.setHeader(header, headers.get(header));
+        }
+        try {
+            builder.sendRequest("", new RestRequestCallback(path, Response.SC_ACCEPTED) {
+                @Override
+                public void onSuccess(Resource object) {
+                    PostRequest.this.onSuccess(object);
+                }
+
+                @Override
+                public Resource deserialize(Response response) {
+                    return Resource.createFromResponse(Resource.class, response, null);
+                }
+
+                @Override
+                public void onError(Request request, Throwable throwable) {
+                    PostRequest.this.onError(throwable);
+                }
+            });
+        }
+        catch (RequestException e) {
+        }
+    }
+
+    public void setHeader(String header, String value) {
+        headers.put(header, value);
+    }
+}