Implemented move folder
[pithos] / web_client / src / gr / grnet / pithos / web / client / GSS.java
index c3e678d..e952aa2 100644 (file)
@@ -1,24 +1,61 @@
 /*
- * Copyright (c) 2011 Greek Research and Technology Network
+ * 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;
 
 import com.google.gwt.core.client.Scheduler;
 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
-import com.google.gwt.user.client.ui.DockPanel;
-import com.google.gwt.user.client.ui.HasVerticalAlignment;
-import com.google.gwt.view.client.ListDataProvider;
+import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.http.client.RequestCallback;
+import com.google.gwt.http.client.RequestException;
+import com.google.gwt.http.client.Response;
+import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONParser;
+import com.google.gwt.json.client.JSONString;
+import com.google.gwt.json.client.JSONValue;
 import com.google.gwt.view.client.SelectionChangeEvent;
-import com.google.gwt.view.client.SelectionChangeEvent.Handler;
 import com.google.gwt.view.client.SingleSelectionModel;
-import com.google.gwt.view.client.TreeViewModel.NodeInfo;
-import gr.grnet.pithos.web.client.clipboard.Clipboard;
 import gr.grnet.pithos.web.client.commands.GetUserCommand;
 import gr.grnet.pithos.web.client.foldertree.AccountResource;
 import gr.grnet.pithos.web.client.foldertree.File;
 import gr.grnet.pithos.web.client.foldertree.Folder;
 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
 import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
+import gr.grnet.pithos.web.client.foldertree.Resource;
+import gr.grnet.pithos.web.client.rest.DeleteRequest;
 import gr.grnet.pithos.web.client.rest.GetRequest;
 import gr.grnet.pithos.web.client.rest.RestException;
 import gr.grnet.pithos.web.client.rest.resource.FileResource;
@@ -46,10 +83,7 @@ import com.google.gwt.http.client.URL;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.resources.client.ClientBundle;
 import com.google.gwt.resources.client.ImageResource;
-import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.Cookies;
-import com.google.gwt.user.client.DOM;
-import com.google.gwt.user.client.DeferredCommand;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.History;
 import com.google.gwt.user.client.Window;
@@ -92,6 +126,10 @@ public class GSS implements EntryPoint, ResizeHandler {
         return account;
     }
 
+    public void updateFolder(Folder f) {
+        folderTreeView.updateFolder(f);
+    }
+
     /**
         * An aggregate image bundle that pulls together all the images for this
         * application into a single bundle.
@@ -161,15 +199,16 @@ public class GSS implements EntryPoint, ResizeHandler {
         */
        private TabPanel inner = new DecoratedTabPanel(){
                
-               public void onBrowserEvent(com.google.gwt.user.client.Event event) {
-                       if (DOM.eventGetType(event) == Event.ONCONTEXTMENU){
-                               if(isFileListShowing()){
-                                       getFileList().showContextMenu(event);
-                               }
-                       }
-               };
+//             public void onBrowserEvent(com.google.gwt.user.client.Event event) {
+//                     if (DOM.eventGetType(event) == Event.ONCONTEXTMENU){
+//                             if(isFileListShowing()){
+//                                     getFileList().showContextMenu(event);
+//                             }
+//                     }
+//             };
        };
 
+
        /**
         * The split panel that will contain the left and right panels.
         */
@@ -250,7 +289,7 @@ public class GSS implements EntryPoint, ResizeHandler {
             @Override
             public void onSelectionChange(SelectionChangeEvent event) {
                 Folder f = folderTreeSelectionModel.getSelectedObject();
-                showFiles(f);
+                updateFolder(f);
             }
         });
 
@@ -308,10 +347,37 @@ public class GSS implements EntryPoint, ResizeHandler {
         });
     }
 
