Use Lytebox (a Lightbox clone) for quick viewing images.
[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.dnd.DnDTreeItem;
25 import gr.ebs.gss.client.rest.PostCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.FolderResource;
29 import gr.ebs.gss.client.rest.resource.GroupResource;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.http.client.URL;
36 import com.google.gwt.user.client.Command;
37 import com.google.gwt.user.client.DeferredCommand;
38 import com.google.gwt.user.client.ui.PopupPanel;
39 import com.google.gwt.user.client.ui.TreeItem;
40
41 /**
42  * @author kman Command for pasting Clipboard contents
43  */
44 public class PasteCommand implements Command {
45
46         private PopupPanel containerPanel;
47
48         public PasteCommand(PopupPanel _containerPanel) {
49                 containerPanel = _containerPanel;
50         }
51
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                                         }
66
67                                         @Override
68                                         public void onError(Throwable t) {
69                                                 GWT.log("", t);
70                                                 if(t instanceof RestException){
71                                                         int statusCode = ((RestException)t).getHttpStatusCode();
72                                                         if(statusCode == 405)
73                                                                 GSS.get().displayError("You don't have the necessary permissions");
74                                                         else if(statusCode == 404)
75                                                                 GSS.get().displayError("User does not exist");
76                                                         else if(statusCode == 409)
77                                                                 GSS.get().displayError("A user with the same name already exists");
78                                                         else if(statusCode == 413)
79                                                                 GSS.get().displayError("Your quota has been exceeded");
80                                                         else
81                                                                 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
82                                                 }
83                                                 else
84                                                         GSS.get().displayError("System error adding user:"+t.getMessage());
85                                         }
86                                 };
87                                 DeferredCommand.addCommand(cg);
88                                 return;
89                         }
90                 }
91                 FolderResource selectedFolder = null;
92                 if(selection != null && selection instanceof FolderResource)
93                         selectedFolder = (FolderResource)selection;
94                 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
95                         selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
96                 if (selectedFolder != null) {
97                         final ClipboardItem citem = GSS.get().getClipboard().getItem();
98                         if (citem != null && citem.getFolderResource() != null) {
99                                 String target = selectedFolder.getUri();
100                                 target = target.endsWith("/") ? target : target + '/';
101                                 target = target + URL.encodeComponent(citem.getFolderResource().getName());
102                                 if (citem.getOperation() == Clipboard.COPY) {
103                                         PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?copy=" + target, "", 200) {
104
105                                                 @Override
106                                                 public void onComplete() {
107                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
108                                                         GSS.get().getStatusPanel().updateStats();
109                                                 }
110
111                                                 @Override
112                                                 public void onError(Throwable t) {
113                                                         GWT.log("", t);
114                                                         if(t instanceof RestException){
115                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
116                                                                 if(statusCode == 405)
117                                                                         GSS.get().displayError("You don't have the necessary permissions");
118
119                                                                 else if(statusCode == 409)
120                                                                         GSS.get().displayError("A folder with the same name already exists");
121                                                                 else if(statusCode == 413)
122                                                                         GSS.get().displayError("Your quota has been exceeded");
123                                                                 else
124                                                                         GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
125                                                         }
126                                                         else
127                                                                 GSS.get().displayError("System error copying folder:"+t.getMessage());
128                                                 }
129                                         };
130                                         DeferredCommand.addCommand(cf);
131                                 } else if (citem.getOperation() == Clipboard.CUT) {
132                                         PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?move=" + target, "", 200) {
133
134                                                 @Override
135                                                 public void onComplete() {
136                                                         List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
137                                                         for (TreeItem item : items)
138                                                                 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
139                                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
140                                                         GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
141                                                         GSS.get().getStatusPanel().updateStats();
142                                                 }
143
144                                                 @Override
145                                                 public void onError(Throwable t) {
146                                                         GWT.log("", t);
147                                                         if(t instanceof RestException){
148                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
149                                                                 if(statusCode == 405)
150                                                                         GSS.get().displayError("You don't have the necessary permissions");
151                                                                 else if(statusCode == 409)
152                                                                         GSS.get().displayError("A folder with the same name already exists");
153                                                                 else if(statusCode == 413)
154                                                                         GSS.get().displayError("Your quota has been exceeded");
155                                                                 else
156                                                                         GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
157                                                         }
158                                                         else
159                                                                 GSS.get().displayError("System error moving folder:"+t.getMessage());
160                                                 }
161                                         };
162                                         DeferredCommand.addCommand(cf);
163                                 }
164                                 return;
165                         } else if (citem != null && citem.getFile() != null) {
166                                 String target = selectedFolder.getUri();
167                                 target = target.endsWith("/") ? target : target + '/';
168                                 target = target + URL.encodeComponent(citem.getFile().getName());
169                                 if (citem.getOperation() == Clipboard.COPY) {
170                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
171
172                                                 @Override
173                                                 public void onComplete() {
174                                                         GSS.get().showFileList(true);
175                                                         GSS.get().getStatusPanel().updateStats();
176                                                 }
177
178                                                 @Override
179                                                 public void onError(Throwable t) {
180                                                         GWT.log("", t);
181                                                         if(t instanceof RestException){
182                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
183                                                                 if(statusCode == 405)
184                                                                         GSS.get().displayError("You don't have the necessary permissions");
185                                                                 else if(statusCode == 404)
186                                                                         GSS.get().displayError("File not found");
187                                                                 else if(statusCode == 409)
188                                                                         GSS.get().displayError("A file with the same name already exists");
189                                                                 else if(statusCode == 413)
190                                                                         GSS.get().displayError("Your quota has been exceeded");
191                                                                 else
192                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
193                                                         }
194                                                         else
195                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
196                                                 }
197                                         };
198                                         DeferredCommand.addCommand(cf);
199                                 } else if (citem.getOperation() == Clipboard.CUT) {
200                                         PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
201
202                                                 @Override
203                                                 public void onComplete() {
204                                                         GSS.get().showFileList(true);
205                                                         GSS.get().getStatusPanel().updateStats();
206                                                 }
207
208                                                 @Override
209                                                 public void onError(Throwable t) {
210                                                         GWT.log("", t);
211                                                         if(t instanceof RestException){
212                                                                 int statusCode = ((RestException)t).getHttpStatusCode();
213                                                                 if(statusCode == 405)
214                                                                         GSS.get().displayError("You don't have the necessary permissions");
215                                                                 else if(statusCode == 404)
216                                                                         GSS.get().displayError("File not found");
217                                                                 else if(statusCode == 409)
218                                                                         GSS.get().displayError("A file with the same name already exists");
219                                                                 else if(statusCode == 413)
220                                                                         GSS.get().displayError("Your quota has been exceeded");
221                                                                 else
222                                                                         GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
223                                                         }
224                                                         else
225                                                                 GSS.get().displayError("System error copying file:"+t.getMessage());
226                                                 }
227                                         };
228                                         DeferredCommand.addCommand(cf);
229                                 }
230                                 return;
231                         } else if (citem != null && citem.getFiles() != null) {
232                                 List<FileResource> res = citem.getFiles();
233                                 List<String> fileIds = new ArrayList<String>();
234                                 String target = selectedFolder.getUri();
235                                 target = target.endsWith("/") ? target : target + '/';
236
237                                 if (citem.getOperation() == Clipboard.COPY) {
238                                         for (FileResource fileResource : res) {
239                                                 String fileTarget = target + fileResource.getName();
240                                                 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
241                                         }
242                                         int index = 0;
243                                         executeCopyOrMove(index, fileIds);
244
245                                 } else if (citem.getOperation() == Clipboard.CUT) {
246                                         for (FileResource fileResource : res) {
247                                                 String fileTarget = target + fileResource.getName();
248                                                 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
249                                         }
250                                         int index =0;
251                                         executeCopyOrMove(index, fileIds);
252                                 }
253                                 return;
254                         }
255                 }
256         }
257
258         private void executeCopyOrMove(final int index, final List<String> paths){
259                 if(index >= paths.size()){
260                         GSS.get().showFileList(true);
261                         GSS.get().getStatusPanel().updateStats();
262                         return;
263                 }
264                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
265
266                         @Override
267                         public void onComplete() {
268                                 executeCopyOrMove(index+1, paths);
269                         }
270
271                         @Override
272                         public void onError(Throwable t) {
273                                 GWT.log("", t);
274                                 if(t instanceof RestException){
275                                         int statusCode = ((RestException)t).getHttpStatusCode();
276                                         if(statusCode == 405)
277                                                 GSS.get().displayError("You don't have the necessary permissions");
278                                         else if(statusCode == 404)
279                                                 GSS.get().displayError("File not found");
280                                         else if(statusCode == 409)
281                                                 GSS.get().displayError("A file with the same name already exists");
282                                         else if(statusCode == 413)
283                                                 GSS.get().displayError("Your quota has been exceeded");
284                                         else
285                                                 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
286                                 }
287                                 else
288                                         GSS.get().displayError("System error copying file:"+t.getMessage());
289                         }
290                 };
291                 DeferredCommand.addCommand(cf);
292         }
293 }