Updated licence in code files. Removed some unused methods
[pithos] / web_client / src / gr / grnet / pithos / web / client / commands / PasteCommand.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client.commands;
36
37 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;
41 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
47 import java.util.ArrayList;
48 import java.util.List;
49
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.http.client.URL;
52 import com.google.gwt.user.client.Command;
53 import com.google.gwt.user.client.DeferredCommand;
54 import com.google.gwt.user.client.ui.PopupPanel;
55
56 public class PasteCommand implements Command {
57
58         private PopupPanel containerPanel;
59
60         public PasteCommand(PopupPanel _containerPanel) {
61                 containerPanel = _containerPanel;
62         }
63
64         @Override
65         public void execute() {
66                 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                 }
247         }
248
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         }
285 }