Updated copyright notice
[pithos] / web_client / src / gr / grnet / pithos / web / client / commands / PasteCommand.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client.commands;
5
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;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
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;
24
25 public class PasteCommand implements Command {
26
27         private PopupPanel containerPanel;
28
29         public PasteCommand(PopupPanel _containerPanel) {
30                 containerPanel = _containerPanel;
31         }
32
33         @Override
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){
42
43                                         @Override
44                                         public void onComplete() {
45                                                 GSS.get().getGroups().updateGroups();
46                                                 GSS.get().showUserList();
47                                                 GSS.get().getClipboard().setItem(null);
48                                         }
49
50                                         @Override
51                                         public void onError(Throwable t) {
52                                                 GWT.log("", t);
53                                                 if(t instanceof RestException){
54                                                         int statusCode = ((RestException)t).getHttpStatusCode();
55                                                         if(statusCode == 405)
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");
63                                                         else
64                                                                 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
65                                                 }
66                                                 else
67                                                         GSS.get().displayError("System error adding user:"+t.getMessage());
68                                         }
69                                 };
70                                 DeferredCommand.addCommand(cg);
71                                 return;
72                         }
73                 }
74                 FolderResource selectedFolder = null;
75                 if(selection != null && selection instanceof RestResourceWrapper)
76                         selectedFolder = ((RestResourceWrapper)selection).getResource();
77                 //TODO:CELLTREE
78                 /*
79                 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
80                         selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
81                 */
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) {
90
91                                                 @Override
92                                                 public void onComplete() {
93                                                         //TODO:CELLTREE
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);
98                                                 }
99
100                                                 @Override
101                                                 public void onError(Throwable t) {
102                                                         GWT.log("", 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");
107
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");
112                                                                 else
113                                                                         GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
114                                                         }
115                                                         else
116                                                                 GSS.get().displayError("System error copying folder:"+t.getMessage());
117                                                 }
118                                         };
119                                         DeferredCommand.addCommand(cf);
120                                 } else if (citem.getOperation() == Clipboard.CUT) {
121                                         PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?move=" + target, "", 200) {
122
123                                                 @Override
124                                                 public void onComplete() {
125                                                         //TODO:CELLTREE
126                                                         /*
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());
132                                                         */
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);
137                                                 }
138
139                                                 @Override
140                                                 public void onError(Throwable t) {
141                                                         GWT.log("", 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");
150                                                                 else
151                                                                         GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
152                                                         }
153                                                         else
154                                                                 GSS.get().displayError("System error moving folder:"+t.getMessage());
155                                                 }
156                                         };
157                                         DeferredCommand.addCommand(cf);
158                                 }
159                                 return;
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) {
166
167                                                 @Override
168                                                 public void onComplete() {
169                                                         GSS.get().showFileList(true);
170                                                         GSS.get().getStatusPanel().updateStats();
171                                                         GSS.get().getClipboard().setItem(null);
172                                                 }
173
174                                                 @Override
175                                                 public void onError(Throwable t) {
176                                                         GWT.log("", 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");
187                                                                 else
188                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
189                                                         }
190                                                         else
191                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
192                                                 }
193                                         };
194                                         DeferredCommand.addCommand(cf);
195                                 } else if (citem.getOperation() == Clipboard.CUT) {
196                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
197
198                                                 @Override
199                                                 public void onComplete() {
200                                                         GSS.get().showFileList(true);
201                                                         GSS.get().getStatusPanel().updateStats();
202                                                         GSS.get().getClipboard().setItem(null);
203                                                 }
204
205                                                 @Override
206                                                 public void onError(Throwable t) {
207                                                         GWT.log("", 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");
218                                                                 else
219                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
220                                                         }
221                                                         else
222                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
223                                                 }
224                                         };
225                                         DeferredCommand.addCommand(cf);
226                                 }
227                                 return;
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 + '/';
233
234                                 if (citem.getOperation() == Clipboard.COPY) {
235                                         for (FileResource fileResource : res) {
236                                                 String fileTarget = target + fileResource.getName();
237                                                 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
238                                         }
239                                         int index = 0;
240                                         executeCopyOrMove(index, fileIds);
241
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);
246                                         }
247                                         int index = 0;
248                                         executeCopyOrMove(index, fileIds);
249                                 }
250                                 return;
251                         }
252                 }
253         }
254
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);
260                         return;
261                 }
262                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
263
264                         @Override
265                         public void onComplete() {
266                                 executeCopyOrMove(index+1, paths);
267                         }
268
269                         @Override
270                         public void onError(Throwable t) {
271                                 GWT.log("", 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");
282                                         else
283                                                 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
284                                 }
285                                 else
286                                         GSS.get().displayError("System error copying file:"+t.getMessage());
287                         }
288                 };
289                 DeferredCommand.addCommand(cf);
290         }
291 }