Revision 6b4a2499 web_client/src/gr/grnet/pithos/web/client/commands/PasteCommand.java

b/web_client/src/gr/grnet/pithos/web/client/commands/PasteCommand.java
34 34
 */
35 35
package gr.grnet.pithos.web.client.commands;
36 36

  
37
import com.google.gwt.core.client.Scheduler;
38
import gr.grnet.pithos.web.client.Clipboard;
37 39
import gr.grnet.pithos.web.client.GSS;
38
import gr.grnet.pithos.web.client.clipboard.Clipboard;
39
import gr.grnet.pithos.web.client.clipboard.ClipboardItem;
40
import gr.grnet.pithos.web.client.rest.PostCommand;
40
import gr.grnet.pithos.web.client.foldertree.File;
41
import gr.grnet.pithos.web.client.foldertree.Folder;
42
import gr.grnet.pithos.web.client.foldertree.Resource;
43
import gr.grnet.pithos.web.client.rest.PutRequest;
41 44
import gr.grnet.pithos.web.client.rest.RestException;
42
import gr.grnet.pithos.web.client.rest.resource.FileResource;
43
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
44
import gr.grnet.pithos.web.client.rest.resource.GroupResource;
45
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
46 45

  
47
import java.util.ArrayList;
46
import java.util.Iterator;
48 47
import java.util.List;
49 48

  
50 49
import com.google.gwt.core.client.GWT;
51
import com.google.gwt.http.client.URL;
52 50
import com.google.gwt.user.client.Command;
53
import com.google.gwt.user.client.DeferredCommand;
54 51
import com.google.gwt.user.client.ui.PopupPanel;
55 52

  
56 53
public class PasteCommand implements Command {
57 54

  
55
    private GSS app;
58 56
	private PopupPanel containerPanel;
57
    private Folder folder;
59 58

  
60
	public PasteCommand(PopupPanel _containerPanel) {
59
	public PasteCommand(GSS _app, PopupPanel _containerPanel, Folder _folder) {
60
        app = _app;
61 61
		containerPanel = _containerPanel;
62
        folder = _folder;
62 63
	}
63 64

  
64 65
	@Override
65 66
	public void execute() {
66 67
		containerPanel.hide();
67
		Object selection = GSS.get().getCurrentSelection();
68
		FolderResource selectedFolder = null;
69
		if(selection != null && selection instanceof RestResourceWrapper)
70
			selectedFolder = ((RestResourceWrapper)selection).getResource();
71
		//TODO:CELLTREE
72
		/*
73
		else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
74
			selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
75
		*/
76
		if (selectedFolder != null) {
77
			final ClipboardItem citem = GSS.get().getClipboard().getItem();
78
			if (citem != null && citem.getRestResourceWrapper() != null) {
79
				String target = selectedFolder.getUri();
80
				target = target.endsWith("/") ? target : target + '/';
81
				target = target + URL.encodeComponent(citem.getRestResourceWrapper().getResource().getName());
82
				if (citem.getOperation() == Clipboard.COPY) {
83
					PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?copy=" + target, "", 200) {
84

  
85
						@Override
86
						public void onComplete() {
87
							//TODO:CELLTREE
88
							//GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
89
							GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
90
							GSS.get().getStatusPanel().updateStats();
91
							GSS.get().getClipboard().setItem(null);
92
						}
93

  
94
						@Override
95
						public void onError(Throwable t) {
96
							GWT.log("", t);
97
							if(t instanceof RestException){
98
								int statusCode = ((RestException)t).getHttpStatusCode();
99
								if(statusCode == 405)
100
									GSS.get().displayError("You don't have the necessary permissions");
101

  
102
								else if(statusCode == 409)
103
									GSS.get().displayError("A folder with the same name already exists");
104
								else if(statusCode == 413)
105
									GSS.get().displayError("Your quota has been exceeded");
106
								else
107
									GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
108
							}
109
							else
110
								GSS.get().displayError("System error copying folder:"+t.getMessage());
111
						}
112
					};
113
					DeferredCommand.addCommand(cf);
114
				} else if (citem.getOperation() == Clipboard.CUT) {
115
					PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?move=" + target, "", 200) {
116

  
117
						@Override
118
						public void onComplete() {
119
							//TODO:CELLTREE
120
							/*
121
							List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
122
							for (TreeItem item : items)
123
								if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
124
									GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
125
							GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
126
							*/
127
							GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
128
							GSS.get().getTreeView().updateNodeChildrenForRemove(citem.getRestResourceWrapper().getResource().getParentURI());
129
							GSS.get().getStatusPanel().updateStats();		
130
							GSS.get().getClipboard().setItem(null);
131
						}
132

  
133
						@Override
134
						public void onError(Throwable t) {
135
							GWT.log("", t);
136
							if(t instanceof RestException){
137
								int statusCode = ((RestException)t).getHttpStatusCode();
138
								if(statusCode == 405)
139
									GSS.get().displayError("You don't have the necessary permissions");
140
								else if(statusCode == 409)
141
									GSS.get().displayError("A folder with the same name already exists");
142
								else if(statusCode == 413)
143
									GSS.get().displayError("Your quota has been exceeded");
144
								else
145
									GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
146
							}
147
							else
148
								GSS.get().displayError("System error moving folder:"+t.getMessage());
149
						}
150
					};
151
					DeferredCommand.addCommand(cf);
152
				}
153
				return;
154
			} else if (citem != null && citem.getFile() != null) {
155
				String target = selectedFolder.getUri();
156
				target = target.endsWith("/") ? target : target + '/';
157
				target = target + URL.encodeComponent(citem.getFile().getName());
158
				if (citem.getOperation() == Clipboard.COPY) {
159
					PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
160

  
161
						@Override
162
						public void onComplete() {
163
							GSS.get().showFileList(true);
164
							GSS.get().getStatusPanel().updateStats();
165
							GSS.get().getClipboard().setItem(null);
166
						}
167

  
168
						@Override
169
						public void onError(Throwable t) {
170
							GWT.log("", t);
171
							if(t instanceof RestException){
172
								int statusCode = ((RestException)t).getHttpStatusCode();
173
								if(statusCode == 405)
174
									GSS.get().displayError("You don't have the necessary permissions");
175
								else if(statusCode == 404)
176
									GSS.get().displayError("File not found");
177
								else if(statusCode == 409)
178
									GSS.get().displayError("A file with the same name already exists");
179
								else if(statusCode == 413)
180
									GSS.get().displayError("Your quota has been exceeded");
181
								else
182
									GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
183
							}
184
							else
185
								GSS.get().displayError("System error copying file:"+t.getMessage());
186
						}
187
					};
188
					DeferredCommand.addCommand(cf);
189
				} else if (citem.getOperation() == Clipboard.CUT) {
190
					PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
191

  
192
						@Override
193
						public void onComplete() {
194
							GSS.get().showFileList(true);
195
							GSS.get().getStatusPanel().updateStats();
196
							GSS.get().getClipboard().setItem(null);
197
						}
198

  
199
						@Override
200
						public void onError(Throwable t) {
201
							GWT.log("", t);
202
							if(t instanceof RestException){
203
								int statusCode = ((RestException)t).getHttpStatusCode();
204
								if(statusCode == 405)
205
									GSS.get().displayError("You don't have the necessary permissions");
206
								else if(statusCode == 404)
207
									GSS.get().displayError("File not found");
208
								else if(statusCode == 409)
209
									GSS.get().displayError("A file with the same name already exists");
210
								else if(statusCode == 413)
211
									GSS.get().displayError("Your quota has been exceeded");
212
								else
213
									GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
214
							}
215
							else
216
								GSS.get().displayError("System error copying file:"+t.getMessage());
217
						}
218
					};
219
					DeferredCommand.addCommand(cf);
220
				}
221
				return;
222
			} else if (citem != null && citem.getFiles() != null) {
223
				List<FileResource> res = citem.getFiles();
224
				List<String> fileIds = new ArrayList<String>();
225
				String target = selectedFolder.getUri();
226
				target = target.endsWith("/") ? target : target + '/';
227

  
228
				if (citem.getOperation() == Clipboard.COPY) {
229
					for (FileResource fileResource : res) {
230
						String fileTarget = target + fileResource.getName();
231
						fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
232
					}
233
					int index = 0;
234
					executeCopyOrMove(index, fileIds);
235

  
236
				} else if (citem.getOperation() == Clipboard.CUT) {
237
					for (FileResource fileResource : res) {
238
						String fileTarget = target + fileResource.getName();
239
						fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
240
					}
241
					int index = 0;
242
					executeCopyOrMove(index, fileIds);
243
				}
244
				return;
245
			}
