2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client.commands;
6 import gr.grnet.pithos.web.client.GSS;
7 import gr.grnet.pithos.web.client.clipboard.Clipboard;
8 import gr.grnet.pithos.web.client.clipboard.ClipboardItem;
9 import gr.grnet.pithos.web.client.rest.PostCommand;
10 import gr.grnet.pithos.web.client.rest.RestException;
11 import gr.grnet.pithos.web.client.rest.resource.FileResource;
12 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
13 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
14 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
16 import java.util.ArrayList;
17 import java.util.List;
19 import com.google.gwt.core.client.GWT;
20 import com.google.gwt.http.client.URL;
21 import com.google.gwt.user.client.Command;
22 import com.google.gwt.user.client.DeferredCommand;
23 import com.google.gwt.user.client.ui.PopupPanel;
25 public class PasteCommand implements Command {
27 private PopupPanel containerPanel;
29 public PasteCommand(PopupPanel _containerPanel) {
30 containerPanel = _containerPanel;
34 public void execute() {
35 containerPanel.hide();
36 Object selection = GSS.get().getCurrentSelection();
37 if(selection != null && selection instanceof GroupResource){
38 final ClipboardItem citem = GSS.get().getClipboard().getItem();
39 GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
40 if(citem.getUser() != null){
41 PostCommand cg = new PostCommand(group.getUri()+"?name="+citem.getUser().getUsername(), "", 201){
44 public void onComplete() {
45 GSS.get().getGroups().updateGroups();
46 GSS.get().showUserList();
47 GSS.get().getClipboard().setItem(null);
51 public void onError(Throwable t) {
53 if(t instanceof RestException){
54 int statusCode = ((RestException)t).getHttpStatusCode();
56 GSS.get().displayError("You don't have the necessary permissions");
57 else if(statusCode == 404)
58 GSS.get().displayError("User does not exist");
59 else if(statusCode == 409)
60 GSS.get().displayError("A user with the same name already exists");
61 else if(statusCode == 413)
62 GSS.get().displayError("Your quota has been exceeded");
64 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
67 GSS.get().displayError("System error adding user:"+t.getMessage());
70 DeferredCommand.addCommand(cg);
74 FolderResource selectedFolder = null;
75 if(selection != null && selection instanceof RestResourceWrapper)
76 selectedFolder = ((RestResourceWrapper)selection).getResource();
79 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
80 selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
82 if (selectedFolder != null) {
83 final ClipboardItem citem = GSS.get().getClipboard().getItem();
84 if (citem != null && citem.getRestResourceWrapper() != null) {
85 String target = selectedFolder.getUri();
86 target = target.endsWith("/") ? target : target + '/';
87 target = target + URL.encodeComponent(citem.getRestResourceWrapper().getResource().getName());
88 if (citem.getOperation() == Clipboard.COPY) {
89 PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?copy=" + target, "", 200) {
92 public void onComplete() {
94 //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
95 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
96 GSS.get().getStatusPanel().updateStats();
97 GSS.get().getClipboard().setItem(null);
101 public void onError(Throwable t) {
103 if(t instanceof RestException){
104 int statusCode = ((RestException)t).getHttpStatusCode();
105 if(statusCode == 405)
106 GSS.get().displayError("You don't have the necessary permissions");
108 else if(statusCode == 409)
109 GSS.get().displayError("A folder with the same name already exists");
110 else if(statusCode == 413)
111 GSS.get().displayError("Your quota has been exceeded");
113 GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
116 GSS.get().displayError("System error copying folder:"+t.getMessage());
119 DeferredCommand.addCommand(cf);
120 } else if (citem.getOperation() == Clipboard.CUT) {
121 PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?move=" + target, "", 200) {
124 public void onComplete() {
127 List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
128 for (TreeItem item : items)
129 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
130 GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
131 GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
133 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
134 GSS.get().getTreeView().updateNodeChildrenForRemove(citem.getRestResourceWrapper().getResource().getParentURI());
135 GSS.get().getStatusPanel().updateStats();
136 GSS.get().getClipboard().setItem(null);
140 public void onError(Throwable t) {
142 if(t instanceof RestException){
143 int statusCode = ((RestException)t).getHttpStatusCode();
144 if(statusCode == 405)
145 GSS.get().displayError("You don't have the necessary permissions");
146 else if(statusCode == 409)
147 GSS.get().displayError("A folder with the same name already exists");
148 else if(statusCode == 413)
149 GSS.get().displayError("Your quota has been exceeded");
151 GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
154 GSS.get().displayError("System error moving folder:"+t.getMessage());
157 DeferredCommand.addCommand(cf);
160 } else if (citem != null && citem.getFile() != null) {
161 String target = selectedFolder.getUri();
162 target = target.endsWith("/") ? target : target + '/';
163 target = target + URL.encodeComponent(citem.getFile().getName());
164 if (citem.getOperation() == Clipboard.COPY) {
165 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
168 public void onComplete() {
169 GSS.get().showFileList(true);
170 GSS.get().getStatusPanel().updateStats();
171 GSS.get().getClipboard().setItem(null);
175 public void onError(Throwable t) {
177 if(t instanceof RestException){
178 int statusCode = ((RestException)t).getHttpStatusCode();
179 if(statusCode == 405)
180 GSS.get().displayError("You don't have the necessary permissions");
181 else if(statusCode == 404)
182 GSS.get().displayError("File not found");
183 else if(statusCode == 409)
184 GSS.get().displayError("A file with the same name already exists");
185 else if(statusCode == 413)
186 GSS.get().displayError("Your quota has been exceeded");
188 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
191 GSS.get().displayError("System error copying file:"+t.getMessage());
194 DeferredCommand.addCommand(cf);
195 } else if (citem.getOperation() == Clipboard.CUT) {
196 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
199 public void onComplete() {
200 GSS.get().showFileList(true);
201 GSS.get().getStatusPanel().updateStats();
202 GSS.get().getClipboard().setItem(null);
206 public void onError(Throwable t) {
208 if(t instanceof RestException){
209 int statusCode = ((RestException)t).getHttpStatusCode();
210 if(statusCode == 405)
211 GSS.get().displayError("You don't have the necessary permissions");
212 else if(statusCode == 404)
213 GSS.get().displayError("File not found");
214 else if(statusCode == 409)
215 GSS.get().displayError("A file with the same name already exists");
216 else if(statusCode == 413)
217 GSS.get().displayError("Your quota has been exceeded");
219 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
222 GSS.get().displayError("System error copying file:"+t.getMessage());
225 DeferredCommand.addCommand(cf);
228 } else if (citem != null && citem.getFiles() != null) {
229 List<FileResource> res = citem.getFiles();
230 List<String> fileIds = new ArrayList<String>();
231 String target = selectedFolder.getUri();
232 target = target.endsWith("/") ? target : target + '/';
234 if (citem.getOperation() == Clipboard.COPY) {
235 for (FileResource fileResource : res) {
236 String fileTarget = target + fileResource.getName();
237 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
240 executeCopyOrMove(index, fileIds);
242 } else if (citem.getOperation() == Clipboard.CUT) {
243 for (FileResource fileResource : res) {
244 String fileTarget = target + fileResource.getName();
245 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
248 executeCopyOrMove(index, fileIds);
255 private void executeCopyOrMove(final int index, final List<String> paths){
256 if(index >= paths.size()){
257 GSS.get().showFileList(true);
258 GSS.get().getStatusPanel().updateStats();
259 GSS.get().getClipboard().setItem(null);
262 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
265 public void onComplete() {
266 executeCopyOrMove(index+1, paths);
270 public void onError(Throwable t) {
272 if(t instanceof RestException){
273 int statusCode = ((RestException)t).getHttpStatusCode();
274 if(statusCode == 405)
275 GSS.get().displayError("You don't have the necessary permissions");
276 else if(statusCode == 404)
277 GSS.get().displayError("File not found");
278 else if(statusCode == 409)
279 GSS.get().displayError("A file with the same name already exists");
280 else if(statusCode == 413)
281 GSS.get().displayError("Your quota has been exceeded");
283 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
286 GSS.get().displayError("System error copying file:"+t.getMessage());
289 DeferredCommand.addCommand(cf);