-    private void showFiles(Folder f) {
-        Set<File> files = f.getFiles();
-        fileList.setFiles(new ArrayList<File>(files));
+    public void showFiles(Folder f) {
         inner.selectTab(0);
+        Set<File> files = f.getFiles();
+        Iterator<File> iter = files.iterator();
+        fetchFile(iter, files);
+    }
+
+    private void fetchFile(final Iterator<File> iter, final Set<File> files) {
+        if (iter.hasNext()) {
+            File file = iter.next();
+            String path = getApiPath() + username + "/" + file.getContainer() + "/" + file.getPath() + "?format=json";
+            GetRequest<File> getFile = new GetRequest<File>(File.class, path, file) {
+                @Override
+                public void onSuccess(File result) {
+                    fetchFile(iter, files);
+                }
+
+                @Override
+                public void onError(Throwable t) {
+                    GWT.log("Error getting file", t);
+                    if (t instanceof RestException)
+                        GSS.get().displayError("Error getting file: " + ((RestException) t).getHttpStatusText());
+                    else
+                        GSS.get().displayError("System error fetching file: " + t.getMessage());
+                }
+            };
+            getFile.setHeader("X-Auth-Token", "0000");
+            Scheduler.get().scheduleDeferred(getFile);
+        }
+        else
+            fileList.setFiles(new ArrayList<File>(files));
     }
 
     /**
@@ -740,4 +806,112 @@ public class GSS implements EntryPoint, ResizeHandler {
                }
                
        }
+
+    public void deleteFolder(final Folder folder) {
+        String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + folder.getPrefix();
+        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
+        builder.setHeader("If-Modified-Since", "0");
+        builder.setHeader("X-Auth-Token", getToken());
+        try {
+            builder.sendRequest("", new RequestCallback() {
+                @Override
+                public void onResponseReceived(Request request, Response response) {
+                    if (response.getStatusCode() == Response.SC_OK) {
+                        JSONValue json = JSONParser.parseStrict(response.getText());
+                        JSONArray array = json.isArray();
+                        int i = 0;
+                        if (array != null) {
+                            deleteObject(folder, i, array);
+                        }
+                    }
+                }
+
+                @Override
+                public void onError(Request request, Throwable exception) {
+                    GSS.get().displayError("System error unable to delete folder: " + exception.getMessage());
+                }
+            });
+        }
+        catch (RequestException e) {
+        }
+    }
+
+    public void deleteObject(final Folder folder, final int i, final JSONArray array) {
+        if (i < array.size()) {
+            JSONObject o = array.get(i).isObject();
+            if (o != null && !o.containsKey("subdir")) {
+                JSONString name = o.get("name").isString();
+                String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "/" + name.stringValue();
+                DeleteRequest delete = new DeleteRequest(path) {
+                    @Override
+                    public void onSuccess(Resource result) {
+                        deleteObject(folder, i + 1, array);
+                    }
+
+                    @Override
+                    public void onError(Throwable t) {
+                        GWT.log("", t);
+                        GSS.get().displayError("System error unable to delete folder: " + t.getMessage());
+                    }
+                };
+                delete.setHeader("X-Auth-Token", getToken());
+                Scheduler.get().scheduleDeferred(delete);
+            }
+            else {
+                String subdir = o.get("subdir").isString().stringValue();
+                subdir = subdir.substring(0, subdir.length() - 1);
+                String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + subdir;
+                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
+                builder.setHeader("If-Modified-Since", "0");
+                builder.setHeader("X-Auth-Token", getToken());
+                try {
+                    builder.sendRequest("", new RequestCallback() {
+                        @Override
+                        public void onResponseReceived(Request request, Response response) {
+                            if (response.getStatusCode() == Response.SC_OK) {
+                                JSONValue json = JSONParser.parseStrict(response.getText());
+                                JSONArray array2 = json.isArray();
+                                if (array2 != null) {
+                                    int l = array.size();
+                                    for (int j=0; j<array2.size(); j++) {
+                                        array.set(l++, array2.get(j));
+                                    }
+                                }
+                                deleteObject(folder, i + 1, array);
+                            }
+                        }
+
+                        @Override
+                        public void onError(Request request, Throwable exception) {
+                            GSS.get().displayError("System error unable to delete folder: " + exception.getMessage());
+                        }
+                    });
+                }
+                catch (RequestException e) {
+                }
+            }
+        }
+        else {
+            String prefix = folder.getPrefix();
+            String path = getApiPath() + getUsername() + "/" + folder.getContainer() + (prefix.length() == 0 ? "" : "/" + prefix);
+            DeleteRequest deleteFolder = new DeleteRequest(path) {
+                @Override
+                public void onSuccess(Resource result) {
+                    updateFolder(folder.getParent());
+                }
+
+                @Override
+                public void onError(Throwable t) {
+                    GWT.log("", t);
+                    if (t instanceof RestException) {
+                        displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
+                    }
+                    else
+                        GSS.get().displayError("System error unable to delete folder: " + t.getMessage());
+                }
+            };
+            deleteFolder.setHeader("X-Auth-Token", getToken());
+            Scheduler.get().scheduleDeferred(deleteFolder);
+        }
+    }
 }