folder refresh after new folder command, rename etc
[pithos] / src / gr / ebs / gss / client / commands / PasteCommand.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client.commands;
20
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.clipboard.Clipboard;
23 import gr.ebs.gss.client.clipboard.ClipboardItem;
24 import gr.ebs.gss.client.rest.PostCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.GroupResource;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.http.client.URL;
35 import com.google.gwt.user.client.Command;
36 import com.google.gwt.user.client.DeferredCommand;
37 import com.google.gwt.user.client.ui.PopupPanel;
38 import com.google.gwt.user.client.ui.TreeItem;
39
40 /**
41  * @author kman Command for pasting Clipboard contents
42  */
43 public class PasteCommand implements Command {
44
45         private PopupPanel containerPanel;
46
47         public PasteCommand(PopupPanel _containerPanel) {
48                 containerPanel = _containerPanel;
49         }
50
51         @Override
52         public void execute() {
53                 containerPanel.hide();
54                 Object selection = GSS.get().getCurrentSelection();
55                 if(selection != null && selection instanceof GroupResource){
56                         final ClipboardItem citem = GSS.get().getClipboard().getItem();
57                         GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
58                         if(citem.getUser() != null){
59                                 PostCommand cg = new PostCommand(group.getUri()+"?name="+citem.getUser().getUsername(), "", 201){
60
61                                         @Override
62                                         public void onComplete() {
63                                                 GSS.get().getGroups().updateGroups();
64                                                 GSS.get().showUserList();
65                                                 GSS.get().getClipboard().setItem(null);
66                                         }
67
68                                         @Override
69                                         public void onError(Throwable t) {
70                                                 GWT.log("", t);
71                                                 if(t instanceof RestException){
72                                                         int statusCode = ((RestException)t).getHttpStatusCode();
73                                                         if(statusCode == 405)
74                                                                 GSS.get().displayError("You don't have the necessary permissions");
75                                                         else if(statusCode == 404)
76                                                                 GSS.get().displayError("User does not exist");
77                                                         else if(statusCode == 409)
78                                                                 GSS.get().displayError("A user with the same name already exists");
79                                                         else if(statusCode == 413)
80                                                                 GSS.get().displayError("Your quota has been exceeded");
81                                                         else
82                                                                 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
83                                                 }
84                                                 else
85                                                         GSS.get().displayError("System error adding user:"+t.getMessage());
86                                         }
87                                 };
88                                 DeferredCommand.addCommand(cg);
89                                 return;
90                         }
91                 }
92                 FolderResource selectedFolder = null;
93                 if(selection != null && selection instanceof FolderResource)
94                         selectedFolder = (FolderResource)selection;
95                 //TODO:CELLTREE
96                 /*
97                 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
98                         selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
99                 */
100                 if (selectedFolder != null) {
101                         final ClipboardItem citem = GSS.get().getClipboard().getItem();
102                         if (citem != null && citem.getFolderResource() != null) {
103                                 String target = selectedFolder.getUri();
104                                 target = target.endsWith("/") ? target : target + '/';
105                                 target = target + URL.encodeComponent(citem.getFolderResource().getName());
106                                 if (citem.getOperation() == Clipboard.COPY) {
107                                         PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?copy=" + target, "", 200) {
108
109                                                 @Override
110                                                 public void onComplete() {
111                                                         //TODO:CELLTREE
112                                                         //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
113                                                         GSS.get().getStatusPanel().updateStats();
114                                                         GSS.get().getClipboard().setItem(null);
115                                                 }
116
117                                                 @Override
118                                                 public void onError(Throwable t) {
119                                                         GWT.log("", t);
120                                                         if(t instanceof RestException){
121                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
122                                                                 if(statusCode == 405)
123                                                                         GSS.get().displayError("You don't have the necessary permissions");
124
125                                                                 else if(statusCode == 409)
126                                                                         GSS.get().displayError("A folder with the same name already exists");
127                                                                 else if(statusCode == 413)
128                                                                         GSS.get().displayError("Your quota has been exceeded");
129                                                                 else
130                                                                         GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
131                                                         }
132                                                         else
133                                                                 GSS.get().displayError("System error copying folder:"+t.getMessage());
134                                                 }
135                                         };
136                                         DeferredCommand.addCommand(cf);
137                                 } else if (citem.getOperation() == Clipboard.CUT) {
138                                         PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?move=" + target, "", 200) {
139
140                                                 @Override
141                                                 public void onComplete() {
142                                                         //TODO:CELLTREE
143                                                         /*
144                                                         List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
145                                                         for (TreeItem item : items)
146                                                                 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
147                                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
148                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
149                                                         */
150                                                         GSS.get().getStatusPanel().updateStats();               
151                                                         GSS.get().getClipboard().setItem(null);
152                                                 }
153
154                                                 @Override
155                                                 public void onError(Throwable t) {
156                                                         GWT.log("", t);
157                                                         if(t instanceof RestException){
158                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
159                                                                 if(statusCode == 405)
160                                                                         GSS.get().displayError("You don't have the necessary permissions");
161                                                                 else if(statusCode == 409)
162                                                                         GSS.get().displayError("A folder with the same name already exists");
163                                                                 else if(statusCode == 413)
164                                                                         GSS.get().displayError("Your quota has been exceeded");
165                                                                 else
166                                                                         GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
167                                                         }
168                                                         else
169                                                                 GSS.get().displayError("System error moving folder:"+t.getMessage());
170                                                 }
171                                         };
172                                         DeferredCommand.addCommand(cf);
173                                 }
174                                 return;
175                         } else if (citem != null && citem.getFile() != null) {
176                                 String target = selectedFolder.getUri();
177                                 target = target.endsWith("/") ? target : target + '/';
178                                 target = target + URL.encodeComponent(citem.getFile().getName());
179                                 if (citem.getOperation() == Clipboard.COPY) {
180                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
181
182                                                 @Override
183                                                 public void onComplete() {
184                                                         GSS.get().showFileList(true);
185                                                         GSS.get().getStatusPanel().updateStats();
186                                                         GSS.get().getClipboard().setItem(null);
187                                                 }
188
189                                                 @Override
190                                                 public void onError(Throwable t) {
191                                                         GWT.log("", t);
192                                                         if(t instanceof RestException){
193                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
194                                                                 if(statusCode == 405)
195                                                                         GSS.get().displayError("You don't have the necessary permissions");
196                                                                 else if(statusCode == 404)
197                                                                         GSS.get().displayError("File not found");
198                                                                 else if(statusCode == 409)
199                                                                         GSS.get().displayError("A file with the same name already exists");
200                                                                 else if(statusCode == 413)
201                                                                         GSS.get().displayError("Your quota has been exceeded");
202                                                                 else
203                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
204                                                         }
205                                                         else
206                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
207                                                 }
208                                         };
209                                         DeferredCommand.addCommand(cf);
210                                 } else if (citem.getOperation() == Clipboard.CUT) {
211                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
212
213                                                 @Override
214                                                 public void onComplete() {
215                                                         GSS.get().showFileList(true);
216                                                         GSS.get().getStatusPanel().updateStats();
217                                                         GSS.get().getClipboard().setItem(null);
218                                                 }
219
220                                                 @Override
221                                                 public void onError(Throwable t) {
222                                                         GWT.log("", t);
223                                                         if(t instanceof RestException){
224                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
225                                                                 if(statusCode == 405)
226                                                                         GSS.get().displayError("You don't have the necessary permissions");
227                                                                 else if(statusCode == 404)
228                                                                         GSS.get().displayError("File not found");
229                                                                 else if(statusCode == 409)
230                                                                         GSS.get().displayError("A file with the same name already exists");
231                                                                 else if(statusCode == 413)
232                                                                         GSS.get().displayError("Your quota has been exceeded");
233                                                                 else
234                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
235                                                         }
236                                                         else
237                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
238                                                 }
239                                         };
240                                         DeferredCommand.addCommand(cf);
241                                 }
242                                 return;
243                         } else if (citem != null && citem.getFiles() != null) {
244                                 List<FileResource> res = citem.getFiles();
245                                 List<String> fileIds = new ArrayList<String>();
246                                 String target = selectedFolder.getUri();
247                                 target = target.endsWith("/") ? target : target + '/';
248
249                                 if (citem.getOperation() == Clipboard.COPY) {
250                                         for (FileResource fileResource : res) {
251                                                 String fileTarget = target + fileResource.getName();
252                                                 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
253                                         }
254                                         int index = 0;
255                                         executeCopyOrMove(index, fileIds);
256
257                                 } else if (citem.getOperation() == Clipboard.CUT) {
258                                         for (FileResource fileResource : res) {
259                                                 String fileTarget = target + fileResource.getName();
260                                                 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
261                                         }
262                                         int index = 0;
263                                         executeCopyOrMove(index, fileIds);
264                                 }
265                                 return;
266                         }
267                 }
268         }
269
270         private void executeCopyOrMove(final int index, final List<String> paths){
271                 if(index >= paths.size()){
272                         GSS.get().showFileList(true);
273                         GSS.get().getStatusPanel().updateStats();
274                         GSS.get().getClipboard().setItem(null);
275                         return;
276                 }
277                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
278
279                         @Override
280                         public void onComplete() {
281                                 executeCopyOrMove(index+1, paths);
282                         }
283
284                         @Override
285                         public void onError(Throwable t) {
286                                 GWT.log("", t);
287                                 if(t instanceof RestException){
288                                         int statusCode = ((RestException)t).getHttpStatusCode();
289                                         if(statusCode == 405)
290                                                 GSS.get().displayError("You don't have the necessary permissions");
291                                         else if(statusCode == 404)
292                                                 GSS.get().displayError("File not found");
293                                         else if(statusCode == 409)
294                                                 GSS.get().displayError("A file with the same name already exists");
295                                         else if(statusCode == 413)
296                                                 GSS.get().displayError("Your quota has been exceeded");
297                                         else
298                                                 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
299                                 }
300                                 else
301                                         GSS.get().displayError("System error copying file:"+t.getMessage());
302                         }
303                 };
304                 DeferredCommand.addCommand(cf);
305         }
306 }