Use Lytebox (a Lightbox clone) for quick viewing images.
[pithos] / src / gr / ebs / gss / client / FileUploadGearsDialog.java
index 9cf23bd..25203a8 100644 (file)
@@ -26,7 +26,9 @@ import gr.ebs.gss.client.rest.resource.FolderResource;
 \r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
+import java.util.HashMap;\r
 import java.util.List;\r
+import java.util.Map;\r
 \r
 import com.google.gwt.core.client.GWT;\r
 import com.google.gwt.gears.client.Factory;\r
@@ -37,6 +39,7 @@ import com.google.gwt.gears.client.httprequest.HttpRequest;
 import com.google.gwt.gears.client.httprequest.ProgressEvent;\r
 import com.google.gwt.gears.client.httprequest.ProgressHandler;\r
 import com.google.gwt.gears.client.httprequest.RequestCallback;\r
+import com.google.gwt.http.client.URL;\r
 import com.google.gwt.json.client.JSONObject;\r
 import com.google.gwt.json.client.JSONString;\r
 import com.google.gwt.user.client.Command;\r
@@ -79,6 +82,10 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
 \r
        private FlexTable generalTable;\r
 \r
+       private Map<String, FileResource> toRename;\r
+\r
+       private List<HttpRequest> requests = new ArrayList<HttpRequest>();\r
+\r
        /**\r
         * The widget's constructor.\r
         */\r
@@ -88,7 +95,7 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
                setAnimationEnabled(true);\r
                // Create a panel to hold all of the dialog widgets.\r
                VerticalPanel panel = new VerticalPanel();\r
-               final HTML info = new HTML("Select one or more files to upload.");\r
+               final HTML info = new HTML("You may select one or more files to upload.");\r
                info.addStyleName("gss-uploadNote");\r
                panel.add(info);\r
                // Add an informative label with the folder name.\r
@@ -131,7 +138,7 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
                Button cancel = new Button("Cancel", new ClickListener() {\r
 \r
                        public void onClick(Widget sender) {\r
-                               hide();\r
+                               cancelUpload();\r
                        }\r
                });\r
                buttons.add(cancel);\r
@@ -170,6 +177,15 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
        }\r
 \r
        /**\r
+        * Cancels the file upload.\r
+        */\r
+       private void cancelUpload() {\r
+               for (HttpRequest request: requests)\r
+                       request.abort();\r
+               hide();\r
+       }\r
+\r
+       /**\r
         * Check whether the specified file name exists in the selected folder.\r
         */\r
        private boolean canContinue(File file) {\r
@@ -190,7 +206,7 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
                        hide();\r
                        return;\r
                }\r
-               for(File file: selectedFiles)\r
+               for (File file: selectedFiles)\r
                        if (!canContinue(file)) {\r
                                app.displayError("The file name " + file.getName() +\r
                                                        " already exists in this folder");\r
@@ -199,41 +215,33 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
                        }\r
                submit.setEnabled(false);\r
                browse.setVisible(false);\r