246
		}
68
        Object clipboardItem = app.getClipboard().getItem();
69
        if (clipboardItem == null)
70
            return;
71
        int operation = app.getClipboard().getOperation();
72
        if (clipboardItem instanceof Folder) {
73
            if (operation == Clipboard.COPY) {
74

  
75
            }
76
            else {
77

  
78
            }
79
        }
80
        else {
81
            List<File> tobeCopied = (List<File>) clipboardItem;
82
            Iterator<File> iter = tobeCopied.iterator();
83
            if (operation == Clipboard.COPY) {
84
                copyFiles(iter);
85
            }
86
            else {
87
                moveFiles(iter);
88
            }
89
        }
247 90
	}
248 91

  
249
	private void executeCopyOrMove(final int index, final List<String> paths){
250
		if(index >= paths.size()){
251
			GSS.get().showFileList(true);
252
			GSS.get().getStatusPanel().updateStats();
253
			GSS.get().getClipboard().setItem(null);
254
			return;
255
		}
256
		PostCommand cf = new PostCommand(paths.get(index), "", 200) {
257

  
258
			@Override
259
			public void onComplete() {
260
				executeCopyOrMove(index+1, paths);
261
			}
262

  
263
			@Override
264
			public void onError(Throwable t) {
265
				GWT.log("", t);
266
				if(t instanceof RestException){
267
					int statusCode = ((RestException)t).getHttpStatusCode();
268
					if(statusCode == 405)
269
						GSS.get().displayError("You don't have the necessary permissions");
270
					else if(statusCode == 404)
271
						GSS.get().displayError("File not found");
272
					else if(statusCode == 409)
273
						GSS.get().displayError("A file with the same name already exists");
274
					else if(statusCode == 413)
275
						GSS.get().displayError("Your quota has been exceeded");
276
					else
277
						GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
278
				}
279
				else
280
					GSS.get().displayError("System error copying file:"+t.getMessage());
281
			}
282
		};
283
		DeferredCommand.addCommand(cf);
284
	}