-               final String fname = getFilename(selectedFiles.get(0).getName());\r
-               if (getFileForName(fname) == null) {\r
-                       // We are going to create a file, so we check to see if there is a\r
-                       // trashed file with the same name.\r
-                       FileResource same = null;\r
-                       for (FileResource fres : folder.getFiles())\r
-                               if (fres.isDeleted() && fres.getName().equals(fname))\r
-                                       same = fres;\r
-                       if (same == null)\r
-                               uploadFiles();\r
-                       else {\r
-                               final FileResource sameFile = same;\r
-                               GWT.log("Same deleted file", null);\r
-                               ConfirmationDialog confirm = new ConfirmationDialog("A file " +\r
-                                               "with the same name exists in the trash. If you " +\r
-                                               "continue,<br/>the trashed file  '" + fname +\r
-                                               "' will be renamed automatically for you.", "Continue") {\r
-\r
-                                       @Override\r
-                                       public void cancel() {\r
-                                               hide();\r
-                                       }\r
-\r
-                                       @Override\r
-                                       public void confirm() {\r
-                                               updateTrashedFile(getBackupFilename(fname), sameFile);\r
-                                       }\r
+               List<String> toUpdate = new ArrayList<String>();\r
+               toRename = new HashMap<String, FileResource>();\r
+               for (File file: selectedFiles) {\r
+                       String fname = getFilename(file.getName());\r
+                       if (getFileForName(fname) == null) {\r
+                               // We are going to create a file, so we check to see if there is a\r
+                               // trashed file with the same name.\r
+                               FileResource same = null;\r
+                               for (FileResource fres : folder.getFiles())\r
+                                       if (fres.isDeleted() && fres.getName().equals(fname))\r
+                                               same = fres;\r
+                               // In that case add it to the list of files to rename.\r
+                               if (same != null)\r
+                                       toRename.put(getBackupFilename(fname), same);\r
+                       } else\r
+                               // If we are updating a file add it to the list of files to update.\r
+                               toUpdate.add(fname);\r
+               }\r
 \r
-                               };\r
-                               confirm.center();\r
-                       }\r
-               } else {\r
-                       // We are going to update an existing file, so show a confirmation dialog.\r
+               if (!toUpdate.isEmpty()) {\r
+                       StringBuffer sb = new StringBuffer();\r
+                       for (String name: toUpdate)\r
+                               sb.append(name).append("<br/>");\r
+                       // We are going to update existing files, so show a confirmation dialog.\r
                        ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +\r
-                                       "you want to update " + fname + "?", "Update"){\r
+                                       "you want to update the following files?<br/><i>" + sb +\r
+                                       "</i>", "Update") {\r
 \r
                                @Override\r
                                public void cancel() {\r
@@ -242,51 +250,90 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
 \r
                                @Override\r
                                public void confirm() {\r
-                                       uploadFiles();\r
+                                       confirmRename();\r
                                }\r
 \r
                        };\r
                        confirm.center();\r
-               }\r
+               } else\r
+                       confirmRename();\r
        }\r
 \r