92
    private void moveFiles(final Iterator<File> iter) {
93
        if (iter.hasNext()) {
94
            File file = iter.next();
95
            String path = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + file.getName();
96
            PutRequest copyFile = new PutRequest(path) {
97
                @Override
98
                public void onSuccess(Resource result) {
99
                    moveFiles(iter);
100
                }
101

  
102
                @Override
103
                public void onError(Throwable t) {
104
                    GWT.log("", t);
105
                    if (t instanceof RestException) {
106
                        GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
107
                    }
108
                    else
109
                        GSS.get().displayError("System error unable to copy file: "+t.getMessage());
110
                }
111
            };
112
            copyFile.setHeader("X-Auth-Token", app.getToken());
113
            copyFile.setHeader("X-Move-From", file.getUri());
114
            Scheduler.get().scheduleDeferred(copyFile);
115
        }
116
        else {
117
            app.getClipboard().clear();
118
            app.updateFolder(folder);
119
        }
120
    }
121

  
122
    private void copyFiles(final Iterator<File> iter) {
123
        if (iter.hasNext()) {
124
            File file = iter.next();
125
            String path = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + file.getName();
126
            PutRequest copyFile = new PutRequest(path) {
127
                @Override
128
                public void onSuccess(Resource result) {
129
                    copyFiles(iter);
130
                }
131

  
132
                @Override
133
                public void onError(Throwable t) {
134
                    GWT.log("", t);
135
                    if (t instanceof RestException) {
136
                        GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
137
                    }
138
                    else
139
                        GSS.get().displayError("System error unable to copy file: "+t.getMessage());
140
                }
141
            };
142
            copyFile.setHeader("X-Auth-Token", app.getToken());
143
            copyFile.setHeader("X-Copy-From", file.getUri());
144
            Scheduler.get().scheduleDeferred(copyFile);
145
        }
146
        else {
147
            app.getClipboard().clear();
148
            app.updateFolder(folder);
149
        }
150
    }
285 151
}

Also available in: Unified diff