-       private void updateTrashedFile(String newName, FileResource trashedFile) {\r
-               JSONObject json = new JSONObject();\r
-               json.put("name", new JSONString(newName));\r
-               PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {\r
+       /**\r
+        * Confirm the renames of synonymous files already in the trash.\r
+        */\r
+       private void confirmRename() {\r
+               if (!toRename.isEmpty()) {\r
+                       StringBuffer sb = new StringBuffer();\r
+                       for (FileResource file: toRename.values())\r
+                               sb.append(file.getName()).append("<br/>");\r
+                       ConfirmationDialog confirm = new ConfirmationDialog("Files " +\r
+                                       "with the following names already exist in the trash. If" +\r
+                                       " you continue,<br/>the trashed files will be renamed " +\r
+                                       "automatically for you:<br/><i>" + sb + "</i>", "Continue") {\r
 \r
-                       @Override\r
-                       public void onComplete() {\r
-                               uploadFiles();\r
-                       }\r
+                               @Override\r
+                               public void cancel() {\r
+                                       hide();\r
+                               }\r
 \r
-                       @Override\r
-                       public void onError(Throwable t) {\r
-                               GWT.log("", t);\r
-                               if (t instanceof RestException) {\r
-                                       int statusCode = ((RestException) t).getHttpStatusCode();\r
-                                       if (statusCode == 405)\r
-                                               GSS.get().displayError("You don't have the necessary permissions");\r
-                                       else if (statusCode == 404)\r
-                                               GSS.get().displayError("User in permissions does not exist");\r
-                                       else if (statusCode == 409)\r
-                                               GSS.get().displayError("A file with the same name already exists");\r
-                                       else if (statusCode == 413)\r
-                                               GSS.get().displayError("Your quota has been exceeded");\r
-                                       else\r
-                                               GSS.get().displayError("Unable to modify file:" +((RestException)t).getHttpStatusText());\r
-                               } else\r
-                                       GSS.get().displayError("System error modifying file:" + t.getMessage());\r
-                       }\r
+                               @Override\r
+                               public void confirm() {\r
+                                       updateTrashedFiles();\r
+                               }\r
 \r
-               };\r
-               DeferredCommand.addCommand(cf);\r
+                       };\r
+                       confirm.center();\r
+               } else\r
+                       uploadFiles();\r
        }\r
 \r
        /**\r
-        * Schedule the PUT requests to upload the files.\r
+        * Rename the conflicting trashed files with the supplied new names.\r
+        */\r
+       private void updateTrashedFiles() {\r
+               for (final String name: toRename.keySet()) {\r
+                       JSONObject json = new JSONObject();\r
+                       json.put("name", new JSONString(name));\r
+                       PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) {\r
+\r
+                               @Override\r
+                               public void onComplete() {\r
+                                       toRename.remove(name);\r
+                                       uploadFiles();\r
+                               }\r
+\r
+                               @Override\r
+                               public void onError(Throwable t) {\r
+                                       GSS app = GSS.get();\r
+                                       GWT.log("", t);\r
+                                       if (t instanceof RestException) {\r
+                                               int statusCode = ((RestException) t).getHttpStatusCode();\r
+                                               if (statusCode == 405)\r
+                                                       app.displayError("You don't have the necessary permissions");\r
+                                               else if (statusCode == 404)\r
+                                                       app.displayError("User in permissions does not exist");\r
+                                               else if (statusCode == 409)\r
+                                                       app.displayError("A file with the same name already exists");\r
+                                               else if (statusCode == 413)\r
+                                                       app.displayError("Your quota has been exceeded");\r
+                                               else\r
+                                                       app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
+                                       } else\r
+                                               app.displayError("System error modifying file:" + t.getMessage());\r
+                               }\r
+\r
+                       };\r
+                       DeferredCommand.addCommand(cf);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Checks if the renaming step for already trashed files is complete and\r
+        * starts file uploads.\r
         */\r
        private void uploadFiles() {\r
+               if (!toRename.isEmpty()) return;\r
                for (int i = 0; i< fileObjects.length; i++) {\r
                        final int index = i;\r
                        DeferredCommand.addCommand(new Command() {\r
@@ -301,21 +348,17 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
         * Perform the HTTP PUT requests to upload the specified file.\r
         */\r
        protected void doPut(final File file, final int index) {\r
-               GSS app = GSS.get();\r
+               final GSS app = GSS.get();\r
                HttpRequest request = factory.createHttpRequest();\r
+               requests.add(request);\r
                String method = "PUT";\r
 \r
                String path;\r
-               String filename = getFilename(file.getName());\r
-               FileResource selectedResource = getFileForName(filename);\r
-               if (selectedResource == null ) {\r
-                       // We are going to create a file.\r
-                       path = folder.getUri();\r
-                       if (!path.endsWith("/"))\r
-                               path = path + "/";\r
-                       path = path + encodeComponent(filename);\r
-               } else\r
-                       path = selectedResource.getUri();\r
+               final String filename = getFilename(file.getName());\r
+               path = folder.getUri();\r
+               if (!path.endsWith("/"))\r
+                       path = path + "/";\r
+               path = path + encode(filename);\r
 \r
                String token = app.getToken();\r
                String resource = path.substring(app.getApiPath().length()-1, path.length());\r
@@ -337,7 +380,21 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
                                                SessionExpiredDialog dlg = new SessionExpiredDialog();\r
                                                dlg.center();\r
                                                break;\r
+                                       case 405:\r
+                                               app.displayError("You don't have permission to " +\r
+                                                               "upload file " + filename);\r
+                                               break;\r
+                                       case 409:\r
+                                               app.displayError("A folder with the name " + filename +\r
+                                                               " already exists at this level");\r
+                                               break;\r
+                                       case 413:\r
+                                               app.displayError("There is not enough free space " +\r
+                                                               "available for uploading " + filename);\r
+                                               break;\r
                                        default:\r
+                                               app.displayError("Error uploading file " + filename +\r
+                                                                       ": " + req.getStatus());\r
                                                DisplayHelper.log(req.getStatus() + ":" + req.getStatusText());\r
                                }\r
                        }\r
@@ -360,4 +417,15 @@ public class FileUploadGearsDialog extends FileUploadDialog implements Updateabl
                GSS.get().showFileList(true);\r
                GSS.get().getStatusPanel().updateStats();\r
        }\r
+\r
+       /**\r
+        * Same as URL.encode, but also encode apostrophe since browsers aren't\r
+        * consistent about it (FF encodes, IE does not).\r
+        */\r
+       private String encode(String decodedURL) {\r
+               String retv = URL.encode(decodedURL);\r
+               retv = retv.replaceAll("'", "%27");\r
+               return retv;\r
+       }\r
+\r
 }\r